-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_path.c
More file actions
104 lines (94 loc) · 1.83 KB
/
get_path.c
File metadata and controls
104 lines (94 loc) · 1.83 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "main.h"
/**
* _path - .....
* @env:the env.
* Return:the path.
*/
char *_path(char **env)
{
int path_idx = 0, path_len = 0;
char *path;
while (env[path_idx] != NULL)
{
if (_strncmp(env[path_idx], "PATH", 4) == 0)
break;
path_idx++;
}
path_len = _strlen(env[path_idx]);
path = malloc(path_len - 4);
if (!path)
return (NULL);
_strcpy(path, env[path_idx] + 5);
return (path);
}
/**
* _array_path - ......
* @path:the path.
* Return:array of path.
*/
char **_array_path(char *path)
{
int i = 0, no_del = 0;
char **array_path, *token;
while (path[i])
{
if (path[i] == ':')
no_del++;
i++;
}
array_path = malloc(sizeof(char *) * (no_del + 2));
i = 0;
token = strtok(path, ":");
while (token != NULL)
{
array_path[i++] = token;
token = strtok(NULL, ":");
}
array_path[i] = NULL;
return (array_path);
}
/**
* concat_command - .....
* @array_path: array of path.
* @command: the command.
* Return:the concated array.
*/
char **concat_command(char **array_path, char *command)
{
int i = 0, total_len = 0;
char **concated_array, *temp;
while (array_path[i])
i++;
concated_array = malloc(sizeof(char *) * (i + 2));
i = 0;
while (array_path[i])
{
if (i == 0)
{
concated_array[0] = malloc(_strlen(command) + 1);
_strcpy(concated_array[0], command);
}
total_len = _strlen(command) + _strlen(array_path[i]) + 2;
concated_array[i + 1] = malloc(total_len);
temp = malloc(total_len);
_strcpy(temp, array_path[i]);
_strcpy(concated_array[i + 1], _strcat(strcat(temp, "/"), command));
i++;
free(temp);
}
concated_array[i + 1] = NULL;
return (concated_array);
}
/**
* free_2darr - ....
* @array_path:the array of path.
* Reutrn:void.
*/
void free_2darr(char **array_path)
{
int i = 0;
while (array_path[i])
free(array_path[i++]);
free(array_path[i]);
free(array_path);
}