MKDIR(2) | System Calls | MKDIR(2) |
mkdir, mkdirat - make a directory
#include <sys/types.h> #include <sys/stat.h> int mkdir(const char *path, mode_t mode);
int mkdirat(int fd, const char *path, mode_t mode);
The mkdir() and mkdirat() functions create a new directory named by the path name pointed to by path. The mode of the new directory is initialized from mode (see chmod(2) for values of mode). The protection part of the mode argument is modified by the process's file creation mask (see umask(2)).
The directory's owner ID is set to the process's effective user ID. The directory's group ID is set to the process's effective group ID, or if the S_ISGID bit is set in the parent directory, then the group ID of the directory is inherited from the parent. The S_ISGID bit of the new directory is inherited from the parent directory.
If path names a symbolic link, mkdir() and mkdirat() fail and set errno to EEXIST.
The newly created directory is empty with the exception of entries for itself (.) and its parent directory (..).
The mkdirat() function behaves similarly to mkdir(); however, if path is a relative path, then the directory represented by fd is used as the starting point for resolving path. To use the processes current working directory, fd may be set to the value AT_FDCWD.
Upon successful completion, mkdir() and mkdirat() mark for update the st_atime, st_ctime and st_mtime fields of the directory. Also, the st_ctime and st_mtime fields of the directory that contains the new entry are marked for update.
Upon successful completion, 0 is returned. Otherwise, −1 is returned, no directory is created, and errno is set to indicate the error.
The mkdir() and mkdirat() functions will fail if:
EACCES
EDQUOT
EEXIST
EFAULT
EINVAL
EIO
EILSEQ
ELOOP
EMLINK
ENAMETOOLONG
ENOENT
ENOLINK
ENOSPC
ENOTDIR
EROFS
The mkdirat() function will fail if:
EBADF
The mkdir() function may fail if:
ENAMETOOLONG
Example 1 Create a directory.
The following example demonstrates how to create a directory named /home/cnd/mod1, with read, write, and search permissions for owner and group, and with read and search permissions for others.
#include <sys/types.h> #include <sys/stat.h> int status; ... status = mkdir("/home/cnd/mod1",
S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
See attributes(7) for descriptions of the following attributes:
ATTRIBUTE TYPE | ATTRIBUTE VALUE |
Interface Stability | Standard |
MT-Level | Async-Signal-Safe |
chmod(2), mknod(2), umask(2), mkdirp(3GEN), stat.h(3HEAD), attributes(7), standards(7)
September 24, 2016 | OmniOS |