Repository navigation
Expand file tree
/
Copy pathshells.c
More file actions
362 lines (322 loc) · 8.3 KB
/
Copy pathshells.c
File metadata and controls
362 lines (322 loc) · 8.3 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include "shell.h"
int our_strcmp(const char *string1, const char *string2);
void signal_handler(int signal);
size_t our_strlen(const char *str);
char *our_strdup(const char *str);
char *our_strcat(char *final_str, char *str_to);
char *our_strcpy(char *dest, char *src);
ssize_t our_getline(char **line, size_t *len, FILE *stream);
/**
* main - building my own shell
* @ac: argument count
* @av: argument vector
*
* desc: program does the following
* 1. prompts a user to enter a line (command)
* 2. command is passed into the program through main ()
* 3. tokenizes the command into smaller strings
* 4. if first token (program name) contains '/' means it is a path
* 5. if fist token (program name) does not contain
* '/' means it is a raw program name
*
* 6. if path () {execve(path, arguments, NULL)}
* 7. if (!path) {search through all directories in $PATH environment
* variable to find a program,
* 8. that has the same name as the program using strcmp. if strcmp == 0,
* execve(strcat(directory, line), arguments, NULL);
* 4. smaller strings will represent the arguments parameter of execve function
* 5. forks program to create a new process (child)
* 6. uses execve function to execute the command in the child process
* 7. parent process waits for another command to be inputed by user
*
* Return: always 0
*/
int main(int ac, char **av)
{
/* for prompting user */
char *prompt = "$ ";
ssize_t characters_read = 0;
/* for testing */
ssize_t prompt_bytes_written = 0;
/* for getline() */
char *line = NULL;
size_t len = 0;
size_t i = 0;
/* for freeing dir*/
size_t free_dir_i = 0;
/* for strtok() */
const char *delimiters = NULL;
char *command_token = NULL;
/* for execve */
char **command = NULL;
/* for allocating space for command array */
size_t max_command = 0;
/* for fork */
int child_pid = 0;
int status = 0;
/* for getenv() */
char *path_env_var = NULL;
/* for checking if string is path */
int slash_i = 0;
char *path = NULL;
/* for allocating space for command array */
size_t max_directories = 0;
/* for tokenized path env variable to directories */
char **directories = NULL;
char *directories_token = NULL;
size_t directories_i = 0;
/* for opening directories in PATH */
DIR *directory_stream = NULL;
size_t searching_i = 0;
/* for reading entries in directory */
struct dirent *entry = NULL;
/* for concatenating directory with program name */
char *concat_path = NULL;
/* for if program is found in an entry */
int found = 0;
/* for printing error message */
char *err_msg = NULL;
/* for malloc */
size_t size = 0;
(void)ac;
/* handling signals */
signal(SIGINT, signal_handler);
while (1)
{
line = NULL;
delimiters = " \n\t\r\a";
path = NULL;
/** 1. prompts a user to enter a line (command) */
if (isatty(STDIN_FILENO))
{
prompt_bytes_written = write(1, prompt, our_strlen(prompt));
if (prompt_bytes_written == -1)
{
perror("write");
exit(1);
}
}
/* flushing stdout */
fflush(stdout);
/** get command from user */
characters_read = our_getline(&line, &len, stdin);
/*printf("line is: %s\n", line);*/
if (characters_read == EOF)
{
break;
}
else if (characters_read == -1)
{
break;
}
/* if command is exit */
if (our_strcmp(line, "exit") == 0)
{
free(line);
exit(0);
}
/* if command is env */
if (our_strcmp(line, "env") == 0)
{
int i = 0;
for (; environ[i]; i++)
{
/*print each environment variable*/
write(1, environ[i], our_strlen(environ[i]));
write(1, "\n", 1);
}
free(line);
continue;
}
/* 3. tokenizes the command into smaller strings */
/**
* 4. smaller strings will represent the arguments
* parameter of execve function
*/
command_token = strtok(line, delimiters);
if (command_token == NULL)
{
free(line);
continue;
}
/* allocating space for command array */
max_command = 10;
command = malloc(sizeof(char *) * max_command);
i = 0;
while (command_token != NULL)
{
/* reallocate memory when max commands reach */
if (i >= max_command)
{
max_command *= 2;
command = realloc(command, sizeof(char *) * max_command);
if (command == NULL)
{
perror("./hsh");
}
}
/* put tokens into command array */
command[i] = our_strdup(command_token);
if (command[i] == NULL)
continue;
command_token = strtok(NULL, delimiters);
i++;
}
/* set last element of command array to NULL */
command[i] = NULL;
/* check if program name is a path or just a file name */
slash_i = 0;
while (line[slash_i] != '\0')
{
if (line[slash_i] == '/')
{
path = line;
break;
}
slash_i++;
}
/* if program name is not a path */
if (path == NULL)
{
/* taking environment variable PATH */
char *PATH = getenv("PATH");
if (PATH == NULL)
continue;
/*CHECK IF STRDUP SUCCEEDED*/
path_env_var = our_strdup(PATH);
if (path_env_var == NULL)
continue;
/* tokenize PATH into array directories*/
directories_token = strtok(path_env_var, ":");
if (directories_token == NULL)
{
perror("av[0]");/*PROGRAM NAME ./hsh*/
}
/* allcoating space for directories array */
max_directories = 100;/*max_path = 4096*/
directories = malloc(sizeof(char *) * max_directories);
directories_i = 0;
while (directories_token != NULL)
{
if (directories_i >= max_directories)
{
max_directories *= 2;
directories = realloc(directories, sizeof(char *) * max_directories);
if (directories == NULL)
{
perror("realloc");
}
}
/*what if strdup fails?*/
directories[directories_i] = our_strdup(directories_token);
if (PATH == NULL)
continue;
directories_token = strtok(NULL, ":");
directories_i++;
}
/* null terminate directories array */
directories[directories_i] = NULL;
/* opening all directories to look for line(e.g "ls") */
searching_i = 0;
while (directories[searching_i] != NULL)
{
directory_stream = opendir(directories[searching_i]);
if (directory_stream == NULL)
{
searching_i++;
continue;
}
found = 0;
entry = readdir(directory_stream);
while (entry != NULL)
{
if (our_strcmp(command[0], entry->d_name) == 0)
{
found = 1;
break;
}
entry = readdir(directory_stream);
}
closedir(directory_stream);
if (found == 1)
break;
searching_i++;
if (searching_i == directories_i)/*directories[searching_i] == NULL*/
{
/*ERRONO = 2: NO SUCH FILE*/
/*concat(command[0], command_count)*/
/*PERROR("LOCATION" ":" "NO SUCH FILE FOUND")*/
/*char *err_msg = ": command not found\n";*/
/*write(2, line, our_strlen(line));*/
/*write(2, err_msg, our_strlen(err_msg));*/
/*found = -1;*/
/* err_msg = NULL;*/
perror(command[0]);
}
}
} /* END OF if (path == NULL) */
/** 5. forks program to create a new process (child) */
/*check if i found executatble path use found*/
child_pid = fork();
if (child_pid == -1)
{
perror("fork");
}
else if (child_pid == 0)
{
/* this is the child */
if (path == NULL)/*if not a path*/
{
size = our_strlen(directories[searching_i]) + our_strlen(line) + 2;
concat_path = malloc(size);
if (concat_path == NULL)
{
perror("malloc");
}
/* forming a path to be executed */
our_strcpy(concat_path, directories[searching_i]);
our_strcat(concat_path, "/");
our_strcat(concat_path, command[0]/*line*/);
/* executing the program */
command[0] = concat_path;
execve(command[0], command, environ);
/* if execve fails */
perror("execve");
free(concat_path);
free(command);
}
else
{
(void)av;
(void)err_msg;
execve(command[0], command, environ);
/* handle execve failure */
perror("execve");
}
}
/* continuing what happens in the parent (this program) */
wait(&status);
/* freeing directories_i */
if (path == NULL)
{
for (free_dir_i = 0; directories[free_dir_i] != NULL; free_dir_i++)
{
free(directories[free_dir_i]);
}
}
/* freeing command[i] */
for (i = 0; command[i] != NULL; i++)
{
free(command[i]);
}
free(directories);
free(path_env_var);
free(concat_path);
free(command);
free(line);
} /*end of loop;*/
free(line);
if (isatty(STDIN_FILENO) == 1)
write(1, "\n", 1);
return (0);
}