Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

open(2) [plan9 man page]

OPEN(2) 							System Calls Manual							   OPEN(2)

NAME
open, create, close - open a file for reading or writing, create file SYNOPSIS
#include <u.h> #include <libc.h> int open(char *file, int omode) int create(char *file, int omode, ulong perm) int close(int fd) DESCRIPTION
Open opens the file for I/O and returns an associated file descriptor. Omode is one of OREAD, OWRITE, ORDWR, or OEXEC, asking for permis- sion to read, write, read and write, or execute, respectively. In addition, there are three values that can be ORed with the omode: OTRUNC says to truncate the file to zero length before opening it; OCEXEC says to close the file when an exec(2) or execl system call is made; and ORCLOSE says to remove the file when it is closed (by everyone who has a copy of the file descriptor). Open fails if the file does not exist or the user does not have permission to open it for the requested purpose (see stat(2) for a description of permissions). The user must have write permission on the file if the OTRUNC bit is set. For the open system call (unlike the implicit open in exec(2)), OEXEC is actually identical to OREAD. Create creates a new file or prepares to rewrite an existing file, opens it according to omode (as described for open), and returns an associated file descriptor. If the file is new, the owner is set to the userid of the creating process group; the group to that of the containing directory; the permissions to perm ANDed with the permissions of the containing directory. If the file already exists, it is truncated to 0 length, and the permissions, owner, and group remain unchanged. The created file is a directory if the CHDIR bit is set in omode. It is an exclusive-use file if the CHEXCL bit is set. Such files may be open for I/O by only one client at a time, but the file descriptor may become invalid if no I/O is done for an extended period; see open(5). Create fails if the path up to the last element of file cannot be evaluated, if the user doesn't have write permission in the final direc- tory, or if the file already exists and does not permit the access defined by omode. If the file is new and the directory in which it is created is a union directory (see intro(2)) then the constituent directory where the file is created depends on the structure of the union: see bind(2). Close closes the file associated with a file descriptor. Provided the file descriptor is a valid open descriptor, close is guaranteed to close it; there will be no error. Files are closed automatically upon termination of a process; close allows the file descriptor to be reused. SOURCE
/sys/src/libc/9syscall SEE ALSO
intro(2), bind(2), stat(2) DIAGNOSTICS
These functions set errstr. OPEN(2)

Check Out this Related Man Page

STAT(2) 							System Calls Manual							   STAT(2)

NAME
stat, fstat, wstat, fwstat, dirstat, dirfstat, dirwstat, dirfwstat - get and put file status SYNOPSIS
#include <u.h> #include <libc.h> int stat(char *name, char *edir) int fstat(int fd, char *edir) int wstat(char *name, char *edir) int fwstat(int fd, char *edir) int dirstat(char *name, Dir *dir) int dirfstat(int fd, Dir *dir) int dirwstat(char *name, Dir *dir) int dirfwstat(int fd, Dir *dir) DESCRIPTION
Given a file's name, or an open file descriptor fd, these routines retrieve or modify file status information. Stat, fstat, wstat, and fwstat are the system calls; they deal with machine-independent directory entries. Their format is defined by stat(5). Stat and fstat retrieve information about name or fd into edir, a buffer of length DIRLEN, defined in <libc.h>. Wstat and fwstat write information back, thus changing file attributes according to edir. Dirstat, dirfstat, dirwstat, and dirfwstat are the same as their counterparts, except that they operate on Dir structures: typedef struct Dir { char name[NAMELEN]; /* last element of path */ char uid[NAMELEN]; /* owner name */ char gid[NAMELEN]; /* group name */ Qid qid; /* unique id from server */ long mode; /* permissions */ long atime; /* last read time */ long mtime; /* last write time */ Length; /* file length: see <u.h> */ ushort type; /* server type */ ushort dev; /* server subtype */ } Dir; This structure, the Qid structure, NAMELEN, and DIRLEN are defined in <libc.h>. The Length structure is defined in </$objtype/u.h>. Length is an unnamed structure (see 2c(1)), which means that its fields are directly accessible; if the length is known to fit in a long, then use length as a field name to retrieve it. If the file resides on permanent storage and is not a directory, the length returned by stat is the number of bytes in the file. For directories, the length returned is zero. For files that are streams (e.g., pipes and net- work connections), the length is the number of bytes that can be read without blocking. Each file is the responsibility of some server: it could be a file server, a kernel device, or a user process. Type identifies the server type, and dev says which of a group of servers of the same type is the one responsible for this file. Qid is a structure containing path and vers fields, each an unsigned long: path is guaranteed to be unique among all path names currently on the file server, and vers changes each time the file is modified. Thus, if two files have the same type, dev, and qid they are the same file. The bits in mode are defined by 0x80000000 directory 0x40000000 append only 0x20000000 exclusive use (locked) 0400 read permission by owner 0200 write permission by owner 0100 execute permission (search on directory) by owner 0070 read, write, execute (search) by group 0007 read, write, execute (search) by others There are constants defined in <libc.h> for these bits: CHDIR, CHAPPEND, and CHEXCL for the first three; and CHREAD, CHWRITE, and CHEXEC for the read, write, and execute bits for others. The two time fields are measured in seconds since the epoch (Jan 1 00:00 1970 GMT). Mtime is the time of the last change of content. Sim- ilarly, atime is set whenever the contents are accessed; also, it is set whenever mtime is set. Uid and gid are the names of the owner and group of the file. Groups are also users, but each server is free to associate a list of users with any user name g, and that list is the set of users in the group g. When an initial attachment is made to a server, the user string in the process group is communicated to the server. Thus, the server knows, for any given file access, whether the accessing process is the owner of, or in the group of, the file. This selects which sets of three bits in mode is used to check permissions. Only some of the fields may be changed with the wstat calls. The name can be changed by anyone with write permission in the parent direc- tory. The mode and mtime can be changed by the owner or the group leader of the file's current group. The gid can be changed by the owner if he or she is a member of the new group. The gid can be changed by the group leader of the file's current group if he or she is the leader of the new group. (See intro(5) for permission information, and users(6) for user and group information). SOURCE
/sys/src/libc/9syscall for the non-dir routines /sys/src/libc/9sys for the routines prefixed dir SEE ALSO
intro(2), fcall(2), dirread(2), stat(5) DIAGNOSTICS
All these functions return 0 on success, -1 on error, and set errstr. STAT(2)
Man Page