/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#define READ_END 0
#define WRITE_END 1
void exec_pipes(char ***argv)
{
int p[2];
int fd_save = 0;
while (*argv)
{
pipe(p);
if (fork() == 0)
{
dup2(fd_save, STDIN_FILENO);
if (*(argv + 1))
dup2(p[WRITE_END], STDOUT_FILENO);
close(p[READ_END]);
execvp(**argv, *argv);
exit(EXIT_FAILURE);
}
else
{
wait(NULL);
fd_save = p[READ_END];
close(p[WRITE_END]);
argv++;
}
}
}
int main()
{
char *rand2[] = {"base64", "/dev/urandom", NULL};
char *head[] = {"head", "-c", "1000", NULL};
char **cmd[] = {rand2, head, NULL};
exec_pipes(cmd);
return (0);
}