-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShell.c
More file actions
66 lines (58 loc) · 2.16 KB
/
Copy pathShell.c
File metadata and controls
66 lines (58 loc) · 2.16 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#define MAX 100
int main() {
int status = 0, pid, i;
char cmd[MAX]; //collect the command from the terminal
char* cmdArray[MAX]; //array of all commands
char* splitCmd = NULL; //run with strtok
char* path = getenv("PATH"); // get the path for the commands
char* tempDir = NULL; //run with strtok
char filePath[MAX] = "";
while(1) {
printf("$ ");
fgets(cmd, MAX, stdin); //fgets return the input by the user + "\n"
cmd[strlen(cmd) - 1] = '\0'; //replace the "\n"
//cmd="leave" - stop the program
if (strcmp(cmd, "leave") == 0) {
break;
}
pid = fork(); //create a child process
if (pid == -1) {
perror("fork"); //prints a message of the error when occurred
exit(EXIT_FAILURE); //value of a non zero integer
}
else if (pid == 0) { // Child process
splitCmd = strtok(cmd, " \t"); //split all the commands
i = 0;
// get array with all the commands
while (splitCmd != NULL) {
cmdArray[i++] = splitCmd;
splitCmd = strtok(NULL, " \t");
}
cmdArray[i] = NULL; //the last char is null in the array of commands
// Loop through directories to find command
tempDir = strtok(path, ":"); //split the directories
while (tempDir != NULL) {
strcpy(filePath, tempDir);
strcat(filePath, "/");
strcat(filePath, cmdArray[0]); //get filePath + the command
execv(filePath, cmdArray);
tempDir = strtok(NULL, ":");
}
// If execv fails, print error message and exit child process
exit(EXIT_FAILURE); //value of a non zero integer
}
else { // Parent process
waitpid(pid, &status, 0);
if (WIFEXITED(status) && WEXITSTATUS(status) == 1) {
printf("Child process exited with failure status\n");
}
}
}
return 0;
}