-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_functions.c
More file actions
210 lines (189 loc) · 4.44 KB
/
path_functions.c
File metadata and controls
210 lines (189 loc) · 4.44 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "shell.h"
/* Path functions */
/**
* duplicate_character - Duplicate a portion of a string
*
* @source_str: The source string (char)
* @start: The starting index of the portion to duplicate (int)
* @stop: The stopping index (exclusive) of the portion to duplicate (int)
*
* Return: A pointer to the duplicated string
*/
char *duplicate_character(char *source_str, int start, int stop)
{
static char buffer[MAX_BUFFER_SIZE];
int i = start, j = 0;
/* Iterate through the specified portion of the string */
while (i < stop)
{
/* Exclude ':' characters while copying */
if (source_str[i] != ':')
buffer[j++] = source_str[i];
i++;
}
/* Null-terminate the resulting string */
buffer[j] = '\0';
return (buffer);
}
/**
* check_command - Check if the given path corresponds to a regular file
*
* @data_use: Pointer to the data structure
* @path: Path to the file (char)
*
* Return: 1 if the path corresponds to a regular file, 0 otherwise
*/
int check_command(data_t *data_use, char *path)
{
struct stat file_stat;
/* Ignore the data_use parameter for now */
(void)data_use;
/* Check if the path is valid and gather information about the file */
if (!path || stat(path, &file_stat))
return (0);
/* Check if the file is a regular file */
if (file_stat.st_mode & S_IFREG)
{
/* Path corresponds to a regular file */
return (1);
}
/* Path does not correspond to a regular file */
return (0);
}
/**
* find_path - Find the full path of a command in the specified paths
*
* @data_use: Pointer to the data structure
* @source_str: String containing the paths separated by colons (char)
* @cmd: Command to find in the paths (char)
*
* Return: Full path of the command if found, NULL otherwise
*/
char *find_path(data_t *data_use, char *source_str, char *cmd)
{
int i = 0, current_position = 0;
char *path;
if (!source_str)
return (NULL);
if ((length_string(cmd) > 2))
{
if (check_needle(cmd, "./"))
{
if (check_command(data_use, cmd))
return (cmd);
}
}
while (1)
{
if (source_str[i] == 0 || source_str[i] == ':')
{
path = duplicate_character(source_str, current_position, i);
if (*path == 0)
concatenate_string(path, cmd);
else
{
concatenate_string(path, "/");
concatenate_string(path, cmd);
}
if (check_command(data_use, path))
return (path);
if (source_str[i] == 0)
break;
current_position = i;
}
i++;
}
return (NULL);
}
/**
* find_command - Find and execute the specified command
*
* @data_use: Pointer to the data structure
*
* Return: None
*/
void find_command(data_t *data_use)
{
int i = 0, j = 0;
char *path = NULL;
data_use->path = data_use->argv[0];
if (data_use->line_flag == 1)
{
data_use->line_count++;
data_use->line_flag = 0;
}
while (data_use->arg[i])
{
if (!check_delimiter(data_use->arg[i], " \t\n"))
j++;
i++;
}
if (j == 0)
return;
path = find_path(data_use, get_environ_var(data_use, "PATH="),
data_use->argv[0]);
if (path != 0)
{
data_use->path = path;
fork_command(data_use);
}
else
{
if ((interactive_mode(data_use) || get_environ_var(data_use, "PATH=")
|| data_use->argv[0][0] == '/') && check_command(data_use,
data_use->argv[0]))
fork_command(data_use);
else if (*(data_use->arg) != '\n')
{
data_use->status = 127;
print_error(data_use, "not found\n");
}
}
}
/**
* fork_command - Fork a child process and execute the command
*
* @data_use: Pointer to the data structure
*
* Return: None
*/
void fork_command(data_t *data_use)
{
pid_t child_pid;
/* Fork the child process */
child_pid = fork();
/* Check for fork failure */
if (child_pid == -1)
{
perror("Error:");
return;
}
/* Parent process */
if (child_pid != 0)
{
/* Wait for the child process to complete */
wait(&(data_use->status));
/* Check if the child process exited normally */
if (WIFEXITED(data_use->status))
{
/* Update the status in the data structure */
data_use->status = WEXITSTATUS(data_use->status);
/* Handle special case when permission is denied */
if (data_use->status == 126)
print_error(data_use, "Permission denied\n");
}
}
/* Child process */
else
{
/* Execute the specified command in the child process */
if (execve(data_use->path, data_use->argv, get_environ(data_use)) == -1)
{
free_info(data_use, 1);
/* Exit with 126 if permission is denied, otherwise exit with 1 */
if (errno == EACCES)
exit(126);
exit(1);
}
}
}