-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin_outfile.c
executable file
·88 lines (81 loc) · 2.21 KB
/
min_outfile.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* min_outfile.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pmoreno- <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/22 12:44:25 by potero-d #+# #+# */
/* Updated: 2022/07/27 13:35:37 by pmoreno- ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static char *ft_strchar(const char *str, int c)
{
size_t i;
i = 0;
while (i <= ft_strlen(str))
{
if (str[i] == (unsigned char)c)
return ((char *)(&str[i + 1]));
i++;
}
if (c == '\0')
return (0);
return (0);
}
static void no_appen(t_argv *argv, int i)
{
if (argv->split[i][1])
{
argv->outfile = ft_strdup(ft_strchar(argv->split[i], '>'));
clean_split(argv, i, 1);
}
else
{
argv->outfile = ft_strdup(argv->split[i + 1]);
clean_split(argv, i, 2);
}
}
static void appen(t_argv *argv, int i)
{
if (argv->split[i][2])
{
argv->outfile = ft_strdup(ft_strchar(argv->split[i], '>') + 1);
clean_split(argv, i, 1);
}
else
{
argv->outfile = ft_strdup(argv->split[i + 1]);
clean_split(argv, i, 2);
}
}
int set_outfile(t_argv *argv, int i)
{
if (argv->split[i][0] == '>' && argv->split[i][1] != '>'
&& (argv->split[i][1] || argv->split[i + 1]))
{
free(argv->outfile);
no_appen(argv, i);
}
else if (argv->split[i][0] == '>' && argv->split[i][1] == '>'
&& (argv->split[i][2] || argv->split[i + 1]))
{
argv->out = 2;
free(argv->outfile);
appen(argv, i);
}
else
{
printf("Minishell: syntax error near unexpected token `newline'\n");
clean_split(argv, i, 1);
return (1);
}
return (0);
}
int check_if_outfile(t_argv *argv, int i)
{
if (argv->split[i][0] == '>')
return (1);
return (0);
}