-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetPath.c
More file actions
56 lines (48 loc) · 1016 Bytes
/
Copy pathgetPath.c
File metadata and controls
56 lines (48 loc) · 1016 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
45
46
47
48
49
50
51
52
53
54
55
56
#include "shell.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* getPath - Get $PATH of command
* @command: command to find a PATH for it
* Return: Full path
*/
char *getPath(char *command);
char *getPath(char *command)
{
char *envPath, *PathOfCommand, *directories;
int i;
struct stat st;
for (i = 0; command[i]; i++)
{
if (command[i] == '/')
{
if (stat(command, &st) == 0)
return (_strdup(command));
return (NULL);
}
}
envPath = getEnv("PATH");
if (!envPath)
return (NULL);
directories = strtok(envPath, ":");
while (directories)
{
PathOfCommand = malloc(_strlen(directories) + _strlen(command) + 2);
if (PathOfCommand)
{
_strcpy(PathOfCommand, directories);
_strcat(PathOfCommand, "/");
_strcat(PathOfCommand, command);
if (stat(PathOfCommand, &st) == 0)
{
free(envPath);
return (PathOfCommand);
}
free(PathOfCommand), PathOfCommand = NULL;
directories = strtok(NULL, ":");
}
}
free(envPath);
return (NULL);
}