Skip to content
This repository was archived by the owner on Dec 25, 2018. It is now read-only.

Commit 8e81c80

Browse files
committed
finish basic builtin function
1 parent 4297a4a commit 8e81c80

File tree

5 files changed

+30
-17
lines changed

5 files changed

+30
-17
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
source
1+
hw1

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
CC=gcc
2-
CFLAGS=-g
2+
CFLAGS=-O3 -Wall -static
33

44
source: source.c builtin.c
5-
cc ${CFLAGS} source.c builtin.c -o source
5+
cc ${CFLAGS} source.c builtin.c -o hw1
66

77
.PHONY: format
88
format:
99
clang-format -style=Google -i *.c *.h
1010

1111
.PHONY: clean
1212
clean:
13-
rm -f source
13+
rm -f hw1

builtin.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <dirent.h>
2+
#include <errno.h>
23
#include <fcntl.h>
34
#include <stdio.h>
45
#include <stdlib.h>
@@ -39,6 +40,11 @@ const char *help_string[] = {
3940
"timestamp.",
4041
"umask {mode}: Change the umask of the current session."};
4142

43+
int set_euid_egid(int euid, int egid) {
44+
if (setegid(egid) == -1) return -1;
45+
return seteuid(euid);
46+
}
47+
4248
const char *get_file_type(int mode) {
4349
const char *file_type[] = {"named pipe (fifo)",
4450
"character special file",
@@ -199,20 +205,23 @@ int builtin_stat(char *line) {
199205
struct stat info;
200206
int ret = stat(arg, &info);
201207
// if (ret == -1)
208+
char atime[N], mtime[N], ctime[N];
209+
strftime(atime, N, "%F %T %z", localtime(&info.st_atime));
210+
strftime(mtime, N, "%F %T %z", localtime(&info.st_mtime));
211+
strftime(ctime, N, "%F %T %z", localtime(&info.st_ctime));
202212
printf(
203213
"\
204214
File: %s\n\
205-
Size: %lld Blocks: %lld IO Block: %ld %s\n\
206-
Device: %s Inode: %ld Links: %d\n\
207-
Access: (%lo) Uid: (%ld) Gid: (%ld)\n\
208-
Access: %s\
209-
Modify: %s\
210-
Change: %s",
215+
Size: %ld Blocks: %ld IO Block: %ld %s\n\
216+
Device: %lxh/%lud Inode: %ld Links: %lu\n\
217+
Access: (%u) Uid: (%5u) Gid: (%5u)\n\
218+
Access: %s\n\
219+
Modify: %s\n\
220+
Change: %s\n",
211221
arg, info.st_size, info.st_blocks, info.st_blksize,
212-
get_file_type(info.st_mode & S_IFMT),
213-
"", // info.st_dev,
222+
get_file_type(info.st_mode & S_IFMT), info.st_dev, info.st_dev,
214223
info.st_ino, info.st_nlink, info.st_mode & 0777, info.st_uid, info.st_gid,
215-
ctime(&info.st_atime), ctime(&info.st_mtime), ctime(&info.st_ctime));
224+
atime, mtime, ctime);
216225
return 0;
217226
}
218227

builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ const int (*builtin_func[])(char *) = {
2121
&builtin_exit, &builtin_find, &builtin_help, &builtin_id,
2222
&builtin_mkdir, &builtin_pwd, &builtin_rm, &builtin_rmdir,
2323
&builtin_stat, &builtin_touch, &builtin_unmask};
24+
int set_euid_egid(int, int);

source.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ int main(int argc, char **argv) {
1919

2020
int arg_parse(int argc, char **argv) {
2121
if (argc == 3) {
22-
int EUID = atoi(argv[1]);
23-
int EGID = atoi(argv[2]);
24-
// TBA change euid egid
22+
int euid = atoi(argv[1]), egid = atoi(argv[2]);
23+
if (set_euid_egid(euid, egid) == -1) {
24+
fprintf(stderr, "Failed to set euid or egid.\n");
25+
return -1;
26+
}
2527
return 0;
2628
}
2729
fprintf(stderr, "Usage: %s {UID} {GID}\n", argv[0]);
@@ -33,7 +35,8 @@ void shell() {
3335
size_t len = 0;
3436
int eof = 0;
3537
do {
36-
printf("~> ");
38+
printf("\x1B[32m~\033[0m> ");
39+
fflush(stdout);
3740
if ((eof = getline(&cmd, &len, stdin)) != -1) eof = shell_exec(cmd);
3841
// cleanup
3942
free(cmd);

0 commit comments

Comments
 (0)