|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | +#include <sys/stat.h> |
| 5 | +#include <sys/types.h> |
| 6 | +#include <unistd.h> |
| 7 | +#define N 1024 |
| 8 | + |
| 9 | +int builtin_cat(char *cmd) { return 0; } |
| 10 | + |
| 11 | +int builtin_cd(char *line) { |
| 12 | + char cmd[N], arg[N], extra[N]; |
| 13 | + if (sscanf(line, "%s %s %s", cmd, arg, extra) != 2) return -1; |
| 14 | + |
| 15 | + int ret = chdir(arg); |
| 16 | + // if (ret == -1) |
| 17 | + return 0; |
| 18 | +} |
| 19 | + |
| 20 | +int builtin_chmod(char *cmd) { return 0; } |
| 21 | + |
| 22 | +int builtin_echo(char *cmd) { return 0; } |
| 23 | + |
| 24 | +int builtin_exit(char *line) { |
| 25 | + char cmd[N], extra[N]; |
| 26 | + if (sscanf(line, "%s %s", cmd, extra) != 1) return -1; |
| 27 | + |
| 28 | + return 1; |
| 29 | +} |
| 30 | + |
| 31 | +int builtin_find(char *cmd) { return 0; } |
| 32 | + |
| 33 | +int builtin_help(char *line) { |
| 34 | + char cmd[N], extra[N]; |
| 35 | + if (sscanf(line, "%s %s", cmd, extra) != 1) return -1; |
| 36 | + |
| 37 | + const char *help_string[] = { |
| 38 | + "cat {file}: Display content of {file}.", |
| 39 | + "cd {dir}: Switch current working directory to {dir}.", |
| 40 | + "chmod {mode} {file/dir}: Change the mode (permission) of a file or " |
| 41 | + "directory.", |
| 42 | + " {mode} is an octal number.", |
| 43 | + " Please do not follow symbolc links.", |
| 44 | + "echo {str} [filename]: Display {str}. If [filename] is given,", |
| 45 | + " open [filename] and append {str} to the file.", |
| 46 | + "exit: Leave the shell.", |
| 47 | + "find [dir]: List files/dirs in the current working " |
| 48 | + "directory", |
| 49 | + " or [dir] if it is given.", |
| 50 | + " Minimum outputs contatin file type, size, and " |
| 51 | + "name.", |
| 52 | + "help: Display help message.", |
| 53 | + "id: Show current euid and egid.", |
| 54 | + "mkdir {dir}: Create a new directory {dir}.", |
| 55 | + "pwd: Print the current working directory.", |
| 56 | + "rm {file}: Remove a file.", |
| 57 | + "rmdir {dir}: Remove an empty directory.", |
| 58 | + "stat {file/dir}: Display detailed information of the given " |
| 59 | + "file/dir.", |
| 60 | + "touch {file}: Create {file} if it does not exist,", |
| 61 | + " or update its access and modification " |
| 62 | + "timestamp.", |
| 63 | + "umask {mode}: Change the umask of the current session."}; |
| 64 | + size_t len = sizeof(help_string) / sizeof(char *); |
| 65 | + for (size_t i = 0; i < len; ++i) printf("%s\n", help_string[i]); |
| 66 | + return 0; |
| 67 | +} |
| 68 | + |
| 69 | +int builtin_id(char *line) { |
| 70 | + char cmd[N], extra[N]; |
| 71 | + if (sscanf(line, "%s %s", cmd, extra) != 1) return -1; |
| 72 | + |
| 73 | + printf("uid=%d gid=%d\n", geteuid(), getegid()); |
| 74 | + return 0; |
| 75 | +} |
| 76 | + |
| 77 | +int builtin_mkdir(char *line) { |
| 78 | + char cmd[N], arg[N], extra[N]; |
| 79 | + if (sscanf(line, "%s %s %s", cmd, arg, extra) != 2) return -1; |
| 80 | + |
| 81 | + int ret = mkdir(arg, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); |
| 82 | + // if (ret == -1) |
| 83 | + return 0; |
| 84 | +} |
| 85 | + |
| 86 | +int builtin_pwd(char *line) { |
| 87 | + char cmd[N], extra[N]; |
| 88 | + if (sscanf(line, "%s %s", cmd, extra) != 1) return -1; |
| 89 | + |
| 90 | + char name[N]; |
| 91 | + getcwd(name, N); |
| 92 | + // if (name == NULL) |
| 93 | + printf("%s\n", name); |
| 94 | + return 0; |
| 95 | +} |
| 96 | + |
| 97 | +int builtin_rm(char *line) { |
| 98 | + char cmd[N], arg[N], extra[N]; |
| 99 | + if (sscanf(line, "%s %s %s", cmd, arg, extra) != 2) return -1; |
| 100 | + |
| 101 | + int ret = remove(arg); |
| 102 | + // if (ret == -1) |
| 103 | + return 0; |
| 104 | +} |
| 105 | + |
| 106 | +int builtin_rmdir(char *line) { |
| 107 | + char cmd[N], arg[N], extra[N]; |
| 108 | + if (sscanf(line, "%s %s %s", cmd, arg, extra) != 2) return -1; |
| 109 | + |
| 110 | + int ret = rmdir(arg); |
| 111 | + // if (ret == -1) |
| 112 | + return 0; |
| 113 | +} |
| 114 | + |
| 115 | +int builtin_stat(char *cmd) { return 0; } |
| 116 | + |
| 117 | +int builtin_touch(char *line) { |
| 118 | + char cmd[N], arg[N], extra[N]; |
| 119 | + if (sscanf(line, "%s %s %s", cmd, arg, extra) != 2) return -1; |
| 120 | + |
| 121 | + FILE *file; |
| 122 | + // if (access(arg, F_OK) != -1) |
| 123 | + file = fopen(arg, "a+"); |
| 124 | + fclose(file); |
| 125 | + return 0; |
| 126 | +} |
| 127 | + |
| 128 | +int builtin_unmask(char *cmd) { return 0; } |
0 commit comments