Search code examples
cposixmknod

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 close? Which way is safer?


Solution

  • 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.