-
Notifications
You must be signed in to change notification settings - Fork 17
/
pipex.c
113 lines (104 loc) · 2.72 KB
/
pipex.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mfassi-f <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/12/31 14:34:16 by mfassi-f #+# #+# */
/* Updated: 2013/12/31 18:50:27 by mfassi-f ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <fcntl.h>
#include <pipex.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
static int open_file(char *file_name, char **envp, int flags, int mode)
{
int i;
char *pwd;
char *new_path;
char *part_path;
i = findi(envp, "PWD=");
pwd = envp[i] + 4;
part_path = ft_strjoin(pwd, "/");
new_path = ft_strjoin(part_path, file_name);
ft_strdel(&part_path);
if ((flags == (O_RDWR | O_CREAT)) || ft_access(new_path))
{
if (mode)
i = open(new_path, flags, mode);
else
i = open(new_path, flags);
ft_strdel(&new_path);
return (i);
}
ft_strdel(&new_path);
return (-1);
}
static void call_child(int *p, char **argv, char **envp)
{
int in;
char **cmd;
close(p[0]);
dup2(p[1], 1);
close(p[1]);
in = open_file(argv[1], envp, O_RDONLY, 0);
if (in == -1)
exit(0);
dup2(in, 0);
cmd = ft_strsplit(argv[2], ' ');
if (execve(get_path(envp, cmd), cmd, envp) == -1)
{
ft_putstr_fd("pipex: command not found: ", 2);
ft_putendl_fd(cmd[0], 2);
exit(0);
}
}
static void call_father(int *p, char **argv, char **envp)
{
int out;
char **cmd;
char *path;
out = open_file(argv[4], envp, O_RDWR | O_CREAT | O_TRUNC, 777);
wait(0);
close(p[1]);
dup2(p[0], 0);
close(p[0]);
dup2(out, 1);
cmd = ft_strsplit(argv[3], ' ');
path = get_path(envp, cmd);
if (execve(path, cmd, envp) == -1)
{
ft_putstr_fd("pipex: command not found: ", 2);
ft_putendl_fd(cmd[0], 2);
free_arr(&cmd);
ft_strdel(&path);
exit(0);
}
}
int main(int argc, char **argv, char **envp)
{
pid_t pid;
int p[2];
if (argc != 5)
{
ft_putstr_fd("Usage : ./pipex infile cmd1 cmd2 outfile\n", 2);
return (0);
}
if (pipe(p) == -1)
return (0);
pid = fork();
if (pid == -1)
{
ft_putstr_fd("Fork failed\n", 2);
return (0);
}
if (pid == 0)
call_child(p, argv, envp);
else
call_father(p, argv, envp);
return (0);
}