自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

open 中文man頁面

系統(tǒng)
open() 通常 用于 將 路徑名 轉(zhuǎn)換為 一個 文件描述符 (一個 非負的 小 整數(shù), 在 read , write 等 I/O操作中 將會被使用). 當 open() 調(diào)用 成功, 它會 返回 一個 新的 文件描述符 (永遠取 未用 描述符的 最小值). 這個調(diào)用 創(chuàng)建 一個 新的 打開文件, 即 分配 一個 新的 獨一無二的 文件描述符, 不會與 運行中的 任何 其他程序 共享 (但 可以 通過 fork (2) 系統(tǒng)調(diào)用 實現(xiàn) 共享). 這個 新的 文件描述符 在其后 對 打開文件操作 的函數(shù) 中 使用.(參考 fcntl(2)). 文件的 讀寫 指針 被 置于 文件頭

NAME

open, creat - 用來 打開和創(chuàng)建 一個 文件或設備  

SYNOPSIS 總覽

#includ e <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)

open() 通常 用于 將 路徑名 轉(zhuǎn)換為 一個 文件描述符 (一個 非負的 小 整數(shù), 在 read , write 等 I/O操作中 將會被使用). 當 open() 調(diào)用 成功, 它會 返回 一個 新的 文件描述符 (永遠取 未用 描述符的 最小值). 這個調(diào)用 創(chuàng)建 一個 新的 打開文件, 即 分配 一個 新的 獨一無二的 文件描述符, 不會與 運行中的 任何 其他程序 共享 (但 可以 通過 fork (2) 系統(tǒng)調(diào)用 實現(xiàn) 共享). 這個 新的 文件描述符 在其后 對 打開文件操作 的函數(shù) 中 使用.(參考 fcntl(2)). 文件的 讀寫 指針 被 置于 文件頭

參數(shù) flags 是通過 O_RDONLY, O_WRONLYO_RDWR (指明 文件 是以 只讀 , 只寫 或 讀寫 方式 打開的) 與 下面的 零個 或 多個 可選模式 按位 -or 操作 得到的:

O_CREAT
若文件 不存在 將 創(chuàng)建 一個 新 文件. 新 文件 的 屬主 (用戶ID) 被 設置 為 此 程序 的 有效 用戶 的 ID. 同樣 文件 所屬 分組 也 被 設置 為 此 程序 的 有效 分組 的 ID 或者 上層 目錄 的 分組 ID (這 依賴 文件系統(tǒng) 類型 ,裝載選項 和 上層目錄 的 模式, 參考,在 mount(8) 中 描述 的 ext2 文件系統(tǒng) 的 裝載選項 bsdgroupssysvgroups )
O_EXCL
通過 O_CREAT, 生成 文件 , 若 文件 已經(jīng) 存在 , 則 open 出錯 , 調(diào)用 失敗 . 若是 存在 符號聯(lián)接 , 將會 把 它的 聯(lián)接指針 的 指向 文件 忽略. O_EXCL is broken on NFS file systems, programs which rely on it for performing locking tasks will contain a race condition. The solution for performing atomic file locking using a lockfile is to create a unique file on the same fs (e.g., incorporating hostname and pid), use link(2) to make a link to the lockfile. If link() returns 0, the lock is successful. Otherwise, use stat(2) on the unique file to check if its link count has increased to 2, in which case the lock is also successful.
O_NOCTTY
假如 pathname 引用 一個 終端設備 --- 參考 tty(4) --- 即使 進程 沒有 控制終端 ,這個 終端 也 不會 變成 進程 的 控制 終端.
O_TRUNC
假如 文件 已經(jīng) 存在 , 且是 一個 普通 文件 ,打開 模式 又是 可寫(即 文件 是 用 O_RDWR 或 O_WRONLY 模式 打開 的) , 就把 文件 的 長度 設置 為 零 , 丟棄 其中的 現(xiàn)有 內(nèi)容.若 文件 是 一個 FIFO 或 終端設備 文件 , O_TRUNC 標志 被 忽略. 其他 O_TRUNC 的 作用 是 不 具體 指定 的 (在 許多 Linux 版本 中 , 通常 會 被 忽略 , 其他 的 一些 版本 將 返回 一個 錯誤)
O_APPEND
文件 以 追加 模式 打開 . 在 以前 , 文件 讀寫 指針 被 置 在 文件 的 末尾 . as if with lseek. O_APPEND may lead to corrupted files on NFS file systems if more than one process appends data to a file at once. This is because NFS does not support appending to a file, so the client kernel has to simulate it, which can't be done without a race condition.
O_NONBLOCKO_NDELAY
打開(open) 文件 可以 以 非塊(non-blocking) 模式 打開 . 此時 文件 并 沒有 打開 , 也 不能 使用 返回 的文件描述符 進行 后續(xù) 操作 , 而是 使 調(diào)用 程序 等待 . 此 模式 是 為了 FIFO (命名管道) 的 處理 , 參考 fifo(4). 這種 模式 對 除了 FIFO 外 沒有 任何 影響 .
O_SYNC
打開 文件 實現(xiàn) I/O 的 同步 . 任何 通過 文件描述符 對 文件 的 write 都會 使 調(diào)用 的 進程 中斷 , 直到 數(shù)據(jù) 被 真正 寫入 硬件 中 . 其他 , 參考 RESTRICTIONS.
O_NOFOLLOW
假如 pathname 是 一個 符號 聯(lián)接 , 則 打開 失敗 . 這是 FreeBSD 的 擴充 , 從 2.1.126 版本 以來 被 引入 到 Linux 中來 . 從 glibc2.0.100 庫 以來 , 頭文件 中 包括 了 這個 參數(shù) 的 定義;
  kernel 2.1.126 以前 將 忽略 它的 使用.
O_DIRECTORY
假如 pathname 不是 目錄 , 打開 就 失敗 . 這個 參數(shù) 是 Linux 特有 的 , 在 kernel 2.1.126 中 加入 , 為了 避免 在 調(diào)用 FIFO 或 磁帶設備 時 的 denial-of-service 問題 , 但是 不應該 在 執(zhí)行 opendir 以外 使用.
O_LARGEFILE
在 32位 系統(tǒng) 中 支持 大 文件系統(tǒng) , 允許 打開 那些 用 31位 都 不能 表示 其 長度 的 大 文件 .

在 文件 打開 后 , 這些 可選 參數(shù) 可以 通過 fcntl 來 改變 .

在 新文件 被 創(chuàng)建 時 , 參數(shù) mode 具體 指明 了 使用 權限 . 他 通常 也 會 被 umask 修改 . 所以 一般 新建 文件 的 權限 為 (mode & ~umask). 注意 模式 只 被 應用 于 將來 對 這 新文件 的 使用 中; open 調(diào)用 創(chuàng)建 一個 新的 只讀 文件 , 但 仍 將 返回 一個 可 讀寫 文件 描述符.

后面 是 一些 mode 的 具體 參數(shù):

S_IRWXU
00700 允許 文件 的 屬主 讀 , 寫 和 執(zhí)行 文件
S_IRUSR (S_IREAD)
00400 允許 文件 的 屬主 讀 文件
S_IWUSR (S_IWRITE)
00200 允許 文件 的 屬主 寫 文件
S_IXUSR (S_IEXEC)
00100 允許 文件 的 屬主 執(zhí)行 文件
S_IRWXG
00070 允許 文件 所在 的 分組 讀 , 寫 和 執(zhí)行 文件
S_IRGRP
00040 允許 文件 所在 的 分組 讀 文件
S_IWGRP
00020 允許 文件 所在 的 分組 寫 文件
S_IXGRP
00010 允許 文件 所在 的 分組 執(zhí)行 文件
S_IRWXO
00007 允許 其他 用戶 讀 , 寫 和 執(zhí)行 文件
S_IROTH
00004 允許 其他 用戶 讀 文件
S_IWOTH
00002 允許 其他 用戶 寫 文件
S_IXOTH
00001 允許 其他 用戶 執(zhí)行 文件

mode 只有 當 在 flags 中 使用 O_CREAT 時 才 有效 , 否則 被 忽略.

creat 相當 于 open 的 參數(shù) flags 等于 O_CREAT|O_WRONLY|O_TRUNC.  

RETURN VALUE 返回值

opencreat 都 返回 一個 新的 文件描述符 (若是 有 錯誤 發(fā)生 返回 -1 ,并在 errno 設置 錯誤 信息). 注意 open 可以 打開 設備 專用 文件 , 但是 creat 不能創(chuàng)建,需要用 mknod(2) 來代替.

On NFS file systems with UID mapping enabled, open may return a file descriptor 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.

若 文件 是 新 建立 的 , 他 的 atime(上次訪問時間), ctime(創(chuàng)建時間), mtime(修改時間) 都 被 修改 為 當前 時間 , 上層 目錄 的atime , ctime 也 被 同樣 修改 . 其他的 , 假如 文件 是 由 O_TRUNC 參數(shù) 修改的 ,它的 ctime , mtime 域 也 被 設置 為 當前 時間.

ERRORS 錯誤信息

EEXIST
參數(shù) O_CREAT and O_EXCL 被使用,但是文件( pathname )已經(jīng)存在.
EISDIR
文件名 ( pathname ) 是 一個 目錄 , 而 又 涉及 到 寫 操作.
EACCES

 訪問 請求 不 允許 (權限不夠) , 在 文件名 ( pathname )中 有 一 目錄 不允許 搜索 (沒有 執(zhí)行權限) , 或者 文件 還 不存在 且 對 上層目錄 的 寫 操作 又 不允許.
ENAMETOOLONG
文件名 ( pathname ) 太 長 了
ENOENT
目錄 ( pathname ) 不存在 或者 是 一個 懸空 的 符號 聯(lián)接.
ENOTDIR
pathname 不是 一個 子目錄
ENXIO
使用 O_NONBLOCK | O_WRONLY, 命名 的 文件 是 FIFO , 所讀 文件 還 沒有 打開 的 文件 , 或者 , 打開 一個 設備 專用 文件 而 相應 的 設備 不存在
ENODEV
文件 ( pathname ) 引用 了 一個 設備 專用 文件 , 而 相應 的 設備 又 不存在. (這是 linux kernel 的 一個bug - ENXIO 一定 會 被 返回 .)
EROFS
文件 ( pathname ) 是 一個 只讀 文件 , 又有 寫 操作 被 請求.
ETXTBSY
文件 ( pathname ) 是 一個 正在 被 執(zhí)行 的 可 執(zhí)行 文件 ,又有 寫 操作 被 請求.
EFAULT
pathname 在一個你不能訪問的地址空間.
ELOOP
在 分解 pathname 時 , 遇到 太多 符號聯(lián)接 或者 指明 O_NOFOLLOW 但是 pathname 是 一個 符號聯(lián)接
ENOSPC
pathname 將要被創(chuàng)建,但是設備又沒有空間儲存 pathname 文件了
ENOMEM
可 獲得 的 核心內(nèi)存(kernel memory) 不夠
EMFILE
程序打開的文件數(shù)已經(jīng)達到最大值了
ENFILE
系統(tǒng)打開的總文件數(shù)已經(jīng)達到了極限

CONFORMING TO

SVr4, SVID, POSIX, X/OPEN, BSD 4.3 The O_NOFOLLOW and O_DIRECTORY flags are Linux-specific. One may have to define the _GNU_SOURCE macro to get their definitions.  

RESTRICTIONS 無限制

There are many infelicities in the protocol underlying NFS, affecting amongst others O_SYNC and O_NDELAY.

POSIX provides for three different variants of synchronised I/O, corresponding to the flags O_SYNC, O_DSYNC and O_RSYNC. Currently (2.1.130) these are all synonymous under Linux.  

SEE ALSO 參見

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)

#p#

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

The open() 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, write, 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.

The parameter flags is one of O_RDONLY, O_WRONLY or O_RDWR which request opening the file read-only, write-only or read/write, respectively, bitwise-or'd with zero or more of the following:

O_CREAT
If the file does not exist it will be created. The owner (user ID) of the file is set to the effective user ID of the process. The group ownership (group ID) is set either to the effective group ID of the process or to the group ID of the parent directory (depending on filesystem type and mount options, and the mode of the parent directory, see, e.g., the mount options bsdgroups and sysvgroups of the ext2 filesystem, as described in mount(8)).
O_EXCL
When used with O_CREAT, if the file already exists it is an error and the open will fail. In this context, a symbolic link exists, regardless of where its points to. O_EXCL is broken on NFS file systems, programs which rely on it for performing locking tasks will contain a race condition. The solution for performing atomic file locking using a lockfile is to create a unique file on the same fs (e.g., incorporating hostname and pid), use link(2) to make a link to the lockfile. If link() returns 0, the lock is successful. Otherwise, use stat(2) on the unique file to check if its link count has increased to 2, in which case the lock is also successful.
O_NOCTTY
If pathname refers to a terminal device --- see tty(4) --- it will not become the process's controlling terminal even if the process does not have one.
O_TRUNC
If the file already exists and is a regular file and the open mode allows writing (i.e., is O_RDWR or O_WRONLY) it will be truncated to length 0. If the file is a FIFO or terminal device file, the O_TRUNC flag is ignored. Otherwise the effect of O_TRUNC is unspecified.
O_APPEND
The file is opened in append mode. Before each write, the file pointer is positioned at the end of the file, as if with lseek. O_APPEND may lead to corrupted files on NFS file systems if more than one process appends data to a file at once. This is because NFS does not support appending to a file, so the client kernel has to simulate it, which can't be done without a race condition.
O_NONBLOCK or O_NDELAY
When possible, the file is opened in non-blocking mode. Neither the open nor any subsequent operations on the file descriptor which is returned will cause the calling process to wait. For the handling of FIFOs (named pipes), see also fifo(4). This mode need not have any effect on files other than FIFOs.
O_SYNC
The file is opened for synchronous I/O. Any writes on the resulting file descriptor will block the calling process until the data has been physically written to the underlying hardware. See RESTRICTIONS below, though.
O_NOFOLLOW
If pathname is a symbolic link, then the open fails. This is a FreeBSD extension, which was added to Linux in version 2.1.126. Symbolic links in earlier components of the pathname will still be followed. The headers from glibc 2.0.100 and later include a definition of this flag; kernels before 2.1.126 will ignore it if used.
O_DIRECTORY
If pathname is not a directory, cause the open to fail. This flag is Linux-specific, and was added in kernel version 2.1.126, to avoid denial-of-service problems if opendir(3) is called on a FIFO or tape device, but should not be used outside of the implementation of opendir.
O_DIRECT
Try to minimize cache effects of the I/O to and from this file. In general this will degrade performance, but it is useful in special situations, such as when applications do their own caching. File I/O is done directly to/from user space buffers. The I/O is synchronous, i.e., at the completion of the read(2) or write(2) system call, data is guaranteed to have been transferred. Under Linux 2.4 transfer sizes, and the alignment of user buffer and file offset must all be multiples of the logical block size of the file system. Under Linux 2.6 alignment to 512-byte boundaries suffices.
A semantically similar interface for block devices is described in raw(8).
O_ASYNC
Generate a signal (SIGIO by default, but this can be changed via fcntl(2)) when input or output becomes possible on this file descriptor. This feature is only available for terminals, pseudo-terminals, and sockets. See fcntl(2) for further details.
O_LARGEFILE
On 32-bit systems that support the Large Files System, allow files whose sizes cannot be represented in 31 bits to be opened.

Some of these optional flags can be altered using fcntl after the file has been opened.

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). Note that this mode only applies to future accesses of the newly created file; the open call that creates a read-only file may well return a read/write file descriptor.

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

mode must be specified when O_CREAT is in the flags, and is ignored otherwise.

creat is equivalent to open with flags equal to O_CREAT|O_WRONLY|O_TRUNC.  

RETURN VALUE

open and creat return the new file descriptor, or -1 if an error occurred (in which case, errno is set appropriately). Note that open can open device special files, but creat cannot create them - use mknod(2) instead.

On NFS file systems with UID mapping enabled, open may return a file descriptor 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.

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.

ERRORS

EEXIST
pathname already exists and O_CREAT and O_EXCL were used.
EISDIR
pathname refers to a directory and the access requested involved writing (that is, O_WRONLY or O_RDWR is set).
EACCES
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.
ENAMETOOLONG
pathname was too long.
ENOENT
O_CREAT is not set and the named file does not exist. Or, a directory component in pathname does not exist or is a dangling symbolic link.
ENOTDIR
A component used as a directory in pathname is not, in fact, a directory, or O_DIRECTORY was specified and pathname was not a directory.
ENXIO
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.
ENODEV
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.)
EROFS
pathname refers to a file on a read-only filesystem and write access was requested.
ETXTBSY
pathname refers to an executable image which is currently being executed and write access was requested.
EFAULT
pathname points outside your accessible address space.
ELOOP
Too many symbolic links were encountered in resolving pathname, or O_NOFOLLOW was specified but pathname was a symbolic link.
ENOSPC
pathname was to be created but the device containing pathname has no room for the new file.
ENOMEM
Insufficient kernel memory was available.
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 been reached.

NOTE

Under Linux, the O_NONBLOCK flag indicates that one wants to open but does not necessarily have the intention to read or write. This is typically used to open devices in order to get a file descriptor for use with ioctl(2).  

CONFORMING TO

SVr4, SVID, POSIX, X/OPEN, BSD 4.3. The O_NOFOLLOW and O_DIRECTORY flags are Linux-specific. One may have to define the _GNU_SOURCE macro to get their definitions.

The (undefined) effect of O_RDONLY | O_TRUNC various among implementations. On many systems the file is actually truncated.

The O_DIRECT flag was introduced in SGI IRIX, where it has alignment restrictions similar to those of Linux 2.4. IRIX has also a fcntl(2) call to query appropriate alignments, and sizes. FreeBSD 4.x introduced a flag of same name, but without alignment restrictions. Support was added under Linux in kernel version 2.4.10. Older Linux kernels simply ignore this flag.  

BUGS

"The thing that has always disturbed me about O_DIRECT is that the whole interface is just stupid, and was probably designed by a deranged monkey on some serious mind-controlling substances." -- Linus  

RESTRICTIONS

There are many infelicities in the protocol underlying NFS, affecting amongst others O_SYNC and O_NDELAY.

POSIX provides for three different variants of synchronised I/O, corresponding to the flags O_SYNC, O_DSYNC and O_RSYNC. Currently (2.1.130) these are all synonymous under Linux.  

SEE ALSO

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)

責任編輯:韓亞珊 來源: CMPP.net
相關推薦

2011-08-25 17:00:46

iconv_open中文man

2011-08-24 16:48:36

man中文man

2011-08-15 10:21:09

man中文man

2011-08-11 16:11:49

at中文man

2011-08-25 10:21:56

man.conf中文man

2011-08-15 15:02:31

usleep中文man

2011-09-23 13:59:40

find中文man

2011-08-12 09:17:57

deallocvt中文man

2011-08-25 17:24:54

puts中文man

2011-07-15 16:58:36

ac中文man

2011-08-11 18:32:21

cp中文man

2011-08-23 11:34:26

ipcs中文man

2011-08-25 10:16:02

lmhosts中文man

2011-08-24 15:29:06

grant中文man

2011-08-25 17:32:40

setbuffer中文man

2011-08-25 16:50:08

getc中文man

2011-08-25 16:52:54

getchar中文man

2011-08-25 17:03:51

pclose中文man

2011-08-25 16:22:29

fputc中文man

2011-08-25 16:28:50

fread中文man
點贊
收藏

51CTO技術棧公眾號