-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_funcs.c
101 lines (92 loc) · 2.37 KB
/
path_funcs.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
93
94
95
96
97
98
99
100
101
#include "shell.h"
/**
* get_path - copies the $PATH into the string path
* @path: string to copy the $PATH into
* @list: environemental variable list
* Return: 0 if successly found PATH variable and 1 if PATH
* not found
*/
int get_path(char *path, env_t *list)
{
env_t *temp;
temp = list;
for (; temp->next != NULL; temp = temp->next)
{
if (_str_match_tonull(temp->value, "PATH=") != 0)
{
_strcpy(path, temp->value);
return (0);
}
}
return (1);
}
/**
* tokenize_path - creates an array of strings, each string is a directory
* in the $PATH variable
* @search_path: array of strings that contains a searchable path for each
* string space.
* @path: the string containing $PATH
* @size: size of buffer
* Description: This also mallocs search_path with how many directories are
* found in path.
* Return: a 2D array of tokens
*/
char **tokenize_path(char **search_path, char *path, int size)
{
int i, count, s_index;
char *temp, *buffer;
buffer = safe_malloc(sizeof(char) * size);
buffer[0] = '\0';
for (i = 0, count = 1; path[i] != '\0'; i++)
{
if (path[i] == ':')
count++;
}
count++;
search_path = safe_malloc(sizeof(char *) * count);
/* skip the PATH= */
for (temp = path; *temp != '='; temp++)
;
temp++, s_index = 0;
do {
if (*temp == ':' || *temp == '\0')
{
_strncat(buffer, "/", 1);
search_path[s_index] = safe_malloc(sizeof(char) * size);
search_path[s_index][0] = '\0';
_strncat(search_path[s_index], buffer, _strlen(buffer));
s_index++;
buffer[0] = '\0';
}
else
_strncat(buffer, temp, 1);
} while (*temp++);
search_path[s_index] = safe_malloc(sizeof(char *));
search_path[s_index] = NULL;
return (search_path);
}
/**
* create_path - checks whether or not the command exist or not
* @cmd: command given by user, need to append to end of path strings
* @search_path: array of path strings to check for existance of command
* Description: Checks whether or not a cmd exist by trying to open commands
* in the different path directories.
* Return: 0 if found and -1 if not;
*/
int create_path(char *cmd, char **search_path)
{
int i, fd;
for (i = 0; search_path[i] != NULL; i++)
{
_strncat(search_path[i], cmd, _strlen(cmd));
fd = open(search_path[i], O_RDONLY);
if (fd > 0)
{
close(fd);
_strcpy(cmd, search_path[i]);
return (0);
}
}
write(0, "Error: command not found\n", 25);
return (-1);
}