-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexec.c
More file actions
49 lines (38 loc) · 1.05 KB
/
exec.c
File metadata and controls
49 lines (38 loc) · 1.05 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
/*
This program fork()'s a child and then uses execl to execute ls
Added the ability to take in user input and execute the ls command
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#define MAX_CMD_LENGTH 1025
int main(int argc, char *argv[])
{
pid_t child_pid = fork();
// returns what status the child exited with
int status;
if(child_pid == -1)
{
perror("fork failed: ");
exit(EXIT_FAILURE);
}
else if(child_pid == 0)
{
char user_command[MAX_CMD_LENGTH];
char full_program_path[MAX_CMD_LENGTH + strlen("/bin/")];
printf("msh> ");
fgets(user_command, MAX_CMD_LENGTH, stdin);
user_command[strlen(user_command) - 1] = '\0';
strncpy(full_program_path, "/bin/", strlen("/bin/"));
strcat(full_program_path, user_command);
// replace the newly created child process by the specified new program
execl(full_program_path, user_command, NULL);
exit(EXIT_SUCCESS);
}
// wait until the child terminates
waitpid(child_pid, &status, 0);
return EXIT_SUCCESS;
}