OS2021_Project1.Shell
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

425 lignes
7.6 KiB

il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <sys/stat.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <dirent.h>
  9. #include <signal.h>
  10. #include <errno.h>
  11. #include <fcntl.h>
  12. #include "yeeshell.h"
  13. /* record cmdline history */
  14. char *history[CMDLINE_HISTORY_MAX_QUANTITY];
  15. int cmdline_amount = 0;
  16. int main()
  17. {
  18. char *cmdline = NULL, *pwd = NULL;
  19. char *args[ARGS_MAX_QUANTITY];
  20. int status = 1;
  21. pwd = (char *)calloc(PATH_MAX_SIZE, sizeof(char));
  22. for (int i = 0; i < CMDLINE_HISTORY_MAX_QUANTITY; i++)
  23. {
  24. history[i] = (char *)calloc(CMDLINE_MAX_SIZE, sizeof(char));
  25. }
  26. /* execute the shell's read, parse and execution loop */
  27. do
  28. {
  29. if (!getcwd(pwd, PATH_MAX_SIZE))
  30. {
  31. printf("yeeshell: The current path cannot be obtained!\n");
  32. exit(0);
  33. }
  34. printf("[root@yeeshell %s]# ", pwd);
  35. cmdline = readline();
  36. strcpy(history[cmdline_amount++], cmdline);
  37. status = execute(cmdline, args);
  38. free(cmdline);
  39. } while (status);
  40. for (int i = 0; i < CMDLINE_HISTORY_MAX_QUANTITY; i++)
  41. {
  42. free(history[i]);
  43. }
  44. exit(EXIT_SUCCESS);
  45. }
  46. char *readline()
  47. {
  48. char *cmdline = NULL;
  49. ssize_t bufsize = 0;
  50. getline(&cmdline, &bufsize, stdin);
  51. return cmdline;
  52. }
  53. int parseline(char *cmdline, char **args)
  54. {
  55. static char array[CMDLINE_MAX_SIZE]; /* holds local copy of command line */
  56. char *buf = array; /* ptr that traverses command line */
  57. char *delim; /* points to first space delimiter */
  58. int argc; /* number of args */
  59. int bg; /* background job? */
  60. strcpy(buf, cmdline);
  61. buf[strlen(buf) - 1] = ' '; /* replace trailing '\n' with space */
  62. while (*buf && (*buf == ' ')) /* ignore leading spaces */
  63. {
  64. buf++;
  65. }
  66. /* Build the argv list */
  67. argc = 0;
  68. if (*buf == '\'')
  69. {
  70. buf++;
  71. delim = strchr(buf, '\'');
  72. }
  73. else
  74. {
  75. delim = strchr(buf, ' ');
  76. }
  77. while (delim)
  78. {
  79. args[argc++] = buf;
  80. *delim = '\0';
  81. buf = delim + 1;
  82. while (*buf && (*buf == ' ')) /* ignore spaces */
  83. {
  84. buf++;
  85. }
  86. if (*buf == '\'')
  87. {
  88. buf++;
  89. delim = strchr(buf, '\'');
  90. }
  91. else
  92. {
  93. delim = strchr(buf, ' ');
  94. }
  95. }
  96. args[argc] = NULL;
  97. if (argc == 0) /* ignore blank line */
  98. {
  99. return 1;
  100. }
  101. /* should the job run in the background? */
  102. if ((bg = (*args[argc - 1] == '&')) != 0)
  103. {
  104. args[--argc] = NULL;
  105. }
  106. return bg;
  107. }
  108. int check_redirect(char **args, char *redirect_filename, char **redirect_args)
  109. {
  110. int i = 0, j = 0, redirect_flag = REDIRECT_NO;
  111. while (args[i] != NULL)
  112. {
  113. if (!strcmp(args[i], ">"))
  114. {
  115. redirect_flag = REDIRECT_OUT;
  116. break;
  117. }
  118. else if (!strcmp(args[i], "<"))
  119. {
  120. redirect_flag = REDIRECT_IN;
  121. break;
  122. }
  123. i++;
  124. }
  125. if ((redirect_flag == 1) || (redirect_flag == 2))
  126. {
  127. strcpy(redirect_filename, args[i + 1]);
  128. for (j = 0; j < i; j++)
  129. {
  130. redirect_args[j] = args[j];
  131. }
  132. }
  133. return redirect_flag;
  134. }
  135. int do_redirect(int redirect_flag, char *redirect_filename, char **redirect_args)
  136. {
  137. pid_t pid;
  138. int fd = 1;
  139. if ((pid = fork()) == 0) /* Child process */
  140. {
  141. if (redirect_flag == 1) /* in or out? */
  142. {
  143. fd = open(redirect_filename, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IXUSR);
  144. close(1);
  145. dup(fd);
  146. }
  147. else if (redirect_flag == 2)
  148. {
  149. fd = open(redirect_filename, O_RDONLY, S_IRUSR);
  150. close(0);
  151. dup(fd);
  152. }
  153. if (execvp(redirect_args[0], redirect_args) <= 0)
  154. {
  155. printf("%s: Command not found\n", redirect_args[0]);
  156. exit(0);
  157. }
  158. close(fd);
  159. }
  160. else /* parent process */
  161. {
  162. waitpid(pid, NULL, 0);
  163. }
  164. return 1;
  165. }
  166. int check_pipe(char **args, char **pipe_arg_1, char **pipe_arg_2)
  167. {
  168. int pipe_flag = 0, i = 0, j = 0;
  169. while (args[i] != NULL)
  170. {
  171. if (!strcmp(args[i], "|"))
  172. {
  173. pipe_flag = 1;
  174. break;
  175. }
  176. pipe_arg_1[j++] = args[i++];
  177. }
  178. pipe_arg_1[j] = NULL;
  179. j = 0;
  180. i++;
  181. while (args[i] != NULL)
  182. {
  183. pipe_arg_2[j++] = args[i++];
  184. }
  185. pipe_arg_2[j] = NULL;
  186. return pipe_flag;
  187. }
  188. int do_pipe(char **pipe_arg_1, char **pipe_arg_2)
  189. {
  190. int fds[2];
  191. pipe(fds);
  192. pid_t prog_1, prog_2;
  193. if ((prog_1 = fork()) == 0) /* Child process 1 */
  194. {
  195. close(1);
  196. dup(fds[1]);
  197. close(fds[0]);
  198. close(fds[1]);
  199. if (execvp(pipe_arg_1[0], pipe_arg_1) <= 0)
  200. {
  201. printf("%s: Command not found\n", pipe_arg_1[0]);
  202. exit(0);
  203. }
  204. }
  205. if ((prog_2 = fork()) == 0) /* Child process 1 */
  206. {
  207. dup2(fds[0], 0);
  208. close(fds[0]);
  209. close(fds[1]);
  210. if (execvp(pipe_arg_2[0], pipe_arg_2) <= 0)
  211. {
  212. printf("%s: Command not found\n", pipe_arg_2[0]);
  213. exit(0);
  214. }
  215. }
  216. close(fds[0]);
  217. close(fds[1]);
  218. waitpid(prog_1, NULL, 0);
  219. waitpid(prog_2, NULL, 0);
  220. return 1;
  221. }
  222. int do_bg_fg(char **args, int bg)
  223. {
  224. pid_t pid;
  225. if ((pid = fork()) == 0)
  226. {
  227. if (execvp(args[0], args) <= 0)
  228. {
  229. printf("%s: Command not found\n", args[0]);
  230. exit(0);
  231. }
  232. }
  233. else
  234. {
  235. if (bg)
  236. {
  237. signal(SIGCHLD, SIG_IGN);
  238. }
  239. else
  240. {
  241. waitpid(pid, NULL, 0);
  242. }
  243. }
  244. return 1;
  245. }
  246. int execute(char *cmdline, char **args)
  247. {
  248. int bg = 0, i = 0, redirect_flag = 0, pipe_num = 0;
  249. pid_t pid;
  250. char *redirect_filename = NULL;
  251. char *redirect_args[ARGS_MAX_QUANTITY];
  252. char *pipe_arg_1[ARGS_MAX_QUANTITY];
  253. char *pipe_arg_2[ARGS_MAX_QUANTITY];
  254. redirect_filename = (char *)calloc(REDIRECT_FILENAME_MAX_SIZE, sizeof(char));
  255. memset(redirect_args, NULL, sizeof(redirect_args));
  256. bg = parseline(cmdline, args);
  257. redirect_flag = check_redirect(args, redirect_filename, redirect_args);
  258. pipe_num = check_pipe(args, pipe_arg_1, pipe_arg_2);
  259. if (args[0] == NULL)
  260. {
  261. return 1;
  262. }
  263. if (pipe_num == 0) /* no pipe */
  264. {
  265. if (!built_in(args)) /* built-in cmd? */
  266. {
  267. if (redirect_flag != 0) /* redirection? */
  268. {
  269. return do_redirect(redirect_flag, redirect_filename, redirect_args);
  270. }
  271. else
  272. {
  273. return do_bg_fg(args, bg);
  274. }
  275. }
  276. else
  277. {
  278. return 1;
  279. }
  280. }
  281. else
  282. {
  283. return do_pipe(pipe_arg_1, pipe_arg_2);
  284. }
  285. }
  286. int built_in(char **args)
  287. {
  288. if (!strcmp(args[0], "exit"))
  289. {
  290. exit(0);
  291. }
  292. else if (!strcmp(args[0], "cd"))
  293. {
  294. return builtin_cd(args);
  295. }
  296. else if (!strcmp(args[0], "history"))
  297. {
  298. return builtin_history(args);
  299. }
  300. else if (!strcmp(args[0], "mytop"))
  301. {
  302. int flag = builtin_mytop();
  303. if (flag == -1)
  304. {
  305. printf("yeeshell: unable to get memory info.\n");
  306. }
  307. else if (flag == -2)
  308. {
  309. printf("yeeshell: unable to get CPU info.\n");
  310. }
  311. return flag;
  312. }
  313. else
  314. {
  315. return 0;
  316. }
  317. }
  318. int builtin_cd(char **args)
  319. {
  320. if (args[1] == NULL)
  321. {
  322. return 1;
  323. }
  324. else
  325. {
  326. if (chdir(args[1]) != 0)
  327. {
  328. perror("yeeshell");
  329. }
  330. return 1;
  331. }
  332. }
  333. int builtin_history(char **args)
  334. {
  335. int n = 0;
  336. if (args[1] == NULL)
  337. {
  338. n = cmdline_amount;
  339. }
  340. else
  341. {
  342. n = atoi(args[1]) < cmdline_amount ? atoi(args[1]) : cmdline_amount;
  343. }
  344. printf("ID\tCommandline\n");
  345. for (int i = 0; i < n; i++)
  346. {
  347. printf("%d\t%s\n", i + 1, history[i]);
  348. }
  349. return 1;
  350. }
  351. int builtin_mytop()
  352. {
  353. int memory_flag = 0, CPU_flag = 0;
  354. memory_flag = mytop_memory();
  355. CPU_flag = mytop_CPU();
  356. if (!memory_flag)
  357. {
  358. return -1;
  359. }
  360. else if (!CPU_flag)
  361. {
  362. return -2;
  363. }
  364. else
  365. {
  366. return 1;
  367. }
  368. }
  369. int mytop_memory()
  370. {
  371. FILE *fp = NULL;
  372. int pagesize;
  373. long total = 0, free = 0, cached = 0;
  374. if ((fp = fopen("/proc/meminfo", "r")) == NULL)
  375. {
  376. return 0;
  377. }
  378. fscanf(fp, "%u %lu %lu %lu", &pagesize, &total, &free, &cached);
  379. fclose(fp);
  380. printf("memmory(KBytes):\t%ld total\t%ld free\t%ld cached\n", (pagesize * total) / 1024, (pagesize * free) / 1024, (pagesize * cached) / 1024);
  381. return 1;
  382. }
  383. int mytop_CPU()
  384. {
  385. }