From owner-freebsd-questions Mon Dec 21 17:21:41 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id RAA25543 for freebsd-questions-outgoing; Mon, 21 Dec 1998 17:21:41 -0800 (PST) (envelope-from owner-freebsd-questions@FreeBSD.ORG) Received: from sam.networx.ie (ts07-011.dublin.indigo.ie [194.125.205.21]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id RAA25522 for ; Mon, 21 Dec 1998 17:21:35 -0800 (PST) (envelope-from mike@NetworX.ie) Received: from mike (mike.NetworX.ie [194.9.12.33]) by sam.networx.ie (8.8.5/8.8.5) with SMTP id BAA23980 for ; Tue, 22 Dec 1998 01:13:34 GMT X-Organisation: I.T. NetworX Ltd X-Business: Network Applications, Consultancy and Training X-Address: Stonebridge House, Shankill, Co. Dublin, Ireland X-Voice: +353-1-28-27-233 X-Fax: +353-1-28-27-230 Date: Tue, 22 Dec 1998 01:21:36 PST From: Michael Ryan Subject: Mystery with pipe(), fork() and dup() To: FreeBSD Support Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; CHARSET=US-ASCII Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Hi all, This one has me completely stumped. I wanted to write a program which has the same effect as grep pattern | more so I wrote the program listed below. As it stands, it works perfectly every time. However, if I change the line which reads if (pid == 0) to read if (pid) it never works! The stdio for 'more' seems to get screwed up. Can anybody explain why it doesn't work? Also, to my amazement, if I leave the line which reads if (pid == 0) alone, but delete the line which reads close(fd[1]); that is the 3rd last line of main(), it also fails to work! Deleting the other "close(fd[n])" calls don't seem to make any difference. I'd be extremely grateful to anybody who can shed light on this. The program: ---------------------------------------------------------------- #include #include #define GREP "grep" #define MORE "more" void main(int, char **); static void error(char *); static void error (char *msg) { fprintf(stderr, "Error: %s\n", msg); exit(1); } void main (int argc, char **argv) { int fd[2]; pid_t pid; if (pipe(fd)) error("pipe() failed"); if ((pid=fork()) < 0) error("fork() failed"); if (pid == 0) { close(1); if (dup(fd[1]) != 1) error("dup(1) failed"); close(fd[0]); close(fd[1]); argv[0] = GREP; execvp(GREP, argv); error("execvp(" GREP ") failed"); } close(0); if (dup(fd[0]) != 0) error("dup(0) failed"); close(fd[0]); close(fd[1]); execlp(MORE, MORE, 0); error("execlp(" MORE ") failed"); } ---------------------------------------------------------------- Thanks for any help. Bye, Mike mike@NetworX.ie www.NetworX.ie --- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message