-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_exe.c
More file actions
52 lines (51 loc) · 1.04 KB
/
Copy path_exe.c
File metadata and controls
52 lines (51 loc) · 1.04 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
52
#include "main.h"
int _exe_path(char **str);
/**
* _exe_path - check if the command exist in the
* path, if it does not creat a path and append it to the directory
* @str: pointer to the command.
*
* Return: return a status value of 1 on success and 0 on failure.
*/
int _exe_path(char **str)
{
char *_path, *_split, *buffer;
static char path_buf[1024], array[1000];
unsigned int i = 0;
if (access(*str, X_OK) == 0)
return (1);
_path = getenv("PATH");
if (_path == NULL)
return (0);
strcpy(path_buf, _path);
_split = strtok(path_buf, ":");
while (_split != NULL)
{
buffer = malloc(strlen(_split) + strlen("/") + strlen(*str) + 1);
if (buffer == NULL)
{
perror(*str);
free(buffer);
return (0);
}
strcpy(buffer, _split);
strcat(buffer, "/");
strcat(buffer, *str);
if (access(buffer, X_OK) == 0)
{
while (i < strlen(buffer))
{
array[i] = buffer[i];
i++;
}
array[i] = '\0';
free(buffer);
*str = array;
return (1);
}
free(buffer);
_split = strtok(NULL, ":");
}
perror(*str);
return (0);
}