-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.c
75 lines (68 loc) · 1.96 KB
/
flags.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* flags.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rklein <rklein@student.hive.fi> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/05 12:38:15 by rklein #+# #+# */
/* Updated: 2020/06/05 12:41:03 by rklein ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_ls.h"
/*
** Functions that takes the input parameters and create a struct
** with a _Bool variable for each option flag. In this project only
** -Ralrt are handled, anything is else is conidered an illegal
** option, and gives error and usage messages.
*/
static int ft_noflag(char c)
{
write(1, "ft_ls: illegal option -- ", 25);
write(1, &c, 1);
write(1, "\nusage: ft_ls [-Ralrt] [file ...]\n", 34);
return (-1);
}
static int ft_isflag(char c, char *ref)
{
while (*ref)
{
if (c == *ref)
return (1);
ref++;
}
return (0);
}
static void ft_options(char c, t_flag *flags)
{
if (c == 'a')
flags->a = 1;
if (c == 'l')
flags->l = 1;
if (c == 'r')
flags->r = 1;
if (c == 'R')
flags->rr = 1;
if (c == 't')
flags->t = 1;
}
int ft_flags(int ac, char **av, t_flag *flags)
{
int i;
int j;
i = 1;
while (i < ac && av[i][0] == '-' && av[i][1])
{
if (av[i][1] == '-' && !av[i][2])
return (i + 1);
j = 0;
while (av[i][++j])
{
if (!ft_isflag(av[i][j], "alrRt"))
return (ft_noflag(av[i][j]));
ft_options(av[i][j], flags);
}
i++;
}
return (i);
}