Untitled
unknown
c_cpp
2 years ago
1.2 kB
8
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char* argv[]) {
// Проверяем наличие аргументов
if (argc < 3) {
fprintf(stderr, "Usage: %s <имя_архива> <прототип>\n", argv[0]);
exit(EXIT_FAILURE);
}
int pipefd[2];
pipe(pipefd);
// Создаем процесс для выполнения ar t
if (fork() == 0) {
dup2(pipefd[1], STDOUT_FILENO);
close(pipefd[0]);
close(pipefd[1]);
execl("/usr/bin/ar", "ar", "t", argv[1], NULL);
perror("execl");
exit(EXIT_FAILURE);
}
// Создаем процесс для выполнения grep
if (fork() == 0) {
dup2(pipefd[0], STDIN_FILENO);
close(pipefd[0]);
close(pipefd[1]);
execl("/bin/grep", "grep", argv[2], NULL);
perror("execl");
exit(EXIT_FAILURE);
}
close(pipefd[0]);
close(pipefd[1]);
// Ждем завершения всех дочерних процессов
while (wait(NULL) > 0);
return EXIT_SUCCESS;
}
Editor is loading...
Leave a Comment