-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunpath.c
132 lines (118 loc) · 3.07 KB
/
runpath.c
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "main.h"
#include "utilities.h"
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
/**
* runpathcmd - function to run a command
* if the path is provided
* @command: user input
* @tokens: a pointer to an array of commands
* Ideally tokens[0] is the command to run
* Return: the status
*/
int runpathcmd(char *command, char **tokens) {
pid_t child_pid;
int status_code = 0;
child_pid = fork();
if (child_pid < 0) {
perror("fork");
return (1);
}
if (child_pid == -1) {
perror("fork");
free(command);
free(tokens);
command = NULL;
tokens = NULL;
}
if (child_pid == 0) {
if (execve(tokens[0], tokens, NULL) == -1) {
perror(command);
freecommandspath(command, tokens);
exit(EXIT_FAILURE);
}
} else {
freecommandspath(command, tokens);
do {
waitpid(child_pid, &status_code, WUNTRACED);
} while (!WIFEXITED(status_code) && !WIFSIGNALED(status_code));
}
return (status_code);
}
/**
* freecommandspath - Free memory allocated for
* command, tokens, and directory.
* @command: The command string to free.
* @tokens: The array of tokens to free.
*/
void freecommandspath(char *command, char **tokens) {
free(command);
free(tokens);
command = NULL;
tokens = NULL;
}
/**
* runpathlesscmd - function to run pathless command.
*
* @command: A pointer to a character array representing the command.
* @tokens: tokens An array of pointers to character arrays
*
* Return: void
*
*/
int runpathlesscmd(char *command, char **tokens) {
pid_t child_pid;
char *path = getenv("PATH");
char *directory = find_command_in_path(path, tokens[0]);
int status_code = 0;
if (directory == NULL) {
perror(command);
freecommands(command, tokens, directory);
} else {
child_pid = fork();
if (child_pid < 0) {
perror("fork");
return (1);
}
if (child_pid == -1) {
perror("fork");
freecommands(command, tokens, directory);
} else if (child_pid == 0) {
exec_and_free(command, tokens, directory);
} else {
freecommands(command, tokens, directory);
do {
waitpid(child_pid, &status_code, WUNTRACED);
} while (!WIFEXITED(status_code) && !WIFSIGNALED(status_code));
}
}
return (status_code);
}
/**
* freecommands - Free memory allocated for
* command, tokens, and directory.
* @command: The command string to free.
* @tokens: The array of tokens to free.
* @directory: the directory where the excutable file is
*/
void freecommands(char *command, char **tokens, char *directory) {
free(command);
free(tokens);
free(directory);
}
/**
* exec_and_free - function to execute pathless commands
* did this just to remove functionality
* @command: The command string to free.
* @tokens: The array of tokens to free.
* @directory: the directory where the excutable file is
*/
void exec_and_free(char *command, char **tokens, char *directory) {
if (execve(directory, tokens, NULL) == -1) {
perror(command);
freecommands(command, tokens, directory);
exit(EXIT_FAILURE);
}
}