#include
#include
#include
#define MAXLINE 512
int main(void) {
char line[MAXLINE];
FILE *fpin = {0};
FILE *fpout = {0};
pid_t child = -1;
int fd[2] = {0};
int i = 0;
pipe(fd);
if((child = fork()) == -1) {
perror("fork");
return 1;
}
if(child == 0) {
close(fd[0]);
fpout = fdopen(fd[1], "w");
if(fpout == NULL) {
fprintf(stderr, "Error - fdopen(child)\n");
return 1;
}
for(i = 0; i < 10; i++)
fprintf(fpout, "%s\n", "jeronimooo...");
fclose(fpout);
return 0;
} else {
close(fd[1]);
fpin = fdopen(fd[0], "r");
if(fpin == NULL) {
fprintf(stderr, "Error - fdopen(parent)\n");
return 1;
}
while(fgets(line, MAXLINE, fpin) != NULL)
printf("%d: %s", i++, line);
fclose(fpin);
}
return 0;
}