-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse.c
91 lines (78 loc) · 1.77 KB
/
parse.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
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
87
88
89
90
91
#include "main.h"
/**
* tokenize_line - Tokenize a line (string) with delimitators.
* @line: line to tokenize.
* Return: an array of strings with tokens-line parts.
*/
char **tokenize_line(char *line)
{
int position = 0;
char *token = NULL;
char **tokens = malloc(sizeof(char *) * 1024);
if (!tokens)
return (NULL);
token = strtok(line, " \t\r\n\a");
while (token)
{
tokens[position] = token;
position++;
token = strtok(NULL, " \t\r\n\a");
}
tokens[position] = NULL;
free(token);
return (tokens);
}
/**
* tokenize_path - Tokenize a the PATH of a Unix-SO.
* @path: string with PATH (environ-variable).
* Return: an array of strings with tokens-PATH directories.
*/
char **tokenize_path(char *path)
{
int position = 0;
char *path_line = NULL;
char **path_tokens = malloc(sizeof(char *) * 1024);
if (!path_tokens)
{
free(path_line);
return (NULL);
}
path_line = strtok(path, ":");
while (path_line)
{
path_tokens[position] = path_line;
position++;
path_line = strtok(NULL, ":");
}
path_tokens[position] = NULL;
free(path_line);
return (path_tokens);
}
/**
* path_cmd - Search a command into the PATH directories.
* @path: string with PATH (environ-variable).
* @cmd: command to find in PATH.
* Return: a string with the command and its PATH.
*/
char *path_cmd(char *path, char *cmd)
{
int position = 0;
char *temporal = NULL;
char **path_tokens = tokenize_path(path);
while (path_tokens[position])
{
path_tokens[position] = _strconcat(path_tokens[position], "/");
temporal = _strconcat(path_tokens[position], cmd);
if (access(temporal, F_OK) == 0)
{
free(path_tokens[position]);
free(path_tokens);
return (temporal);
}
free(temporal);
free(path_tokens[position]);
position++;
}
free(path_tokens);
return (NULL);
}