Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.13 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<sys/wait.h>
  3. #include<unistd.h>
  4. #include<stdlib.h>
  5. #include<string.h>
  6.  
  7. #define READLINE_BUFFER 1024
  8. #define BUFSIZE 64
  9. #define DELIMITER " \t\r\n\a"
  10.  
  11. char* buffer = NULL;
  12.  
  13. int startFunc(char **args){
  14.     pid_t pid, wpid;
  15.     int process_status;
  16.  
  17.     pid = fork();
  18.     if(pid == 0){
  19.                          //Child process
  20.         if(execvp(args[0], args) < 0){
  21.             perror(args[0]);
  22.             free(args);
  23.             free(buffer);
  24.             buffer = NULL;
  25.             exit(1);
  26.         }
  27.     } else if(pid < 0){
  28.                             //Error forking
  29.             perror("fork");
  30.       } else{
  31.                         //Parent process
  32.             waitpid(pid, &process_status, 0);
  33.  
  34.         }
  35.     return 1;
  36. }
  37.  
  38. int executeFunc(char **args){
  39.  
  40.     if(args[0] == NULL){
  41.         return 1;  //An empty command was entered
  42.     }
  43.  
  44.     return startFunc(args);
  45. }
  46.  
  47.  
  48. char *readLine(){
  49.     int bufsize = READLINE_BUFFER;
  50.     int position = 0;
  51.     buffer = malloc(sizeof(char) * bufsize);
  52.     int c;
  53.  
  54.     if(!buffer){
  55.         fprintf(stderr, "No such file or directory\n");
  56.         exit(EXIT_FAILURE);   // an unsuccessfull termination status is returned to the host env
  57.     }
  58.  
  59.     while(1){
  60.                      //reads character
  61.         c = getchar();
  62.         if(c == EOF){
  63.             free(buffer);
  64.             buffer = NULL;
  65.             exit(0);
  66.         }
  67.         if(c == '\n'){
  68.             buffer[position] = '\0';
  69.             return buffer;
  70.         } else{
  71.                 buffer[position] = c;
  72.             }
  73.         position++;
  74.  
  75.         if(position >= bufsize){
  76.             bufsize += READLINE_BUFFER;
  77.             buffer = realloc(buffer, bufsize);
  78.             if(!buffer){
  79.                 fprintf(stderr, "No such file or directory");
  80.                 exit(EXIT_FAILURE); // an unsuccessfull termination status is returned to the host env
  81.             }
  82.         }
  83.     }
  84.  
  85. }
  86.  
  87.  
  88. char **parse_cmdline(const char *line){
  89.     int bufsize = BUFSIZE, pos = 0;
  90.     char **tokens = malloc(bufsize * sizeof(char*));
  91.     char *token;
  92.  
  93.     if(!tokens){
  94.         fprintf(stderr, "No such file or directory\n");
  95.         exit(EXIT_FAILURE); // an unsuccessfull termination status is returned to the host env
  96.     }
  97.     token = strtok((char*)line, DELIMITER); //breaks string str into a series of tokens using the delimiter delim.
  98.     while(token != NULL){
  99.         tokens[pos] = token;
  100.         pos++;
  101.  
  102.         if(pos >= bufsize){
  103.             bufsize += BUFSIZE;
  104.             tokens = realloc(tokens, bufsize * sizeof(char*));
  105.             if(!tokens){
  106.                 fprintf(stderr, "Permission denied");
  107.             }
  108.         }
  109.         token = strtok(NULL , DELIMITER);
  110.     }
  111.     tokens[pos] = NULL;
  112.     return tokens;
  113. }
  114.  
  115. void Cyclecommands(){
  116.     int tmpexe;
  117.     char *cmdline;
  118.     char **arg;
  119.    
  120.     while(tmpexe){
  121.     write(0, "$ ", 2);
  122.         cmdline = readLine();
  123.         arg = parse_cmdline(cmdline);
  124.         tmpexe = executeFunc(arg);
  125.  
  126.         free(cmdline);
  127.     free(arg);
  128.         buffer = NULL;
  129.        
  130.     }
  131.  
  132. }
  133.  
  134. int main(int argc, char **argv){
  135. Cyclecommands();
  136.  
  137. return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement