-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_path.c
51 lines (42 loc) · 959 Bytes
/
check_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
44
45
46
47
48
49
50
51
#include "holberton.h"
/**
* check_path - Check if the file is in the path.
*
* @ar: Entered commands.
*
* Return: If it finds the file in the path, returns the full path.
* Else, returns the same command entered.
*/
char *check_path(char **ar)
{
char *value = NULL, *path = NULL;
char *env = NULL, *name = NULL, *ex = NULL;
size_t size_name, size;
struct stat st;
env = get_env();
if (env != NULL)
{
name = ar[0];
size_name = _strlen(name);
ex = malloc(sizeof(char) * _strlen(env) + 1);
ex = _strcpy(ex, env);
path = strtok(ex, ":");
while (path != NULL)
{
size = _strlen(path) + size_name + 2;
value = malloc(sizeof(char) * size);
value = _strcpy(value, path);
value = _strcat(value, "/");
value = _strcat(value, name);
if (stat(value, &st) == 0 && st.st_mode & X_OK)
{
free(ex);
return (value);
}
path = strtok(NULL, ":");
free(value);
}
free(ex);
}
return (name);
}