Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- int main() {
- char command[256];
- printf(“Введите команду (или ‘exit’ для выхода)”);
- while (1) {
- printf("Command: ('exit' to quit): ");
- if (fgets(command, sizeof(command), stdin) == NULL) {
- perror("Input error");
- exit(1);
- }
- command[strcspn(command, "\n")] = '\0';
- if (strcmp(command, "exit") == 0) {
- break;
- }
- pid_t pid = fork();
- if (pid < 0) {
- perror("Fork error");
- exit(1);
- }
- if (pid == 0) {
- char *args[256];
- int i = 0;
- char *token = strtok(command, " ");
- while (token != NULL) {
- args[i++] = token;
- token = strtok(NULL, " ");
- }
- args[i] = NULL;
- if (execvp(args[0], args) == -1) {
- perror("execvp error");
- exit(1);
- }
- } else {
- wait(NULL);
- }
- }
- printf("Exit.\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement