-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_path.c
43 lines (37 loc) · 968 Bytes
/
find_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
#include "main.h"
#include "utilities.h"
#include <stdlib.h>
/**
* find_command_in_path - Find a command in the given path.
* @path: The search path to look for the command.
* @command: The command to find.
*
* Return: A pointer to the full path of the
* command if found, or NULL if not found.
*/
char *find_command_in_path(char *path, char *command) {
char *full_path = NULL;
char *path_copy = _strdup(path);
char *token;
size_t len;
token = strtok(path_copy, ":");
while (token != NULL) {
len = _strlen(token) + 1 + _strlen(command) + 1;
full_path = (char *)malloc(len);
if (full_path == NULL) {
free(path_copy);
return (NULL);
}
_strcpy(full_path, token);
_strcat(full_path, "/");
_strcat(full_path, command);
if (access(full_path, F_OK) == 0) {
free(path_copy);
return (full_path);
}
free(full_path);
token = strtok(NULL, ":");
}
free(path_copy);
return (NULL);
}