Name
mkdir
- create a directory
Synopsis
1
2
3
4
#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);
Description
The mkdir()
function attempts to create a directory named pathname
.
The argument mode specifies the mode for the new directory. In this course,
you will only need to use 0777
(see example below). Directories will not
be created recursively by this function!
Example
1
2
3
4
5
6
7
8
#include <sys/stat.h>
#include <sys/types.h>
#include <assert.h>
void main(void) {
mode_t mode = 0777;
assert(mkdir("directory", mode) == 0);
}
Return Value
Returns zero on success, or -1 if an error occurred (in which case, errno
is set appropriately).
Errors
- EACCES
- The parent directory does not allow write permission to the process, or one of the directories in pathname did not allow search permission. (See also path_resolution(7).)
- EDQUOT
- The user's quota of disk blocks or inodes on the filesystem has been exhausted.
- EEXIST
- pathname already exists (not necessarily as a directory). This includes the case where pathname is a symbolic link, dangling or not.
- EFAULT
- pathname points outside your accessible address space.
- EINVAL
- The final component ("basename") of the new directory's pathname is invalid (e.g., it contains characters not permitted by the underlying filesystem).
- ELOOP
- Too many symbolic links were encountered in resolving pathname.
- EMLINK
- The number of links to the parent directory would exceed LINK_MAX.
- ENAMETOOLONG
- pathname was too long.
- ENOENT
- A directory component in pathname does not exist or is a dangling symbolic link.
- ENOMEM
- Insufficient kernel memory was available.
- ENOSPC
- The device containing pathname has no room for the new directory.
- ENOSPC
- The new directory cannot be created because the user's disk quota is exhausted.
- ENOTDIR
- A component used as a directory in pathname is not, in fact, a directory.
- EPERM
- The filesystem containing pathname does not support the creation of directories.
- EROFS
- pathname refers to a file on a read-only filesystem.