Repository navigation
Expand file tree
/
Copy pathpath_split.c
More file actions
59 lines (53 loc) · 1.55 KB
/
Copy pathpath_split.c
File metadata and controls
59 lines (53 loc) · 1.55 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* path_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mserovat <mserovat@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/10/06 16:50:00 by mserovat #+# #+# */
/* Updated: 2026/10/06 16:50:00 by mserovat ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int path_count(char *path)
{
int count;
count = 1;
while (*path)
{
if (*path == ':')
count++;
path++;
}
return (count);
}
static char *path_component(char *path, int len)
{
if (!len)
return (ft_strdup("."));
return (ft_substr(path, 0, len));
}
char **split_path(char *path)
{
char **parts;
int i;
int len;
parts = ft_calloc(path_count(path) + 1, sizeof(char *));
if (!parts)
return (NULL);
i = 0;
while (1)
{
len = 0;
while (path[len] && path[len] != ':')
len++;
parts[i] = path_component(path, len);
if (!parts[i++])
return (env_free(parts), NULL);
if (!path[len])
break ;
path += len + 1;
}
return (parts);
}