-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpath.c
111 lines (97 loc) · 2.4 KB
/
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
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
103
104
105
106
107
108
109
110
111
#include "shell.h"
/**
* filter_cmd - we have data->get_cmd as a full command now
* we will seperate the spaces and put it as a list in data->cmd
*
* @data: data_t struct that holds the commands
*/
void filter_cmd(data_t *data)
{
char *delimiter = " \t";
int i, j, counter = 2;
for (i = 0; data->get_cmd[i]; i++)
for (j = 0; delimiter[j]; j++)
if (data->get_cmd[i] == delimiter[j])
counter++;
data->cmd = malloc(counter * sizeof(char *));
if (data->cmd == NULL)
{
perror(data->prog_name);
exit(errno);
}
i = 0;
data->cmd[i] = full_strdup(strtok(data->get_cmd, delimiter));
while (data->cmd[i++])
data->cmd[i] = full_strdup(strtok(NULL, delimiter));
}
/**
* get_location - after filtering the inputed string,
* an example of data->cmd : {"ls", "-l", "/tmp", NULL}
* now we want the first command "ls" to be included with
* the command path, like "usr/bin/ls"
*
* @data: data_t struct holds the commands
*
* Return: full command with path or null if DNE
*/
char *get_location(data_t *data)
{
char *path, *path_copy, *path_token, *file_path;
int cmd_len, dir_len;
struct stat sfile;
path = _getenv("PATH", data);
if (path)
{
path_copy = full_strdup(path);
cmd_len = _strlen(data->cmd[0]);
path_token = strtok(path_copy, ":");
while (path_token)
{
dir_len = _strlen(path_token);
file_path = malloc(cmd_len + dir_len + 2);
if (file_path == NULL)
{
perror(data->prog_name);
exit(errno);
}
_strcpy(file_path, path_token);
_strcat(file_path, "/");
_strcat(file_path, data->cmd[0]);
_strcat(file_path, "\0");
if (stat(file_path, &sfile) == 0)
{
free(path_copy);
data->run_cmd = full_strdup(file_path);
free(file_path);
return (data->cmd[0]);
}
free(file_path);
path_token = strtok(NULL, ":");
}
free(path_copy);
if (stat(data->cmd[0], &sfile) == 0)
return (data->run_cmd = full_strdup(data->cmd[0]));
}
return (NULL);
}
/**
* _getenv - getting the path envirment variable from
* data->env list
* @key: var name "PATH"
* @data: data_t struct that holds everything
*
* Return: the path var value
*
*/
char *_getenv(char *key, data_t *data)
{
int i, key_len = 0;
if (key == NULL || data->env == NULL)
return (NULL);
key_len = _strlen(key);
for (i = 0; data->env[i]; i++)
if (_strncmp(key, data->env[i], key_len) &&
data->env[i][key_len] == '=')
return (data->env[i] + key_len + 1);
return (NULL);
}