-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandleCommand.c
92 lines (85 loc) · 1.97 KB
/
handleCommand.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
#include "shell.h"
/**
* handleCommand - see if executable exists, if so runs it, if not handle error
* @argv: initial hsh call and arguments passed through main
* @argTokes: tokenized command line str
* @pathTokes: tokenized copy of the path
* Return: void??
*/
void handleCommand(char **argv, char **argTokes, char **pathTokes)
{
pid_t pid;
int status, p_success;
pid = fork();
if (pid == -1)
perror(argv[0]);
if (pid == 0)
{
p_success = parser(argTokes, pathTokes);
if (p_success == 0)
{
execve(argTokes[0], argTokes, environ);
globes.error = errno;
}
errno = globes.error;
if (p_success != -2)
_error();
freeTokes(globes.pathTokes);
freeTokes(globes.argTokes);
free(globes.line);
exit(globes.last_exit_status);
}
else
{
wait(&status);
globes.last_exit_status = WEXITSTATUS(status);
}
freeTokes(globes.argTokes);
freeTokes(globes.pathTokes);
}
/**
* parser - point to "value" in PATH variable | format key=value
* Description: Tokenize "value" in PATH, concatenate argTokes0 to tokens,
* check if full_path exists and if it does, set argTokes0 to full_path
* @pathTokes: broken up path environmental variable
* @argTokes: tokenized input from getline
* Return: void
*/
int parser(char **argTokes, char **pathTokes)
{
char *full_path, **cpy = pathTokes;
int i, dir = 0;
struct stat st;
for (i = 0; (*argTokes)[i] != '\0'; i++)
if ((*argTokes)[i] == '/')
/* execve(argTokes[0], argTokes, environ); */
return (0);
for (; *cpy != NULL; cpy++)
{
full_path = str_concat(*cpy, *argTokes);
if (full_path == NULL)
return (-1);
if (stat(full_path, &st) == 0)
{
switch (st.st_mode & S_IFMT)
{
case S_IFDIR:
free(full_path), dir = 1;
break;
default:
*argTokes = full_path;
/* execve(argTokes[0], argTokes, environ); */
break;
}
break;
}
else if (errno == ENAMETOOLONG)
globes.error = errno;
free(full_path);
}
if (*cpy == NULL)
return (-1);
if (dir)
return (-2);
return (0);
}