Yes, /dev/null
is always openable -- except when it isn't.
That sounds silly, but I'm not joking. If /dev/null
can't be opened, you may have a badly broken, probably borderline nonfunctional system -- but knowing this is not the same as a guarantee that the file is openable.
There's always a reason that opening a file can fail. You should never look for an excuse not to check the return value of fopen
for failure.
It may never happen that you know about, it may never happen on a properly-functioning system, but ask yourself, what will happen if opening /dev/null
"impossibly" fails?
If your program checks for fopen
failure, it will print a message like "Impossible error! Can't open /dev/null"
, and it will be clear what's going on.
If your program fails to check for fopen
failure, it will mysteriously crash the first time it tries to print something to whereToPrint
, and your user will be left wondering what went wrong.
Programs that crash mysteriously are Bad. Programs that tell you what's going on are Good.
And the more you can tell your user about what's going on, the better. I suggested printing "Impossible error! Can't open /dev/null"
, and that's better than nothing, but it's actually still significantly incomplete. You should really write code that behaves something like this:
#include <stdio,h>
#include <string.h>
#include <errno.h>
FILE *whereToPrint;
if(strcmp(custom_topic, ERROR_TOPIC) != 0)
whereToPrint = stdout;
else if((whereToPrint = fopen("/dev/null", "w")) == NULL) {
fprintf(stderr, "Impossible error! Can't open /dev/null: %s\n", strerror(errno));
exit(1);
}
Now, on that "impossible" occasion that it fails, it will tell you why it couldn't open /dev/null
, and that may be fantastically useful information. It might print
Impossible error! Can't open /dev/null: No such file or directory
if /dev/null
somehow doesn't exist. Or it might print
Impossible error! Can't open /dev/null: Permission denied
if, as others have suggested, someone wrongly restricted the permissions on /dev/null
on your system. Or it might print
Impossible error! Can't open /dev/null: Too many open files
And that, in fact, is a way it could fail even on a correctly-configured system, due to a bug in your program!
For example, going back to your "nice ternary operator", if you ever wrote something like
void log_message(const char *msg)
{
FILE *whereToPrint = (strcmp(custom_topic, ERROR_TOPIC) == 0) ?
fopen("/dev/null", "w") : stdout;
fprintf(whereToPrint, "%s", msg);
}
you would be quite likely to get the "Too many open files" error sooner or later, because of course the log_message()
function I've written here has a bug: it opens the file (maybe) every time it's called, but never closes it.
"Nice" uses of the ternary operator -- or any other "nice" tricks -- are fun to write, and they're fine if they work, but don't please don't cling to them at the expense of other, more important facets of your code, like making sure that it works well under all circumstances. :-)