-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask1.c
More file actions
47 lines (39 loc) · 1.09 KB
/
task1.c
File metadata and controls
47 lines (39 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#define MAX_LINE_LENGTH 80
int main() {
char line[MAX_LINE_LENGTH];
pid_t pid;
int status;
while (1) {
printf("#cisfun$ ");
fflush(stdout); /* Ensure prompt is displayed */
if (fgets(line, MAX_LINE_LENGTH, stdin) == NULL) {
/* Handle end of file (Ctrl+D) */
printf("\n");
break;
}
/* Remove trailing newline character */
line[strcspn(line, "\n")] = '\0';
/* Create a child process and execute the command */
pid = fork();
if (pid < 0) {
perror("fork failed");
exit(1);
}
if (pid == 0) {
/* Child process */
execlp(line, line, NULL);
/* If execution reaches here, it means the command was not found */
fprintf(stderr, "%s: command not found\n", line);
exit(1);
} else {
/* Parent process */
waitpid(pid, &status, 0);
}
}
return 0;
}