CREAT(2) — SYSTEM CALLS
NAME
creat − create a new file
SYNOPSIS
int creat(name, mode)
char ∗name;
int mode;
DESCRIPTION
This interface is made obsolete by open(2V).
creat() creates a new ordinary file or prepares to rewrite an existing file named by the path name pointed to by name. If the file did not exist, it is given mode mode, as modified by the process’s mode mask (see umask(2)). Also see chmod(2) for the construction of the mode argument.
If the file exists, its mode and owner remain unchanged, but it is truncated to 0 length. Otherwise, the file’s owner ID is set to the effective user ID of the process.
The file’s group ID is set to either:
• the effective group ID of the process, if the filesystem was not mounted with the BSD file-creation semantics flag (see mount(2)) and the set-gid bit of the parent directory is clear, or
• the group ID of the directory in which the file is created.
The low-order 12 bits of the file mode are set to the value of mode, modified as follows:
• All bits set in the process’s file mode creation mask are cleared. See umask(2).
• The “save text image after execution” (sticky) bit of the mode is cleared. See chmod(2).
• The “set group ID on execution” bit of the mode is cleared if the effective user ID of the process is not super-user and the process is not a member of the group of the created file.
Upon successful completion, the file descriptor is returned and the file is open for writing, even if the mode does not permit writing. The file pointer is set to the beginning of the file. The file descriptor is set to remain open across execve(2) system calls. See fcntl(2V).
NOTES
The mode given is arbitrary; it need not allow writing. This feature has been used in the past by programs to construct a simple exclusive locking mechanism. It is replaced by the O_EXCL open mode, or flock(2) facility.
RETURN VALUE
The value −1 is returned if an error occurs. Otherwise, the call returns a non-negative descriptor which only permits writing.
ERRORS
creat() will fail and the file will not be created or truncated if one of the following occur:
ENOTDIR A component of the path prefix of name is not a directory.
ENAMETOOLONG The length of a component of name exceeds 255 characters, or the length of name exceeds 1023 characters.
ENOENT A component of the path prefix of name does not exist.
ELOOP Too many symbolic links were encountered in translating name.
EACCES Search permission is denied for a component of the path prefix of name.
EACCES The file referred to by name does not exist and the directory in which it is to be created is not writable.
EACCES The file referred to by name exists, but it is unwritable.
EISDIR The file referred to by name is a directory.
EMFILE There are already too many files open.
ENFILE The system file table is full.
ENOSPC The directory in which the entry for the new file is being placed cannot be extended because there is no space left on the file system containing the directory.
ENOSPC There are no free inodes on the file system on which the file is being created.
EDQUOT The directory in which the entry for the new file is being placed cannot be extended because the user’s quota of disk blocks on the file system containing the directory has been exhausted.
EDQUOT The user’s quota of inodes on the file system on which the file is being created has been exhausted.
EROFS The file referred to by name resides, or would reside, on a read-only file system.
ENXIO The file is a character special or block special file, and the associated device does not exist.
EIO An I/O error occurred while making the directory entry or allocating the inode.
EFAULT name points outside the process’s allocated address space.
EOPNOTSUPP The file was a socket (not currently implemented).
SEE ALSO
close(2), chmod(2), execve(2), fcntl(2V), flock(2), mount(2), open(2V), write(2V), umask(2)
Sun Release 4.0 — Last change: 22 November 1987