-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.c
More file actions
130 lines (110 loc) · 2.11 KB
/
utils.c
File metadata and controls
130 lines (110 loc) · 2.11 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "main.h"
/**
* validate_slash - Validate if the command have a slash
* if not have that slash add it
*
* @cmd: Command.
*
* @holder: Contains a copy of the path.
*
* Return: Add a slash to the end
*/
char *validate_slash(char *cmd, char *holder)
{
char *arg = NULL, *result = NULL;
if (cmd[0] != '/')
{
arg = str_concat("/", cmd);
result = str_concat(holder, arg);
free(arg);
}
else
{
result = str_concat(holder, cmd);
}
return (result);
}
/**
* find_char - Search a char inside of a str
*
* @str: String to search a char.
*
* @character: char to search in the str.
*
* Return: A pointer to the string before the char
* if not find that char return the str completed.
*/
char *find_char(char *str, char character)
{
char *result = NULL;
size_t i = 0, j = 0;
int flag = 0;
while (str[i] != '\0')
{
if (str[i] == character)
{
flag = 1;
break;
}
i++;
}
if (flag == 1 && str[i] != '\0')
return (NULL);
result = malloc((i + 1) * sizeof(char));
if (!result)
return (NULL);
for (j = 0; j < i; j++)
result[j] = str[j];
result[j] = '\0';
free(str);
return (result);
}
/**
* display_path - Display in the terminal the path.
*
* Return: Void.
*/
void display_path(void)
{
char *path = NULL, *address = NULL;
int i = 0, size = 0;
path = getenv("PATH");
size = strlen(path);
while (path[i])
{
if (i > size)
break;
address = find_char(&path[i], ':');
printf("%s\n", address);
i += (strlen(address) + 1);
free(address);
}
}
/**
* str_concat - Concatenate two strings in a new pointer
*
* @s1: String one.
*
* @s2: String two.
*
* Return: If success return a pointer with the concat otherwise null.
*/
char *str_concat(char *s1, char *s2)
{
char *final_str = NULL;
int size1 = 0, size2 = 0, total_size = 0, i = 0;
size1 = strlen(s1);
size2 = strlen(s2);
total_size = size1 + size2 + 1;
final_str = malloc(total_size * sizeof(char));
if (!final_str)
return (NULL);
for (i = 0; i < total_size; i++)
{
if (size1 && i < size1)
final_str[i] = s1[i];
if (i >= size1 && s2)
final_str[i] = s2[i - size1];
}
return (final_str);
}