SHARE
TWEET

myshell_v2

framaras Dec 16th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <unistd.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <stdbool.h>
  8. #include <pwd.h>
  9. #include "rlutil.h"
  10.  
  11. const char *getUserName()
  12. {
  13.     uid_t uid = geteuid();
  14.     struct passwd *pw = getpwuid(uid);
  15.     if (pw)
  16.     {
  17.       return pw->pw_name;
  18.     }
  19.  
  20.     return "";
  21. }
  22.  
  23.  
  24. int main(void)
  25. {
  26.     setColor(RED);
  27.     int s, id;
  28.     char input[100];
  29.     printf("%s@bash: ", getUserName());
  30.     scanf("%s", input);
  31.     while (strcmp(input, "exit"))
  32.     {
  33.         id = fork();
  34.         if (id > 0)
  35.         {
  36.             wait(&s);
  37.             printf("%s@bash: ", getUserName());
  38.             scanf("%s", input);
  39.         }
  40.         else
  41.         {
  42.             execlp(input, input, NULL);
  43.             perror("Error");
  44.             exit(0);
  45.         }
  46.     }
  47.     return 0;
  48. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top