-
Notifications
You must be signed in to change notification settings - Fork 0
/
Path_Old
40 lines (39 loc) · 908 Bytes
/
Path_Old
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
#include "main.h"
/**
* get_path - prints the name of the program
* @command: command from main
* Return: file path
*/
char *get_path(char *command)
{
char *path, *path_cp, *path_tok, *file_path;
int command_length, directory_length;
struct stat buf;
path = getenv("PATH");
if (path)
{ path_cp = strdup(path);
command_length = strlen(command);
path_tok = strtok(path_cp, ":");
while (path_tok != NULL)
{ directory_length = strlen(path_tok);
file_path = malloc(command_length + directory_length + 2);
strcpy(file_path, path_tok);
strcat(file_path, "/");
strcat(file_path, command);
strcat(file_path, "\0");
if (stat(file_path, &buf) == 0)
{ free(path_cp);
return (file_path);
}
else
{ free(file_path);
path_tok = strtok(NULL, ":");
}
}
free(path_cp);
if (stat(command, &buf) == 0)
{ return (command); }
return (NULL);
}
return (NULL);
}