-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path_which.c
More file actions
51 lines (50 loc) · 1.24 KB
/
Copy path_which.c
File metadata and controls
51 lines (50 loc) · 1.24 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
#include "holberton.h"
/**
* _which - searches directories in PATH variable for command
* @command: to search for
* @fullpath: full path of command to execute
* @path: full PATH variable
* Return: pointer to full_path
*/
char *_which(char *command, char *fullpath, char *path)
{
unsigned int command_length, path_length, original_path_length;
char *path_copy, *token;
command_length = _strlen(command);
original_path_length = _strlen(path);
path_copy = malloc(sizeof(char) * original_path_length + 1);
if (path_copy == NULL)
{
errors(3);
return (NULL);
}
_strcpy(path_copy, path);
/* copy PATH directory + command name and check if it exists */
token = strtok(path_copy, ":");
if (token == NULL)
token = strtok(NULL, ":");
while (token != NULL)
{
path_length = _strlen(token);
fullpath = malloc(sizeof(char) * (path_length + command_length) + 2);
if (fullpath == NULL)
{
errors(3);
return (NULL);
}
_strcpy(fullpath, token);
fullpath[path_length] = '/';
_strcpy(fullpath + path_length + 1, command);
fullpath[path_length + command_length + 1] = '\0';
if (access(fullpath, X_OK) != 0)
{
free(fullpath);
fullpath = NULL;
token = strtok(NULL, ":");
}
else
break;
}
free(path_copy);
return (fullpath);
}