[Previous]
[Contents]
[Next]

mknod()

make a new filesystem entry point

Synopsis:

#include <sys/types.h>
#include <unistd.h>
int mknod( const char *path,
           mode_t mode,
           int dev );

Description:

The mknod() function makes a file, named path, using the filetype encoded in the mode argument. Supported filetypes are directories and FIFOs. For POSIX portability the mkdir() and mkfifo() functions should be used instead.

This function is included to enhance portability with software written for UNIX-compatible operating systems. The argument dev is ignored in the QNX implementation.

To make a directory with read-write-execute permissions for everyone, you could use the following:

mknod (name, S_IFDIR|0777,0);

Returns:

0
Success
-1
An error occurred. errno is set to indicate the error.

Errors:

EACCES
A component of the path prefix denies search permission, or write permission is denied for the parent directory.
EEXIST
The named file already exists.
EMLINK
The link count of the parent directory would exceed LINK_MAX.
ENAMETOOLONG
The length of the path string exceeds PATH_MAX, or a pathname component is longer than NAME_MAX.
ENOENT
A component of the path prefix doesn't exist, or the path arguments points to an empty string.
ENOSPC
The directory that would contain the new file cannot be extended or the file system is out of file allocation resources (that is, the disk is full).
ENOTDIR
A component of the path prefix isn't a directory.
EROFS
The named file resides on a read-only file system.

Examples:

/*
 * create special files as a directory or FIFO
 */
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>

void main( int argc, char **argv )
  {
    int c;
    mode_t mode = 0666;
    int ecode = 0;

    if( argc == 1 ) {
      printf( "Use: %s [-d directory] ... [-f fifo] ... \n",
          argv[1] );
      exit( 0 );
    }

    while(( c = getopt( argc, argv, "d:f:" )) != -1 ) {
      switch( c ) {
        case 'd': mode = S_IFDIR | 0666; break;
        case 'f': mode = S_IFIFO | 0666; break;
      }

      if( mknod( optarg, mode, 0 ) != 0 ) {
        perror( optarg );
        ++ecode;
      }
    }

    exit( ecode );
  }

Classification:

UNIX

Safety:
Interrupt handler No
Signal handler Yes
Thread Yes

See also:

errno, mkdir(), mkfifo()


[Previous]
[Contents]
[Next]