-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_get_command_from_path.c
More file actions
39 lines (37 loc) · 884 Bytes
/
_get_command_from_path.c
File metadata and controls
39 lines (37 loc) · 884 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
#include "shell.h"
/**
* _get_command_from_path - function that takes command
* and return it's corresponding path
*
* @command: command to lookup for it's path
* Return: path where command located
*/
char *_get_command_from_path(char *command)
{
char *proper_command, *path,
**path_2d, **iterator;
struct stat st;
if ((command[0] == '.' || command[0] == '/') &&
!stat(command, &st))
return (_strdup(command));
path = _enviroment_management(GET_VALUE, "PATH", NULL);
if (!path)
return (_strdup(command));
iterator = path_2d = _split(path, ":");
free(path);
while (*iterator)
{
path = _strcat(*iterator, "/");
proper_command = _strcat(path, command);
free(path);
if (!stat(proper_command, &st))
{
_free_split(&path_2d);
return (proper_command);
}
free(proper_command);
iterator++;
}
_free_split(&path_2d);
return (_strdup(command));
}