-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommand_path.c
More file actions
45 lines (41 loc) · 1.09 KB
/
Copy pathcommand_path.c
File metadata and controls
45 lines (41 loc) · 1.09 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
#include "shell.h"
/* in this file, the function to get the executable from the name
* given in the command line
*/
/**
* what_path - get executable from name
* @name: name of the command passed inline
* @pathl: linked list of the path
* the function loops throught the PATH, transformed into a linked list
* and stops when it finds a match, otherwise returns NULL
* Return: path for success, NULL for fail
*/
char *what_path(char *name, node_t *pathl)
{
int length_path, length_name;
char *fullname;
struct stat st;
if (_strchr(name, '/') != NULL)
return (_strdup(name));
length_name = _strlen(name);
while (pathl != NULL)
{
length_path = _strlen(pathl->name);
fullname = malloc(sizeof(char) * (length_path + length_name + 2));
if (fullname == NULL)
{
return (NULL);
}
_memcpy(fullname, pathl->name, length_path);
fullname[length_path] = '/';
_memcpy(fullname + length_path + 1, name, length_name);
fullname[length_name + length_path + 1] = '\0';
if (stat(fullname, &st) == 0)
{
return (fullname);
}
free(fullname);
pathl = pathl->next;
}
return (NULL);
}