Hi i don't know what index is. I made a simple prog that work to show you some mistake that you make.
int main()
{
int pfds[2] = { -1, -1 };
int input;
char *argvv = "";
char *tab = malloc(500);
int i;
int nbRead;
int stat;
input = dup(0);
if (pipe(pfds) == -1)
exit(-1);
if (fork() == 0)
{ /* child */
dup2(pfds[1], 1);
close(pfds[0]);
execlp("ls", argvv, NULL);
exit(-1);
}
else
{ /* parent */
close(pfds[1]);
wait(&stat);
do
{
nbRead = read(pfds[0], tab, 500);
for (i = 0; i < nbRead; i++)
printf("%c", tab[i]);
} while (nbRead > 0);
close(pfds[0]);
}
return (0);
}
First : Instead of regarding pfds[0] and pfds[1] value, look at the return value of the function pipe. Function pipe return -1 in case of error (see man pipe).
Second : Don't forget to close all open fd, it's important for the system.
If you can told us more about
input
maybe we can found your problem.