-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_exec_path.c
More file actions
44 lines (43 loc) · 1001 Bytes
/
Copy pathget_exec_path.c
File metadata and controls
44 lines (43 loc) · 1001 Bytes
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
#include "shell.h"
/**
* get_executable_path - Get the full path of an executable command.
* @command: The command to find.
* @environment: The array of environment variables.
* Return: A pointer to the full path of the command, or NULL if not found.
*/
char *get_executable_path(char *command, char **environment)
{
int i = 0;
char *path, *env, *env_copy, **parsed_env;
struct stat cstat;
if (*command == '/')
{
if (stat("/bin/ls", &cstat) == 0)
return (command);
return (NULL);
}
else
{
env = _getenv(environment, "PATH=", 5);
env_copy = malloc(string_length(env) + 1);
if (env_copy == NULL)
return (NULL);
env_copy = copy_string(env_copy, env);
parsed_env = split_string(env_copy, ":");
while (parsed_env[i])
{
concatenate_strings(3, &path, parsed_env[i], "/", command);
if (stat(path, &cstat) == 0)
{
free(parsed_env);
free(env_copy);
return (path);
}
free(path);
i++;
}
}
free(env_copy);
free(parsed_env);
return (NULL);
}