-
Notifications
You must be signed in to change notification settings - Fork 9
/
check_path.c
73 lines (69 loc) · 1.42 KB
/
check_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
#include "holberton.h"
/**
* checkPath - searches $PATH for directory of command
* @build: input build
*/
_Bool checkPath(config *build)
{
register int len;
static char buffer[BUFSIZE];
struct stat st;
char *tok, *copy, *delim = ":", *tmp;
_Bool inLoop = false;
if (checkEdgeCases(build))
return (true);
copy = _strdup(build->path);
tok = _strtok(copy, delim);
while (tok)
{
tmp = inLoop ? tok - 2 : tok;
if (*tmp == 0 && stat(build->args[0], &st) == 0)
{
build->fullPath = build->args[0];
free(copy);
return (true);
}
len = _strlen(tok) + _strlen(build->args[0]) + 2;
_strcat(buffer, tok);
_strcat(buffer, "/");
_strcat(buffer, build->args[0]);
insertNullByte(buffer, len - 1);
if (stat(buffer, &st) == 0)
{
free(copy);
build->fullPath = buffer;
return (true);
}
insertNullByte(buffer, 0);
tok = _strtok(NULL, delim);
inLoop = true;
}
build->fullPath = build->args[0];
free(copy);
return (false);
}
/**
* checkEdgeCases - helper func for check path to check edge cases
* @build: input build
* Return: true if found, false if not
*/
_Bool checkEdgeCases(config *build)
{
char *copy;
struct stat st;
copy = _strdup(build->path);
if (!copy)
{
build->fullPath = build->args[0];
free(copy);
return (true);
}
if (*copy == ':' && stat(build->args[0], &st) == 0)
{
build->fullPath = build->args[0];
free(copy);
return (true);
}
free(copy);
return (false);
}