Question

Possible Duplicate:
redirecting output to a file in C

I'm running a process as follows:

char* [NUM];
char[0] = processName;
char[1] = arg0;
...
char[NUM] = 0;
execv(args[0],args);

The question is how I can make it redirect its output to

/dev/null

I tried adding it to the args array but it sent it as arguments to the program...

Any idea anyone?

Thanks

Was it helpful?

Solution

First open the target you want, then close the fd you want to reassign, use dup2 to connect it there, and close the previous open:

int tmpFd = open( "/dev/null", O_WRONLY );
if ( tmpFd == -1 ) {
    //  Real problem, couldn't open /dev/null
}
if ( dup2( tmpFd, 1 ) ) != 1 ) {
    //  Real problem, dup2 failed.
}
close( tmpFd ); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top