Date: Mon, 22 Oct 2001 23:18:49 -0700 (PDT) From: David Kirchner <davidk@accretivetg.com> To: Matthew Blacklow <matthew.blacklow@ticca.com> Cc: freebsd-questions <freebsd-questions@FreeBSD.ORG> Subject: Re: C clue on FreeBSD again! Message-ID: <20011022230153.Q85958-100000@localhost> In-Reply-To: <NFBBICMIIEMNIMGOBPCMGEIKCEAA.matthew.blacklow@ticca.com>
index | next in thread | previous in thread | raw e-mail
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 <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>
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
home |
help
Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20011022230153.Q85958-100000>
