-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.c
More file actions
63 lines (57 loc) · 1.47 KB
/
path.c
File metadata and controls
63 lines (57 loc) · 1.47 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
#include "main.h"
/**
* new_path - fun the change the size of the pathname to the new one
* @pathname: the name of the command
* @fullpath: the new pathname
*/
void new_path(char **pathname, char *fullpath)
{
if (fullpath != NULL)
{
/* change the allocated memory for the argv[0] */
*pathname = realloc(*pathname, (sizeof(char) * strlen(fullpath)));
if (*pathname == NULL)
return;
*pathname = fullpath;/* add the new path to argv[0] */
}
}
/**
* get_path - fun that get the full path of a given command
* @pathname: the command we want to get its path
* Return: the fullpath, NULL otherwise
*/
char *get_path(char *pathname)
{
char *path = NULL, *dir = NULL, *cp_path = NULL, *fullpath = NULL;
struct stat buffer;
/* get the path */
path = getenv("PATH");
cp_path = strdup(path);/* copy it to can add the /bin */
dir = strtok(cp_path, ":");/* separate the cp_path */
if (stat(pathname, &buffer) != 0)
{
/* walk through the full path unil we get the right directory */
while (dir)
{
fullpath = malloc((sizeof(char) *
(strlen(dir) + (strlen(pathname)) + 2)));
if (fullpath == NULL)
exit(EXIT_FAILURE);
/* concat the path with the pathname */
strcpy(fullpath, dir);
strcat(fullpath, "/");
strcat(fullpath, pathname);
/* check if the full path exist */
if (stat(fullpath, &buffer) == 0)
{
free(cp_path);
return (fullpath);
}
free(fullpath);
dir = strtok(NULL, ":");
}
}
free(cp_path);
free(dir);
return (NULL);
}