Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.40 KB | None | 0 0
  1.  
  2. //Tomasz Bachosz
  3. //NYU ABU DHABI
  4. //tb1747
  5.  
  6. #define F_OK
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <sys/wait.h>
  12.  
  13. #define MAXLINE 1024  //Max line size
  14. #define MAXARGS 128 //Maximum arguments
  15.  
  16. //Declaring functions
  17. void parselines(const char *cmdline, char **argv);
  18. void pwd();
  19. void cd(char *);
  20. void executeCommands(char **argv,char * cmd_out);
  21. void saveToHistory(char* str);
  22. void showHistory();
  23. void runCommandFromHistory(char* str);
  24. void printExport();
  25. void addVariableToExport(char* argv);
  26. int checkIfCommandExists(char* str);
  27. int splitInput(char input[MAXLINE],char * filename);
  28. //Declaring global variables
  29. char *globalVariables[1024];
  30. char *curPath;
  31.  
  32. //Main function
  33. int main() {
  34.     char input[MAXLINE];
  35.     char *argv[128];
  36.  
  37.     //Setting first global variable
  38.     globalVariables[0]="PATH=/bin";
  39.  
  40.     //Making sure that the history file exists
  41.     FILE *file;
  42.     file = fopen("history.txt", "a");
  43.     fclose(file);
  44.     char cmd_out[MAXLINE];
  45.  
  46.     //Getting input, saving it to history, reading the command
  47.     while (1) {
  48.         printf(">>" );
  49.         char * pipe_c;
  50.          char *temp=cmd_out;
  51.         while(*temp){
  52.             *temp++=' ';
  53.         }
  54.         fgets(input, 1024, stdin);
  55.         saveToHistory(input);
  56.         char *cursor=input;
  57.         int fl=1;
  58.         do{
  59.             fl=1;
  60.             if(pipe_c=strchr(cursor,'|'))
  61.                 *pipe_c++='\0';
  62.             else fl=0;
  63.             char fname[MAXLINE];
  64.  
  65.             if(splitInput(input,fname)){
  66.                 char content[MAXLINE];
  67.                 loadFile(fname,content);
  68.                 char * temp=cmd_out;
  69.                 temp=strchr(cmd_out,'\0');
  70.                 strcpy(temp,content);
  71.  
  72.             };
  73.             parselines(cursor,argv);
  74.             executeCommands(argv,cmd_out);
  75.             if(fl)cursor=pipe_c+1;
  76.  
  77.         }while(fl);
  78.         printf("%s",cmd_out);
  79.     }
  80.  
  81.     return 0;
  82. }
  83.  
  84. void executeCommands(char **argv,char * cmd_out){
  85.     //Finding appropriate command and executing the function
  86.     if (argv[0] == NULL) {
  87.         return;
  88.     }
  89.     if (strcmp(argv[0], "pwd")==0) {
  90.         pwd();
  91.     } else if(strcmp(argv[0], "cd")==0) {
  92.         cd(argv[1]);
  93.     } else if(strcmp(argv[0], "export")==0) {
  94.         if(argv[1]!=NULL){
  95.             //Saving the argument for use.
  96.             char* unkCommand = malloc(MAXLINE);
  97.             strcpy(unkCommand, argv[1]);
  98.             addVariableToExport(unkCommand);
  99.         }else{
  100.             printExport();
  101.         }
  102.     } else if(strcmp(argv[0], "history")==0) {
  103.         showHistory(cmd_out);
  104.     } else if(strcmp(argv[0], "exit")==0){
  105.         exit(0);
  106.     } else {
  107.         //Checking if it is not a recall to history
  108.         static char array[MAXLINE];
  109.         char *unkCommand=array;
  110.         strcpy(unkCommand, argv[0]);
  111.  
  112.         if(unkCommand[0]=='!'){
  113.             //Getting the number to history
  114.             while (*unkCommand && (*unkCommand == '!')){
  115.                 unkCommand++;
  116.             }
  117.             runCommandFromHistory(unkCommand);
  118.  
  119.         } else {
  120.             //Else we check if the command is in PATH. Otherwise not found.
  121.                 char* unkCommand1 = malloc(MAXLINE);
  122.                 strcpy(unkCommand1, argv[0]);
  123.                 if(checkIfCommandExists(unkCommand1)==1){
  124.  
  125.                 int fd[2];
  126.                 int fd_in[2];
  127.                 pipe(fd);
  128.                 pipe(fd_in);
  129.  
  130.                 pid_t pid2 = fork();
  131.  
  132.  
  133.                 if(pid2==0){
  134.  
  135.                     close(fd_in[0]);
  136.                     dup2(fd_in[1],STDOUT_FILENO);
  137.                     close(fd_in[1]);
  138.                     printf("%s",cmd_out);
  139.  
  140.                     exit(1);
  141.                 }
  142.                 pid_t pid = fork();
  143.                 if(pid==0){
  144.  
  145.                     close(fd_in[1]);
  146.                     dup2(fd_in[0],STDIN_FILENO);
  147.  
  148.  
  149.  
  150.                     close(fd[0]);
  151.                     dup2(fd[1],1);
  152.                     close(fd[1]);
  153.                     close(fd_in[0]);
  154.                     if(execv(curPath,argv)==-1){
  155.                         printf("error with subprocess\n");
  156.                         exit(1);
  157.                     }
  158.  
  159.                     exit(1);
  160.                 }
  161.  
  162.                 close(fd_in[0]);
  163.                 close(fd_in[1]);
  164.                 close(fd[1]);
  165.                 waitpid(pid,NULL,0);
  166.                 waitpid(pid2,NULL,0);
  167.                // char cmd_out[MAXLINE];
  168.                 char *temp = cmd_out;
  169.                 while(*temp!=NULL){
  170.                     *temp++='\0';
  171.                 }
  172.  
  173.                 if(pid==0||pid2==0)exit(1);
  174.                 read(fd[0],cmd_out,MAXLINE);
  175.                 close(fd[0]);
  176.                // fgets(cmd_out, MAXLINE, fd[0]);
  177.                 //printf("NO: %s",cmd_out);
  178.  
  179.                 //fflush(stdout);
  180.                 //fflush(stdin);
  181.                 /*
  182.                 char * iter[128];
  183.                 parselines(cmd_out,iter);
  184.  
  185.                 int i=0;
  186.                 //printf("DEBUGGING ###\n###");
  187.                 //printf("%s",iter[i]);
  188.  
  189.                 while(iter[i]!=NULL){
  190.                     printf("%s\n###",iter[i]);
  191.                     i++;
  192.                 }
  193.                 */
  194.                 //CHANGES
  195.                 return;
  196.                 }
  197.                 else{
  198.                     printf("command not found\n");
  199.                 }
  200.             }
  201.     }
  202. }
  203.  
  204. //Checking for command's existance in PATH
  205. int checkIfCommandExists(char* str){
  206.     char *buf = malloc(MAXLINE); //Buffer for global Variable
  207.     char *buf2 = malloc(MAXLINE); //Buffer for reading the path
  208.     char *argv[128];
  209.     char *argv2[128];
  210.     char *delim;
  211.  
  212.     int argc;
  213.  
  214.     //First separating data from globalVariables (PATH and JAVA)
  215.     strcpy(buf2, globalVariables[0]);
  216.     //Setting last character to equal sign
  217.     buf2[strlen(buf2)] = '=';
  218.  
  219.     argc = 0;
  220.     delim = strchr(buf2, '=');
  221.     //Replacing = to \0 and taking in between as arguments
  222.     //Here we care only about command
  223.     while (delim) {
  224.         argv2[argc++] = buf2;
  225.         *delim = '\0';
  226.         buf2 = delim + 1;
  227.         while (*buf2 && (*buf2 == '='))
  228.             buf2++;
  229.  
  230.             delim = strchr(buf2, '=');
  231.     }
  232.     argv2[argc] = NULL;
  233.  
  234.     //Setting a flag to check whether access was granted
  235.     int isThereAccess=0;
  236.  
  237.     //If there is no PATH in a variable we know that access won't be there
  238.     //so we can skip
  239.  
  240.     if(argv2[1]!=NULL){
  241.         strcpy(buf, argv2[1]);
  242.  
  243.         //Similarily like above with =, we do same algorithm with :
  244.         buf[strlen(buf)] = ':'; // check this
  245.  
  246.         argc = 0;
  247.         delim = strchr(buf, ':');
  248.  
  249.         while (delim) {
  250.             argv[argc++] = buf;
  251.             *delim = '\0';
  252.             buf = delim + 1;
  253.             while (*buf && (*buf == ':'))
  254.                 buf++;
  255.  
  256.                 delim = strchr(buf, ':');
  257.         }
  258.  
  259.         //Setting the last element to NULL
  260.         argv[argc] = NULL;
  261.  
  262.         int i=0;
  263.         //Iterating through paths and adding command to it
  264.         //If access granted, setting the flag
  265.  
  266.  
  267.         while(argv[i]!=NULL){
  268.  
  269.             strcat(argv[i],"/");
  270.             strcat(argv[i],str);
  271.             if(access(argv[i],F_OK)==0){
  272.                 curPath=argv[i];
  273.                 isThereAccess=1;
  274.  
  275.                 break;
  276.             }
  277.             i++;
  278.         }
  279.  
  280.  
  281.  
  282.     }
  283.  
  284.     //Returning a value according to the flag
  285.     if(isThereAccess){
  286.         return 1;
  287.     }else{
  288.         return 0;
  289.     }
  290. }
  291.  
  292. //Adding global variables
  293. void addVariableToExport(char* str){
  294.     char *tempArray[128];
  295.     char *tempVarName[128];
  296.  
  297.     static char array[MAXLINE];
  298.     static char array2[MAXLINE];
  299.  
  300.     char *tempVarPath = array2; //Holds full command entered
  301.     char *tempPath = array;     //Will be used as a buffer pointer
  302.     char *delim;
  303.     int argc;                   //Arguments counter
  304.  
  305.     //Creating local copies
  306.     strcpy(tempVarPath,str);
  307.     strcpy(tempPath, str);
  308.  
  309.     //Getting rid of leading spaces, if any
  310.     while (*tempPath && (*tempPath == ' ')){
  311.         tempPath++;
  312.     }
  313.  
  314.     argc = 0;
  315.     delim = strchr(tempPath, '=');
  316.     //Replacing = to \0 and taking in between as arguments
  317.     while (delim) {
  318.         tempArray[argc++] = tempPath;
  319.         *delim = '\0';
  320.         tempPath = delim + 1;
  321.         while (*tempPath && (*tempPath == ' '))
  322.             tempPath++;
  323.  
  324.             delim = strchr(tempPath, ' ');
  325.     }
  326.  
  327.     //Now tempArray[0] is the Variable name
  328.     //and tempPath is the path entered.
  329.  
  330.     //Setting the last element to NULL
  331.     tempArray[argc] = NULL;
  332.     //Setting flag to check whether element already
  333.     //exists in local variables
  334.  
  335.  
  336.  
  337.     int isNewFlag = 0;
  338.     int counter=0;
  339.     while(globalVariables[counter]!=NULL){
  340.         //Working on local copy of a variable
  341.         strcpy(tempPath, globalVariables[counter]);
  342.         tempPath[strlen(tempPath)] = '=';
  343.         argc = 0;
  344.         delim = strchr(tempPath, '=');
  345.         //Replacing = to \0 and taking in between as arguments
  346.         while (delim) {
  347.             tempVarName[argc++] = tempPath;
  348.             *delim = '\0';
  349.             tempPath = delim + 1;
  350.             while (*tempPath && (*tempPath == '='))
  351.                 tempPath++;
  352.  
  353.                 delim = strchr(tempPath, '=');
  354.         }
  355.         //If variable from counter is same as variableName entered
  356.         //we overwrite current variable
  357.         if(strcmp(tempArray[0], tempVarName[0])==0){
  358.             globalVariables[counter]=str;
  359.             isNewFlag=1;
  360.             break;
  361.         }
  362.  
  363.         counter++;
  364.     }
  365.  
  366.     if(isNewFlag==0){
  367.         //If variable was not found in globalVariables
  368.         //we add a new variable
  369.         int counterx = 0;
  370.         while(globalVariables[counterx]!=NULL)
  371.         {
  372.             counterx++;
  373.         }
  374.         globalVariables[counterx]=str;
  375.     }
  376.  
  377. }
  378.  
  379. //Function to print all local variables
  380. void printExport(){
  381.     int i = 0;
  382.     while(globalVariables[i]!=NULL){
  383.         printf("%s \n", globalVariables[i]);
  384.         i++;
  385.     }
  386. }
  387.  
  388. //Parsing the command line
  389. void parselines(const char *cmdline, char **argv) {
  390.     static char array[MAXLINE]; //Command line stored locally
  391.     char *buf = array;          //Pointer to go through the cl
  392.     char *delim;                //Delimeter
  393.     int argc;
  394.  
  395.     //Creating a local copy
  396.     strcpy(buf, cmdline);
  397.     //Changing the last character to space for the algorithm
  398.     buf[strlen(buf)-1] = ' ';
  399.     //Deleting spaces in the beginning
  400.     while (*buf && (*buf == ' '))
  401.         buf++;
  402.  
  403.     argc = 0;
  404.     delim = strchr(buf, ' ');
  405.     //Replacing spaces to \0 and taking in between as arguments
  406.     while (delim) {
  407.         argv[argc++] = buf;
  408.         *delim = '\0';
  409.         buf = delim + 1;
  410.         while (*buf && (*buf == ' '))
  411.             buf++;
  412.  
  413.             delim = strchr(buf, ' ');
  414.     }
  415.     //Setting the last element to NULL
  416.     argv[argc] = NULL;
  417. }
  418.  
  419. //Function for pwd command
  420. void pwd()
  421. {
  422.     char cwd[1024];
  423.     getcwd(cwd, sizeof(cwd));
  424.     printf("%s \n", cwd);
  425. }
  426.  
  427. //Function for change directory command
  428. void cd(char *path) {
  429.     chdir(path);
  430. }
  431.  
  432. //Function for running command from history
  433. void runCommandFromHistory(char* number){
  434.     FILE *file;
  435.     char str[1024];
  436.     char* filename = "history.txt";
  437.     int counter = 1;
  438.     char *argv[128];
  439.     char cmd_out[MAXLINE];
  440.  
  441.     static char array[MAXLINE];
  442.     char *newInteger = array;
  443.  
  444.     //Creating a local copy
  445.     strcpy(newInteger, number);
  446.     int numberCall = atoi(newInteger);
  447.  
  448.     file = fopen(filename, "r");
  449.     if (file == NULL){
  450.         printf("Could not open file %s",filename);
  451.     }
  452.     while (fgets(str, 1024, file) != NULL){
  453.         if(counter==numberCall){
  454.             //Executing the command under this counter number
  455.             parselines(str, argv);
  456.             executeCommands(argv,cmd_out);
  457.  
  458.         }
  459.         counter++;
  460.     }
  461.  
  462.     fclose(file);
  463. }
  464.  
  465. // Function to save commands in history file
  466. void saveToHistory(char* str)
  467. {
  468.     FILE *file;
  469.     file = fopen("history.txt", "a");
  470.     fprintf(file, "%s", str);
  471.     fclose(file);
  472. }
  473.  
  474. //Function to show history
  475. void showHistory(char *cmd_out)
  476. {
  477.     FILE *file;
  478.     char str[1024];
  479.     char* filename = "history.txt";
  480.     int counter = 1;
  481.  
  482.     file = fopen(filename, "r");
  483.     if (file == NULL){
  484.         printf("Could not open file %s",filename);
  485.         return;
  486.     }
  487.     char out[MAXLINE];
  488.     char * temp=out;
  489.     while (fgets(str, 1024, file) != NULL){
  490.         sprintf(temp,"%d %s\0", counter, str);
  491.         counter++;
  492.         temp=strchr(temp,'\0');
  493.  
  494.     }
  495.     strcpy(cmd_out,out);
  496.     fclose(file);
  497.  
  498. }
  499. int splitInput(char input[MAXLINE], char * filename){
  500.         char * f;
  501.         if(!(f=strchr(input,'<')))return 0;
  502.         *f++='\0';
  503.  
  504.         while(*f&&*f==' '){
  505.             f++;
  506.         }
  507.         f=strcat(f,"\0");
  508.         strcpy(filename,f);
  509.         return 1;
  510. }
  511. int loadFile(char * filename, char * content){
  512.      char * f;
  513.      f=filename;
  514.      while(*f&&(*f!=' '&&*f!='\n')){
  515.         f++;
  516.     }
  517.     *f='\0';
  518.  
  519.     FILE* file = fopen(filename, "r");
  520.  
  521.       if (file == NULL){
  522.         printf("Could not open file %s",filename);
  523.         return 0;
  524.     }
  525.     char * temp=content;
  526.     char str[MAXLINE];
  527.         while (fgets(str, 1024, file) != NULL){
  528.         temp=strcat(temp,str);
  529.     }
  530.     temp=strcat(temp,"\0");
  531. return 1;
  532. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement