Guest User

Untitled

a guest
Apr 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.73 KB | None | 0 0
  1. //  Coded by Jeffrey Garcia
  2.  
  3. //  Everything should work fine, fully supports cd
  4.  
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <sys/wait.h>
  11.  
  12. void parseline(char *line, char **args) {
  13.         while(*line != '\0') {
  14.                 while(*line == ' ' || *line == '\n' || *line == '\t')
  15.                         *line++ = '\0';
  16.                 *args++ = line;
  17.                 while(*line != ' ' &&  *line != '\n' && *line != '\t' &&
  18.                 *line != '\0')
  19.                         line++;
  20.         }
  21.         *args--;
  22.         *args = '\0';
  23. }
  24.  
  25. void exec(char **args) {
  26.         int pid;
  27.  
  28.         if((pid = fork()) < 0) {
  29.                 printf( "The fork failed.\n");
  30.                 exit(1);
  31.         }
  32.         else if(pid > 0) {
  33.                 if((strcmp(args[0], "cd") == 0)) {  // || strcmp(args[0], "chdir") == 0 || strcmp(args[0], "dir") == 0)) {
  34.  
  35.                         if(!(chdir(args[1])==0)) printf("chdir failed.\n");
  36.                 }
  37.                 wait(NULL);
  38.         }
  39.         else if(pid == 0) {
  40.                 if((strcmp(args[0], "cd") == 0)) exit(0);  // || strcmp(args[0], "chdir") == 0 || strcmp(args[0], "dir") == 0)) {
  41.                 else if(execvp(args[0], args) < 0) {
  42.                         printf("Exec failed.\n");
  43.                         exit(1);
  44.                 }
  45.         }
  46. }
  47.  
  48. int main() {
  49.         char buffer[256], *cptr;
  50.         char *args[64];
  51.  
  52.         while(1) {
  53.                 printf("[jeffshell]: ");
  54.                 cptr = fgets(buffer, 256, stdin);
  55.                 parseline(cptr, args);
  56.                 if(strcmp(args[0], "exit") == 0) exit(0);
  57.                 exec(args);
  58.         }
  59.         return(0);
  60. }
Add Comment
Please, Sign In to add comment