Guest User

Untitled

a guest
Nov 21st, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <wait.h>
  4. #include <sys/types.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7.  
  8. #define SIZE_BUFF 127
  9.  
  10. void main(int argc, char const *argv[])
  11. {
  12. char str[SIZE_BUFF];
  13. char *arg[3];
  14. arg[2] = NULL;
  15. char *name_prog = NULL;
  16. char *options = NULL;
  17. int i = 0;
  18. pid_t pid;
  19. int status = 0;
  20. while(1){
  21. printf("%s$", getenv("USER"));
  22.  
  23. fgets(str, 10, stdin);
  24.  
  25. if(strlen(str) > SIZE_BUFF){
  26. printf("ERROR!n");
  27. continue;
  28. }
  29.  
  30. if(!strcmp(str, "stop"))
  31. exit(0);
  32.  
  33.  
  34. for(i = 0; (str[i] != ' ') && (str[i] != ''); i++);
  35.  
  36. strncpy(name_prog = realloc(name_prog, sizeof(char)*(i + 1)), str, i);
  37. *(name_prog + i) = '';
  38. arg[0] = name_prog;
  39. if(i == strlen(str)) {
  40. arg[1] = NULL;
  41. }
  42. else {
  43. strncpy(options = realloc(options, (strlen(str) - i + 1)), str + i + 1, strlen(str) - i);
  44. *(options + strlen(str) - i) = '';
  45. arg[1] = options;
  46. }
  47.  
  48. pid = fork();
  49. if (pid == 0){
  50. execvp(name_prog,arg);
  51. system("ps");
  52. } else if(pid > 0){
  53. wait(&status);
  54. printf("RESU: %in", status);
  55. }
  56.  
  57. }
  58. free(name_prog);
  59. }
Add Comment
Please, Sign In to add comment