Advertisement
Guest User

Shell

a guest
Apr 13th, 2014
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <sys/wait.h>
  7.  
  8. #define MAX_LENGTH 1024
  9. #define DELIMS " \t\r\n"
  10.  
  11. int main(int argc, char *argv[]) {
  12.     call_func(stdin);
  13.     return 0;
  14. }
  15.  
  16. void call_func(FILE *stream) {
  17.     char *cmd;
  18.     char *line;
  19.     char cwd[MAX_LENGTH];
  20.     char** commands;
  21.     char commands_args[MAX_LENGTH];
  22.  
  23.     while(1) {
  24.         errno = 0;
  25.         if (getcwd(cwd, sizeof(cwd)) != NULL) {
  26.             fprintf(stdout, "%s $ ", cwd);
  27.         }
  28.        
  29.         else {
  30.             perror("getcwd() error");
  31.         }
  32.        
  33.         line = malloc(100 * sizeof(char));
  34.        
  35.         if (!fgets(line, MAX_LENGTH, stream)) {
  36.             break;
  37.         }
  38.         line = strtok(line, "\n");
  39.         commands = (char**)malloc(50 * sizeof(char*));
  40.         for (int i = 0; i < 50; i++) {
  41.             commands[i] = (char*)malloc(50 * sizeof(char));
  42.         }
  43.         if((commands[0] = strtok(line, "|"))) {
  44.             int i = 1;
  45.             while(1) {
  46.                 commands[i] = strtok(0, "|");
  47.                 if(commands[i] == NULL) {
  48.                     break;
  49.                 }
  50.             }
  51.             if((cmd = strtok(commands[0], " "))) {
  52.                 if (strcmp(cmd, "cd") == 0) {
  53.                     char *arg = strtok(0, DELIMS);
  54.                     if (!arg) {
  55.                         fprintf(stderr, "cd missing argument.\n");
  56.                     }
  57.                    
  58.                     else {
  59.                         chdir(arg);
  60.                     }
  61.                 }
  62.                 else if (strcmp(cmd, "exit") == 0) {
  63.                     break;
  64.                 }
  65.                 else if (strcmp(cmd, ".") == 0) {
  66.                     char *arg = strtok(0, DELIMS);
  67.                     FILE * pFile;
  68.                     pFile = fopen (arg,"r");
  69.                     call_func(pFile);
  70.                 }
  71.                 else if (cmd == NULL) {
  72.                     system(line);
  73.                 }
  74.                 else {
  75.                     int pfds[2];
  76.                     pipe(pfds);
  77.                     int id = fork();
  78.                     if(id == 0) {
  79.                         dup2(pfds[1], 1);
  80.                         close(pfds[0]);
  81.                         char* argb[20];
  82.                         argb[0] = cmd;
  83.                         argb[1] = strtok(0, DELIMS);
  84.                         argb[2] = NULL;
  85.                         execvp(argb[0], argb);
  86.                         exit(o);
  87.                     }
  88.                     else {
  89.                         int id_2 = fork();
  90.                         if(id_2 == 0) {
  91.                             dup2(pfds[0], 0);
  92.                             close(pfds[1]);
  93.                             wait(&id);
  94.                             char* argb[20];
  95.                             argb[0] = "grep";
  96.                             argb[1] = "c";
  97.                             argb[2] = NULL;
  98.                             execvp(argb[0], argb);
  99.                             exit(o);
  100.                         }
  101.                         else {
  102.                             wait(&id);
  103.                             wait(&id_2);
  104.                             printf("done");
  105.                         }
  106.                     }
  107.                 }
  108.                 if (errno) {
  109.                     perror("Command failed");
  110.                 }
  111.             }
  112.         }
  113.     }
  114. }
  115. //Gemaakt door Joeri van Vuuren en Gijs Schrijver
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement