-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspec6.c
More file actions
73 lines (60 loc) · 1.83 KB
/
spec6.c
File metadata and controls
73 lines (60 loc) · 1.83 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAX_COMMAND_LENGTH 100
#define MAX_ARGS 10
int main2() {
char input[MAX_COMMAND_LENGTH];
char *args[MAX_ARGS];
int background = 0;
while (1) {
printf("Enter a command: ");
fflush(stdout);
// Read user input
fgets(input, sizeof(input), stdin);
input[strlen(input) - 1] = '\0'; // Remove newline
if (strcmp(input, "exit") == 0) {
break; // Exit the shell
}
// Tokenize the input into arguments
char *token = strtok(input, " ");
int argIndex = 0;
while (token != NULL && argIndex < MAX_ARGS - 1) {
args[argIndex] = token;
token = strtok(NULL, " ");
argIndex++;
}
args[argIndex] = NULL; // Null-terminate the arguments array
// Check if the command should be run in the background
if (argIndex > 0 && strcmp(args[argIndex - 1], "&") == 0) {
background = 1;
args[argIndex - 1] = NULL; // Remove the "&"
} else {
background = 0;
}
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
} else if (pid == 0) {
// Child process
// Execute the command
execvp(args[0], args);
// If execvp returns, there was an error
perror("execvp");
exit(EXIT_FAILURE);
} else {
// Parent process
// If not in the background, wait for the child process to complete
if (!background) {
int status;
waitpid(pid, &status, 0);
}
}
}
return 0;
}
*/