-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.c
More file actions
59 lines (56 loc) · 1.19 KB
/
Copy pathpath.c
File metadata and controls
59 lines (56 loc) · 1.19 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
#include "main.h"
/**
* get_fpath - helper function that concats user input and path
* @temp: temp path
* @usr_cmd: command to be executed
* Return: pointer to path or NULL on failure
*/
char *get_fpath(char *temp, char *usr_cmd)
{
size_t totalLen = 0;
char *concated = NULL;
totalLen = totalLen + strlen(temp) + strlen(usr_cmd) + 2;
concated = malloc(totalLen);
if (concated == NULL)
{
return (NULL);
}
_strcpy(concated, temp);
_strcat(concated, "/");
_strcat(concated, usr_cmd);
return (concated);
}
/**
* get_fullpath - retrieves the full absolute full path
* @usr_cmd: user input command
* Return: pointer and NULL on failure
*/
char *get_fullpath(char *usr_cmd)
{
char *path = getenv("PATH");
char *temp = NULL;
char *fullpath = NULL;
char *temp_copy = strdup(path);
if (temp_copy == NULL)
return (NULL);
if (access(usr_cmd, F_OK) != -1)
{
fullpath = strdup(usr_cmd);
free(temp_copy);
return (fullpath);
}
temp = strtok(temp_copy, ":");
while (temp != NULL)
{
fullpath = get_fpath(temp, usr_cmd);
if (access(fullpath, F_OK) != -1)
{
free(temp_copy);
return (fullpath);
}
free(fullpath);
temp = strtok(NULL, ":");
}
free(temp_copy);
return (NULL);
}