-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
97 lines (87 loc) · 2.36 KB
/
parser.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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ylagtab <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/25 10:23:55 by ylagtab #+# #+# */
/* Updated: 2020/10/22 11:30:04 by ylagtab ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_ls.h"
t_bool g_options[256];
static void exit_illegal_opt(char c)
{
ft_printf(
"ft_ls: illegal option -- %c\n"
"usage: ft_ls [-1adlprtR] [file ...]\n", c);
exit(1);
}
static void parse_opt(char *opt_str)
{
while (*opt_str)
{
if (ft_strchr(OPTIONS, *opt_str))
{
g_options[(int)*opt_str] = 1;
if (*opt_str == 'l')
g_options['1'] = 0;
if (*opt_str == '1')
g_options['l'] = 0;
}
else
exit_illegal_opt(*opt_str);
opt_str++;
}
}
static int parse_options(char **av)
{
int i;
i = 0;
while (av[i] != NULL)
{
if (av[i][0] == '-' && av[i][1] == '-' && av[i][2] == '\0')
return (i + 1);
if (av[i][0] == '-')
parse_opt(av[i] + 1);
else
return (i);
i++;
}
return (i);
}
static void parse_args(t_args *ls_args, char **av)
{
struct stat st;
t_inode inode;
t_error error;
while (*av != NULL)
{
if (lstat(*av, &st) == -1)
{
new_error(&error, *av);
ft_enqueue(&(ls_args->errors), &error, sizeof(t_error));
av++;
continue ;
}
new_inode(&inode, &st, "", *av);
if (S_ISDIR(st.st_mode) && g_options[OPT_D] != 1)
ft_enqueue(&(ls_args->dirs), &inode, sizeof(t_inode));
else if (S_ISLNK(st.st_mode) && g_options[OPT_D] != 1)
handle_link(&inode, ls_args);
else
ft_enqueue(&(ls_args->files), &inode, sizeof(t_inode));
av++;
}
}
void ft_parse(t_args *ls_args, char **av)
{
char **args;
int i;
i = parse_options(av);
args = av + i;
if (*args == NULL)
args = (char*[2]){".", NULL};
parse_args(ls_args, args);
}