Guest User

Untitled

a guest
Nov 17th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <linux/limits.h>
  5. #include <ctype.h>
  6. #include <unistd.h>
  7.  
  8. void run(char *filename, char *cmd);
  9.  
  10. int main() {
  11.     char input[ARG_MAX], cmd[ARG_MAX];
  12.    
  13.     while (1) {
  14.         printf("$ ");
  15.         fgets(input, ARG_MAX-1, stdin);
  16.  
  17.         // "ls"
  18.         // "ls -la"
  19.         // "kill -9 pid"
  20.        
  21.         // Read the command from the input
  22.         int i;
  23.         for (i = 0; i < ARG_MAX; i++) {
  24.             if (i == 0 && input[i] == '\n')
  25.                 cmd[i] = '\0';
  26.             else if (isspace(input[i])) {
  27.                 break;             
  28.             }
  29.             else
  30.                 cmd[i] = input[i];
  31.         }
  32.  
  33.         if (!strcmp("ls", cmd)) {
  34.             run("/bin/ls", "ls");
  35.         }
  36.         else if (!strcmp("pwd", cmd)) {
  37.             run("/bin/pwd", "pwd");
  38.         }
  39.         else if (!strcmp("ps", cmd)) {
  40.             run("/bin/ps", "ps");
  41.         }
  42.         else if (!strcmp("kill", cmd)) {
  43.             run("/bin/kill", "kill");
  44.         }
  45.         else {
  46.             run(cmd, cmd);
  47.         }
  48.     }
  49.     return 0;
  50. }
  51.  
  52. void run(char *filename, char *cmd) {
  53.     pid_t child = fork();
  54.     if (child == 0)
  55.         execlp(filename, cmd, NULL);
  56.     else
  57.         wait(child);
  58.     return;
  59. }
Add Comment
Please, Sign In to add comment