-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpath.c
More file actions
86 lines (74 loc) · 1.37 KB
/
path.c
File metadata and controls
86 lines (74 loc) · 1.37 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "shell.h"
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
/**
* _path - is path?
* @path: path
*
* Return: 1 (not absolute) 0 (absolute)
**/
int _path(char *path)
{
if (strlen(path) > 3)
{
if ((path[0] == '.' && path[1] == '/') || path[0] == '/' ||
(path[0] == '.' && path[1] == '.' && path[2] == '/'))
return (1);
}
return (0);
}
/**
* make_path - make path to file from directory and file
* @path: path to directory
* @file: file
* Return: New Path
**/
char *make_path(char *path, char *file)
{
char *n;
if (path == NULL || file == NULL)
return (NULL);
n = malloc(sizeof(char) *
(strlen(path) + strlen(file) + 2));
if (!n)
return (NULL);
strcpy(n, path);
n[strlen(path)] = '/';
n[strlen(path) + 1] = '\0';
strcat(n, file);
return (n);
}
/**
* _match - find directory insid PATH
* that file resides in if any
* @exec: executable name
* Return: full path of executable or NULL if it's not found
**/
char *_match(char **exec)
{
char **array, *p, *path;
int i = 0;
struct stat st;
path = getenv("PATH");
if (strlen(path) == 0)
return (NULL);
array = str_split(path, ':');
if (!array)
return (NULL);
while (array[i] != NULL)
{
p = make_path(array[i], *exec);
if (stat(p, &st) == 0)
{
free(*exec);
*exec = p;
free_arr(array);
return (p);
}
free(p);
i++;
}
free_arr(array);
return (NULL);
}