1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
| #include <stdio.h> #include <sys/types.h> #include <wait.h> #include <stdbool.h> #include <sys/syscall.h> #include <string.h> #include <stdlib.h> #include <stddef.h> #include <malloc.h> #include <errno.h> #include <unistd.h> #include <libgen.h> #include <sys/stat.h> #include <fcntl.h> #include <dirent.h> #define PWD_size 512 #define command_size 256 #define tmp_size 1024
bool command_switch(char *command); bool command_list(char *command2, bool isPwd); bool command_rename(); bool command_copyFile(); bool command_rm(); int command_systempath(); bool command_pwd();
int main() { while (1) { char pwd[PWD_size], command[command_size]; getcwd(pwd, sizeof(pwd)); printf("basher^%s>", pwd); scanf("%s", command); if (!strcmp(command, "exit")) { break; } else { if (!command_switch(command)) printf("command Error\n"); fflush(stdin); } } printf("Successfully Exit!\n"); return 0; }
bool command_switch(char *command) { if (!strcmp(command, "list")) { char command2[command_size]; scanf("%s", command2); if (!strcmp(command2, "0")) { char pwd[PWD_size]; getcwd(pwd, sizeof(pwd)); command_list(pwd, 1); } else { command_list(command2, 0); } return 1; } else if (!strcmp(command, "rename")) { if (command_rename()) return 1; else return 0; } else if (!strcmp(command, "cp")) { if (command_copyFile()) return 1; else return 0; } else if (!strcmp(command, "rm")) { if (command_rm()) return 1; else return 0; } else if (!strcmp(command, "spath")) { if (command_systempath()) return 1; else return 0; } else if (!strcmp(command, "pwd")) { if (command_pwd()) return 1; else return 0; } else return 0; }
bool command_list(char *command2, bool isPwd) { struct dirent *entry; DIR *olist = NULL; int i = 1; fflush(stdin); if ((olist = opendir(command2)) == NULL) { printf("opendir false!\n"); return 0; } while (entry = readdir(olist)) { if ((strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))) { printf("%s\n", entry->d_name); i++; } } if (closedir(olist) != 0 && !isPwd) { printf("closedir false!\n"); return 0; } return 1; } bool command_rename() { char newname[30], oldname[30]; scanf("%s", oldname); scanf("%s", newname); fflush(stdin); if (rename(oldname, newname) != 0) { printf("change the new name false! \n"); return 0; } else { printf("change the new name success! \n"); return 1; } }
bool command_copyFile() { char cp1[30], cp2[30], buf[tmp_size]; int fd1, fd2; int n; bool flag = 1; scanf("%s", cp1); scanf("%s", cp2); fflush(stdin); if ((fd1 = syscall(SYS_open, cp1, O_RDONLY)) == -1) { printf("open the file false!"); flag = 0; } if ((n = syscall(SYS_read, fd1, buf, tmp_size)) == -1) { printf("read the first file false! \n"); flag = 0; } if ((fd2 = syscall(SYS_creat, cp2, 066)) == -1) { printf("create the file false!\n"); flag = 0; } if (syscall(SYS_write, fd2, buf, n) == -1) { printf("write the second file false!\n"); flag = 0; } if (syscall(SYS_close, fd1) == -1) { printf("close the first file false! \n"); flag = 0; } if (syscall(SYS_close, fd2) == -1) { printf("close the second file false! \n"); flag = 0; } if (flag) { printf("copy the new file success! \n"); } return !flag; }
bool command_rm() { char file_name[30]; scanf("%s", file_name); fflush(stdin); remove(file_name); return 1; }
int command_systempath() { pid_t pid = fork(); if (pid < 0) { fprintf(stderr, "Fork Failed"); return 1; } else if (pid == 0) { execlp("env", "", NULL); } else { return 0; } return 0; }
bool command_pwd() { char pwd[PWD_size]; getcwd(pwd, sizeof(pwd)); printf("%s", pwd); return 1; }
|