Advertisement
dxvmxnd

Untitled

Dec 12th, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7.  
  8. int main() {
  9. char command[256];
  10. printf(“Введите команду (или ‘exit’ для выхода)”);
  11.  
  12. while (1) {
  13. printf("Command: ('exit' to quit): ");
  14. if (fgets(command, sizeof(command), stdin) == NULL) {
  15. perror("Input error");
  16. exit(1);
  17. }
  18. command[strcspn(command, "\n")] = '\0';
  19. if (strcmp(command, "exit") == 0) {
  20. break;
  21. }
  22.  
  23. pid_t pid = fork();
  24.  
  25. if (pid < 0) {
  26. perror("Fork error");
  27. exit(1);
  28. }
  29. if (pid == 0) {
  30. char *args[256];
  31. int i = 0;
  32. char *token = strtok(command, " ");
  33. while (token != NULL) {
  34. args[i++] = token;
  35. token = strtok(NULL, " ");
  36. }
  37. args[i] = NULL;
  38.  
  39. if (execvp(args[0], args) == -1) {
  40. perror("execvp error");
  41. exit(1);
  42. }
  43. } else {
  44. wait(NULL);
  45. }
  46. }
  47. printf("Exit.\n");
  48. return 0;
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement