Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <signal.h>
  5. #include <unistd.h>
  6. #include <limits.h>
  7. #include <sys/wait.h>
  8. #include <sys/types.h>
  9. #include <fcntl.h>
  10. #include <sys/stat.h>
  11.  
  12. #define LOGOUT 1000
  13. #define MAXNUM 40
  14. #define MAXLEN 160
  15.  
  16. void sighandler(int sig)
  17. {
  18.         switch (sig) {
  19.                 case SIGALRM:
  20.                         printf("\nautologout\n");
  21.                         exit(0);
  22.                 default:
  23.                         break;
  24.         }
  25.         return;
  26. }
  27.  
  28.  
  29. void histRead(char* currentdir){
  30.         char* history;
  31.         char* line;
  32.         size_t len = 0;
  33.         ssize_t read;
  34.     int id = 1;
  35.  
  36.         FILE *tiedosto;
  37.  
  38.         tiedosto = fopen(currentdir, "r");
  39.         while((read = getline(&line, &len, tiedosto)) != -1) {
  40.                 printf("%d %s", id,  line);
  41.                 id ++;
  42.         }
  43.         fclose(tiedosto);
  44. }
  45.  
  46. char* historynum(int i) {
  47.         char* history;
  48.         char* line;
  49.         size_t len = 0;
  50.         ssize_t read;
  51.         int j = 1, cmd_in=0;
  52.         char * cmd, * args[MAXNUM];
  53.         int pid;
  54.         int background;
  55.     int id = 1;
  56.  
  57.         FILE *tiedosto;
  58.         tiedosto = fopen("history.txt", "r");
  59.         while((read = getline(&line, &len, tiedosto)) != -1) {
  60.                 if (i==j) {            
  61.                         printf("%s", line);
  62.                         cmd = line;
  63.                 break;
  64.                 }
  65.             else {
  66.                         j++;
  67.                 }
  68.          
  69.     }
  70.     fclose(tiedosto);
  71.     return cmd;
  72. }
  73.  
  74. //void run_command(args) {
  75.  
  76.   //    }
  77. int main(void)
  78. {
  79.         char * cmd, line[MAXLEN], * args[MAXNUM];
  80.         int background, i;
  81.         int pid;
  82.         char buff[PATH_MAX + 1];
  83.         char* cwd;
  84.         char buf[512];
  85.         char* joku;
  86.         int cmd_in = 0;
  87.        
  88.         signal(SIGALRM, sighandler);
  89.         signal(SIGINT, sighandler);
  90.  
  91.         char currentdir[1234];
  92.  
  93.     if ((getcwd(currentdir, sizeof(currentdir)) != NULL))
  94.         strcat(currentdir, "/history.txt");
  95.  
  96.         while (1) {
  97.                 background = 0;
  98.                 cwd = getcwd(buff, PATH_MAX + 1);
  99.                
  100.                 /* print the prompt */
  101.                
  102.                 if (cwd != NULL) {
  103.                         printf("%s ", cwd);
  104.                 };
  105.                 /* set the timeout for alarm signal (autologout) */
  106.                 alarm(LOGOUT);
  107.                
  108.                 /* read the users command */
  109.                 if (fgets(line,MAXLEN,stdin) == NULL) {
  110.                         printf("\nlogout\n");
  111.                         exit(0);
  112.                 }
  113.  
  114.                 line[strlen(line) - 1] = '\0';
  115.                
  116.                 if (strlen(line) == 0)
  117.                         continue;
  118.                
  119.                 /* start to background? */
  120.                 if (line[strlen(line)-1] == '&') {
  121.                         line[strlen(line)-1] = 0;
  122.                         background = 1;
  123.                 }
  124.  
  125.                  
  126.                 /* write history log */
  127.                
  128.                 if (strlen(line) != 0) {
  129.                         FILE* tiedosto = fopen(currentdir, "a");
  130.                         fprintf(tiedosto, "%s\n", line);
  131.                         fclose(tiedosto);
  132.                 }
  133.  
  134.                 /* split the command line */
  135.                 i = 0;
  136.                 cmd = line;
  137.  
  138.                 while ( (args[i] = strtok(cmd, " ")) != NULL) {
  139.                         printf("arg %d: %s\n", i, args[i]);
  140.                         i++;
  141.             cmd = NULL;    
  142.                        
  143.                 }
  144. /*tarkistetaan onko kyseessä pipetys*/
  145.         int x=0;
  146.                 pid_t   childpid;
  147.  
  148.                
  149.         while(args[x]){
  150.                     if (strchr(args[x], '|')!=NULL) {
  151.                             printf("PIPE\n");              
  152.                 break;
  153.             }
  154.             x++;       
  155.         }
  156.  
  157. /*tarkistetaan onko kyseessä redirect*/
  158.     /*  int k = 0;
  159.         while(args[k]){
  160.                     if (strchr(args[k], '<')!=NULL || strchr(args[k], '>')!=NULL) {
  161.                             printf("redirect\n");              
  162.                 break;
  163.             }
  164.             k++;
  165.               int in, out;
  166.               char *grep_args[] = {"grep", "line", NULL};
  167.  
  168.               // open input and output files
  169.  
  170.               in = open("ht.c", O_RDONLY);
  171.               out = open("checksum.txt", O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
  172.  
  173.               // replace standard input with input file
  174.  
  175.               dup2(in, 0);
  176.  
  177.               // replace standard output with output file
  178.  
  179.               dup2(out, 1);
  180.  
  181.               // close unused file descriptors
  182.  
  183.               close(in);
  184.               close(out);
  185.         }*/
  186.        
  187.        
  188.  
  189.             int cmd_in = 0;
  190.                 /* print history number */
  191.                 if (strlen(args[0]) >= 2 && args[0][0] == '!' && atoi(args[0]+1)) {
  192.                         int i = atoi(args[0]+1);
  193.                         joku = historynum(i);
  194.                     //printf("%s", joku);
  195.                     joku[strlen(joku) - 1] = '\0';
  196.              
  197.                         while ( (args[cmd_in] = strtok(joku, " ")) != NULL) {
  198.                                 printf("arg %d: %s\n", cmd_in, args[cmd_in]);
  199.                                 cmd_in++;
  200.                                 joku = NULL;
  201.                         }
  202.      
  203.                 }
  204.  
  205.  
  206.                 if (strcmp(args[0],"exit")==0) {
  207.                         exit(0);
  208.                 }
  209.                
  210.  
  211.                 if (strcmp(args[0], "cd")==0) {
  212.                         if (args[1] == NULL) {
  213.                                 const char* home = getenv("HOME");
  214.                                 chdir(home);
  215.                         }
  216.                         char *token = args[1];
  217.                         chdir(token);
  218.                         continue;
  219.                 }
  220.  
  221.                 /* print history log */
  222.                 if (strcmp(args[0],"history")==0) {
  223.                         histRead(currentdir);
  224.                         continue;
  225.                 }
  226.              
  227.         /******************************************************************************/
  228.         /*PIPE*/
  229.               /*  int     fd[2], nbytes;
  230.                 pid_t   childpid;
  231.                 char    string[] = "Hello, world!\n";
  232.                 char    readbuffer[80];
  233.                 int x;
  234.                 pipe(fd);
  235.                
  236.                 while ( (args[x] = strtok(cmd, " ")) != NULL) {
  237.                                 printf("arg %d: %s\n", x, args[x]);
  238.                                 x++;
  239.                                 cmd = NULL;
  240.                 }
  241.                  if (strchr(args[0], '|')!=NULL) {
  242.                         printf("PIPE\n");
  243.                         if((childpid = fork()) == -1) {
  244.                                 perror("fork");
  245.                                 exit(1);
  246.                         }
  247.  
  248.                         if(childpid == 0) {  
  249.                                 /* Child process closes up input side of pipe */
  250.                               // close(fd[0]);
  251.  
  252.                                 /* Send "string" through the output side of pipe */
  253.                             //   write(fd[1], string, (strlen(string)+1));
  254.                               // exit(0);
  255.                         //}
  256.                         //else {
  257.                                 /* Parent process closes up output side of pipe */
  258.                           //      close(fd[1]);
  259.  
  260.                                 /* Read in a string from the pipe */
  261.                             //   nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
  262.                               // printf("Received string: %s", readbuffer);
  263.                         //}
  264.                 //}
  265.  
  266.        
  267. /* end of PIPE*/
  268.  
  269.         /* REDIRECT */
  270.                 /*int in, out;
  271.                 char *grep_args[] = {"grep", "Villanova", NULL};
  272.  
  273.                 // open input and output files
  274.  
  275.                 in = open("scores", O_RDONLY);
  276.                 out = open("out", O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
  277.  
  278.                 // replace standard input with input file
  279.  
  280.                 dup2(in, 0);
  281.  
  282.                 // replace standard output with output file
  283.  
  284.                 dup2(out, 1);
  285.  
  286.                 // close unused file descriptors
  287.  
  288.                 close(in);
  289.                 close(out);
  290.  
  291.                 // execute grep
  292.  
  293.                 execvp("grep", grep_args);
  294.  
  295.  
  296. */
  297.         /* end of REDIRECT */
  298.  
  299.         /*int do_command(char **args, int pipes) {
  300.             // The number of commands to run
  301.             const int commands = pipes + 1;
  302.             int i = 0;
  303.  
  304.             int pipefds[2*pipes];
  305.  
  306.             for(i = 0; i < pipes; i++){
  307.                 if(pipe(pipefds + i*2) < 0) {
  308.                     perror("Couldn't Pipe");
  309.                     exit(EXIT_FAILURE);
  310.                 }
  311.             }
  312.  
  313.             int pid;
  314.             int status;
  315.  
  316.             int j = 0;
  317.             int k = 0;
  318.             int s = 1;
  319.             int place;
  320.             int commandStarts[10];
  321.             commandStarts[0] = 0;
  322.  
  323.             // This loop sets all of the pipes to NULL
  324.             // And creates an array of where the next
  325.             // Command starts
  326.  
  327.             while (args[k] != NULL){
  328.                 if(!strcmp(args[k], "|")){
  329.                     args[k] = NULL;
  330.                     // printf("args[%d] is now NULL", k);
  331.                     commandStarts[s] = k+1;
  332.                     s++;
  333.                 }
  334.                 k++;
  335.             }
  336.  
  337.  
  338.  
  339.             for (i = 0; i < commands; ++i) {
  340.                 // place is where in args the program should
  341.                 // start running when it gets to the execution
  342.                 // command
  343.                 place = commandStarts[i];
  344.  
  345.                 pid = fork();
  346.                 if(pid == 0) {
  347.                     //if not last command
  348.                     if(i < pipes){
  349.                         if(dup2(pipefds[j + 1], 1) < 0){
  350.                             perror("dup2");
  351.                             exit(EXIT_FAILURE);
  352.                         }
  353.                     }
  354.  
  355.                     //if not first command&& j!= 2*pipes
  356.                     if(j != 0 ){
  357.                         if(dup2(pipefds[j-2], 0) < 0){
  358.                             perror("dup2");
  359.                             exit(EXIT_FAILURE);
  360.                         }
  361.                     }
  362.  
  363.                     int q;
  364.                     for(q = 0; q < 2*pipes; q++){
  365.                             close(pipefds[q]);
  366.                     }
  367.  
  368.                     // The commands are executed here,
  369.                     // but it must be doing it a bit wrong          
  370.                     if( execvp(args[place], args) < 0 ){
  371.                             perror(*args);
  372.                             exit(EXIT_FAILURE);
  373.                     }
  374.                 }
  375.                 else if(pid < 0){
  376.                     perror("error");
  377.                     exit(EXIT_FAILURE);
  378.                 }
  379.  
  380.                 j+=2;
  381.             }
  382.  
  383.             for(i = 0; i < 2 * pipes; i++){
  384.                 close(pipefds[i]);
  385.             }
  386.  
  387.             for(i = 0; i < pipes + 1; i++){
  388.                 wait(&status);
  389.             }
  390.         }
  391.         /* fork to run the command */
  392.         /******************************************************************************/
  393.                
  394.  
  395.                 switch (pid = fork()) {
  396.                         case -1:
  397.                                 /* error */
  398.                                 perror("fork");
  399.                                 continue;
  400.                         case 0:
  401.                                 /* child process */
  402.                 /*if (output_mode == 1) {
  403.                     f = fopen(args[outArg], "w");
  404.                     if (f==NULL) {
  405.                         perror("fopen");
  406.                         exit(1);
  407.                     }
  408.                     dup2(fileno(f), fileno(stdout));
  409.                     fclose(f);
  410.                 }
  411.                 if (input_mode == 1) {
  412.                     f = fopen(args[inArg], "r");
  413.                     if (f==NULL) {
  414.                         perror("fopen");
  415.                         exit(1);
  416.                     }
  417.                     dup2(fileno(f), fileno(stdin));
  418.                     fclose(f);
  419.                 }*/
  420.                                 execvp(args[0], args);
  421.                                 perror("execvp");
  422.                                 exit(1);
  423.                         default:
  424.                                 /* parent (shell) */
  425.                                 if (!background) {
  426.                                         alarm(0);
  427.                                         //waitpid(pid, NULL, 0);
  428.                                         while (wait(NULL)!=pid)
  429.                                                 printf("some other child process exited\n");
  430.                                 }
  431.                                 break;
  432.                 }
  433.                
  434.         }
  435.         return 0;
  436. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement