-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecmd.c
More file actions
50 lines (43 loc) · 985 Bytes
/
execmd.c
File metadata and controls
50 lines (43 loc) · 985 Bytes
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
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include "main.h"
extern char **environ;
void execmd(char **argv)
{
char *command = NULL, *actual_command = NULL;
pid_t pid;
int status;
if (argv)
{
command = argv[0];
actual_command = get_location(command);
if (actual_command == NULL)
{
fprintf(stderr, "Command not found: %s\n", command);
return;
}
pid = fork();
if (pid == 0)
{
if (execve(actual_command, argv, environ) == -1)
{
perror("Error:");
free(actual_command);
exit(EXIT_FAILURE);
}
}
else if (pid > 0)
{
waitpid(pid, &status, 0);
free(actual_command);
}
else
{
perror("fork failed");
free(actual_command);
}
}
}