-
Notifications
You must be signed in to change notification settings - Fork 0
/
0files.c
97 lines (88 loc) · 2.43 KB
/
0files.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 0files.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: guilmira <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/01 12:48:01 by guilmira #+# #+# */
/* Updated: 2021/12/06 10:16:38 by guilmira ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
/** PURPOSE : Searchs on arguments for '<' symbol. */
static int file_symbol_detected(char *str)
{
int i;
i = -1;
while (str[++i])
{
if (str[i] == '<' && (str[i + 1] == ' ' || !str[i + 1]))
{
if (i == 0)
return (1);
else if (i != 0)
{
str[i - 1] = ' ';
return (1);
}
}
}
return (0);
}
/** PURPOSE : Is a file being introduced as an input to the program? */
static int file_detector(int argc, char *argv[])
{
int i;
int j;
j = -1;
i = -1;
while (++i < argc)
if (file_symbol_detected(argv[i]))
if (file_exists(argv[i + 1]))
return (1);
return (0);
}
/** PURPOSE : Load into struct file descriptors for input and output. */
static int file_arrangement(int argc, char *argv[], t_arguments *args)
{
int i;
int counter;
i = -1;
counter = 0;
args->flag_file = 1;
while (++i < argc)
{
if (ft_strncmp(argv[i], "1files/", 7) == 0)
{
if (!counter++)
args->file_input = argv[i];
else
{
args->file_output = argv[i];
return (1);
}
}
}
return (0);
}
/** PURPOSE : Load arguments into structure.
* 1. Checks whether files are given as parameter.
* 2. Sets adresses to input and output files.
* 3. Sets some of the values of "args" struct. */
int file_management(int argc, char *argv[], t_arguments *args)
{
int modifier;
modifier = 1;
args->total_commands = argc - 1;
if (file_detector(argc, argv))
{
file_arrangement(argc, argv, args);
args->total_commands = argc - NOT_COMMANDS;
modifier = 3;
}
else
args->flag_file = 0;
args->command_number = 0;
return (modifier);
}