-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch_path.c
68 lines (62 loc) · 1.5 KB
/
search_path.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
#include "shell.h"
/**
* print_env - prints the current env built in
*/
void print_env(void)
{
int i = 0;
/* prints in form of "variable=value" */
while (environ[i])
{
write(STDIN_FILENO, environ[i], _strlen(environ[i]));
write(STDIN_FILENO, "\n", 1);
i++;
}
}
/**
* search_path - searches along the PATH to find a command that matches
* @head: pointer to the head node of the linked list
* @c: the string we have from the user command
* @av: the arguments from the CLI
* @to_string: the counter as an int but converted to a string
*
* Description: this tests if user has access permissions also
* Return: Null if no access or the string if we have access
*/
char *search_path(list_t *head, char *c, char **av, char *to_string)
{
list_t *node;
/* struct stat buf; */
char *full_command;
char *command = str_concat("/", c);
(void) av;
(void) to_string;
for (node = head; node != NULL; node = node->next)
{
full_command = str_concat(node->str, command);
/*
* if (stat(full_command, &buf) == 0)
* return (full_command);
*/
if (access(full_command, X_OK) == 0)
{
return (full_command);
}
/*
* if (access(full_command, X_OK) == 0)
*
* {
* write(STDOUT_FILENO, av[0], _strlen(av[0]));
* write(STDOUT_FILENO, ": ", 2);
* write(STDOUT_FILENO, to_string, _strlen(to_string));
* write(STDOUT_FILENO, ": ", 2);
* write(STDOUT_FILENO, c, _strlen(c));
* write(STDOUT_FILENO, ": Permission denied\n", 20);
* exit(0);
* }
*/
free(full_command);
}
free(command);
return (NULL);
}