creat - open and possibly create a file or device
#include <sys/types.h> /* for mode_t /
#include <sys/stat.h> / for the S_* contants /
#include <fcntl.h> / for creat() prototype */
int creat(const char *pathname, mode_t mode);
creat(2) is a special form of open(2), refer to open(2) for more information.
The creat(2) system call is used to convert a pathname into a file descriptor (a small, non-negative integer for use in subsequent I/O as with read(2), write(2), etc.). When the call is successful, the file descriptor returned will be the lowest file descriptor not currently open for the process. This call creates a new open file, not shared with any other process. (But shared open files may arise via the fork(2) system call.) The new file descriptor is set to remain open across exec functions (see fcntl(2)). The file offset is set to the beginning of the file.
creat(2) is equivalent to open(2) with flags equal to O_CREAT|O_WRONLY|O_TRUNC.
Since creat(2) doesn't take any flags, any additional flags you may wish to pass, can be modified using fcntl(2) instead.
The argument mode specifies the permissions to use in case a new file is created. It is modified by the process's umask in the usual way: the permissions of the created file are (mode & umask).
The following symbolic constants are provided for mode:
|S_IRWXU|00700| user (file owner) has read, write and execute permission
|S_IRUSR (S_IREAD)|00400| user has read permission
|S_IWUSR (S_IWRITE)|00200| user has write permission
|S_IXUSR (S_IEXEC)|00100| user has execute permission
|S_IRWXG|00070| group has read, write and execute permission
|S_IRGRP|00040| group has read permission
|S_IWGRP|00020| group has write permission
|S_IXGRP|00010| group has execute permission
|S_IRWXO|00007| others have read, write and execute permission
|S_IROTH|00004| others have read permission
|S_IWOTH|00002| others have write permisson
|S_IXOTH|00001| others have execute permission
creat(2) returns the new file descriptor, or -1 if an error occurred (in which case, errno is set appropriately). Note that while open(2) can open device special files, but creat(2) cannot create them - use mknod(2) instead.
If the file is newly created, its atime, ctime, mtime fields are set to the current time, and so are the ctime and mtime fields of the parent directory. Otherwise, if the file is modified because of the O_TRUNC flag, its ctime and mtime fields are set to the current time.
- pathname already exists
- pathname refers to a directory and the access requested involved writing.
- The requested access to the file is not allowed, or one of the directories in pathname did not allow search (execute) permission, or the file did not exist yet and write access to the parent directory is not allowed.
- pathname was too long.
- A directory component in pathname does not exist or is a dangling symbolic link.
- A component used as a directory in pathname is not, in fact, a directory.
- O_NONBLOCK | O_WRONLY is set, the named file is a FIFO and no process has the file open for reading. Or, the file is a device special file and no corresponding device exists.
- pathname refers to a device special file and no corresponding device exists. (This is a Linux kernel bug - in this situation ENXIO must be returned.)
- pathname refers to a file on a read-only filesystem and write access was requested.
- pathname refers to an executable image which is currently being executed and write access was requested.
- pathname points outside your accessible address space.
- Too many symbolic links were encountered in resolving pathname
- pathname was to be created but the device containing pathname has no room for the new file.
- Insufficient kernel memory was available.
- The process already has the maximum number of files open.
- The limit on the total number of files open on the system has been reached.
SVr4, SVID, POSIX, X/OPEN, BSD 4.3
read(2), write(2), fcntl(2), close(2), link(2), mknod(2), mount(2), stat(2), umask(2), unlink(2), socket(2), fopen(3), fifo(4)