📄 llio.texi
字号:
Here is an example showing how to use @code{dup2} to do redirection.Typically, redirection of the standard streams (like @code{stdin}) isdone by a shell or shell-like program before calling one of the@code{exec} functions (@pxref{Executing a File}) to execute a newprogram in a child process. When the new program is executed, itcreates and initializes the standard streams to point to thecorresponding file descriptors, before its @code{main} function isinvoked.So, to redirect standard input to a file, the shell could do somethinglike:@smallexamplepid = fork ();if (pid == 0) @{ char *filename; char *program; int file; @dots{} file = TEMP_FAILURE_RETRY (open (filename, O_RDONLY)); dup2 (file, STDIN_FILENO); TEMP_FAILURE_RETRY (close (file)); execv (program, NULL); @}@end smallexampleThere is also a more detailed example showing how to implement redirectionin the context of a pipeline of processes in @ref{Launching Jobs}.@node Descriptor Flags@section File Descriptor Flags@cindex file descriptor flags@dfn{File descriptor flags} are miscellaneous attributes of a filedescriptor. These flags are associated with particular filedescriptors, so that if you have created duplicate file descriptorsfrom a single opening of a file, each descriptor has its own set of flags.Currently there is just one file descriptor flag: @code{FD_CLOEXEC},which causes the descriptor to be closed if you use any of the@code{exec@dots{}} functions (@pxref{Executing a File}).The symbols in this section are defined in the header file@file{fcntl.h}.@pindex fcntl.h@comment fcntl.h@comment POSIX.1@deftypevr Macro int F_GETFDThis macro is used as the @var{command} argument to @code{fcntl}, tospecify that it should return the file descriptor flags associatedwith the @var{filedes} argument. The normal return value from @code{fcntl} with this command is anonnegative number which can be interpreted as the bitwise OR of theindividual flags (except that currently there is only one flag to use).In case of an error, @code{fcntl} returns @code{-1}. The following@code{errno} error conditions are defined for this command:@table @code@item EBADFThe @var{filedes} argument is invalid.@end table@end deftypevr@comment fcntl.h@comment POSIX.1@deftypevr Macro int F_SETFDThis macro is used as the @var{command} argument to @code{fcntl}, tospecify that it should set the file descriptor flags associated with the@var{filedes} argument. This requires a third @code{int} argument tospecify the new flags, so the form of the call is:@smallexamplefcntl (@var{filedes}, F_SETFD, @var{new-flags})@end smallexampleThe normal return value from @code{fcntl} with this command is anunspecified value other than @code{-1}, which indicates an error.The flags and error conditions are the same as for the @code{F_GETFD}command.@end deftypevrThe following macro is defined for use as a file descriptor flag withthe @code{fcntl} function. The value is an integer constant usableas a bit mask value.@comment fcntl.h@comment POSIX.1@deftypevr Macro int FD_CLOEXEC@cindex close-on-exec (file descriptor flag)This flag specifies that the file descriptor should be closed whenan @code{exec} function is invoked; see @ref{Executing a File}. Whena file descriptor is allocated (as with @code{open} or @code{dup}),this bit is initially cleared on the new file descriptor, meaning thatdescriptor will survive into the new program after @code{exec}.@end deftypevrIf you want to modify the file descriptor flags, you should get thecurrent flags with @code{F_GETFD} and modify the value. Don't assumethat the flags listed here are the only ones that are implemented; yourprogram may be run years from now and more flags may exist then. Forexample, here is a function to set or clear the flag @code{FD_CLOEXEC}without altering any other flags:@smallexample/* @r{Set the @code{FD_CLOEXEC} flag of @var{desc} if @var{value} is nonzero,} @r{or clear the flag if @var{value} is 0.} @r{Return 0 on success, or -1 on error with @code{errno} set.} */ intset_cloexec_flag (int desc, int value)@{ int oldflags = fcntl (desc, F_GETFD, 0); /* @r{If reading the flags failed, return error indication now.} if (oldflags < 0) return oldflags; /* @r{Set just the flag we want to set.} */ if (value != 0) oldflags |= FD_CLOEXEC; else oldflags &= ~FD_CLOEXEC; /* @r{Store modified flag word in the descriptor.} */ return fcntl (desc, F_SETFD, oldflags);@}@end smallexample@node File Status Flags@section File Status Flags@cindex file status flags@dfn{File status flags} are used to specify attributes of the opening of afile. Unlike the file descriptor flags discussed in @ref{DescriptorFlags}, the file status flags are shared by duplicated file descriptorsresulting from a single opening of the file. The file status flags arespecified with the @var{flags} argument to @code{open};@pxref{Opening and Closing Files}.File status flags fall into three categories, which are described in thefollowing sections.@itemize @bullet@item@ref{Access Modes}, specify what type of access is allowed to thefile: reading, writing, or both. They are set by @code{open} and arereturned by @code{fcntl}, but cannot be changed.@item@ref{Open-time Flags}, control details of what @code{open} will do.These flags are not preserved after the @code{open} call.@item@ref{Operating Modes}, affect how operations such as @code{read} and@code{write} are done. They are set by @code{open}, and can be fetched orchanged with @code{fcntl}.@end itemizeThe symbols in this section are defined in the header file@file{fcntl.h}.@pindex fcntl.h@menu* Access Modes:: Whether the descriptor can read or write.* Open-time Flags:: Details of @code{open}.* Operating Modes:: Special modes to control I/O operations.* Getting File Status Flags:: Fetching and changing these flags.@end menu@node Access Modes@subsection File Access ModesThe file access modes allow a file descriptor to be used for reading,writing, or both. (In the GNU system, they can also allow none of these,and allow execution of the file as a program.) The access modes are chosenwhen the file is opened, and never change.@comment fcntl.h@comment POSIX.1@deftypevr Macro int O_RDONLYOpen the file for read access.@end deftypevr@comment fcntl.h@comment POSIX.1@deftypevr Macro int O_WRONLYOpen the file for write access.@end deftypevr@comment fcntl.h@comment POSIX.1@deftypevr Macro int O_RDWROpen the file for both reading and writing.@end deftypevrIn the GNU system (and not in other systems), @code{O_RDONLY} and@code{O_WRONLY} are independent bits that can be bitwise-ORed together,and it is valid for either bit to be set or clear. This means that@code{O_RDWR} is the same as @code{O_RDONLY|O_WRONLY}. A file accessmode of zero is permissible; it allows no operations that do input oroutput to the file, but does allow other operations such as@code{fchmod}. On the GNU system, since ``read-only'' or ``write-only''is a misnomer, @file{fcntl.h} defines additional names for the fileaccess modes. These names are preferred when writing GNU-specific code.But most programs will want to be portable to other POSIX.1 systems andshould use the POSIX.1 names above instead.@comment fcntl.h@comment GNU@deftypevr Macro int O_READOpen the file for reading. Same as @code{O_RDWR}; only defined on GNU.@end deftypevr@comment fcntl.h@comment GNU@deftypevr Macro int O_WRITEOpen the file for reading. Same as @code{O_WRONLY}; only defined on GNU.@end deftypevr@comment fcntl.h@comment GNU@deftypevr Macro int O_EXECOpen the file for executing. Only defined on GNU.@end deftypevrTo determine the file access mode with @code{fcntl}, you must extractthe access mode bits from the retrieved file status flags. In the GNUsystem, you can just test the @code{O_READ} and @code{O_WRITE} bits inthe flags word. But in other POSIX.1 systems, reading and writingaccess modes are not stored as distinct bit flags. The portable way toextract the file access mode bits is with @code{O_ACCMODE}.@comment fcntl.h@comment POSIX.1@deftypevr Macro int O_ACCMODEThis macro stands for a mask that can be bitwise-ANDed with the filestatus flag value to produce a value representing the file access mode.The mode will be @code{O_RDONLY}, @code{O_WRONLY}, or @code{O_RDWR}.(In the GNU system it could also be zero.)@end deftypevr@node Open-time Flags@subsection Open-time FlagsThe open-time flags specify options affecting how @code{open} will behave.These options are not preserved once the file is open. The exception tothis is @code{O_NONBLOCK}, which is also an I/O operating mode and so it@emph{is} saved. @xref{Opening and Closing Files}, for how to call@code{open}.There are two sorts of options specified by open-time flags.@itemize @bullet@item@dfn{File name translation flags} affect how @code{open} looks up thefile name to locate the file, and whether the file can be created.@cindex file name translation flags@cindex flags, file name translation@item@dfn{Open-time action flags} specify extra operations that @code{open} willperform on the file once it is open.@cindex open-time action flags@cindex flags, open-time action@end itemizeHere are the file name translation flags.@comment fcntl.h@comment POSIX.1@deftypevr Macro int O_CREATIf set, the file will be created if it doesn't already exist.@cindex create on open (file status flag)@end deftypevr@comment fcntl.h@comment POSIX.1@deftypevr Macro int O_EXCLIf both @code{O_CREAT} and @code{O_EXCL} are set, then @code{open} failsif the specified file already exists. This is guaranteed to neverclobber an existing file.@end deftypevr@comment fcntl.h@comment POSIX.1@deftypevr Macro int O_NONBLOCK@cindex non-blocking openThis prevents @code{open} from blocking for a ``long time'' to open thefile. This is only meaningful for some kinds of files, usually devicessuch as serial ports. Often opening a port to a modem blocks until themodem reports carrier detection; if @code{O_NONBLOCK} is specified,@code{open} will return immediately without a carrier.Note that the @code{O_NONBLOCK} flag is overloaded as both an I/O operatingmode and a file name translation flag. This means that specifying@code{O_NONBLOCK} in @code{open} also sets nonblocking I/O mode;@pxref{Operating Modes}. To open the file without blocking but do normalI/O that blocks, you must call @code{open} with @code{O_NONBLOCK} set andthen call @code{fcntl} to turn the bit off.@end deftypevr@comment fcntl.h@comment POSIX.1@deftypevr Macro int O_NOCTTYIf the named file is a terminal device, don't make it the controllingterminal for the process. @xref{Job Control}, for information aboutwhat it means to be the controlling terminal. In the GNU system and 4.4BSD, opening a file never makes it the controlling terminal and@code{O_NOCTTY} is zero.@cindex controlling terminal, setting@end deftypevrThese three file name translation flags exist only in the GNU system.@comment fcntl.h@comment GNU@deftypevr Macro int O_IGNORE_CTTYDo not recognize the named file as the controlling terminal, even if itrefers to the process's existing controlling terminal device. Operationson the new file descriptor will never induce job control signals.@xref{Job Control}.@end deftypevr@comment fcntl.h@comment GNU@deftypevr Macro int O_NOLINKIf the named file is a symbolic link, open the link itself instead ofthe file it refers to. (@code{fstat} on the new file descriptor willreturn the information returned by @code{lstat} on the link's name.)@cindex symbolic link, opening@end deftypevr@comment fcntl.h@comment GNU@deftypevr Macro int O_NOTRANSIf the named file is specially translated, do not invoke the translator.Open the bare file the translator itself sees.@end deftypevrThe open-time action flags tell @code{open} to do additional operationswhich are not really related to opening the file. The reason to do themas part of @code{open} instead of in separate calls is that @code{open}can do them @i{atomically}.@comment fcntl.h@comment POSIX.1@deftypevr Macro int O_TRUNC
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -