Guest User

Untitled

a guest
Aug 30th, 2017
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.15 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. #define LSH_TOK_BUFSIZE 64
  11. #define LSH_TOK_DELIM " \t\r\n\a"
  12.  
  13. #define PARALLEL_DELIM "&"
  14.  
  15. char homeDir[1000],hostname[1000];
  16.  
  17. char *username;
  18.  
  19. void p(char * l) {
  20.    printf("%s\n",l);
  21.  }
  22.  
  23. void removeSubstring(char *s,const char *toremove)
  24. {
  25.   while( s=strstr(s,toremove) )
  26.     memmove(s,s+strlen(toremove),1+strlen(s+strlen(toremove)));
  27. }
  28.  
  29. void printStylo(char * cwd)
  30. {
  31.     char s=' ';
  32.     if(strstr(cwd,homeDir)!=NULL)
  33.     {
  34.       removeSubstring(cwd,homeDir);
  35.       s='~';
  36.     }
  37.     fprintf(stdout, "\033[33;1m%s@%s:\033[0m\e[32m%c%s$\033[0m", username, hostname,s, cwd);
  38. }
  39.  
  40.  
  41. char *lsh_read_line(void)
  42. {
  43.     char *line = NULL;
  44.     ssize_t bufsize = 0; // have getline allocate a buffer for us
  45.     getline(&line, &bufsize, stdin);
  46.     return line;
  47. }
  48.  
  49. char **lsh_split_line(char *line, char * delim)
  50. {
  51.     int bufsize = LSH_TOK_BUFSIZE, position = 0;
  52.     char **tokens = malloc(bufsize * sizeof(char*));
  53.     char *token;
  54.     if (!tokens) {
  55.         fprintf(stderr, "lsh: allocation error\n");
  56.         exit(EXIT_FAILURE);
  57.     }
  58.     token = strtok(line, delim);
  59.     while (token != NULL) {
  60.         tokens[position] = token;
  61.         position++;
  62.         if (position >= bufsize) {
  63.             bufsize += LSH_TOK_BUFSIZE;
  64.             tokens = realloc(tokens, bufsize * sizeof(char*));
  65.             if (!tokens) {
  66.                 fprintf(stderr, "lsh: allocation error\n");
  67.                 exit(EXIT_FAILURE);
  68.             }
  69.         }token = strtok(NULL, LSH_TOK_DELIM);
  70.     }
  71.     tokens[position] = NULL;
  72.     return tokens;
  73. }
  74.  
  75. int lsh_launch(char **args)
  76. {
  77.     pid_t pid, wpid;
  78.     int status;
  79.     pid = fork();
  80.     if (pid == 0) {
  81.         // Child process
  82.         if (execvp(args[0], args) == -1) {
  83.             perror("lsh");
  84.         }
  85.         exit(EXIT_FAILURE);
  86.     } else if (pid < 0) {
  87.         // Error forking
  88.         perror("lsh");
  89.     } else {
  90.         // Parent process
  91.         do {
  92.     printf("parent process\n");
  93.             wpid = waitpid(pid, &status, WUNTRACED);
  94.         } while (!WIFEXITED(status) && !WIFSIGNALED(status));
  95.         //exit loop if the child process terminated normally
  96.     }
  97.   printf("parent process exited\n");
  98.     //printf("%s process of pid:%d is exited with status:%d",args[0],pid,status);
  99.     return 1;
  100. }
  101.  
  102. /*
  103.    Function Declarations for builtin shell commands:
  104.  */
  105. int lsh_cd(char **args);
  106. int lsh_help(char **args);
  107. int lsh_exit(char **args);
  108. int pinfo(char **args);
  109. /*
  110.    List of builtin commands, followed by their corresponding functions.
  111.  */
  112. char *builtin_str[] = {
  113.     "cd",
  114.     "help",
  115.     "exit",
  116.   "pinfo"
  117. };
  118. int (*builtin_func[]) (char **) = {
  119.     &lsh_cd,&lsh_help,
  120.     &lsh_exit,&pinfo
  121. };
  122. int lsh_num_builtins() {
  123.     return sizeof(builtin_str) / sizeof(char *);
  124. }
  125. /*
  126.    Builtin function implementations.
  127.  */
  128. int lsh_cd(char **args)
  129. {
  130.     if (args[1] == NULL) {
  131.         fprintf(stderr, "lsh: expected argument to \"cd\"\n");
  132.     } else {
  133.         if (chdir(args[1]) != 0) {
  134.             perror("lsh");
  135.         }
  136.     }
  137.     return 1;
  138. }
  139.  
  140.  
  141. int lsh_help(char **args)
  142. {
  143.     int i;
  144.     printf("Stephen Brennan's LSH\n");
  145.     printf("Type program names and arguments, and hit enter.\n");
  146.     printf("The following are built in:\n");
  147.     for (i = 0; i < lsh_num_builtins(); i++) {
  148.         printf(" %s\n", builtin_str[i]);
  149.     }
  150.     printf("Use the man command for information on other programs.\n");
  151.     return 1;
  152. }
  153.  
  154. int lsh_exit(char **args)
  155. {
  156.     return 0;
  157. }
  158.  
  159. int lsh_execute(char **args)
  160. {
  161.     int i;
  162.     if (args[0] == NULL) {
  163.         // An empty command was entered.
  164.         return 1;}
  165.     for (i = 0; i < lsh_num_builtins(); i++) {
  166.         if (strcmp(args[0], builtin_str[i]) == 0) {
  167.             return (*builtin_func[i])(args);
  168.         }
  169.     }
  170.     return lsh_launch(args);
  171. }
  172.  
  173. int ksh_run(char **commands)
  174. {
  175.     int run=1,status;
  176.     char **args;
  177.     pid_t pid,wpid;
  178.     for(int i=0;commands[i]!=NULL;i++)
  179.         {
  180.             if(strcmp("quit",commands[i])==0){
  181.                 run=0;
  182.             }
  183.  
  184.             pid=fork();
  185.             if(pid==0)
  186.             {
  187.       //  printf("Child processs\n");
  188.                 //printf("i:%d\ncommand:%s\n",i,commands[i]);
  189.                 args = lsh_split_line(commands[i],LSH_TOK_DELIM);
  190.                 run=lsh_execute(args);
  191.  
  192.                 exit(EXIT_FAILURE);
  193.                 printf("uff\n");
  194.             }
  195.         }
  196.     //printf("parent processs\n");
  197.         // do {
  198.         //  wpid = waitpid(pid, &status, WUNTRACED);
  199.         // } while (!WIFEXITED(status) && !WIFSIGNALED(status));
  200.     return run;
  201. }
  202.  
  203. void lsh_loop(void)
  204. {
  205.     char *line;
  206.     char **commands;
  207.     int status,flag,run=1;
  208.  
  209.   char home[]="HOME:";
  210.   if(gethostname(hostname, sizeof(hostname)) == -1)
  211.         perror("gethostname() error\n");
  212.  
  213.   char cwd[1000];
  214.   getcwd(cwd, 1000);
  215.  
  216. //  strcat(home,cwd);
  217.   strcpy(homeDir,cwd);
  218.  
  219.   struct passwd *pass;
  220.     pass = getpwuid(getuid());
  221.     username = pass->pw_name;
  222.  
  223.     do {
  224.         flag=1;
  225.     getcwd(cwd, 1000);
  226.         printStylo(cwd);
  227.     line = lsh_read_line();
  228.     if(strstr(line,"&")!=NULL)
  229.         {
  230.       commands = lsh_split_line(line,PARALLEL_DELIM);
  231.           run=ksh_run(commands);
  232.     }
  233.     else{
  234.       commands=lsh_split_line(line,LSH_TOK_DELIM);
  235.       run=lsh_execute(commands);
  236.     }
  237.     //printf("done running\n");
  238.         free(line);
  239.         free(commands);
  240.     } while (run);
  241. }
  242.  
  243.  
  244. int main(int argc, char **argv)
  245. {
  246.     // Load config files, if any.
  247.     // Run command loop.
  248.     lsh_loop();
  249.     // Perform any shutdown/cleanup.
  250.     return EXIT_SUCCESS;
  251. }
Add Comment
Please, Sign In to add comment