/
path_and_env_fuctions.c
162 lines (142 loc) · 3.59 KB
/
path_and_env_fuctions.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "shell.h"
/**
* store_tokens - Funcion that separates the user command line
* @line: The line that user writes on the stdin
* Return: A double poniter to an array that stores the arguments (tokens)
*/
char **store_tokens(char *line)
{
char **array_tok = NULL, *token = NULL;
int i = 0, line_len = 0, j = 0;
if (line == NULL)
return (NULL);
line_len = str_count(line);
if (line_len > 0)
{
array_tok = malloc(sizeof(char *) * (line_len + 1));
if (array_tok == NULL) /*allocate memory if is possible*/
return (NULL);
token = strtok(line, "\n\t "); /*tokenize the line*/
while (token != NULL)
{
array_tok[i] = strdup(token);
if (array_tok[i] == NULL)
{
for (j = 0; j <= i; j++)
free(array_tok[j]);
free(array_tok);
free(token);
return (NULL);
}
token = strtok(NULL, "\t\n ");
i++;
}
array_tok[i] = NULL;
}
return (array_tok);
}
/**
* str_count - Function that ounts the number of words in a string
* @str: The line given by the user
* Return: The number of words in the string line
*/
int str_count(char *str)
{
int i = 0, count = 0;
while (str[i])
{
if (str[i] == ' ') /*if is space, skip to the next*/
i++;
else
{
count++; /*words accountat*/
i++;
while (str[i] && str[i] != ' ')
i++;
}
}
return (count);
}
/**
* get_env - Function that gets the value of enviroment variable.
* @var_name: The name of the enviroment variable.
* Return: The value of the environment Variable or NULL.
*/
char *get_env(char *var_name)
{
int i = 0;
char *var_value;
size_t len_value, len_name = strlen(var_name);
while (environ[i] != NULL)
{ /*alloc memory for the path value*/
len_value = strlen(environ[i]);
var_value = malloc((len_value + 1) * sizeof(char));
if (var_value == NULL) /*if alloc fails, return NULL*/
{
perror("malloc");
return (NULL);
}
if ((strncmp(var_name, environ[i], len_name) == 0)
&& (environ[i][len_name] == '='))
{ /*if the environ variable is found, return it*/
strcpy(var_value, environ[i]);
return (var_value);
}
free(var_value); /*free the allocated memory*/
i++;
}
return (NULL);
}
/**
* path_match - Searchs for the command in the path
* @array_tok: The array of tokens containing the user line command
* Return: 0 if success, 1 if fail
*/
int path_match(char **array_tok)
{
char *path, *fullpath, *path_value;
struct stat ex;
path = get_env("PATH"); /*call get_env to get the PATH value*/
if (path == NULL)
return (127);
path_value = strtok(path, ":"); /*tokenize the path*/
while (path_value != NULL)
{
fullpath = fullpath_func(*array_tok, path_value);
if (stat(fullpath, &ex) == 0)
{ /*built the fullpath*/
free(array_tok[0]);
*array_tok = strdup(fullpath);
free(fullpath);
free(path);
return (0);
}
free(fullpath);
path_value = strtok(NULL, ":");
}
free(path);
return (1);
}
/**
* fullpath_func - Function that concatenates the fullpath
* @array_tok: The tokens
* @path_value: The path value
* Return: A pointer to the complete command
*/
char *fullpath_func(char *array_tok, char *path_value)
{
char *command;
size_t len = 0;
len = strlen(path_value) + strlen(array_tok) + 2;
command = malloc(sizeof(char) * len);
if (command == NULL)
{ /*alloc memory to the fullpath*/
return (NULL);
}
memset(command, 0, len); /*array initialiced*/
/*concatenate the path with the command name*/
command = strcat(command, path_value);
command = strcat(command, "/");
command = strcat(command, array_tok);
return (command); /*return the fullpath*/
}