/* gcc callfilter.c -o callfilter */ #include #include #include #include int main (int argc, char *argv[]) { int writer[2]; /* From the parent's point of view */ int reader[2]; /* From the parent's point of view */ int pid; char *msg = "hullo"; char buf[10]; int nread; /* Get pipes on */ if (pipe(reader)) { perror("reader pipe(): "); return 1; } if (pipe(writer)) { perror("writer pipe(): "); return 1; } pid = fork(); if (pid < 0) { perror("fork(): "); return 1; } else if (pid == 0) { /* In the child */ /* close unneeded endpoints */ close(reader[0]); close(writer[1]); /* Duplicate handles on STDIN and STDOUT for child */ dup2(writer[0], 0); dup2(reader[1], 1); /* call filter */ execl("./filter.pl", "./filter.pl", NULL); /* patched, thanks to djp */ perror("execl(): "); return 1; } else { /* In the parent */ /* close unneeded endpoints */ close(reader[1]); close(writer[0]); /* Send something to the filter */ write(writer[1], msg, strlen(msg)); close(writer[1]); /* Read response from the filter */ printf("filter says:\n"); while ((nread = read(reader[0], buf, 9)) > 0) { buf[nread] = '\x0'; printf("%s", buf); } if (nread < 0) { perror("read(): "); return 1; } printf("\n"); close(reader[0]); } return 0; } #### #!/usr/bin/perl use strict; use warnings; local $/; my $in = ; print {*STDOUT} scalar reverse $in; #### poletti@PolettiX:~/sviluppo/perl/dup$ ./callfilter filter says: olluh