From owner-freebsd-questions Mon Oct 22 23:18:56 2001 Delivered-To: freebsd-questions@freebsd.org Received: from webs1.accretive-networks.net (webs1.accretive-networks.net [207.246.154.13]) by hub.freebsd.org (Postfix) with ESMTP id DA2F837B403 for ; Mon, 22 Oct 2001 23:18:53 -0700 (PDT) Received: from localhost (davidk@localhost) by webs1.accretive-networks.net (8.11.1/8.11.3) with ESMTP id f9N6Inv17319; Mon, 22 Oct 2001 23:18:50 -0700 (PDT) Date: Mon, 22 Oct 2001 23:18:49 -0700 (PDT) From: David Kirchner X-X-Sender: To: Matthew Blacklow Cc: freebsd-questions Subject: Re: C clue on FreeBSD again! In-Reply-To: Message-ID: <20011022230153.Q85958-100000@localhost> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG On Tue, 23 Oct 2001, Matthew Blacklow wrote: > Yesterday I made a post asking how to suppress the output of a program > spawned in C using the System() function. > The replies I had were very helpful and successfully supressed the output. > However they worked a little too good and they even suppressed the return > value of the program which i really need to capture. > > When I dont suppress the output it works fine so I know that the suppressed > output is why i am getting weird return values. Here's another way to do it, without system () (I don't recall if someone posted this way before. Also I can't guarantee this is the best way to do it, :) ) #include #include #include #include #include int main (int argc, char *argv) { pid_t pid; int status; if (pid = fork()) { printf ("this is the parent speaking\n"); wait4(pid, &status, 0, NULL); printf ("%d\n", WEXITSTATUS (status)); } else if (pid == 0) { char buf[1024]; sprintf (buf, "/usr/bin/false"); close (0); close (1); close (2); execlp (buf, NULL); exit (254); } else { perror ("fork"); exit (254); } } This program will fork off a child, close the standard input, output, and error file descriptors, and then calls execlp to replace itself with a new process image (that of /usr/bin/false in this case, there are better examples out there I'm sure. :) To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message