-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_path.c
More file actions
74 lines (70 loc) · 1.39 KB
/
_path.c
File metadata and controls
74 lines (70 loc) · 1.39 KB
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
#include "holberton.h"
/**
* pathChecker - searches $PATH for directory of command
* @ssh: input ssh
* Return: true if command found
*/
_Bool pathChecker(_unix *ssh)
{
register int len;
static char buffer[BUFSIZE];
struct stat st;
char *tok, *copy, *delim = ":", *tmp;
_Bool inLoop = false;
if (_eCases(ssh))
return (true);
copy = _strdup(ssh->path);
tok = _strtok(copy, delim);
while (tok)
{
tmp = inLoop ? tok - 2 : tok;
if (*tmp == 0 && stat(ssh->args[0], &st) == 0)
{
ssh->fullPath = ssh->args[0];
free(copy);
return (true);
}
len = _strlen(tok) + _strlen(ssh->args[0]) + 2;
_strcat(buffer, tok);
_strcat(buffer, "/");
_strcat(buffer, ssh->args[0]);
insertNullByte(buffer, len - 1);
if (stat(buffer, &st) == 0)
{
free(copy);
ssh->fullPath = buffer;
return (true);
}
insertNullByte(buffer, 0);
tok = _strtok(NULL, delim);
inLoop = true;
}
ssh->fullPath = ssh->args[0];
free(copy);
return (false);
}
/**
* _eCases - helper func for check path to check edge cases
* @ssh: input ssh
* Return: true if found, false if not
*/
_Bool _eCases(_unix *ssh)
{
char *copy;
struct stat st;
copy = _strdup(ssh->path);
if (!copy)
{
ssh->fullPath = ssh->args[0];
free(copy);
return (true);
}
if (*copy == ':' && stat(ssh->args[0], &st) == 0)
{
ssh->fullPath = ssh->args[0];
free(copy);
return (true);
}
free(copy);
return (false);
}