MAN page from Mandrake Other man-pages-gz-1.21-2.noarch.rpm
OPEN
Section: Linux Programmer's Manual (2)
Updated: December 20, 1996
Index NAME
open, creat - open and possibly create a file or device
SYNOPSIS
#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>int open(const char *pathname, int flags);int open(const char *pathname, int flags, mode_t mode);int creat(const char *pathname, mode_t mode);
DESCRIPTION
openattempts to open a file and return a file descriptor (a small,non-negative integer for use in
read,
write, etc.)
flagsis one ofO_RDONLY, O_WRONLY or O_RDWRwhich request opening the file read-only, write-only or read/write,respectively.
flagsmay also bebitwise-or'dwith one or more of the following:
- O_CREAT
- If the file does not exist it will be created.
- O_EXCL
- When used withO_CREAT,if the file already exists it is an error and theopenwill fail.O_EXCLis broken on NFS file systems, programs which rely on it for performinglocking tasks will contain a race condition. The solution for performingatomic file locking using a lockfile is to create a unique file on the samefs (e.g., incorporating hostname and pid), uselink(2)to make a link to the lockfile and usestat(2)on the unique file to check if its link count has increased to 2. Do notuse the return value of the link() call.
- O_NOCTTY
- Ifpathnamerefers to a terminal device --- seetty(4)--- it will not become the process's controlling terminal even if theprocess does not have one.
- O_TRUNC
- If the file already exists it will be truncated.
- O_APPEND
- The file is opened in append mode. Initially, and before eachwrite,the file pointer is positioned at the end of the file, as ifwithlseek.O_APPENDmay lead to corrupted files on NFS file systems if more than one process appends data to afile at once. This is because NFS does not support appending to a file, so theclient kernel has to simulate it, which can't be done without a race condition.
- O_NONBLOCK or O_NDELAY
- The file is opened in non-blocking mode. Neither theopennor any subsequent operations on the file descriptor which isreturned will cause the calling process to wait.
- O_SYNC
- The file is opened for synchronous I/O. Anywriteson the resulting file descriptor will block the calling process untilthe data has been physically written to the underlying hardware.See RESTRICTIONS below, though.
Some of these optional flags can be altered usingfcntlafter the file has been opened.
modespecifies the permissions to use if a new file is created. It ismodified by the process'sumaskin the usual way: the permissions of the created file are(mode & ~umask).
The following symbolic constants are provided formode:
- 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
modeshould always be specified whenO_CREATis in theflags,and is ignored otherwise.
creatis equivalent toopenwithflagsequal toO_CREAT|O_WRONLY|O_TRUNC.
RETURN VALUE
open and
creatreturn the new file descriptor, or -1 if an error occurred (in which case,
errnois set appropriately).Note that
opencan open device special files, but
creatcannot create them - use
mknod(2)instead.
On NFS file systems with UID mapping enabled, open may return a filedescriptor but e.g. read(2) requests are denied with EACCES.This is because the client performs open by checking the permissions,but UID mapping is performed by the server upon read and write requests.
ERRORS
- EEXIST
- pathnamealready exists andO_CREAT and O_EXCLwere used.
- EISDIR
- pathnamerefers to a directory and the access requested involved writing.
- ETXTBSY
- pathnamerefers to an executable image which is currently being executed andwrite access was requested.
- EFAULT
- pathname points outside your accessible address space.
- EACCES
- The requested access to the file is not allowed, or one of thedirectories inpathnamedid not allow search (execute) permission.
- ENAMETOOLONG
- pathname was too long.
- ENOENT
- A directory component inpathnamedoes not exist or is a dangling symbolic link.
- ENOTDIR
- A component used as a directory inpathnameis not, in fact, a directory.
- EMFILE
- The process already has the maximum number of files open.
- ENFILE
- The limit on the total number of files open on the system has beenreached.
- ENOMEM
- Insufficient kernel memory was available.
- EROFS
- pathnamerefers to a file on a read-only filesystem and write access wasrequested.
- ELOOP
- Too many symbolic links were encountered in resolvingpathname.
- ENOSPC
- pathnamewas to be created but the device containingpathnamehas no room for the new file.
CONFORMING TO
SVr4, SVID, POSIX, X/OPEN, BSD 4.3
RESTRICTIONS
There are many infelicities in the protocol underlying NFS, affectingamongst others
O_SYNC and
O_NDELAY.
SEE ALSO
read(2),
write(2),
fcntl(2),
close(2),
unlink(2),
mknod(2),
stat(2),
umask(2),
mount(2),
socket(2),
socket(2),
fopen(3),
link(2).
Index
- NAME
- SYNOPSIS
- DESCRIPTION
- RETURN VALUE
- ERRORS
- CONFORMING TO
- RESTRICTIONS
- SEE ALSO
This document was created byman2html,using the manual pages.