Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/wait.h>
  5. #include <string.h>
  6.  
  7. struct command {
  8.     char *prog;
  9.     char **args;
  10. };
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14.     char *path = "/bin/";
  15.  
  16.     while (1) {
  17.            
  18.         char input[512];
  19.         printf("$sshell ");
  20.        
  21.         scanf("%s", input);
  22.  
  23.         if (strcmp(input, "exit") == 0) {
  24.             exit(0);
  25.         } else {
  26.  
  27.             char *cmd = malloc(strlen(path) + strlen(input) + 1);
  28.            
  29.             if (cmd == NULL) {
  30.                 printf("Malloc failed.\n");
  31.                 exit(69);
  32.             }
  33.  
  34.             strcat(cmd, path);
  35.             strcat(cmd, input);
  36.             printf("%s\n", cmd);
  37.        
  38.             char *args[] = {cmd, NULL};
  39.             pid_t PID = fork();
  40.            
  41.  
  42.             if (PID == 0) {
  43.                 int status;
  44.                 status = execvp(cmd, args);
  45.                 if (status != 0) {
  46.                     fprintf(stderr, "Error: command not found.\n");
  47.                     exit(42);
  48.                 } else {
  49.                     exit(0);
  50.                 }
  51.             } else {
  52.                 int status;
  53.                 wait(&status);
  54.                 fprintf(stderr, "+ completed '%s %s' [%d]\n", path, args[1], WEXITSTATUS(status));
  55.             }
  56.         }
  57.     }
  58.     return EXIT_SUCCESS;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement