Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <signal.h>
  6. #include <unistd.h>
  7. #include <sys/types.h>
  8. #include <sys/wait.h>
  9.  
  10. void handler(int sig)
  11. {
  12.     FILE *fp;
  13.     char c;
  14.     fp = fopen("history", "r");
  15.  
  16.     if (fp != NULL)
  17.     {
  18.         while ((c = getc(fp)) != EOF)
  19.             printf("%c", c);
  20.     }
  21.     else
  22.     {
  23.         fprintf(stderr, "%s \n", "Błąd odczytu histori");
  24.         exit(EXIT_FAILURE);
  25.     }
  26.  
  27.     if (fclose(fp) != EOF)
  28.     {
  29.         fprintf(stderr, "%s\n", "Blad zamkniecia pliku historia");
  30.         exit(EXIT_FAILURE);
  31.     }
  32. }
  33.  
  34. int splittoTask(char *tasks, int *countoftasks, char *pipes[100])
  35. {
  36.     char *pipesep = "|";
  37.     char *redirectsep = ">>";
  38.     *countoftasks = 0;
  39.     pipes[*countoftasks] = strtok(tasks, pipesep);
  40.     while (pipes[*countoftasks] != NULL)
  41.     {
  42.         pipes[++*countoftasks] = strtok(NULL, pipesep);
  43.     }
  44.     char *redirect = strtok(pipes[*countoftasks - 1], redirectsep);
  45.     if (redirect != NULL)
  46.     {
  47.         pipes[*countoftasks - 1] = redirect;
  48.         pipes[*countoftasks] = strtok(NULL, redirectsep);
  49.         return 1;
  50.     }
  51.  
  52.     return 0;
  53. }
  54. static void pipeline(char ***cmd)
  55. {
  56.     int fd[2];
  57.     pid_t pid;
  58.     int fdd = 0; /* Backup */
  59.  
  60.     while (*cmd != NULL)
  61.     {
  62.         pipe(fd);
  63.         if ((pid = fork()) == -1)
  64.         {
  65.             perror("fork");
  66.             exit(1);
  67.         }
  68.         else if (pid == 0)
  69.         {
  70.             umask(0);
  71.             dup2(fdd, 0);
  72.             if (*(cmd + 1) != NULL)
  73.             {
  74.                 dup2(fd[1], 1);
  75.             }
  76.             close(fd[0]);
  77.             //execvp((*cmd)[0],(*cmd)[1]);
  78.             execvp((*cmd)[0], *cmd);
  79.             exit(1);
  80.         }
  81.         else
  82.         {
  83.             wait(NULL); /* Collect childs */
  84.             close(fd[1]);
  85.             fdd = fd[0];
  86.             cmd++;
  87.         }
  88.     }
  89. }
  90.  
  91. int main(int argc, char *argv[])
  92. {
  93.     signal(SIGQUIT, handler);
  94.     char bufor[1024];
  95.     char *task;
  96.     int countoftasks;
  97.     char *pipes[100];
  98.     //https://gist.github.com/iomonad/a66f6e9cfb935dc12c0244c1e48db5c8
  99.     char *ls[] = {"ls", "-al", NULL};
  100.     char *rev[] = {"rev", NULL};
  101.     char *nl[] = {"nl", NULL};
  102.     char *cat[] = {"cat", "-e", NULL};
  103.     char **cmd[] = {ls, rev, nl, cat, NULL};
  104.  
  105.     pipeline(cmd);
  106.  
  107.     while ((task = fgets(bufor, sizeof(bufor), stdin)) != 0)
  108.     {
  109.         int redirect = splittoTask(task, &countoftasks, &pipes);
  110.         int i = 0;
  111.         char **cmda[countoftasks + 1];
  112.         for (i = 0; i < countoftasks; i++)
  113.         {
  114.             if (i == countoftasks - 1)
  115.             {
  116.                 int j, enter = 0;
  117.                 for (j = 0; pipes[i][j] != '\0'; j++)
  118.                 {
  119.                     if (pipes[i][j] == '\n')
  120.                     {
  121.                         enter = 1;
  122.                         break;
  123.                     }
  124.                 }
  125.                 if (enter == 1)
  126.                 {
  127.                     pipes[i][j] = '\0';
  128.                 }
  129.             }
  130.             char *command = strtok(pipes[i], " ");
  131.             char *args = strtok(NULL, " ");
  132.             if (args == NULL)
  133.             {
  134.                 char *comm[] = {command, args};
  135.                 *cmda[i] = *comm;
  136.             }
  137.             else
  138.             {
  139.                 char *comm[] = {command, args, NULL};
  140.                 *cmda[i] = *comm;
  141.             }
  142.         }
  143.         cmda[i] = NULL;
  144.         pipeline(cmda);
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement