Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #include "stdlib.h"
  2. #include "stdio.h"
  3. #include "err.h"
  4. #include "sys/wait.h"
  5. #include "sysexits.h"
  6. #include "unistd.h"
  7. #include "sys/types.h"
  8.  
  9. int main() {
  10. char str[1024], command[1024], argument[1024];
  11. pid_t pid;
  12. const char * argv[] = {"", "", NULL};
  13.  
  14. printf("shell_jr: ");
  15. fflush(stdout);
  16. while(fgets (str, 1024, stdin)){
  17. sscanf(str, "%s %s", command, argument);
  18. if(!strcmp(command, "exit") || !strcmp(command, "hastalavista")) {
  19. printf("See you\n");
  20. return 0;
  21. }
  22.  
  23. if(strcmp(command, "cd") != 0){
  24. chdir(argument);
  25. continue;
  26. }
  27.  
  28. argv[0] = command;
  29. argv[1] = argument;
  30. if ((pid = fork()) < 0) {
  31. err(EX_OSERR, "fork error");
  32. }
  33. if (pid) { /* parent code */
  34. wait(NULL); /* waiting for child to finish */
  35. } else { /* child code */
  36. execvp(argv[0], (char * const *)argv);
  37. fflush(stdout);
  38. err(EX_OSERR, "Failed to execute %s", command);
  39. }
  40. printf("shell_jr: ");
  41. fflush(stdout);
  42. }
  43.  
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement