Guest User

Untitled

a guest
Aug 30th, 2017
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.91 KB | None | 0 0
  1. #include <sys/wait.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. #include <pwd.h>
  9. #include <signal.h>
  10.  
  11. #define LSH_TOK_BUFSIZE 64
  12. #define LSH_TOK_DELIM " \t\r\n\a"
  13. #define KSH_SPLIT_COMMAND ";"
  14.  
  15.  
  16. char homeDir[1000],hostname[1000];
  17.  
  18.  
  19.  
  20. char *username;
  21.  
  22. void p(char * l) {
  23.    printf("%s\n",l);
  24.  }
  25.  
  26. void removeSubstring(char *s,const char *toremove)
  27. {
  28.   while( s=strstr(s,toremove) )
  29.     memmove(s,s+strlen(toremove),1+strlen(s+strlen(toremove)));
  30. }
  31.  
  32. void printStylo(char * cwd)
  33. {
  34.     char *temp_cwd;
  35.     if(strstr(cwd,homeDir)!=NULL)
  36.     {
  37.       removeSubstring(cwd,homeDir);
  38.     }
  39.     fprintf(stdout, "\033[33;1m%s@%s:\033[0m\e[32m%s$\033[0m", username, hostname, cwd);
  40. }
  41.  
  42.  
  43. char *lsh_read_line(void)
  44. {
  45.     //printf("This is read\n");
  46.     char *line = NULL;
  47.     ssize_t bufsize = 0; // have getline allocate a buffer for us
  48.     getline(&line, &bufsize, stdin);
  49.     return line;
  50. }
  51.  
  52. char **lsh_split_line(char *line,int flag)
  53. {
  54.     //printf("This is split\n");
  55.     int bufsize = LSH_TOK_BUFSIZE, position = 0;
  56.     char **tokens = malloc(bufsize * sizeof(char*));
  57.     char *token;
  58.     if (!tokens) {
  59.         fprintf(stderr, "KShell: allocation error\n");
  60.         exit(EXIT_FAILURE);
  61.     }
  62.     //printf("Line:%s",line);
  63.     if(flag==0)
  64.     token = strtok(line, LSH_TOK_DELIM);
  65.     else
  66.     token = strtok(line, KSH_SPLIT_COMMAND);
  67.     while (token != NULL) {
  68.         //printf("Before:%s",token);
  69.         tokens[position] = token;
  70.         position++;
  71.         if (position >= bufsize) {
  72.             bufsize += LSH_TOK_BUFSIZE;
  73.             tokens = realloc(tokens, bufsize * sizeof(char*));
  74.             if (!tokens) {
  75.                 fprintf(stderr, "KShell: allocation error\n");
  76.                 exit(EXIT_FAILURE);
  77.             }
  78.         }
  79.         if(flag==0)
  80.             token = strtok(NULL, LSH_TOK_DELIM);
  81.         else
  82.             token = strtok(NULL, KSH_SPLIT_COMMAND);
  83.         //printf("After:%s",token);
  84.     }
  85.     tokens[position] = NULL;
  86.     return tokens;
  87. }
  88.  
  89. int lsh_launch(char **args)
  90. {
  91.     //printf("This is launch\n");
  92.     pid_t pid, wpid;
  93.     int status,bufsize=LSH_TOK_BUFSIZE;
  94.     //printf("args:%s\n",args[0]);
  95.     pid = fork();
  96.     if (pid == 0) {
  97.         // Child process
  98.         //printf("This is child\n");
  99.         if (execvp(args[0], args) == -1) {
  100.             perror("KShell Error:");
  101.         }
  102.         exit(EXIT_FAILURE);
  103.     } else if (pid < 0) {
  104.         // Error forking
  105.         perror("KShell Error");
  106.     } else {
  107.         //printf("This is parent\n");
  108.         // Parent process
  109.         do {
  110.             wpid = waitpid(pid, &status, WUNTRACED);
  111.         } while (!WIFEXITED(status) && !WIFSIGNALED(status));
  112.         //exit loop if the child process terminated normally
  113.     }
  114.     return 1;
  115. }
  116.  
  117. int lsh_execute(char **args)
  118. {
  119.     //printf("\nThis is exec args:%s\n",args[0]);
  120.     if (args[0] == NULL) {
  121.         // An empty command was entered.
  122.         return 1;
  123.     }
  124.     if(strcmp("quit",args[0])==0){
  125.         //printf("fu\n");
  126.         return 0;
  127.     }
  128.     return lsh_launch(args);
  129. }
  130.  
  131. int ksh_run(char **commands)
  132. {
  133.     int run=1,status;
  134.     char **args;
  135.     pid_t pid,wpid;
  136.     for(int i=0;commands[i]!=NULL;i++)
  137.         {
  138.             if(strcmp("quit",commands[i])==0){
  139.                 run=0;
  140.             }
  141.  
  142.             pid=fork();
  143.             if(pid==0)
  144.             {
  145.                 //printf("i:%d\ncommand:%s\n",i,commands[i]);
  146.                 args = lsh_split_line(commands[i],0);
  147.                 run=lsh_execute(args);
  148.                 exit(EXIT_FAILURE);
  149.                 printf("uff\n");
  150.             }
  151.         }
  152.         do {
  153.             wpid = waitpid(pid, &status, WUNTRACED);
  154.         } while (!WIFEXITED(status) && !WIFSIGNALED(status));
  155.     return run;
  156. }
  157.  
  158. void lsh_loop(void)
  159. {
  160.     char *line;
  161.     char **commands;
  162.     int status,flag,run=1;
  163.  
  164.   char home[]="HOME:";
  165.   if(gethostname(hostname, sizeof(hostname)) == -1)
  166.         perror("gethostname() error\n");
  167.  
  168.   char cwd[1000];
  169.   getcwd(cwd, 1000);
  170.  
  171.   strcat(home,cwd);
  172.   strcpy(homeDir,cwd);
  173.  
  174.   struct passwd *pass;
  175.     pass = getpwuid(getuid());
  176.     username = pass->pw_name;
  177.  
  178.     do {
  179.         flag=1;
  180.         printStylo(cwd);
  181.         line = lsh_read_line();
  182.         commands=lsh_split_line(line,1);
  183.         run=ksh_run(commands);
  184.     } while (run);
  185. }
  186.  
  187. int main(int argc, char **argv)
  188. {
  189.     //printf("This is main\n");
  190.     lsh_loop();
  191.     return EXIT_SUCCESS;
  192. }
Add Comment
Please, Sign In to add comment