-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkbin.c
193 lines (187 loc) · 3.57 KB
/
checkbin.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "sshell.h"
/**
* _getpwd - get the PWD env variable
* @m: copoy of environment variables
* Return: the string inside PWD env variable
*/
char *_getpwd(char **m)
{
int i, j, k = 0, cont = 0;
char str[] = "PWD=";
char *pwd;
for (i = 0; m[i] != NULL; i++)
{
for (j = 0; m[i][j] != '\0'; j++)
{
if (cont == 4)
break;
if (m[i][j] == str[j])
cont++;
else
break;
}
if (cont == 4)
break;
}
if (cont == 4)
{
while (m[i][k] != '\0')
k++;
pwd = _calloc(k + 1, sizeof(char));
if (pwd == NULL)
return (NULL);
k = 4;
while (m[i][k] != '\0')
{
pwd[k - 4] = m[i][k];
k++;
}
return (pwd);
}
return (NULL);
}
/**
* _verifypath - check if the path has a : at the begining
* or if exist ::
*@path: string inside PATH env varible
*@pwd: string inside PWD env variable
*Return: path, or pwd concatenated to path if there is a : at the begining or
*pwd concatenated when there is ::
*/
char *_verifypath(char *path, char *pwd)
{
char *newpath = NULL, *str1 = NULL, *str2 = NULL, dosp = ':';
int cont, pa, pw, k1, k2;
if (path == NULL || pwd == NULL)
return (NULL);
for (pw = 0; pwd[pw] != '\0'; pw++)
;
for (pa = 0; path[pa] != '\0'; pa++)
;
for (cont = 0; path[cont] != '\0'; cont++)
{
if (path[0] == dosp)
{
newpath = str_concat(pwd, path);
free(pwd), free(path);
return (newpath);
}
else if (path[cont] == dosp && path[cont + 1] == dosp)
{
str1 = _calloc(pa + 1, sizeof(char));
if (!str1)
return (NULL);
str2 = _calloc(pa + 1, sizeof(char));
if (!str2)
return (NULL);
for (k1 = 0; k1 <= cont; k1++)
str1[k1] = path[k1];
for (k2 = 0; path[k1] != '\0'; k2++, k1++)
str2[k2] = path[k1];
newpath = str_concat(pwd, str2);
newpath = str_concat(str1, newpath);
free(str1);
free(str2);
free(pwd);
free(path);
return (newpath);
}
}
free(pwd);
return (path);
}
/**
*_getpath - get the string in PATH env
* @m: environment variables
* Return: string inside PATH env variable
*/
char *_getpath(char **m)
{
int i, j, k = 0, w = 0, cont = 0;
char str[] = "PATH=";
char *path;
for (i = 0; m[i] != NULL; i++)
{
for (j = 0; m[i][j] != '\0'; j++)
{
if (cont == 5)
break;
if (m[i][j] == str[j])
cont++;
else
break;
}
if (cont == 5)
break;
}
k = cont;
if (cont == 5)
{
while (m[i][k] != '\0')
{
w++;
k++;
}
if (w == 0)
return (NULL);
path = _calloc(w + 1, sizeof(char));
if (path == NULL)
return (NULL);
k = 5;
while (m[i][k] != '\0')
{
path[k - 5] = m[i][k];
k++;
}
return (path);
}
return (NULL);
}
/**
* checkbin - checks if arg[0] has /bin/
* @b: input of user, array of pointers
* @m: copy of environment variables
* Return: 0.
*/
char **checkbin(char **b, char **m)
{
unsigned int i = 0, j = 0, k = 0;
struct stat veri;
char *path, *tokens, *buf, *pwd, *newpath;
char *valor;
i = _strlen(b[0]);
if (b == NULL || i == 0)
return (NULL);
path = _getpath(m);
if (path == NULL)
return (b);
pwd = _getpwd(m);
newpath = _verifypath(path, pwd);
tokens = _strtok(newpath, ":");
if (!tokens)
return (NULL);
while (tokens != NULL)
{
while (tokens[j] != '\0')
j++;
buf = _calloc((j + 2), sizeof(char));
if (buf == NULL)
perror("No memory");
for (k = 0; k < j; k++)
buf[k] = tokens[k];
buf[k] = '/';
valor = str_concat(buf, b[0]);
if (stat(valor, &veri) == 0)
{
b[0] = _realloc2(buf, b[0], i, _strlen(valor));
free(buf), free(valor);
free(newpath);
return (b);
}
tokens = _strtok(NULL, ":");
j = 0;
free(buf), free(valor);
}
free(newpath);
return (b);
}