Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.82 KB | None | 0 0
  1. /*
  2. **  Alibek Manabayev
  3. **  This program is a very simple shell that only handles
  4. **  single word commands (no arguments).
  5. **  Type "quit" to quit.
  6. */
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <fcntl.h>
  13. #include <sys/types.h>
  14. #include <sys/wait.h>
  15. #include <unistd.h>
  16.  
  17. #define CMDLEN 80
  18. #define ARGCNT 10
  19. #define BUFFLEN 2048
  20. #define SEPARATOR " "
  21. #define bool _Bool
  22. #define true 1
  23. #define false 0
  24.  
  25. int parseArguments(char *buff, char **args);
  26. void tee(int in, int out1, int out2);
  27. void child_proc(char *arguments[ARGCNT], int fd);
  28. void d_log(char * str);
  29. int b_fd = -1;
  30.  
  31. int main(int argc, char ** argv) {
  32.     if (argc > 1) {
  33.         b_fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, 0644);
  34.         if (b_fd < 0) {
  35.             puts("Error opening backup file");
  36.             exit(1);
  37.         }
  38.         printf("Backing up to file: %s\n", argv[1]);
  39.     }
  40.  
  41.   int   childpid;
  42.   int   status;
  43.   int   i, fd;
  44.   char  buff[CMDLEN];
  45.   char  command[CMDLEN];
  46.   char *arguments[ARGCNT];
  47.   bool  background = false;
  48.  
  49.   d_log("Program begins.\n");
  50.  
  51.   for (;;) {
  52.     d_log("Please enter a command:   ");
  53.     fgets(buff, CMDLEN, stdin);
  54.         d_log(buff);
  55.     buff[strlen(buff) - 1] = '\0';
  56.  
  57.     // printf("|%s|%d\n", buff, strcmp(buff, "quit"));
  58.  
  59.     if (strcmp(buff, "quit") == 0) break;
  60.  
  61.     int n = parseArguments(buff, arguments);
  62.  
  63.     // for (int i = 0; arguments[i] != NULL; i++) {
  64.     //   printf("\\%s/\n", arguments[i]);
  65.     // }
  66.  
  67.     background = n > 0 && !strcmp(arguments[n - 1], "&");
  68.  
  69.     if (background) {
  70.       arguments[n - 1] = NULL;
  71.     }
  72.  
  73.         int fds[2];
  74.         pipe(fds);
  75.  
  76.     childpid = fork();
  77.  
  78.     if (childpid < 0) {
  79.       d_log("Error in fork.\n");
  80.       exit(-1);
  81.     }
  82.  
  83.     if (childpid != 0) {
  84.       /* Parent process closes up output side of pipe */
  85.       close(fds[1]);
  86.  
  87.       /* Read in a string from the pipe (fds[0])*/
  88.             tee(fds[0], 1, b_fd);
  89.       if (!background) {
  90.         waitpid(childpid, &status, 0);
  91.       }
  92.     } else {
  93.       /* Child process closes up input side of pipe */
  94.       close(fds[0]);
  95.  
  96.             child_proc(arguments, fds[1]);
  97.             exit(0);
  98.     }
  99.   }
  100. }
  101.  
  102. int parseArguments(char *buff, char **args) {
  103.   int i = 0;
  104.  
  105.   args[i] = strtok(buff, SEPARATOR);
  106.  
  107.   while (args[i] != NULL) {
  108.     // printf("%s\n", args[i]);
  109.     args[++i] = strtok(NULL, SEPARATOR);
  110.   }
  111.  
  112.   // printf("%d\n", i);
  113.   return i;
  114. }
  115.  
  116. void tee(int in, int out1, int out2) {
  117.     char BUFF[BUFFLEN];
  118.     int n = 0;
  119.     // puts("-- take a cop of tee --");
  120.     while ((n = read(in, BUFF, BUFFLEN)) > 0) {
  121.         // printf("|%s(%d)|", BUFF, n);
  122.         write(out1, BUFF, n);
  123.         write(out2, BUFF, n);
  124.     }
  125.     // puts("-- enough --");
  126. }
  127.  
  128. void child_proc(char *arguments[ARGCNT], int fd) {
  129.     dup2(fd, 1);
  130.     execvp(arguments[0], arguments);
  131. }
  132.  
  133. void d_log(char * str) {
  134.     printf("%s", str);
  135.     if ()
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement