-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
130 lines (119 loc) · 2.87 KB
/
main.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
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
123
124
125
126
127
128
129
130
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emansoor <emansoor@student.hive.fi> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/16 06:35:48 by emansoor #+# #+# */
/* Updated: 2024/05/05 13:41:59 by emansoor ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
/*
finds allowed paths and splits them into 2D array
*/
static char **path_finder(char **envp)
{
int index;
char **paths;
index = 0;
while (envp[index])
{
if (ft_strncmp(envp[index], "PATH=", 5) == 0)
break ;
index++;
}
if (!envp[index])
return (NULL);
paths = ft_split(envp[index] + 5, ':');
if (!paths)
{
perror(NULL);
return (NULL);
}
return (paths);
}
/*
opens infile and saves it's file descriptor to first command
*/
static int open_files(t_cmds **cmds, int argc, char **argv)
{
t_cmds *command;
t_cmds *last;
command = *cmds;
command->fd_infile = open(argv[1], O_RDONLY, 0666);
if (command->fd_infile < 0)
{
perror("open");
return (1);
}
last = ft_lstlast_pipex(*cmds);
last->fd_outfile = open(argv[argc - 1], O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (last->fd_outfile < 0)
{
perror("open");
return (1);
}
return (0);
}
/*
closes any open filedescriptors
*/
void close_files(t_cmds **cmds)
{
t_cmds *command;
command = *cmds;
while (command)
{
if (command->fd_infile != -1)
close(command->fd_infile);
if (command->fd_outfile != -1)
close(command->fd_outfile);
command = command->next;
}
}
/*
adds the number of pipes to each command's struct
*/
static void add_cmdinfo(t_cmds **cmds)
{
t_cmds *command;
int nbr_of_cmds;
nbr_of_cmds = ft_lstsize_pipex(*cmds);
command = *cmds;
while (command)
{
command->commands = nbr_of_cmds;
command = command->next;
}
}
/*
it's a slightly above-average main
*/
int main(int argc, char **argv, char **envp)
{
t_cmds *cmds;
char **paths;
if (argc < 5)
return (1);
paths = path_finder(envp);
if (!paths)
return (1);
cmds = NULL;
if (collect_commands(argc, argv, &cmds, paths) > 0)
{
free_array(paths);
ft_lstclear_pipex(&cmds, free);
return (1);
}
free_array(paths);
add_cmdinfo(&cmds);
if (open_files(&cmds, argc, argv) > 0)
error(&cmds, NULL, 1);
run_commands(&cmds, envp);
printf("do I ever get here\n");
close_files(&cmds);
ft_lstclear_pipex(&cmds, free);
return (0);
}