-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshell_function_3.c
More file actions
102 lines (87 loc) · 1.69 KB
/
shell_function_3.c
File metadata and controls
102 lines (87 loc) · 1.69 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
#include "main.h"
/**
* fail_route - Validates if a route fails.
*
* @r: Pointer to a structure.
*
* @av: Arguments vector.
*
* @err: Pointer to the counter of errors.
*
* @exit_status: Pointer to the exit status of the prev command.
*
* Return: 1 if the route fails, 0 otherwise.
*/
int fail_route(response *r, char *av, int *err, int *exit_status)
{
if (access(r->toks[0], F_OK) != 0 && strchr(r->toks[0], '/'))
{
printf("%s: %d: %s :not found\n", av, *err, clean_spaces(r->toks[0]));
free_tokens(r->toks);
free(r->hold);
free(r);
*err += 1;
*exit_status = 127;
return (1);
}
return (0);
}
/**
* clean_spaces - Find the spaces and clean it.
*
* @command: Command to search.
*
* Return: The path if success, NULL if failure.
*/
char *clean_spaces(char *command)
{
int i = 0;
while (command[i] != '\0')
{
if (command[i] != ' ' && command[i] != '\t')
return (&command[i]);
i++;
}
return (NULL);
}
/**
* which - Search the path of a command
*
* @command: Command to search.
*
* Return: The path if success, NULL if failure.
*/
char *which(char *command)
{
char *path = NULL, *route = "\0", *full = NULL, *holder = NULL;
char *command_holder = NULL;
if (!command)
return (NULL);
if (!getenv("PATH"))
return (command);
path = str_concat(getenv("PATH"), ":");
holder = path;
command_holder = clean_spaces(command);
while (route != NULL)
{
route = strchr(holder, ':');
if (!route)
break;
route[0] = '\0';
full = validate_slash(command_holder, holder);
if (access(full, 0) == 0)
{
free(path);
free(command);
return (full);
}
else
{
holder = route + 1;
free(full);
continue;
}
}
free(path);
return (command);
}