Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7.  
  8. #define MAX_ARGS 256
  9.  
  10. int main()
  11. {
  12. char* input = NULL;
  13. size_t n = 0;
  14.  
  15. printf("***MIRKOSHELL***\n");
  16. printf("type exit to exit\n");
  17.  
  18. while (1)
  19. {
  20. printf("> ");
  21.  
  22.  
  23. ssize_t bytes_read = getline(&input, &n, stdin);
  24. if (bytes_read <= 0) {
  25. fprintf(stderr, "Invalid input\n");
  26. continue;
  27. }
  28. if (input[bytes_read - 1] == '\n')
  29. input[bytes_read - 1] = 0;
  30.  
  31.  
  32. if (strcmp(input, "exit") == 0) {
  33. break;
  34. }
  35.  
  36. char* args[MAX_ARGS + 1] = { strtok(input, " "), 0 };
  37. char* ptr;
  38. int i = 1;
  39. while ((ptr = strtok(NULL, " ")) && i < MAX_ARGS)
  40. args[i++] = ptr;
  41.  
  42.  
  43. if (pid == -1) {
  44. perror("fork");
  45. exit(EXIT_FAILURE);
  46. } else if (pid == 0) {
  47. execvp(args[0], args);
  48. perror("execvp");
  49. exit(EXIT_FAILURE);
  50. } else {
  51. waitpid(pid, NULL, 0);
  52. }
  53.  
  54. }
  55. exit(EXIT_SUCCESS);
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement