is mknod portable? if not, what is the alternative?
I would like to use mknod
in my code to create a file, but man
says, that
The only portable use of mknod() is to create a FIFO-special file. If mode is not S_IFIFO or dev is not 0, the behavior of mknod() is unspecified.
Does that mean, that mknod is not really portable and I should use some other way to create a function? How about calling open
and the instantly clos开发者_Python百科e
? Which way is safer?
mknod
is not for creating files. It's for creating device nodes. A portable application will never need to create device nodes because whether they exist, what they are, and how they're implemented/numbered is an implementation detail.
mknod
historically allowed you to create fifos too (and perhaps ordinary files?), but there are instead standard interfaces for this: mkfifo
for fifos, and creat
(or open
with O_CREAT
) for ordinary files.
If you read the rest of that paragraph, you will see:
However, nowadays one should never use mknod() for this purpose;
one should use mkfifo(3), a function especially defined for this purpose.
so what POSIX.1-2001
says here is already outdated. I would just ignore it. mknod
is still being used by the init scripts in the Linux systems.
精彩评论