C
Chris M. Thomasson said:
Chris M. Thomasson said:
[...]Chris M. Thomasson said:int
stdnull_init(void)
{
if (! g_stdnull)
{
g_stdnull = fopen("/dev/nul", "r+");
if (! g_stdnull)
{
g_stdnull = fopen("nul", "r+");
if (! g_stdnull)
{
g_stdnull = tmpfile();
if (! g_stdnull)
{
return 0;
}
}
}
}
return 1;
}
Keith Thompson said:[...]Chris M. Thomasson said:int
stdnull_init(void)
{
if (! g_stdnull)
{
g_stdnull = fopen("/dev/nul", "r+");
if (! g_stdnull)
{
g_stdnull = fopen("nul", "r+");
if (! g_stdnull)
{
g_stdnull = tmpfile();
if (! g_stdnull)
{
return 0;
}
}
}
}
return 1;
}
What system has a "/dev/nul"? (It's "/dev/null".)
On the other hand, you've provided a way to test the use of tmpfile().
[...]Chris M. Thomasson said:What system has a "/dev/nul"? (It's "/dev/null".)
Typo. Humm, funny that `/dev/nul' works for me on WinXP.
[...]On the other hand, you've provided a way to test the use of tmpfile().
int
stdnull_init(void)
{
if (! g_stdnull)
{
g_stdnull = fopen("/dev/null", "r+");
if (! g_stdnull)
{
g_stdnull = fopen("/dev/nul", "r+");
if (! g_stdnull)
{
g_stdnull = fopen("nul", "r+");
if (! g_stdnull)
{
g_stdnull = tmpfile();
if (! g_stdnull)
{
return 0;
}
}
}
}
}
return 1;
}
Chris said:Typo. Humm, funny that `/dev/nul' works for me on WinXP.
Keith Thompson said:[...]Chris M. Thomasson said:Typo. Humm, funny that `/dev/nul' works for me on WinXP.
If "/dev/nul" works on WinXP, it's probably just ignoring the
directory and treating any file named "nul" or "NUL" as a null device.
I don't know of any system where "/dev/nul" is really what you want.
[...]
BTW, I'm not sure I'd fall back to tmpfile() if neither /dev/null
nor nul works -- or if I did I'd give the function a different name
that doesn't suggest it works with a general-purpose null device.
[snip]Chris M. Thomasson said:Keith Thompson said:[...]Chris M. Thomasson said:news:[email protected]... [...]
What system has a "/dev/nul"? (It's "/dev/null".)
Typo. Humm, funny that `/dev/nul' works for me on WinXP.
On the other hand, you've provided a way to test the use of tmpfile().
If "/dev/nul" works on WinXP, it's probably just ignoring the
directory and treating any file named "nul" or "NUL" as a null device.
I don't know of any system where "/dev/nul" is really what you want.
[...]
BTW, I'm not sure I'd fall back to tmpfile() if neither /dev/null
nor nul works -- or if I did I'd give the function a different name
that doesn't suggest it works with a general-purpose null device.
Good point. Perhaps I should just allow the user to specify a file:
_________________________________________________________________
int
stdnull_init(FILE* file)
{
if (g_stdnull)
{
return 1;
}
else if (file)
{
g_stdnull = file;
return 1;
}
else if ((g_stdnull = fopen("/dev/null", "r+")))
{
return 1;
}
else if ((g_stdnull = fopen("/nul", "r+")))
{
return 1;
}
return 0;
}
Keith Thompson said:[...]Chris M. Thomasson said:Keith Thompson said:[...]
What system has a "/dev/nul"? (It's "/dev/null".)
Typo. Humm, funny that `/dev/nul' works for me on WinXP.
On the other hand, you've provided a way to test the use of tmpfile().
[...]
If "/dev/nul" works on WinXP, it's probably just ignoring the
directory and treating any file named "nul" or "NUL" as a null device.
I don't know of any system where "/dev/nul" is really what you want.
[...]
BTW, I'm not sure I'd fall back to tmpfile() if neither /dev/null
nor nul works -- or if I did I'd give the function a different name
that doesn't suggest it works with a general-purpose null device.
Good point. Perhaps I should just allow the user to specify a file:
_________________________________________________________________
[snip]
I'm a bit concerned about the possiblity that, on a system other than
Unix/Linux or Windows, "nul" or "/nul" might be a valid file name, but
not one you'd want to use.
Sometimes when you're doing system-specific things (even though you're
trying to hide them), it might be better just to bite the bullet and
use system-specific code:
#if defined THIS_IS_UNIX_OR_LINUX
#define NULL_DEVICE "/dev/null"
#elif defined THIS_IS_WINDOWS
#define NULL_DEVICE "NUL"
#else
#undef NULL_DEVICE
#endif
#ifdef NULL_DEVICE
g_stdnull = fopen(NULL_DEVICE", "r+);
...
#else
#error "Need to define NULL_DEVICE for this platform"
#endif
Replace THIS_IS_UNIX_OR_LINUX and THIS_IS_WINDOWS with appropriate
macros that are actually defined on the respective systems, and
consider replacing #error with some other fallback strategy.
Twisty mazes of #ifdefs can be ugly, but you can usually restrict the
ugliness to a very small portion of your code.
DOS and its derivatives (e.g. Windows) consider "NUL" to be a magic
filename* regardless of case or any path that precedes it.
(* Along with CON, AUX, PRN, COM1-COM4, LPT1, and a few others.)
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.