Advertisement
Guest User

c

a guest
Sep 26th, 2022
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5.  
  6. int main() {
  7. char *line;
  8. size_t line_size = 100;
  9. line = malloc(line_size);
  10. do
  11. {
  12. printf("%s", "wish> ");
  13. getline(&line,&line_size,stdin);
  14. if (strcmp(line, "exit\n") != 0) {
  15. line[strcspn(line, "\n")] = 0;
  16.  
  17. char *argv_for_program[10];
  18. char separator[2] = " ";
  19.  
  20. char *name_of_program;
  21. name_of_program = strtok(line, separator);
  22. argv_for_program[0] = name_of_program;
  23.  
  24. char *arg;
  25. size_t arg_size = 20;
  26. arg = malloc(20);
  27. int i = 1;
  28. while( arg != NULL ) {
  29. arg = strtok(NULL, separator);
  30. argv_for_program[i] = arg;
  31. i++;
  32. }
  33. free(arg);
  34.  
  35. char path[100] = "/usr/bin/";
  36. strcat(path, name_of_program);
  37.  
  38. fork();
  39. int check = execv(path , argv_for_program);
  40. printf("%d\n",check);
  41. exit(0);
  42. }
  43. } while (strcmp(line, "exit\n") != 0);
  44.  
  45. free(line);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement