-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuiltin_cd_path.c
70 lines (63 loc) · 1.15 KB
/
builtin_cd_path.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
#include "path.h"
#include "env.h"
#include "minishell.h"
#include <string.h>
static void update_path(char **path, char *retrieve_path)
{
char *tmp;
if (ft_strncmp(retrieve_path, ".", 2) == 0)
return ;
else if (ft_strncmp(retrieve_path, "..", 3) == 0)
{
tmp = *path;
*path = get_parent_dir(*path);
free(tmp);
}
else
{
tmp = *path;
*path = path_join(*path, retrieve_path);
free(tmp);
}
}
static bool is_double_slash(char *path)
{
return (ft_strlen(path) >= 2
&& path[0] == '/' && path[1] == '/' && path[2] != '/');
}
static void add_double_slash(char **path)
{
char *tmp;
tmp = *path;
*path = ft_strjoin("/", *path);
free(tmp);
}
/* Canonicalize path.
*
* ex:
* - "////hoge//fuga//../././//gaga" -> "/hoge/gaga"
*/
char *canonicalize_path(char *path)
{
char **dirs;
char *result;
int i;
dirs = ft_split(path, '/');
i = 0;
result = ft_strdup("/");
while (dirs[i])
{
update_path(&result, dirs[i]);
if (!result || !is_directory(result))
{
free(result);
free_ptrarr((void **)dirs);
return (NULL);
}
i++;
}
free_ptrarr((void **)dirs);
if (is_double_slash(path))
add_double_slash(&result);
return (result);
}