-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathglob.c
More file actions
122 lines (112 loc) · 3.05 KB
/
glob.c
File metadata and controls
122 lines (112 loc) · 3.05 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* glob.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jkong <jkong@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/23 10:11:45 by jkong #+# #+# */
/* Updated: 2022/06/24 20:22:22 by jkong ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include "libft.h"
#include "util_flag.h"
#include "string_buffer.h"
#include "string_vector.h"
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
static int _match_continuable(const char **pattern, const char **name,
const char **pattern_backup, const char **name_backup)
{
if (**pattern == '/' && ((*pattern)[1] == '\0' || (*pattern)[1] == '/'))
{
(*pattern)++;
return (1);
}
if (**pattern == '\001')
{
*pattern_backup = (*pattern)++;
*name_backup = *name + 1;
return (1);
}
else if (**pattern != '\0' && **name == **pattern)
{
(*pattern)++;
(*name)++;
return (1);
}
if (*name_backup && (*name)[-1] != '\0')
{
*pattern = *pattern_backup;
*name = *name_backup;
return (1);
}
return (0);
}
static int _match(const char *pattern, const char *name)
{
const char *pattern_backup;
const char *name_backup;
pattern_backup = NULL;
name_backup = NULL;
while (*pattern != '\0' || *name != '\0')
{
if (!_match_continuable(&pattern, &name, &pattern_backup, &name_backup))
return (0);
}
return (1);
}
static t_file_flags _flags(const char *pattern)
{
size_t last;
t_file_flags result;
result = 0;
if (ft_strlen(pattern) != 0)
{
last = 0;
while (pattern[last + 1] != '\0')
last++;
if (pattern[last] == '/')
set_flag(&result, FF_DIRECTORY);
if (pattern[0] == '.')
set_flag(&result, FF_HIDDEN);
}
return (result);
}
static char *_entry_name(char *name, t_file_flags flags)
{
t_str_buf *buf;
buf = str_append(NULL, name);
if (has_flag(flags, FF_DIRECTORY))
buf = str_append(buf, "/");
return (str_dispose(buf));
}
char **glob_to_strvec(const char *path, const char *pattern)
{
DIR *const dir = opendir(path);
const t_file_flags flags = _flags(pattern);
struct dirent *ent;
t_str_vec *vec;
if (!dir)
return (NULL);
vec = NULL;
while (1)
{
ent = readdir(dir);
if (!ent)
break ;
if (_match(pattern, ent->d_name))
{
if (has_flag(flags, FF_DIRECTORY) && ent->d_type != DT_DIR)
continue ;
if (has_flag(flags ^ _flags(ent->d_name), FF_HIDDEN))
continue ;
vec = strv_append(vec, _entry_name(ent->d_name, flags));
}
}
closedir(dir);
return (strv_dispose(vec));
}