So hi,
my goal with my code is to have a program similar to pipes in unix like $printenv | sort | less
using recursion. I'm pretty new to pipes and file descriptor manipulation so I don't know if I use them as good as I could. Also, my problem is that this code, although it works, doesn't give me the userability from less. I get the text from less but get sent back to my terminal. Why is that? I haven't bothered with error checking just yet but it'll come! So, here goes:
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define AMOUNT (2)
enum read_write{READ, WRITE};
int std_out;
void rec(char **file_names, int count){
int pid, fd[2];
if(count >= AMOUNT){
fd[WRITE] = std_out;
pid = 1;
}
else{
pipe(fd);
pid = fork();
}
if(!pid){
close(fd[WRITE]);
dup2(fd[READ], STDIN_FILENO);
rec(file_names, count+1);
}
else{
close(fd[READ]);
dup2(fd[WRITE], STDOUT_FILENO);
execl(*(file_names+count), *(file_names+count), (char*) 0);
if(argc==1){}
}
}
int main(int argc, char **argv){
char **name = malloc(1024);
std_out = dup(STDOUT_FILENO);
*name = "/usr/bin/emerge";
*(name+1) = "/bin/sort";
*(name+2) = "/usr/bin/less";
rec(name, 0);
return 0;
}