-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell_functions.c
51 lines (48 loc) · 1.02 KB
/
shell_functions.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
#include "shell.h"
/**
* main - runs the shell function
* @argc: argument count
* @argv: array of argument values
* Return: 0 Always Success
*/
int main(int argc, char **argv)
{
char *RD_line = NULL, *parsed, *path;
size_t memo_size = 0;
ssize_t get_line;
pid_t child_pid;
int i = 0, status;
char **storage;
(void)argc, (void)argv;
do {
if (isatty(STDIN_FILENO))
write(STDOUT_FILENO, "cisfun$ ", 9);
get_line = getline(&RD_line, &memo_size, stdin);
if (get_line == -1)
exit(0);
parsed = strtok(RD_line, "\n");
storage = malloc(sizeof(char *) * 1024);
for (i = 0; parsed != NULL; i++)
{
storage[i] = parsed;
parsed = strtok(NULL, "\n"); }
storage[i] = NULL;
path = get_path(storage[0]);
child_pid = fork();
if (child_pid == -1)
{
perror("Failed to create child");
exit(50); }
else if (child_pid == 0)
{
if (execve(path, storage, NULL) == -1)
{
perror("Failed to execute");
exit(100); }
}
wait(&status);
} while (1);
free(path);
free(RD_line);
return (0);
}