-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipex-bonus.c
126 lines (112 loc) · 3.04 KB
/
pipex-bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex-bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jleslee <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/07 22:03:56 by jleslee #+# #+# */
/* Updated: 2022/02/06 23:57:22 by jleslee ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
// Открываем файл на чтение и возвращаем
// Файловый дескриптор
int openfile(char *filename, int mode)
{
if (mode == 0)
{
if (access(filename, F_OK))
{
write(2, "File not found\n", 15);
return (0);
}
return (open(filename, O_RDONLY));
}
else
return (open(filename, O_CREAT | O_WRONLY | O_TRUNC));
}
// Парсим путь к введённой команде из переменных окружения
char *full_command_path(char *cmd, char **env)
{
char *path;
char *dir;
char *bin;
int i;
i = 0;
while (env[i] && ncompare(env[i], "PATH=", 5))
i++;
if (!env[i])
return (cmd);
path = env[i] + 5;
while (path && len_ch(path, ':') > -1)
{
dir = str_ndup(path, len_ch(path, ':'));
bin = make_command(dir, cmd);
free(dir);
if (access(bin, F_OK) == 0)
return (bin);
free(bin);
path += len_ch(path, ':') + 1;
}
return (cmd);
}
// Выполняем команду по распарсенному пути
void second_command_processing(char *cmd, char **env)
{
char **args;
char *path;
args = str_split(cmd, ' ');
if (len_ch(args[0], '/') > -1)
path = args[0];
else
path = full_command_path(args[0], env);
execve(path, args, env);
write(2, "Сommand not found\n", 19);
exit(127);
}
// Создаём дочерний процесс под каждую команду
// И выполняем в нём команду
void first_command_processing(char *cmd, char **env, int fdin)
{
pid_t pid;
int pipefd[2];
pipe(pipefd);
pid = fork();
if (pid)
{
close(pipefd[1]);
dup2(pipefd[0], 0);
waitpid(pid, NULL, 0);
}
else
{
close(pipefd[0]);
dup2(pipefd[1], 1);
if (fdin == 0)
exit(1);
else
second_command_processing(cmd, env);
}
}
int main(int ac, char **av, char **env)
{
int fdin;
int fdout;
int i;
i = 3;
if (ac >= 5)
{
fdin = openfile(av[1], 0);
fdout = openfile(av[ac - 1], 1);
dup2(fdin, 0);
dup2(fdout, 1);
first_command_processing(av[2], env, fdin);
while (i < ac - 2)
first_command_processing(av[i++], env, 1);
second_command_processing(av[i], env);
}
else
write(2, "Invalid number of arguments.\n", 29);
return (1);
}