Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6.  
  7. // Assume that each command line has at most 256 characters (including NULL)
  8. #define MAX_CMDLINE_LEN 256
  9.  
  10. // Assume that we have at most 16 pipe segments
  11. #define MAX_PIPE_SEGMENTS 16
  12.  
  13. // Assume that each segment has at most 256 characters (including NULL)
  14. #define MAX_SEGMENT_LENGTH 256
  15.  
  16. // This function will be invoked by main()
  17. // TODO: Implement the multi-level pipes below
  18. void process_cmd(char *cmdline);
  19.  
  20. // tokenize function is given
  21. // This function helps you parse the command line
  22. //
  23. // Suppose the following variables are defined:
  24. //
  25. // char *pipe_segments[MAX_PIPE_SEGMENTS]; // character array buffer to store the pipe segements
  26. // int num_pipe_segments; // an output integer to store the number of pipe segment parsed by this function
  27. // char cmdline[MAX_CMDLINE_LEN]; // The input command line
  28. //
  29. // Sample usage of this tokenize function:
  30. //
  31. //  tokenize(pipe_segments, cmdline, &num_pipe_segments, "|");
  32. //
  33. void tokenize(char **argv, char *line, int *numTokens, char *token);
  34.  
  35.  
  36. /* The main function implementation */
  37. int main()
  38. {
  39.     char cmdline[MAX_CMDLINE_LEN];
  40.     fgets(cmdline, MAX_CMDLINE_LEN, stdin);
  41.     process_cmd(cmdline);
  42.     return 0;
  43. }
  44.  
  45. /*
  46.     Implementation of process_cmd
  47.  
  48.     TODO: Clearly explain how you implement process_cmd in point form. For example:
  49.  
  50.     Step 1: ....
  51.     Step 2: ....
  52.         Step 2.1: .....
  53.         Step 2.2: .....
  54.             Step 2.2.1: .....
  55.     Step 3: ....
  56.  
  57.  */
  58. void process_cmd(char *cmdline)
  59. {
  60.     // Delete this line to start your work
  61.     printf("Debug: %s\n", cmdline);
  62.     char *pipe_segments[MAX_PIPE_SEGMENTS]; // character array buffer to store the pipe segements
  63.     int num_pipe_segments; // an output integer to store the number of pipe segment parsed by this function
  64.     // char cmdline[MAX_CMDLINE_LEN];
  65.     // int num_pipes;
  66.     // int num_pipe_segments;
  67.     tokenize(pipe_segments, cmdline, &num_pipe_segments, "|");
  68.     int num_pipes = num_pipe_segments;
  69.  
  70.     for (int i = 0; i < num_pipes; i++)
  71.     {
  72.         int pfds[2];
  73.         if (i < num_pipes - 1) {pipe(pfds);}
  74.         pid_t pid = fork();
  75.         if (pid == 0)
  76.         {
  77.             close(1);
  78.             if (i< num_pipes -1) {dup2(pfds[1], 1); close(pfds[0]);}
  79.             char* piper[MAX_SEGMENT_LENGTH];
  80.             // int y = 0;
  81.             int numpipes = 0;
  82.             tokenize(piper, pipe_segments[i], &numpipes, " ");
  83.             // close(1);
  84.            
  85.             // close(pfds[0]);
  86.             execvp(piper[0], piper);
  87.             exit(1);
  88.         }
  89.         else
  90.         {
  91.             wait(0);
  92.             /* code */
  93.             close(0);
  94.             // dup2(pfds[0], 0);
  95.             // close(pfds[1]);
  96.            
  97.         }  
  98.        
  99.     }
  100. }
  101.  
  102. // Implementation of tokenize function
  103. void tokenize(char **argv, char *line, int *numTokens, char *delimiter)
  104. {
  105.     int argc = 0;
  106.     char *token = strtok(line, delimiter);
  107.     while (token != NULL)
  108.     {
  109.         argv[argc++] = token;
  110.         token = strtok(NULL, delimiter);
  111.     }
  112.     argv[argc++] = NULL;
  113.     *numTokens = argc - 1;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement