You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
2.4 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <signal.h>
  5. #include<fcntl.h>
  6. #include "mytop.c"
  7. //#include "fun.h"
  8. void eval(char *cmdline)
  9. {
  10. char *argv[MAXLINE];
  11. char buf[MAXLINE];
  12. int isBg;
  13. int pid;
  14. strcpy(buf,cmdline);
  15. isBg = parseline(cmdline,argv);
  16. if(argv[0] == NULL)
  17. {
  18. return;
  19. }
  20. if(!builtin_cmd(argv))
  21. {
  22. if((pid=fork())==0)
  23. {
  24. if(isBg)
  25. {
  26. int fd = open( "/dev/null", O_RDWR );
  27. dup2( fd, 0 );
  28. dup2( fd, 1 );
  29. }
  30. if(execvp(argv[0],argv)<0)
  31. {
  32. printf("%s:Command not found.\n",argv[0]);
  33. exit(0);
  34. }
  35. }
  36. if(!isBg)
  37. {
  38. wait(0);
  39. return;
  40. }
  41. else
  42. {
  43. signal(SIGCHLD, SIG_IGN);
  44. return;
  45. }
  46. }
  47. }
  48. int parseline(const char *cmdline, char **argv)
  49. {
  50. static char array[MAXLINE];
  51. char *buf = array;
  52. char *delim;
  53. int argc;
  54. int bg;
  55. strcpy(buf, cmdline);
  56. buf[strlen(buf)-1] = ' ';
  57. while (*buf && (*buf == ' '))
  58. buf++;
  59. argc = 0;
  60. if (*buf == '\'')
  61. {
  62. buf++;
  63. delim = strchr(buf, '\'');
  64. }
  65. else
  66. {
  67. delim = strchr(buf, ' ');
  68. }
  69. while (delim)
  70. {
  71. argv[argc++] = buf;
  72. *delim = '\0';
  73. buf = delim + 1;
  74. while (*buf && (*buf == ' '))
  75. {
  76. buf++;
  77. }
  78. if (*buf == '\'')
  79. {
  80. buf++;
  81. delim = strchr(buf, '\'');
  82. }
  83. else
  84. {
  85. delim = strchr(buf, ' ');
  86. }
  87. }
  88. argv[argc] = NULL;
  89. if (argc == 0)
  90. return 1;
  91. if ((bg = (*argv[argc-1] == '&')) != 0)
  92. {
  93. argv[--argc] = NULL;
  94. }
  95. return bg;
  96. }
  97. int builtin_cmd(char **argv)
  98. {
  99. if(Is_pipe(argv)==1)
  100. {
  101. return 1;
  102. }
  103. if(Is_redirect(argv)==1)
  104. {
  105. return 1;
  106. }
  107. if(!strcmp(argv[0],"quit")||!strcmp(argv[0],"exit"))
  108. {
  109. exit(0);
  110. }
  111. if(!strcmp(argv[0],"history"))
  112. {
  113. print_history(argv[1]);
  114. return 1;
  115. }
  116. if(!strcmp(argv[0],"cd"))
  117. {
  118. if (argv[1] == NULL)
  119. {
  120. return 1;
  121. }
  122. else
  123. {
  124. if (chdir(argv[1]) != 0)
  125. {
  126. perror("Error: ");
  127. }
  128. return 1;
  129. }
  130. }
  131. if(!strcmp(argv[0],"mytop"))
  132. {
  133. mytop();
  134. return 1;
  135. }
  136. return 0;
  137. }