-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimplement_path.c
59 lines (56 loc) · 1.21 KB
/
implement_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
#include "main.h"
/**
* is_implemented_path - checks if a command's path is implemented
* @command: command to check
* @path: array of paths to check
* Return: integer denoting whether path is implemented or not
*/
int is_implemented_path(char *command, char **path)
{
char **traverse;
int i = 0;
for (traverse = path; *traverse; traverse++)
{
while ((*traverse)[i] != '\0')
{
if (command[i] != '\0')
{
if ((*traverse)[i] != command[i])
break;
}
if (command[i] == '\0')
return (1);
i++;
}
if ((*traverse)[i] == '\0')
return (1);
}
if (command[i] == '.')
return (1);
return (0);
}
/**
* implement_path - implements a command path
* @command: command whose path is to be implemented
* @path: an array of paths to use to implement path
* Return: implemented path
*/
char *implement_path(char *command, char **path)
{
char **traverse, *string;
struct stat st;
if (is_implemented_path(command, path))
{
if (!stat(command, &st))
return (_strdup(command));
return (NULL);
}
for (traverse = path; *traverse; traverse++)
{
string = concat(*traverse, "/", command);
if (!stat(string, &st))
return (string);
free(string);
}
return (NULL);
}