Wednesday, August 1, 2007

Mangling with fd and /dev/null

When doing the code faced a problem with the SSL Libraries doing error output and want to ignore it. Just got a nice idea from my friend Michael Waters. Here it is.

#include <iostream>
#include <fcntl.h>

using namespace std;

int main(int argc,char *argv[])
{
int devnullfd = open("/dev/null",O_RDWR);
int stderrfd = dup(2);
dup2(devnullfd,2);
cerr << "Am I out?" <<>
dup2(stderrfd,2);
cerr << "Am I In?" <<>
close(devnullfd);
}

svn also has the example in C.

You can download from svn with the command below.

svn checkout http://codeoftheday.googlecode.com/svn/trunk/01Aug2007

Enjoy. Will try to get something out tomorrow.

No comments: