Advertisement
Guest User

p11

a guest
Mar 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/wait.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <fcntl.h>
  7. #include <string.h>
  8.  
  9. const int BUF_SIZE = 1024;
  10. const int TOK_BUF_SIZE = 64;
  11. const char DELIM[] = " ";
  12.  
  13. static int isOutput = 0;
  14. static char *path;
  15. static size_t size = 0;
  16.  
  17. void msh_loop(void);
  18. char* msh_readLine(void);
  19. char** msh_readArgs(char *line);
  20. int msh_executeCommand(char **args);
  21. void msh_releaseMemory(char *line, char **args, char *path);
  22. void mhs_removeChar(char *str, char garbage);
  23.  
  24. int main(int argc, char* argv[]) {
  25.  
  26.     msh_loop();
  27.  
  28.     return 0;
  29. }
  30.  
  31. void msh_loop(void) {
  32.  
  33.     char *line;
  34.     char **args;
  35.     int status;
  36.  
  37.     do {
  38.         printf("minish > ");
  39.        
  40.         line = msh_readLine();
  41.         args = msh_readArgs(line);
  42.         status = msh_executeCommand(args);
  43.  
  44.         msh_releaseMemory(line, args, path);
  45.     }
  46.     while(status);
  47. }
  48.  
  49. char* msh_readLine(void) {
  50.  
  51.     int i = 0;
  52.     char *buffer = malloc(sizeof(char) * BUF_SIZE);
  53.  
  54.     if(buffer == NULL) {
  55.         perror("allocatting memory for buffer");
  56.         exit(1);
  57.     }
  58.  
  59.     //Reads input from console
  60.     while(1) {
  61.  
  62.         char c = getchar();
  63.  
  64.         if(c == EOF || c == '\n')
  65.             break;
  66.         else
  67.             buffer[i] = c;
  68.  
  69.         i++;
  70.     }
  71.  
  72.     return buffer;
  73. }
  74.  
  75. char** msh_readArgs(char *line) {
  76.  
  77.     if(line == NULL)
  78.         return NULL;
  79.    
  80.     int i = 0;
  81.  
  82.     char *token;
  83.     char **tokens = malloc(TOK_BUF_SIZE * sizeof(char*));
  84.  
  85.     if(tokens == NULL) {
  86.         perror("allocatting memory");
  87.         exit(1);
  88.     }
  89.  
  90.     token = strtok(line, DELIM);
  91.  
  92.     //Separate words from line
  93.     while(token != NULL) {
  94.  
  95.         //No need to save "-o outputFileName" in args
  96.         if(strcmp(token, "-o") == 0) {
  97.             isOutput = 1;
  98.             i++;
  99.             break;
  100.         }  
  101.  
  102.         tokens[i] = malloc(strlen(token) * sizeof(char));
  103.  
  104.         if(tokens[i] == NULL) {
  105.             perror("allocatting memory");
  106.             exit(1);
  107.         }
  108.  
  109.         strcpy(tokens[i], token);
  110.  
  111.         i++;
  112.         token = strtok(NULL, DELIM);
  113.     }
  114.  
  115.     size = i - 1;
  116.  
  117.     tokens[i] = NULL;
  118.  
  119.     //Save output filename
  120.     if(isOutput == 1) {
  121.  
  122.         //Get output file from line
  123.         token = strtok(NULL, DELIM);
  124.         mhs_removeChar(token, '\n');
  125.  
  126.         const char* txt = ".txt";
  127.  
  128.         path = malloc(strlen(token) + 1 + 4);
  129.  
  130.         if(path == NULL) {
  131.             perror("allocating memory");
  132.             exit(1);
  133.         }
  134.  
  135.         strcpy(path, token);
  136.         strcat(path, txt);
  137.     }
  138.     else
  139.         mhs_removeChar(tokens[i - 1], '\n');
  140.  
  141.     return tokens;
  142. }
  143.  
  144. int msh_executeCommand(char **args) {
  145.  
  146.     if(args == NULL)
  147.         return 1;
  148.  
  149.     pid_t pid;
  150.  
  151.     pid = fork();
  152.  
  153.     if(pid < 0)
  154.         perror("fork");
  155.     else if(pid > 0) {
  156.         int status;
  157.         waitpid(pid, &status, 0);
  158.     }
  159.     else if(pid == 0) {
  160.  
  161.         if(isOutput == 1) {
  162.  
  163.             int fd = open(path, O_CREAT | O_WRONLY, S_IRUSR);
  164.  
  165.             if(fd < 0) {
  166.                 perror("creating file.");
  167.                 exit(1);
  168.             }
  169.            
  170.             if(dup2(fd, 1) < 0) {
  171.                 perror("redirectioning output.");
  172.                 exit(1);
  173.             }
  174.  
  175.             if(fd != STDOUT_FILENO)
  176.                 close(fd);
  177.  
  178.             isOutput = 0;
  179.         }
  180.  
  181.         if(execvp(args[0], args) == -1)
  182.             perror("exec");
  183.     }
  184.  
  185.     return 1;
  186. }
  187.  
  188. void msh_releaseMemory(char *line, char **args, char *path) {
  189.  
  190.     if(line == NULL)
  191.         return;
  192.     else
  193.         free(line);
  194.  
  195.     if(args == NULL)
  196.         return;
  197.     else {
  198.  
  199.         int i;
  200.         for(i = 0; i < size; i++)
  201.             free(args[i]);
  202.  
  203.         free(args);
  204.        
  205.         size = 0;
  206.     }
  207.  
  208.     if(path == NULL)
  209.         return;
  210.     else
  211.         free(path);
  212.  
  213. }
  214.  
  215. void mhs_removeChar(char *str, char garbage) {
  216.  
  217.     char *src, *dst;
  218.     for (src = dst = str; *src != '\0'; src++) {
  219.         *dst = *src;
  220.  
  221.         if (*dst != garbage)
  222.             dst++;
  223.     }
  224.  
  225.     *dst = '\0';
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement