Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <sys/wait.h>
  6.  
  7. void commands(char *s)
  8. {
  9.     int status;
  10.     char *argv[2];
  11.     pid_t childpid;
  12.  
  13.     argv[0] = s;
  14.     argv[1] = NULL;
  15.     printf("%s", argv[0]);
  16.     childpid = fork();
  17.     if (childpid < 0)
  18.     {
  19.         exit (-1);
  20.     }
  21.     else if (childpid == 0)
  22.     {
  23.         printf("%s", argv[0]);
  24.         if (execve(argv[0], argv, NULL) == -1)
  25.         {
  26.             exit (-1);
  27.         }
  28.     }
  29.     else
  30.     {
  31.         wait(&status);
  32.     }
  33. }
  34.  
  35. int main()
  36. {
  37.     char *buffer;
  38.     size_t bufsize = 32;
  39.     char cwd[1024];
  40.     char *token;
  41.  
  42.     printf("\033[1;1H\033[2J");
  43.     while (EOF)
  44.     {
  45.         getcwd(cwd, sizeof(cwd));
  46.  
  47.         buffer = malloc(bufsize * sizeof(char));
  48.         if (buffer == NULL)
  49.         {
  50.             perror("Unable to allocate buffer");
  51.             exit(-1);
  52.         }
  53.  
  54.         printf("%s -> $ ", cwd);
  55.         if ((getline(&buffer, &bufsize, stdin)) != -1)
  56.         {
  57.             token = strtok(buffer, " ");
  58.             while (token != NULL)
  59.             {
  60.                 commands(token);
  61.                 token = strtok(NULL, " ");
  62.             }
  63.             free(buffer);
  64.         }
  65.         else
  66.         {
  67.             free(buffer);
  68.             exit (-1);
  69.         }
  70.     }
  71.     free(buffer);
  72.     return (0);
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement