Blame manual/io.texi

Packit Service 82fcde
@node I/O Overview, I/O on Streams, Pattern Matching, Top
Packit Service 82fcde
@c %MENU% Introduction to the I/O facilities
Packit Service 82fcde
@chapter Input/Output Overview
Packit Service 82fcde
Packit Service 82fcde
Most programs need to do either input (reading data) or output (writing
Packit Service 82fcde
data), or most frequently both, in order to do anything useful.  @Theglibc{}
Packit Service 82fcde
provides such a large selection of input and output functions
Packit Service 82fcde
that the hardest part is often deciding which function is most
Packit Service 82fcde
appropriate!
Packit Service 82fcde
Packit Service 82fcde
This chapter introduces concepts and terminology relating to input
Packit Service 82fcde
and output.  Other chapters relating to the GNU I/O facilities are:
Packit Service 82fcde
Packit Service 82fcde
@itemize @bullet
Packit Service 82fcde
@item
Packit Service 82fcde
@ref{I/O on Streams}, which covers the high-level functions
Packit Service 82fcde
that operate on streams, including formatted input and output.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
@ref{Low-Level I/O}, which covers the basic I/O and control
Packit Service 82fcde
functions on file descriptors.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
@ref{File System Interface}, which covers functions for operating on
Packit Service 82fcde
directories and for manipulating file attributes such as access modes
Packit Service 82fcde
and ownership.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
@ref{Pipes and FIFOs}, which includes information on the basic interprocess
Packit Service 82fcde
communication facilities.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
@ref{Sockets}, which covers a more complicated interprocess communication
Packit Service 82fcde
facility with support for networking.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
@ref{Low-Level Terminal Interface}, which covers functions for changing
Packit Service 82fcde
how input and output to terminals or other serial devices are processed.
Packit Service 82fcde
@end itemize
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@menu
Packit Service 82fcde
* I/O Concepts::       Some basic information and terminology.
Packit Service 82fcde
* File Names::         How to refer to a file.
Packit Service 82fcde
@end menu
Packit Service 82fcde
Packit Service 82fcde
@node I/O Concepts, File Names,  , I/O Overview
Packit Service 82fcde
@section Input/Output Concepts
Packit Service 82fcde
Packit Service 82fcde
Before you can read or write the contents of a file, you must establish
Packit Service 82fcde
a connection or communications channel to the file.  This process is
Packit Service 82fcde
called @dfn{opening} the file.  You can open a file for reading, writing,
Packit Service 82fcde
or both.
Packit Service 82fcde
@cindex opening a file
Packit Service 82fcde
Packit Service 82fcde
The connection to an open file is represented either as a stream or as a
Packit Service 82fcde
file descriptor.  You pass this as an argument to the functions that do
Packit Service 82fcde
the actual read or write operations, to tell them which file to operate
Packit Service 82fcde
on.  Certain functions expect streams, and others are designed to
Packit Service 82fcde
operate on file descriptors.
Packit Service 82fcde
Packit Service 82fcde
When you have finished reading to or writing from the file, you can
Packit Service 82fcde
terminate the connection by @dfn{closing} the file.  Once you have
Packit Service 82fcde
closed a stream or file descriptor, you cannot do any more input or
Packit Service 82fcde
output operations on it.
Packit Service 82fcde
Packit Service 82fcde
@menu
Packit Service 82fcde
* Streams and File Descriptors::    The GNU C Library provides two ways
Packit Service 82fcde
			             to access the contents of files.
Packit Service 82fcde
* File Position::                   The number of bytes from the
Packit Service 82fcde
                                     beginning of the file.
Packit Service 82fcde
@end menu
Packit Service 82fcde
Packit Service 82fcde
@node Streams and File Descriptors, File Position,  , I/O Concepts
Packit Service 82fcde
@subsection Streams and File Descriptors
Packit Service 82fcde
Packit Service 82fcde
When you want to do input or output to a file, you have a choice of two
Packit Service 82fcde
basic mechanisms for representing the connection between your program
Packit Service 82fcde
and the file: file descriptors and streams.  File descriptors are
Packit Service 82fcde
represented as objects of type @code{int}, while streams are represented
Packit Service 82fcde
as @code{FILE *} objects.
Packit Service 82fcde
Packit Service 82fcde
File descriptors provide a primitive, low-level interface to input and
Packit Service 82fcde
output operations.  Both file descriptors and streams can represent a
Packit Service 82fcde
connection to a device (such as a terminal), or a pipe or socket for
Packit Service 82fcde
communicating with another process, as well as a normal file.  But, if
Packit Service 82fcde
you want to do control operations that are specific to a particular kind
Packit Service 82fcde
of device, you must use a file descriptor; there are no facilities to
Packit Service 82fcde
use streams in this way.  You must also use file descriptors if your
Packit Service 82fcde
program needs to do input or output in special modes, such as
Packit Service 82fcde
nonblocking (or polled) input (@pxref{File Status Flags}).
Packit Service 82fcde
Packit Service 82fcde
Streams provide a higher-level interface, layered on top of the
Packit Service 82fcde
primitive file descriptor facilities.  The stream interface treats all
Packit Service 82fcde
kinds of files pretty much alike---the sole exception being the three
Packit Service 82fcde
styles of buffering that you can choose (@pxref{Stream Buffering}).
Packit Service 82fcde
Packit Service 82fcde
The main advantage of using the stream interface is that the set of
Packit Service 82fcde
functions for performing actual input and output operations (as opposed
Packit Service 82fcde
to control operations) on streams is much richer and more powerful than
Packit Service 82fcde
the corresponding facilities for file descriptors.  The file descriptor
Packit Service 82fcde
interface provides only simple functions for transferring blocks of
Packit Service 82fcde
characters, but the stream interface also provides powerful formatted
Packit Service 82fcde
input and output functions (@code{printf} and @code{scanf}) as well as
Packit Service 82fcde
functions for character- and line-oriented input and output.
Packit Service 82fcde
@c !!! glibc has dprintf, which lets you do printf on an fd.
Packit Service 82fcde
Packit Service 82fcde
Since streams are implemented in terms of file descriptors, you can
Packit Service 82fcde
extract the file descriptor from a stream and perform low-level
Packit Service 82fcde
operations directly on the file descriptor.  You can also initially open
Packit Service 82fcde
a connection as a file descriptor and then make a stream associated with
Packit Service 82fcde
that file descriptor.
Packit Service 82fcde
Packit Service 82fcde
In general, you should stick with using streams rather than file
Packit Service 82fcde
descriptors, unless there is some specific operation you want to do that
Packit Service 82fcde
can only be done on a file descriptor.  If you are a beginning
Packit Service 82fcde
programmer and aren't sure what functions to use, we suggest that you
Packit Service 82fcde
concentrate on the formatted input functions (@pxref{Formatted Input})
Packit Service 82fcde
and formatted output functions (@pxref{Formatted Output}).
Packit Service 82fcde
Packit Service 82fcde
If you are concerned about portability of your programs to systems other
Packit Service 82fcde
than GNU, you should also be aware that file descriptors are not as
Packit Service 82fcde
portable as streams.  You can expect any system running @w{ISO C} to
Packit Service 82fcde
support streams, but @nongnusystems{} may not support file descriptors at
Packit Service 82fcde
all, or may only implement a subset of the GNU functions that operate on
Packit Service 82fcde
file descriptors.  Most of the file descriptor functions in @theglibc{}
Packit Service 82fcde
are included in the POSIX.1 standard, however.
Packit Service 82fcde
Packit Service 82fcde
@node File Position,  , Streams and File Descriptors, I/O Concepts
Packit Service 82fcde
@subsection File Position
Packit Service 82fcde
Packit Service 82fcde
One of the attributes of an open file is its @dfn{file position} that
Packit Service 82fcde
keeps track of where in the file the next character is to be read or
Packit Service 82fcde
written.  On @gnusystems{}, and all POSIX.1 systems, the file position
Packit Service 82fcde
is simply an integer representing the number of bytes from the beginning
Packit Service 82fcde
of the file.
Packit Service 82fcde
Packit Service 82fcde
The file position is normally set to the beginning of the file when it
Packit Service 82fcde
is opened, and each time a character is read or written, the file
Packit Service 82fcde
position is incremented.  In other words, access to the file is normally
Packit Service 82fcde
@dfn{sequential}.
Packit Service 82fcde
@cindex file position
Packit Service 82fcde
@cindex sequential-access files
Packit Service 82fcde
Packit Service 82fcde
Ordinary files permit read or write operations at any position within
Packit Service 82fcde
the file.  Some other kinds of files may also permit this.  Files which
Packit Service 82fcde
do permit this are sometimes referred to as @dfn{random-access} files.
Packit Service 82fcde
You can change the file position using the @code{fseek} function on a
Packit Service 82fcde
stream (@pxref{File Positioning}) or the @code{lseek} function on a file
Packit Service 82fcde
descriptor (@pxref{I/O Primitives}).  If you try to change the file
Packit Service 82fcde
position on a file that doesn't support random access, you get the
Packit Service 82fcde
@code{ESPIPE} error.
Packit Service 82fcde
@cindex random-access files
Packit Service 82fcde
Packit Service 82fcde
Streams and descriptors that are opened for @dfn{append access} are
Packit Service 82fcde
treated specially for output: output to such files is @emph{always}
Packit Service 82fcde
appended sequentially to the @emph{end} of the file, regardless of the
Packit Service 82fcde
file position.  However, the file position is still used to control where in
Packit Service 82fcde
the file reading is done.
Packit Service 82fcde
@cindex append-access files
Packit Service 82fcde
Packit Service 82fcde
If you think about it, you'll realize that several programs can read a
Packit Service 82fcde
given file at the same time.  In order for each program to be able to
Packit Service 82fcde
read the file at its own pace, each program must have its own file
Packit Service 82fcde
pointer, which is not affected by anything the other programs do.
Packit Service 82fcde
Packit Service 82fcde
In fact, each opening of a file creates a separate file position.
Packit Service 82fcde
Thus, if you open a file twice even in the same program, you get two
Packit Service 82fcde
streams or descriptors with independent file positions.
Packit Service 82fcde
Packit Service 82fcde
By contrast, if you open a descriptor and then duplicate it to get
Packit Service 82fcde
another descriptor, these two descriptors share the same file position:
Packit Service 82fcde
changing the file position of one descriptor will affect the other.
Packit Service 82fcde
Packit Service 82fcde
@node File Names,  , I/O Concepts, I/O Overview
Packit Service 82fcde
@section File Names
Packit Service 82fcde
Packit Service 82fcde
In order to open a connection to a file, or to perform other operations
Packit Service 82fcde
such as deleting a file, you need some way to refer to the file.  Nearly
Packit Service 82fcde
all files have names that are strings---even files which are actually
Packit Service 82fcde
devices such as tape drives or terminals.  These strings are called
Packit Service 82fcde
@dfn{file names}.  You specify the file name to say which file you want
Packit Service 82fcde
to open or operate on.
Packit Service 82fcde
Packit Service 82fcde
This section describes the conventions for file names and how the
Packit Service 82fcde
operating system works with them.
Packit Service 82fcde
@cindex file name
Packit Service 82fcde
Packit Service 82fcde
@menu
Packit Service 82fcde
* Directories::                 Directories contain entries for files.
Packit Service 82fcde
* File Name Resolution::        A file name specifies how to look up a file.
Packit Service 82fcde
* File Name Errors::            Error conditions relating to file names.
Packit Service 82fcde
* File Name Portability::       File name portability and syntax issues.
Packit Service 82fcde
@end menu
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@node Directories, File Name Resolution,  , File Names
Packit Service 82fcde
@subsection Directories
Packit Service 82fcde
Packit Service 82fcde
In order to understand the syntax of file names, you need to understand
Packit Service 82fcde
how the file system is organized into a hierarchy of directories.
Packit Service 82fcde
Packit Service 82fcde
@cindex directory
Packit Service 82fcde
@cindex link
Packit Service 82fcde
@cindex directory entry
Packit Service 82fcde
A @dfn{directory} is a file that contains information to associate other
Packit Service 82fcde
files with names; these associations are called @dfn{links} or
Packit Service 82fcde
@dfn{directory entries}.  Sometimes, people speak of ``files in a
Packit Service 82fcde
directory'', but in reality, a directory only contains pointers to
Packit Service 82fcde
files, not the files themselves.
Packit Service 82fcde
Packit Service 82fcde
@cindex file name component
Packit Service 82fcde
The name of a file contained in a directory entry is called a @dfn{file
Packit Service 82fcde
name component}.  In general, a file name consists of a sequence of one
Packit Service 82fcde
or more such components, separated by the slash character (@samp{/}).  A
Packit Service 82fcde
file name which is just one component names a file with respect to its
Packit Service 82fcde
directory.  A file name with multiple components names a directory, and
Packit Service 82fcde
then a file in that directory, and so on.
Packit Service 82fcde
Packit Service 82fcde
Some other documents, such as the POSIX standard, use the term
Packit Service 82fcde
@dfn{pathname} for what we call a file name, and either @dfn{filename}
Packit Service 82fcde
or @dfn{pathname component} for what this manual calls a file name
Packit Service 82fcde
component.  We don't use this terminology because a ``path'' is
Packit Service 82fcde
something completely different (a list of directories to search), and we
Packit Service 82fcde
think that ``pathname'' used for something else will confuse users.  We
Packit Service 82fcde
always use ``file name'' and ``file name component'' (or sometimes just
Packit Service 82fcde
``component'', where the context is obvious) in GNU documentation.  Some
Packit Service 82fcde
macros use the POSIX terminology in their names, such as
Packit Service 82fcde
@code{PATH_MAX}.  These macros are defined by the POSIX standard, so we
Packit Service 82fcde
cannot change their names.
Packit Service 82fcde
Packit Service 82fcde
You can find more detailed information about operations on directories
Packit Service 82fcde
in @ref{File System Interface}.
Packit Service 82fcde
Packit Service 82fcde
@node File Name Resolution, File Name Errors, Directories, File Names
Packit Service 82fcde
@subsection File Name Resolution
Packit Service 82fcde
Packit Service 82fcde
A file name consists of file name components separated by slash
Packit Service 82fcde
(@samp{/}) characters.  On the systems that @theglibc{} supports,
Packit Service 82fcde
multiple successive @samp{/} characters are equivalent to a single
Packit Service 82fcde
@samp{/} character.
Packit Service 82fcde
Packit Service 82fcde
@cindex file name resolution
Packit Service 82fcde
The process of determining what file a file name refers to is called
Packit Service 82fcde
@dfn{file name resolution}.  This is performed by examining the
Packit Service 82fcde
components that make up a file name in left-to-right order, and locating
Packit Service 82fcde
each successive component in the directory named by the previous
Packit Service 82fcde
component.  Of course, each of the files that are referenced as
Packit Service 82fcde
directories must actually exist, be directories instead of regular
Packit Service 82fcde
files, and have the appropriate permissions to be accessible by the
Packit Service 82fcde
process; otherwise the file name resolution fails.
Packit Service 82fcde
Packit Service 82fcde
@cindex root directory
Packit Service 82fcde
@cindex absolute file name
Packit Service 82fcde
If a file name begins with a @samp{/}, the first component in the file
Packit Service 82fcde
name is located in the @dfn{root directory} of the process (usually all
Packit Service 82fcde
processes on the system have the same root directory).  Such a file name
Packit Service 82fcde
is called an @dfn{absolute file name}.
Packit Service 82fcde
@c !!! xref here to chroot, if we ever document chroot. -rm
Packit Service 82fcde
Packit Service 82fcde
@cindex relative file name
Packit Service 82fcde
Otherwise, the first component in the file name is located in the
Packit Service 82fcde
current working directory (@pxref{Working Directory}).  This kind of
Packit Service 82fcde
file name is called a @dfn{relative file name}.
Packit Service 82fcde
Packit Service 82fcde
@cindex parent directory
Packit Service 82fcde
The file name components @file{.} (``dot'') and @file{..} (``dot-dot'')
Packit Service 82fcde
have special meanings.  Every directory has entries for these file name
Packit Service 82fcde
components.  The file name component @file{.} refers to the directory
Packit Service 82fcde
itself, while the file name component @file{..} refers to its
Packit Service 82fcde
@dfn{parent directory} (the directory that contains the link for the
Packit Service 82fcde
directory in question).  As a special case, @file{..} in the root
Packit Service 82fcde
directory refers to the root directory itself, since it has no parent;
Packit Service 82fcde
thus @file{/..} is the same as @file{/}.
Packit Service 82fcde
Packit Service 82fcde
Here are some examples of file names:
Packit Service 82fcde
Packit Service 82fcde
@table @file
Packit Service 82fcde
@item /a
Packit Service 82fcde
The file named @file{a}, in the root directory.
Packit Service 82fcde
Packit Service 82fcde
@item /a/b
Packit Service 82fcde
The file named @file{b}, in the directory named @file{a} in the root directory.
Packit Service 82fcde
Packit Service 82fcde
@item a
Packit Service 82fcde
The file named @file{a}, in the current working directory.
Packit Service 82fcde
Packit Service 82fcde
@item /a/./b
Packit Service 82fcde
This is the same as @file{/a/b}.
Packit Service 82fcde
Packit Service 82fcde
@item ./a
Packit Service 82fcde
The file named @file{a}, in the current working directory.
Packit Service 82fcde
Packit Service 82fcde
@item ../a
Packit Service 82fcde
The file named @file{a}, in the parent directory of the current working
Packit Service 82fcde
directory.
Packit Service 82fcde
@end table
Packit Service 82fcde
Packit Service 82fcde
@c An empty string may ``work'', but I think it's confusing to
Packit Service 82fcde
@c try to describe it.  It's not a useful thing for users to use--rms.
Packit Service 82fcde
A file name that names a directory may optionally end in a @samp{/}.
Packit Service 82fcde
You can specify a file name of @file{/} to refer to the root directory,
Packit Service 82fcde
but the empty string is not a meaningful file name.  If you want to
Packit Service 82fcde
refer to the current working directory, use a file name of @file{.} or
Packit Service 82fcde
@file{./}.
Packit Service 82fcde
Packit Service 82fcde
Unlike some other operating systems, @gnusystems{} don't have any
Packit Service 82fcde
built-in support for file types (or extensions) or file versions as part
Packit Service 82fcde
of its file name syntax.  Many programs and utilities use conventions
Packit Service 82fcde
for file names---for example, files containing C source code usually
Packit Service 82fcde
have names suffixed with @samp{.c}---but there is nothing in the file
Packit Service 82fcde
system itself that enforces this kind of convention.
Packit Service 82fcde
Packit Service 82fcde
@node File Name Errors, File Name Portability, File Name Resolution, File Names
Packit Service 82fcde
@subsection File Name Errors
Packit Service 82fcde
Packit Service 82fcde
@cindex file name errors
Packit Service 82fcde
@cindex usual file name errors
Packit Service 82fcde
Packit Service 82fcde
Functions that accept file name arguments usually detect these
Packit Service 82fcde
@code{errno} error conditions relating to the file name syntax or
Packit Service 82fcde
trouble finding the named file.  These errors are referred to throughout
Packit Service 82fcde
this manual as the @dfn{usual file name errors}.
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item EACCES
Packit Service 82fcde
The process does not have search permission for a directory component
Packit Service 82fcde
of the file name.
Packit Service 82fcde
Packit Service 82fcde
@item ENAMETOOLONG
Packit Service 82fcde
This error is used when either the total length of a file name is
Packit Service 82fcde
greater than @code{PATH_MAX}, or when an individual file name component
Packit Service 82fcde
has a length greater than @code{NAME_MAX}.  @xref{Limits for Files}.
Packit Service 82fcde
Packit Service 82fcde
On @gnuhurdsystems{}, there is no imposed limit on overall file name
Packit Service 82fcde
length, but some file systems may place limits on the length of a
Packit Service 82fcde
component.
Packit Service 82fcde
Packit Service 82fcde
@item ENOENT
Packit Service 82fcde
This error is reported when a file referenced as a directory component
Packit Service 82fcde
in the file name doesn't exist, or when a component is a symbolic link
Packit Service 82fcde
whose target file does not exist.  @xref{Symbolic Links}.
Packit Service 82fcde
Packit Service 82fcde
@item ENOTDIR
Packit Service 82fcde
A file that is referenced as a directory component in the file name
Packit Service 82fcde
exists, but it isn't a directory.
Packit Service 82fcde
Packit Service 82fcde
@item ELOOP
Packit Service 82fcde
Too many symbolic links were resolved while trying to look up the file
Packit Service 82fcde
name.  The system has an arbitrary limit on the number of symbolic links
Packit Service 82fcde
that may be resolved in looking up a single file name, as a primitive
Packit Service 82fcde
way to detect loops.  @xref{Symbolic Links}.
Packit Service 82fcde
@end table
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@node File Name Portability,  , File Name Errors, File Names
Packit Service 82fcde
@subsection Portability of File Names
Packit Service 82fcde
Packit Service 82fcde
The rules for the syntax of file names discussed in @ref{File Names},
Packit Service 82fcde
are the rules normally used by @gnusystems{} and by other POSIX
Packit Service 82fcde
systems.  However, other operating systems may use other conventions.
Packit Service 82fcde
Packit Service 82fcde
There are two reasons why it can be important for you to be aware of
Packit Service 82fcde
file name portability issues:
Packit Service 82fcde
Packit Service 82fcde
@itemize @bullet
Packit Service 82fcde
@item
Packit Service 82fcde
If your program makes assumptions about file name syntax, or contains
Packit Service 82fcde
embedded literal file name strings, it is more difficult to get it to
Packit Service 82fcde
run under other operating systems that use different syntax conventions.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
Even if you are not concerned about running your program on machines
Packit Service 82fcde
that run other operating systems, it may still be possible to access
Packit Service 82fcde
files that use different naming conventions.  For example, you may be
Packit Service 82fcde
able to access file systems on another computer running a different
Packit Service 82fcde
operating system over a network, or read and write disks in formats used
Packit Service 82fcde
by other operating systems.
Packit Service 82fcde
@end itemize
Packit Service 82fcde
Packit Service 82fcde
The @w{ISO C} standard says very little about file name syntax, only that
Packit Service 82fcde
file names are strings.  In addition to varying restrictions on the
Packit Service 82fcde
length of file names and what characters can validly appear in a file
Packit Service 82fcde
name, different operating systems use different conventions and syntax
Packit Service 82fcde
for concepts such as structured directories and file types or
Packit Service 82fcde
extensions.  Some concepts such as file versions might be supported in
Packit Service 82fcde
some operating systems and not by others.
Packit Service 82fcde
Packit Service 82fcde
The POSIX.1 standard allows implementations to put additional
Packit Service 82fcde
restrictions on file name syntax, concerning what characters are
Packit Service 82fcde
permitted in file names and on the length of file name and file name
Packit Service 82fcde
component strings.  However, on @gnusystems{}, any character except
Packit Service 82fcde
the null character is permitted in a file name string, and
Packit Service 82fcde
on @gnuhurdsystems{} there are no limits on the length of file name
Packit Service 82fcde
strings.