Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/wait.h>
  5. #include <errno.h>
  6. #include <string.h>
  7. #include <sys/types
  8. #include <unistd.h>
  9. #include <error.h>
  10.  
  11. char prompt[] = "$ ";
  12.  
  13. static int
  14. Fork()
  15. {
  16. pid_t pid;
  17.  
  18. if ((pid = fork()) < 0)
  19. error(EXIT_FAILURE, errno, "fork error");
  20. return(pid);
  21. }
  22.  
  23. int
  24. main(void)
  25. {
  26. long MAX = sysconf(_SC_LINE_MAX);
  27. char buf[MAX];
  28. pid_t pid;
  29. int status, n;
  30.  
  31. do {
  32. write(STDOUT_FILENO, prompt, strlen(prompt));
  33. fflush(NULL);
  34. memset(buf, 0, MAX);
  35. if((n = read(STDIN_FILENO, buf, MAX)) == 0) {
  36. printf("use exit to exit shelln");
  37. continue;
  38. }
  39. buf[strlen(buf) - 1] = ''; // chomp 'n'
  40.  
  41. if (strncmp(buf, "exit", MAX) == 0) { // match
  42. break;
  43. }
  44. pid = Fork();
  45. if (pid == 0) { // child
  46. execlp(buf, buf, (char *)NULL);
  47. error(EXIT_FAILURE, errno, "exec failure");
  48. }
  49. // parent
  50. if ((pid = waitpid(pid, &status, 0)) < 0)
  51. error(EXIT_FAILURE, errno, "waitpid error");
  52. } while(1);
  53. exit(EXIT_SUCCESS);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement