@shorttitlepage The GNU C Library Reference Manual The GNU C Library
Reference Manual
Sandra Loosemore with Roland McGrath, Andrew Oram, and Richard M. Stallman
last updated 9 April 1993
for version 1.06 Beta Copyright (C) 1993 Free Software Foundation, Inc.
The C language provides no built-in facilities for performing such common operations as input/output, memory management, string manipulation, and the like. Instead, these facilities are defined in a standard library, which you compile and link with your programs.
The GNU C library, described in this document, defines all of the library functions that are specified by the ANSI C standard, as well as additional features specific to POSIX and other derivatives of the Unix operating system, and extensions specific to the GNU system.
The purpose of this manual is to tell you how to use the facilities of the GNU library. We have mentioned which features belong to which standards to help you identify things that are potentially nonportable to other systems. But the emphasis on this manual is not on strict portability.
This manual is written with the assumption that you are at least somewhat familiar with the C programming language and basic programming concepts. Specifically, familiarity with ANSI standard C (see section ANSI C), rather than "traditional" pre-ANSI C dialects, is assumed.
The GNU C library includes several header files, each of which provides definitions and declarations for a group of related facilities; this information is used by the C compiler when processing your program. For example, the header file `stdio.h' declares facilities for performing input and output, and the header file `string.h' declares string processing utilities. The organization of this manual generally follows the same division as the header files.
If you are reading this manual for the first time, you should read all of the introductory material and skim the remaining chapters. There are a lot of functions in the GNU C library and it's not realistic to expect that you will be able to remember exactly how to use each and every one of them. It's more important to become generally familiar with the kinds of facilities that the library provides, so that when you are writing your programs you can recognize when to make use of library functions, and where in this manual you can find more specific information about them.
This section discusses the various standards and other sources that the GNU C library is based upon. These sources include the ANSI C and POSIX standards, and the System V and Berkeley Unix implementations.
The primary focus of this manual is to tell you how to make effective use of the GNU library facilities. But if you are concerned about making your programs compatible with these standards, or portable to operating systems other than GNU, this can affect how you use the library. This section gives you an overview of these standards, so that you will know what they are when they are mentioned in other parts of the manual.
See section Summary of Library Facilities, for an alphabetical list of the functions and other symbols provided by the library. This list also states which standards each function or symbol comes from.
The GNU C library is compatible with the C standard adopted by the American National Standards Institute (ANSI): American National Standard X3.159-1989---"ANSI C". The header files and library facilities that make up the GNU library are a superset of those specified by the ANSI C standard.
If you are concerned about strict adherence to the ANSI C standard, you should use the `-ansi' option when you compile your programs with the GNU C compiler. This tells the compiler to define only ANSI standard features from the library header files, unless you explicitly ask for additional features. See section Feature Test Macros, for information on how to do this.
Being able to restrict the library to include only ANSI C features is important because ANSI C puts limitations on what names can be defined by the library implementation, and the GNU extensions don't fit these limitations. See section Reserved Names, for more information about these restrictions.
This manual does not attempt to give you complete details on the differences between ANSI C and older dialects. It gives advice on how to write programs to work portably under multiple C dialects, but does not aim for completeness.
The GNU library is also compatible with the IEEE POSIX family of standards, known more formally as the Portable Operating System Interface for Computer Environments. POSIX is derived mostly from various versions of the Unix operating system.
The library facilities specified by the POSIX standard are a superset of those required by ANSI C; POSIX specifies additional features for ANSI C functions, as well as specifying new additional functions. In general, the additional requirements and functionality defined by the POSIX standard are aimed at providing lower-level support for a particular kind of operating system environment, rather than general programming language support which can run in many diverse operating system environments.
The GNU C library implements all of the functions specified in IEEE Std 1003.1-1988, the POSIX System Application Program Interface, commonly referred to as POSIX.1. The primary extensions to the ANSI C facilities specified by this standard include file system interface primitives (see section File System Interface), device-specific terminal control functions (see section Low-Level Terminal Interface), and process control functions (see section Child Processes).
Some facilities from draft 11 of IEEE Std 1003.2, the POSIX Shell and Utilities standard (POSIX.2) are also implemented in the GNU library. These include utilities for dealing with regular expressions and other pattern matching facilities (see section Pattern Matching).
The GNU C library defines facilities from some other versions of Unix, specifically from the 4.2 BSD and 4.3 BSD Unix systems (also known as Berkeley Unix) and from SunOS (a popular 4.2 BSD derivative that includes some Unix System V functionality).
The BSD facilities include symbolic links (see section Symbolic Links), the
select function (see section Waiting for Input or Output), the BSD signal
functions (see section BSD Signal Handling), and sockets (see section Sockets).
The System V Interface Description (SVID) is a document describing the AT&T Unix System V operating system. It is to some extent a superset of the POSIX standard (see section POSIX (The Portable Operating System Interface)).
The GNU C library defines some of the facilities required by the SVID that are not also required by the ANSI or POSIX standards, for compatibility with System V Unix and other Unix systems (such as SunOS) which include these facilities. However, many of the more obscure and less generally useful facilities required by the SVID are not included. (In fact, Unix System V itself does not provide them all.)
Incomplete: Are there any particular System V facilities that ought to be mentioned specifically here?
This section describes some of the practical issues involved in using the GNU C library.
Libraries for use by C programs really consist of two parts: header files that define types and macros and declare variables and functions; and the actual library or archive that contains the definitions of the variables and functions.
(Recall that in C, a declaration merely provides information that a function or variable exists and gives its type. For a function declaration, information about the types of its arguments might be provided as well. The purpose of declarations is to allow the compiler to correctly process references to the declared variables and functions. A definition, on the other hand, actually allocates storage for a variable or says what a function does.)
In order to use the facilities in the GNU C library, you should be sure that your program source files include the appropriate header files. This is so that the compiler has declarations of these facilities available and can correctly process references to them. Once your program has been compiled, the linker resolves these references to the actual definitions provided in the archive file.
Header files are included into a program source file by the `#include' preprocessor directive. The C language supports two forms of this directive; the first,
#include "header"
is typically used to include a header file header that you write yourself; this would contain definitions and declarations describing the interfaces between the different parts of your particular application. By contrast,
#include <file.h>
is typically used to include a header file `file.h' that contains definitions and declarations for a standard library. This file would normally be installed in a standard place by your system administrator. You should use this second form for the C library header files.
Typically, `#include' directives are placed at the top of the C source file, before any other code. If you begin your source files with some comments explaining what the code in the file does (a good idea), put the `#include' directives immediately afterwards, following the feature test macro definition (see section Feature Test Macros).
For more information about the use of header files and `#include' directives, see section 'Header Files' in The GNU C Preprocessor Manual.
The GNU C library provides several header files, each of which contains the type and macro definitions and variable and function declarations for a group of related facilities. This means that your programs may need to include several header files, depending on exactly which facilities you are using.
Some library header files include other library header files automatically. However, as a matter of programming style, you should not rely on this; it is better to explicitly include all the header files required for the library facilities you are using. The GNU C library header files have been written in such a way that it doesn't matter if a header file is accidentally included more than once; including a header file a second time has no effect. Likewise, if your program needs to include multiple header files, the order in which they are included doesn't matter.
Compatibility Note: Inclusion of standard header files in any order and any number of times works in any ANSI C implementation. However, this has traditionally not been the case in many older C implementations.
Strictly speaking, you don't have to include a header file to use a function it declares; you could declare the function explicitly yourself, according to the specifications in this manual. But it is usually better to include the header file because it may define types and macros that are not otherwise available and because it may define more efficient macro replacements for some functions. It is also a sure way to have the correct declaration.
If we describe something as a function in this manual, it may have a macro definition as well. This normally has no effect on how your program runs--the macro definition does the same thing as the function would. In particular, macro equivalents for library functions evaluate arguments exactly once, in the same way that a function call would. The main reason for these macro definitions is that sometimes they can produce an inline expansion that is considerably faster than an actual function call.
Taking the address of a library function works even if it is also defined as a macro. This is because, in this context, the name of the function isn't followed by the left parenthesis that is syntactically necessary to recognize the a macro call.
You might occasionally want to avoid using the a macro definition of a function--perhaps to make your program easier to debug. There are two ways you can do this:
For example, suppose the header file `stdlib.h' declares a function
named abs with
extern int abs (int);
and also provides a macro definition for abs. Then, in:
#include <stdlib.h>
int f (int *i) { return (abs (++*i)); }
the reference to abs might refer to either a macro or a function.
On the other hand, in each of the following examples the reference is
to a function and not a macro.
#include <stdlib.h>
int g (int *i) { return ((abs)(++*i)); }
#undef abs
int h (int *i) { return (abs (++*i)); }
Since macro definitions that double for a function behave in exactly the same way as the actual function version, there is usually no need for any of these methods. In fact, removing macro definitions usually just makes your program slower.
The names of all library types, macros, variables and functions that come from the ANSI C standard are reserved unconditionally; your program may not redefine these names. All other library names are reserved if your programs explicitly includes the header file that defines or declares them. There are several reasons for these restrictions:
exit to do something completely different from
what the standard exit function does, for example. Preventing
this situation helps to make your programs easier to understand and
contributes to modularity and maintainability.
In addition to the names documented in this manual, reserved names include all external identifiers (global functions and variables) that begin with an underscore (`_') and all identifiers regardless of use that begin with either two underscores or an underscore followed by a capital letter are reserved names. This is so that the library and header files can define functions, variables, and macros for internal purposes without risk of conflict with names in user programs.
Some additional classes of identifier names are reserved for future extensions to the C language. While using these names for your own purposes right now might not cause a problem, they do raise the possibility of conflict with future versions of the C standard, so you should avoid these names.
float or long double arguments,
respectively.
In addition, some individual header files reserve names beyond those that they actually define. You only need to worry about these restrictions if your program includes that particular header file.
The exact set of features available when you compile a source file is controlled by which feature test macros you define.
If you compile your programs using `gcc -ansi', you get only the ANSI C library features, unless you explicitly request additional features by defining one or more of the feature macros. See section 'Options' in The GNU CC Manual, for more information about GCC options.
You should define these macros by using `#define' preprocessor directives at the top of your source code files. You could also use the `-D' option to GCC, but it's better if you make the source files indicate their own meaning in a self-contained way.
If you define this macro, then the functionality from the POSIX.1 standard (IEEE Standard 1003.1) is available, as well as all of the ANSI C facilities.
If you define this macro with a value of 1, then the
functionality from the POSIX.1 standard (IEEE Standard 1003.1) is made
available. If you define this macro with a value of 2, then both
the functionality from the POSIX.1 standard and the functionality from
the POSIX.2 standard (IEEE Standard 1003.2) are made available. This is
in addition to the ANSI C facilities.
If you define this macro, functionality derived from 4.3 BSD Unix is included as well as the ANSI C, POSIX.1, and POSIX.2 material.
Some of the features derived from 4.3 BSD Unix conflict with the corresponding features specified by the POSIX.1 standard. If this macro is defined, the 4.3 BSD definitions take precedence over the POSIX definitions.
If you define this macro, functionality derived from SVID is included as well as the ANSI C, POSIX.1, and POSIX.2 material.
If you define this macro, everything is included: ANSI C, POSIX.1, POSIX.2, BSD, SVID, and GNU extensions. In the cases where POSIX.1 conflicts with BSD, the POSIX definitions take precedence.
If you want to get the full effect of _GNU_SOURCE but make the
BSD definitions take precedence over the POSIX definitions, use this
sequence of definitions:
#define _GNU_SOURCE #define _BSD_SOURCE #define _SVID_SOURCE
We recommend you use _GNU_SOURCE in new programs.
If you don't specify the `-ansi' option to GCC and don't define
any of these macros explicitly, the effect as the same as defining
_GNU_SOURCE.
When you define a feature test macro to request a larger class of
features, it is harmless to define in addition a feature test macro for
a subset of those features. For example, if you define
_POSIX_C_SOURCE, then defining _POSIX_SOURCE as well has
no effect. Likewise, if you define _GNU_SOURCE, then defining
either _POSIX_SOURCE or _POSIX_C_SOURCE or
_SVID_SOURCE as well has no effect.
Note, however, that the features of _BSD_SOURCE are not a subset
of any of the other feature test macros supported. This is because it
defines BSD features that take precedence over the POSIX features that
are requested by the other macros. For this reason, defining
_BSD_SOURCE in addition to the other feature test macros does
have an effect: it causes the BSD features to take priority over the
conflicting POSIX features.
Here is an overview of the contents of the remaining chapters of this manual.
sizeof operator and the symbolic constant NULL, and how to
write functions accepting variable numbers of arguments.
isspace) and functions for
performing case conversion.
char data type.
FILE * objects). These are the normal C library functions
from `stdio.h'.
setjmp and
longjmp functions.
If you already know the name of the facility you are interested in, you can look it up in section Summary of Library Facilities. This gives you a summary of its syntax and a pointer to where you can find a more detailed description. This appendix is particularly useful if you just want to verify the order and type of arguments to a function, for example.
Many functions in the GNU C library detect and report error conditions, and sometimes your programs need to check for these error conditions. For example, when you open an input file, you should verify that the file was actually opened correctly, and print an error message or take other appropriate action if the call to the library function failed.
This chapter describes how the error reporting facility works. Your program should include the header file `errno.h' to use this facility.
Most library functions return a special value to indicate that they have
failed. The special value is typically -1, a null pointer, or a
constant such as EOF that is defined for that purpose. But this
return value tells you only that an error has occurred. To find out
what kind of error it was, you need to look at the error code stored in the
variable errno. This variable is declared in the header file
`errno.h'.
The variable errno contains the system error number. You can
change the value of errno.
Since errno is declared volatile, it might be changed
asynchronously by a signal handler; see section Defining Signal Handlers.
However, a properly written signal handler saves and restores the value
of errno, so you generally do not need to worry about this
possibility except when writing signal handlers.
The initial value of errno at program startup is zero. Many
library functions are guaranteed to set it to certain nonzero values
when they encounter certain kinds of errors. These error conditions are
listed for each function. These functions do not change errno
when they succeed; thus, the value of errno after a successful
call is not necessarily zero, and you should not use errno to
determine whether a call failed. The proper way to do that is
documented for each function. If the call the failed, you can
examine errno.
Many library functions can set errno to a nonzero value as a
result of calling other library functions which might fail. You should
assume that any library function might alter errno.
Portability Note: ANSI C specifies errno as a
"modifiable lvalue" rather than as a variable, permitting it to be
implemented as a macro. For example, its expansion might involve a
function call, like *_errno (). In fact, that is what it is
on the GNU system itself. The GNU library, on non-GNU systems, does
whatever is right for the particular system.
There are a few library functions, like sqrt and atan,
that return a perfectly legitimate value in case of an error, but also
set errno. For these functions, if you want to check to see
whether an error occurred, the recommended method is to set errno
to zero before calling the function, and then check its value afterward.
All the error codes have symbolic names; they are macros defined in `errno.h'. The names start with `E' and an upper-case letter or digit; you should consider names of this form to be reserved names. See section Reserved Names.
The error code values are all positive integers and are all distinct.
(Since the values are distinct, you can use them as labels in a
switch statement, for example.) Your program should not make any
other assumptions about the specific values of these symbolic constants.
The value of errno doesn't necessarily have to correspond to any
of these macros, since some library functions might return other error
codes of their own for other situations. The only values that are
guaranteed to be meaningful for a particular library function are the
ones that this manual lists for that function.
On non-GNU systems, almost any system call can return EFAULT if
it is given an invalid pointer as an argument. Since this could only
happen as a result of a bug in your program, and since it will not
happen on the GNU system, we have saved space by not mentioning
EFAULT in the descriptions of individual functions.
The error code macros are defined in the header file `errno.h'. All of them expand into integer constant values. Some of these error codes can't occur on the GNU system, but they can occur using the GNU library on other systems.
Operation not permitted; only the owner of the file (or other resource) or processes with special privileges can perform the operation.
No such file or directory. This is a "file doesn't exist" error for ordinary files that are referenced in contexts where they are expected to already exist.
No process matches the specified process ID.
Interrupted function call; an asynchronous signal occured and prevented completion of the call. When this happens, you should try the call again.
You can choose to have functions resume after a signal that is handled,
rather than failing with EINTR; see section Primitives Interrupted by Signals.
Input/output error; usually used for physical read or write errors.
No such device or address. Typically, this means that a file representing a device has been installed incorrectly, and the system can't find the right kind of device driver for it.
Argument list too long; used when the arguments passed to a new program
being executed with one of the exec functions (see section Executing a File) occupy too much memory space. This condition never arises in the
GNU system.
Invalid executable file format. This condition is detected by the
exec functions; see section Executing a File.
Bad file descriptor; for example, I/O on a descriptor that has been closed or reading from a descriptor open only for writing (or vice versa).
There are no child processes. This error happens on operations that are supposed to manipulate child processes, when there aren't any processes to manipulate.
Deadlock avoided; allocating a system resource would have resulted in a deadlock situation. For an example, See section File Locks.
No memory available. The system cannot allocate more virtual memory because its capacity is full.
Permission denied; the file permissions do not allow the attempted operation.
Bad address; an invalid pointer was detected.
A file that isn't a block special file was given in a situation that requires one. For example, trying to mount an ordinary file as a file system in Unix gives this error.
Resource busy; a system resource that can't be shared is already in use. For example, if you try to delete a file that is the root of a currently mounted filesystem, you get this error.
File exists; an existing file was specified in a context where it only makes sense to specify a new file.
An attempt to make an improper link across file systems was detected.
The wrong type of device was given to a function that expects a particular sort of device.
A file that isn't a directory was specified when a directory is required.
File is a directory; attempting to open a directory for writing gives this error.
Invalid argument. This is used to indicate various kinds of problems with passing the wrong argument to a library function.
There are too many distinct file openings in the entire system. Note that any number of linked channels count as just one file opening; see section Linked Channels.
The current process has too many files open and can't open any more. Duplicate descriptors do count toward this limit.
Inappropriate I/O control operation, such as trying to set terminal modes on an ordinary file.
An attempt to execute a file that is currently open for writing, or write to a file that is currently being executed. (The name stands for "text file busy".) This is not an error in the GNU system; the text is copied as necessary.
File too big; the size of a file would be larger than allowed by the system.
No space left on device; write operation on a file failed because the disk is full.
Invalid seek operation (such as on a pipe).
An attempt was made to modify a file on a read-only file system.
Too many links; the link count of a single file is too large.
Broken pipe; there is no process reading from the other end of a pipe.
Every library function that returns this error code also generates a
SIGPIPE signal; this signal terminates the program if not handled
or blocked. Thus, your program will never actually see EPIPE
unless it has handled or blocked SIGPIPE.
Domain error; used by mathematical functions when an argument value does not fall into the domain over which the function is defined.
Range error; used by mathematical functions when the result value is not representable because of overflow or underflow.
Resource temporarily unavailable; the call might work if you try again
later. Only fork returns error code EAGAIN for such a
reason.
An operation that would block was attempted on an object that has non-blocking mode selected.
Portability Note: In 4.4BSD and GNU, EWOULDBLOCK and
EAGAIN are the same. Earlier versions of BSD (see section Berkeley Unix) have two distinct codes, and use EWOULDBLOCK to indicate
an I/O operation that would block on an object with non-blocking mode
set, and EAGAIN for other kinds of errors.
An operation that cannot complete immediately was initiated on an object that has non-blocking mode selected.
An operation is already in progress on an object that has non-blocking mode selected.
A file that isn't a socket was specified when a socket is required.
No destination address was supplied on a socket operation.
The size of a message sent on a socket was larger than the supported maximum size.
The socket type does not support the requested communications protocol.
You specified a socket option that doesn't make sense for the particular protocol being used by the socket. See section Socket Options.
The socket domain does not support the requested communications protocol. See section Creating a Socket.
The socket type is not supported.
The operation you requested is not supported. Some socket functions don't make sense for all types of sockets, and others may not be implemented for all communications protocols.
The socket communications protocol family you requested is not supported.
The address family specified for a socket is not supported; it is inconsistent with the protocol being used on the socket. See section Sockets.
The requested socket address is already in use. See section Socket Addresses.
The requested socket address is not available; for example, you tried to give a socket a name that doesn't match the local host name. See section Socket Addresses.
A socket operation failed because the network was down.
A socket operation failed because the subnet containing the remost host was unreachable.
A network connection was reset because the remote host crashed.
A network connection was aborted locally.
A network connection was closed for reasons outside the control of the local host, such as by the remote machine rebooting.
The kernel's buffers for I/O operations are all in use.
You tried to connect a socket that is already connected. See section Making a Connection.
The socket is not connected to anything. You get this error when you try to transmit data over a socket, without first specifying a destination for the data.
The socket has already been shut down.
A socket operation with a specified timeout received no response during the timeout period.
A remote host refused to allow the network connection (typically because it is not running the requested service).
Too many levels of symbolic links were encountered in looking up a file name. This often indicates a cycle of symbolic links.
Filename too long (longer than PATH_MAX; see section Limits on File System Capacity) or host name too long (in gethostname or
sethostname; see section Host Identification).
The remote host for a requested network connection is down.
The remote host for a requested network connection is not reachable.
Directory not empty, where an empty directory was expected. Typically, this error occurs when you are trying to delete a directory.
The file quota system is confused because there are too many users.
The user's disk quota was exceeded.
Stale NFS file handle. This indicates an internal confusion in the NFS system which is due to file system rearrangements on the server host. Repairing this condition usually requires unmounting and remounting the NFS file system on the local host.
An attempt was made to NFS-mount a remote file system with a file name that already specifies an NFS-mounted file. (This is an error on some operating systems, but we expect it to work properly on the GNU system, making this error code impossible.)
No locks available. This is used by the file locking facilities; see section File Locks.
Function not implemented. Some functions have commands or options defined that might not be supported in all implementations, and this is the kind of error you get if you request them and they are not supported.
The experienced user will know what is wrong.
This error code has no purpose.
The library has functions and variables designed to make it easy for
your program to report informative error messages in the customary
format about the failure of a library call. The functions
strerror and perror give you the standard error message
for a given error code; the variable
program_invocation_short_name gives you convenient access to the
name of the program that encountered the error.
Function: char * strerror (int errnum)
The strerror function maps the error code (see section Checking for Errors) specified by the errnum argument to a descriptive error
message string. The return value is a pointer to this string.
The value errnum normally comes from the variable errno.
You should not modify the string returned by strerror. Also, if
you make subsequent calls to strerror, the string might be
overwritten. (But it's guaranteed that no library function ever calls
strerror behind your back.)
The function strerror is declared in `string.h'.
Function: void perror (const char *message)
This function prints an error message to the stream stderr;
see section Standard Streams.
If you call perror with a message that is either a null
pointer or an empty string, perror just prints the error message
corresponding to errno, adding a trailing newline.
If you supply a non-null message argument, then perror
prefixes its output with this string. It adds a colon and a space
character to separate the message from the error string corresponding
to errno.
The function perror is declared in `stdio.h'.
strerror and perror produce the exact same message for any
given error code; the precise text varies from system to system. On the
GNU system, the messages are fairly short; there are no multi-line
messages or embedded newlines. Each error message begins with a capital
letter and does not include any terminating punctuation.
Compatibility Note: The strerror function is a new
feature of ANSI C. Many older C systems do not support this function
yet.
Many programs that don't read input from the terminal are designed to
exit if any system call fails. By convention, the error message from
such a program should start with the program's name, sans directories.
You can find that name in the variable
program_invocation_short_name; the full file name is stored the
variable program_invocation_name:
Variable: char * program_invocation_name
This variable's value is the name that was used to invoke the program
running in the current process. It is the same as argv[0].
Variable: char * program_invocation_short_name
This variable's value is the name that was used to invoke the program
running in the current process, with directory names removed. (That is
to say, it is the same as program_invocation_name minus
everything up to the last slash, if any.)
Both program_invocation_name and
program_invocation_short_name are set up by the system before
main is called.
Portability Note: These two variables are GNU extensions. If
you want your program to work with non-GNU libraries, you must save the
value of argv[0] in main, and then strip off the directory
names yourself. We added these extensions to make it possible to write
self-contained error-reporting subroutines that require no explicit
cooperation from main.
Here is an example showing how to handle failure to open a file
correctly. The function open_sesame tries to open the named file
for reading and returns a stream if successful. The fopen
library function returns a null pointer if it couldn't open the file for
some reason. In that situation, open_sesame constructs an
appropriate error message using the strerror function, and
terminates the program. If we were going to make some other library
calls before passing the error code to strerror, we'd have to
save it in a local variable instead, because those other library
functions might overwrite errno in the meantime.
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *
open_sesame (char *name)
{
FILE *stream;
errno = 0;
stream = fopen (name, "r");
if (!stream) {
fprintf (stderr, "%s: Couldn't open file %s; %s\n",
program_invocation_short_name, name, strerror (errno));
exit (EXIT_FAILURE);
} else
return stream;
}
The GNU system provides several methods for allocating memory space under explicit program control. They vary in generality and in efficiency.
malloc facility allows fully general dynamic allocation.
See section Unconstrained Allocation.
malloc but more
efficient and convenient for stacklike allocation. See section Obstacks.
alloca lets you allocate storage dynamically that
will be freed automatically. See section Automatic Storage with Variable Size.
Dynamic memory allocation is a technique in which programs determine as they are running where to store some information. You need dynamic allocation when the number of memory blocks you need, or how long you continue to need them, depends on the data you are working on.
For example, you may need a block to store a line read from an input file; since there is no limit to how long a line can be, you must allocate the storage dynamically and make it dynamically larger as you read more of the line.
Or, you may need a block for each record or each definition in the input data; since you can't know in advance how many there will be, you must allocate a new block for each record or definition as you read it.
When you use dynamic allocation, the allocation of a block of memory is an action that the program requests explicitly. You call a function or macro when you want to allocate space, and specify the size with an argument. If you want to free the space, you do so by calling another function or macro. You can do these things whenever you want, as often as you want.
The C language supports two kinds of memory allocation through the variables in C programs:
In GNU C, the length of the automatic storage can be an expression that varies. In other C implementations, it must be a constant.
Dynamic allocation is not supported by C variables; there is no storage class "dynamic", and there can never be a C variable whose value is stored in dynamically allocated space. The only way to refer to dynamically allocated space is through a pointer. Because it is less convenient, and because the actual process of dynamic allocation requires more computation time, programmers use dynamic allocation only when neither static nor automatic allocation will serve.
For example, if you want to allocate dynamically some space to hold a
struct foobar, you cannot declare a variable of type struct
foobar whose contents are the dynamically allocated space. But you can
declare a variable of pointer type struct foobar * and assign it the
address of the space. Then you can use the operators `*' and
`->' on this pointer variable to refer to the contents of the space:
{
struct foobar *ptr
= (struct foobar *) malloc (sizeof (struct foobar));
ptr->name = x;
ptr->next = current_foobar;
current_foobar = ptr;
}
The most general dynamic allocation facility is malloc. It
allows you to allocate blocks of memory of any size at any time, make
them bigger or smaller at any time, and free the blocks individually at
any time (or never).
To allocate a block of memory, call malloc. The prototype for
this function is in `stdlib.h'.
Function: void * malloc (size_t size)
This function returns a pointer to a newly allocated block size bytes long, or a null pointer if the block could not be allocated.
The contents of the block are undefined; you must initialize it yourself
(or use calloc instead; see section Allocating Cleared Space).
Normally you would cast the value as a pointer to the kind of object
that you want to store in the block. Here we show an example of doing
so, and of initializing the space with zeros using the library function
memset (see section Copying and Concatenation):
struct foo *ptr; ... ptr = (struct foo *) malloc (sizeof (struct foo)); if (ptr == 0) abort (); memset (ptr, 0, sizeof (struct foo));
You can store the result of malloc into any pointer variable
without a cast, because ANSI C automatically converts the type
void * to another type of pointer when necessary. But the cast
is necessary in contexts other than assignment operators or if you might
want your code to run in traditional C.
Remember that when allocating space for a string, the argument to
malloc must be one plus the length of the string. This is
because a string is terminated with a null character that doesn't count
in the "length" of the string but does need space. For example:
char *ptr; ... ptr = (char *) malloc (length + 1);
See section Representation of Strings, for more information about this.
malloc
If no more space is available, malloc returns a null pointer.
You should check the value of every call to malloc. It is
useful to write a subroutine that calls malloc and reports an
error if the value is a null pointer, returning only if the value is
nonzero. This function is conventionally called xmalloc. Here
it is:
void *
xmalloc (size_t size)
{
register void *value = malloc (size);
if (value == 0)
fatal ("virtual memory exhausted");
return value;
}
Here is a real example of using malloc (by way of xmalloc).
The function savestring will copy a sequence of characters into
a newly allocated null-terminated string:
char *
savestring (const char *ptr, size_t len)
{
register char *value = (char *) xmalloc (len + 1);
memcpy (value, ptr, len);
value[len] = 0;
return value;
}
The block that malloc gives you is guaranteed to be aligned so
that it can hold any type of data. In the GNU system, the address is
always a multiple of eight; if the size of block is 16 or more, then the
address is always a multiple of 16. Only rarely is any higher boundary
(such as a page boundary) necessary; for those cases, use
memalign or valloc (see section Allocating Aligned Memory Blocks).
Note that the memory located after the end of the block is likely to be
in use for something else; perhaps a block already allocated by another
call to malloc. If you attempt to treat the block as longer than
you asked for it to be, you are liable to destroy the data that
malloc uses to keep track of its blocks, or you may destroy the
contents of another block. If you have already allocated a block and
discover you want it to be bigger, use realloc (see section Changing the Size of a Block).
malloc
When you no longer need a block that you got with malloc, use the
function free to make the block available to be allocated again.
The prototype for this function is in `stdlib.h'.
Function: void free (void *ptr)
The free function deallocates the block of storage pointed at
by ptr.
Function: void cfree (void *ptr)
This function does the same thing as free. It's provided for
backward compatibility with SunOS; you should use free instead.
Freeing a block alters the contents of the block. Do not expect to find any data (such as a pointer to the next block in a chain of blocks) in the block after freeing it. Copy whatever you need out of the block before freeing it! Here is an example of the proper way to free all the blocks in a chain, and the strings that they point to:
struct chain
{
struct chain *next;
char *name;
}
void
free_chain (struct chain *chain)
{
while (chain != 0)
{
struct chain *next = chain->next;
free (chain->name);
free (chain);
chain = next;
}
}
Occasionally, free can actually return memory to the operating
system and make the process smaller. Usually, all it can do is allow a
later later call to malloc to reuse the space. In the mean time,
the space remains in your program as part of a free-list used internally
by malloc.
There is no point in freeing blocks at the end of a program, because all of the program's space is given back to the system when the process terminates.
Often you do not know for certain how big a block you will ultimately need at the time you must begin to use the block. For example, the block might be a buffer that you use to hold a line being read from a file; no matter how long you make the buffer initially, you may encounter a line that is longer.
You can make the block longer by calling realloc. This function
is declared in `stdlib.h'.
Function: void * realloc (void *ptr, size_t newsize)
The realloc function changes the size of the block whose address is
ptr to be newsize.
Since the space after the end of the block may be in use, realloc
may find it necessary to copy the block to a new address where more free
space is available. The value of realloc is the new address of the
block. If the block needs to be moved, realloc copies the old
contents.
Like malloc, realloc may return a null pointer if no
memory space is available to make the block bigger. When this happens,
the original block is untouched; it has not been modified or relocated.
In most cases it makes no difference what happens to the original block
when realloc fails, because the application program cannot continue
when it is out of memory, and the only thing to do is to give a fatal error
message. Often it is convenient to write and use a subroutine,
conventionally called xrealloc, that takes care of the error message
as xmalloc does for malloc:
void *
xrealloc (void *ptr, size_t size)
{
register void *value = realloc (ptr, size);
if (value == 0)
fatal ("Virtual memory exhausted");
return value;
}
You can also use realloc to make a block smaller. The reason you
would do this is to avoid tying up a lot of memory space when only a little
is needed. Making a block smaller sometimes necessitates copying it, so it
can fail if no other space is available.
If the new size you specify is the same as the old size, realloc
is guaranteed to change nothing and return the same address that you gave.
The function calloc allocates memory and clears it to zero. It
is declared in `stdlib.h'.
Function: void * calloc (size_t count, size_t eltsize)
This function allocates a block long enough to contain a vector of
count elements, each of size eltsize. Its contents are
cleared to zero before calloc returns.
You could define calloc as follows:
void *
calloc (size_t count, size_t eltsize)
{
size_t size = count * eltsize;
void *value = malloc (size);
if (value != 0)
memset (value, 0, size);
return value;
}
We rarely use calloc today, because it is equivalent to such a
simple combination of other features that are more often used. It is a
historical holdover that is not quite obsolete.
malloc
To make the best use of malloc, it helps to know that the GNU
version of malloc always dispenses small amounts of memory in
blocks whose sizes are powers of two. It keeps separate pools for each
power of two. This holds for sizes up to a page size. Therefore, if
you are free to choose the size of a small block in order to make
malloc more efficient, make it a power of two.
Once a page is split up for a particular block size, it can't be reused for another size unless all the blocks in it are freed. In many programs, this is unlikely to happen. Thus, you can sometimes make a program use memory more efficiently by using blocks of the same size for many different purposes.
When you ask for memory blocks of a page or larger, malloc uses a
different strategy; it rounds the size up to a multiple of a page, and
it can coalesce and split blocks as needed.
The reason for the two strategies is that it is important to allocate and free small blocks as fast as possible, but speed is less important for a large block since the program normally spends a fair amount of time using it. Also, large blocks are normally fewer in number. Therefore, for large blocks, it makes sense to use a method which takes more time to minimize the wasted space.
The address of a block returned by malloc or realloc in
the GNU system is always a multiple of eight. If you need a block whose
address is a multiple of a higher power of two than that, use
memalign or valloc. These functions are declared in
`stdlib.h'.
With the GNU library, you can use free to free the blocks that
memalign and valloc return. That does not work in BSD,
however--BSD does not provide any way to free such blocks.
Function: void * memalign (size_t size, int boundary)
The memalign function allocates a block of size bytes whose
address is a multiple of boundary. The boundary must be a
power of two! The function memalign works by calling
malloc to allocate a somewhat larger block, and then returning an
address within the block that is on the specified boundary.
Function: void * valloc (size_t size)
Using valloc is like using memalign and passing the page size
as the value of the second argument.
You can ask malloc to check the consistency of dynamic storage by
using the mcheck function. This function is a GNU extension,
declared in `malloc.h'.
Function: void mcheck (void (*abortfn) (void))
Calling mcheck tells malloc to perform occasional
consistency checks. These will catch things such as writing
past the end of a block that was allocated with malloc.
The abortfn argument is the function to call when an inconsistency
is found. If you supply a null pointer, the abort function is
used.
It is too late to begin allocation checking once you have allocated
anything with malloc. So mcheck does nothing in that
case. The function returns -1 if you call it too late, and
0 otherwise (when it is successful).
The easiest way to arrange to call mcheck early enough is to use
the option `-lmcheck' when you link your program.
The GNU C library lets you modify the behavior of malloc,
realloc, and free by specifying appropriate hook
functions. You can use these hooks to help you debug programs that use
dynamic storage allocation, for example.
The hook variables are declared in `malloc.h'.
The value of this variable is a pointer to function that malloc
uses whenever it is called. You should define this function to look
like malloc; that is, like:
void *function (size_t size)
The value of this variable is a pointer to function that realloc
uses whenever it is called. You should define this function to look
like realloc; that is, like:
void *function (void *ptr, size_t size)
The value of this variable is a pointer to function that free
uses whenever it is called. You should define this function to look
like free; that is, like:
void function (void *ptr)
You must make sure that the function you install as a hook for one of these functions does not call that function recursively without restoring the old value of the hook first! Otherwise, your program will get stuck in an infinite recursion.
Here is an example showing how to use __malloc_hook properly. It
installs a function that prints out information every time malloc
is called.
static void *(*old_malloc_hook) (size_t);
static void *
my_malloc_hook (size_t size)
{
void *result;
__malloc_hook = old_malloc_hook;
result = malloc (size);
__malloc_hook = my_malloc_hook;
printf ("malloc (%u) returns %p\n", (unsigned int) size, result);
return result;
}
main ()
{
...
old_malloc_hook = __malloc_hook;
__malloc_hook = my_malloc_hook;
...
}
The mcheck function (see section Heap Consistency Checking) works by
installing such hooks.
malloc
You can get information about dynamic storage allocation by calling the
mstats function. This function and its associated data type are
declared in `malloc.h'; they are a GNU extension.
This structure type is used to return information about the dynamic storage allocator. It contains the following members:
size_t bytes_total
size_t chunks_used
malloc requests; see section Efficiency Considerations for malloc.)
size_t bytes_used
size_t chunks_free
size_t bytes_free
Function: struct mstats mstats (void)
This function returns information about the current dynamic memory usage
in a structure of type struct mstats.
malloc-Related Functions
Here is a summary of the functions that work with malloc:
void *malloc (size_t size)
void free (void *addr)
malloc. See section Freeing Memory Allocated with malloc.
void *realloc (void *addr, size_t size)
malloc larger or smaller,
possibly by copying it to a new location. See section Changing the Size of a Block.
void *calloc (size_t count, size_t eltsize)
malloc, and set its contents to zero. See section Allocating Cleared Space.
void *valloc (size_t size)
void *memalign (size_t size, size_t boundary)
void mcheck (void (*abortfn) (void))
malloc to perform occasional consistency checks on
dynamically allocated memory, and to call abortfn when an
inconsistency is found. See section Heap Consistency Checking.
void *(*__malloc_hook) (size_t size)
malloc uses whenever it is called.
void *(*__realloc_hook) (void *ptr, size_t size)
realloc uses whenever it is called.
void (*__free_hook) (void *ptr)
free uses whenever it is called.
void struct mstats mstats (void)
malloc.
An obstack is a pool of memory containing a stack of objects. You can create any number of separate obstacks, and then allocate objects in specified obstacks. Within each obstack, the last object allocated must always be the first one freed, but distinct obstacks are independent of each other.
Aside from this one constraint of order of freeing, obstacks are totally general: an obstack can contain any number of objects of any size. They are implemented with macros, so allocation is usually very fast as long as the objects are usually small. And the only space overhead per object is the padding needed to start each object on a suitable boundary.
The utilities for manipulating obstacks are declared in the header file `obstack.h'.
An obstack is represented by a data structure of type struct
obstack. This structure has a small fixed size; it records the status
of the obstack and how to find the space in which objects are allocated.
It does not contain any of the objects themselves. You should not try
to access the contents of the structure directly; use only the functions
described in this chapter.
You can declare variables of type struct obstack and use them as
obstacks, or you can allocate obstacks dynamically like any other kind
of object. Dynamic allocation of obstacks allows your program to have a
variable number of different stacks. (You can even allocate an
obstack structure in another obstack, but this is rarely useful.)
All the functions that work with obstacks require you to specify which
obstack to use. You do this with a pointer of type struct obstack
*. In the following, we often say "an obstack" when strictly
speaking the object at hand is such a pointer.
The objects in the obstack are packed into large blocks called
chunks. The struct obstack structure points to a chain of
the chunks currently in use.
The obstack library obtains a new chunk whenever you allocate an object
that won't fit in the previous chunk. Since the obstack library manages
chunks automatically, you don't need to pay much attention to them, but
you do need to supply a function which the obstack library should use to
get a chunk. Usually you supply a function which uses malloc
directly or indirectly. You must also supply a function to free a chunk.
These matters are described in the following section.
Each source file in which you plan to use the obstack functions must include the header file `obstack.h', like this:
#include <obstack.h>
Also, if the source file uses the macro obstack_init, it must
declare or define two functions or macros that will be called by the
obstack library. One, obstack_chunk_alloc, is used to allocate the
chunks of memory into which objects are packed. The other,
obstack_chunk_free, is used to return chunks when the objects in
them are freed.
Usually these are defined to use malloc via the intermediary
xmalloc (see section Unconstrained Allocation). This is done with
the following pair of macro definitions:
#define obstack_chunk_alloc xmalloc #define obstack_chunk_free free
Though the storage you get using obstacks really comes from malloc,
using obstacks is faster because malloc is called less often, for
larger blocks of memory. See section Obstack Chunks, for full details.
At run time, before the program can use a struct obstack object
as an obstack, it must initialize the obstack by calling
obstack_init.
Function: void obstack_init (struct obstack *obstack_ptr)
Initialize obstack obstack_ptr for allocation of objects.
Here are two examples of how to allocate the space for an obstack and initialize it. First, an obstack that is a static variable:
struct obstack myobstack; ... obstack_init (&myobstack);
Second, an obstack that is itself dynamically allocated:
struct obstack *myobstack_ptr = (struct obstack *) xmalloc (sizeof (struct obstack)); obstack_init (myobstack_ptr);
The most direct way to allocate an object in an obstack is with
obstack_alloc, which is invoked almost like malloc.
Function: void * obstack_alloc (struct obstack *obstack_ptr, size_t size)
This allocates an uninitialized block of size bytes in an obstack
and returns its address. Here obstack_ptr specifies which obstack
to allocate the block in; it is the address of the struct obstack
object which represents the obstack. Each obstack function or macro
requires you to specify an obstack_ptr as the first argument.
For example, here is a function that allocates a copy of a string str
in a specific obstack, which is the variable string_obstack:
struct obstack string_obstack;
char *
copystring (char *string)
{
char *s = (char *) obstack_alloc (&string_obstack,
strlen (string) + 1);
memcpy (s, string, strlen (string));
return s;
}
To allocate a block with specified contents, use the function
obstack_copy, declared like this:
Function: void * obstack_copy (struct obstack *obstack_ptr, void *address, size_t size)
This allocates a block and initializes it by copying size bytes of data starting at address.
Function: void * obstack_copy0 (struct obstack *obstack_ptr, void *address, size_t size)
Like obstack_copy, but appends an extra byte containing a null
character. This extra byte is not counted in the argument size.
The obstack_copy0 function is convenient for copying a sequence
of characters into an obstack as a null-terminated string. Here is an
example of its use:
char *
obstack_savestring (char *addr, size_t size)
{
return obstack_copy0 (&myobstack, addr, size);
}
Contrast this with the previous example of savestring using
malloc (see section Basic Storage Allocation).
To free an object allocated in an obstack, use the function
obstack_free. Since the obstack is a stack of objects, freeing
one object automatically frees all other objects allocated more recently
in the same obstack.
Function: void obstack_free (struct obstack *obstack_ptr, void *object)
If object is a null pointer, everything allocated in the obstack is freed. Otherwise, object must be the address of an object allocated in the obstack. Then object is freed, along with everything allocated in obstack since object.
Note that if object is a null pointer, the result is an
uninitialized obstack. To free all storage in an obstack but leave it
valid for further allocation, call obstack_free with the address
of the first object allocated on the obstack:
obstack_free (obstack_ptr, first_object_allocated_ptr);
Recall that the objects in an obstack are grouped into chunks. When all the objects in a chunk become free, the obstack library automatically frees the chunk (see section Preparing for Using Obstacks). Then other obstacks, or non-obstack allocation, can reuse the space of the chunk.
The interfaces for using obstacks may be defined either as functions or as macros, depending on the compiler. The obstack facility works with all C compilers, including both ANSI C and traditional C, but there are precautions you must take if you plan to use compilers other than GNU C.
If you are using an old-fashioned non-ANSI C compiler, all the obstack "functions" are actually defined only as macros. You can call these macros like functions, but you cannot use them in any other way (for example, you cannot take their address).
Calling the macros requires a special precaution: namely, the first operand (the obstack pointer) may not contain any side effects, because it may be computed more than once. For example, if you write this:
obstack_alloc (get_obstack (), 4);
you will find that get_obstack may be called several times.
If you use *obstack_list_ptr++ as the obstack pointer argument,
you will get very strange results since the incrementation may occur
several times.
In ANSI C, each function has both a macro definition and a function definition. The function definition is used if you take the address of the function without calling it. An ordinary call uses the macro definition by default, but you can request the function definition instead by writing the function name in parentheses, as shown here:
char *x; void *(*funcp) (); /* Use the macro. */ x = (char *) obstack_alloc (obptr, size); /* Call the function. */ x = (char *) (obstack_alloc) (obptr, size); /* Take the address of the function. */ funcp = obstack_alloc;
This is the same situation that exists in ANSI C for the standard library functions. See section Macro Definitions of Functions.
Warning: When you do use the macros, you must observe the precaution of avoiding side effects in the first operand, even in ANSI C.
If you use the GNU C compiler, this precaution is not necessary, because various language extensions in GNU C permit defining the macros so as to compute each argument only once.
Because storage in obstack chunks is used sequentially, it is possible to build up an object step by step, adding one or more bytes at a time to the end of the object. With this technique, you do not need to know how much data you will put in the object until you come to the end of it. We call this the technique of growing objects. The special functions for adding data to the growing object are described in this section.
You don't need to do anything special when you start to grow an object.
Using one of the functions to add data to the object automatically
starts it. However, it is necessary to say explicitly when the object is
finished. This is done with the function obstack_finish.
The actual address of the object thus built up is not known until the object is finished. Until then, it always remains possible that you will add so much data that the object must be copied into a new chunk.
While the obstack is in use for a growing object, you cannot use it for ordinary allocation of another object. If you try to do so, the space already added to the growing object will become part of the other object.
Function: void obstack_blank (struct obstack *obstack_ptr, size_t size)
The most basic function for adding to a growing object is
obstack_blank, which adds space without initializing it.
Function: void obstack_grow (struct obstack *obstack_ptr, void *data, size_t size)
To add a block of initialized space, use obstack_grow, which is
the growing-object analogue of obstack_copy. It adds size
bytes of data to the growing object, copying the contents from
data.
Function: void obstack_grow0 (struct obstack *obstack_ptr, void *data, size_t size)
This is the growing-object analogue of obstack_copy0. It adds
size bytes copied from data, followed by an additional null
character.
Function: void obstack_1grow (struct obstack *obstack_ptr, char c)
To add one character at a time, use the function obstack_1grow.
It adds a single byte containing c to the growing object.
Function: void * obstack_finish (struct obstack *obstack_ptr)
When you are finished growing the object, use the function
obstack_finish to close it off and return its final address.
Once you have finished the object, the obstack is available for ordinary allocation or for growing another object.
When you build an object by growing it, you will probably need to know
afterward how long it became. You need not keep track of this as you grow
the object, because you can find out the length from the obstack just
before finishing the object with the function obstack_object_size,
declared as follows:
Function: size_t obstack_object_size (struct obstack *obstack_ptr)
This function returns the current size of the growing object, in bytes.
Remember to call this function before finishing the object.
After it is finished, obstack_object_size will return zero.
If you have started growing an object and wish to cancel it, you should finish it and then free it, like this:
obstack_free (obstack_ptr, obstack_finish (obstack_ptr));
This has no effect if no object was growing.
You can use obstack_blank with a negative size argument to make
the current object smaller. Just don't try to shrink it beyond zero
length--there's no telling what will happen if you do that.
The usual functions for growing objects incur overhead for checking whether there is room for the new growth in the current chunk. If you are frequently constructing objects in small steps of growth, this overhead can be significant.
You can reduce the overhead by using special "fast growth" functions that grow the object without checking. In order to have a robust program, you must do the checking yourself. If you do this checking in the simplest way each time you are about to add data to the object, you have not saved anything, because that is what the ordinary growth functions do. But if you can arrange to check less often, or check more efficiently, then you make the program faster.
The function obstack_room returns the amount of room available
in the current chunk. It is declared as follows:
Function: size_t obstack_room (struct obstack *obstack_ptr)
This returns the number of bytes that can be added safely to the current growing object (or to an object about to be started) in obstack obstack using the fast growth functions.
While you know there is room, you can use these fast growth functions for adding data to a growing object:
Function: void obstack_1grow_fast (struct obstack *obstack_ptr, char c)
The function obstack_1grow_fast adds one byte containing the
character c to the growing object in obstack obstack_ptr.
Function: void obstack_blank_fast (struct obstack *obstack_ptr, size_t size)
The function obstack_blank_fast adds size bytes to the
growing object in obstack obstack_ptr without initializing them.
When you check for space using obstack_room and there is not
enough room for what you want to add, the fast growth functions
are not safe. In this case, simply use the corresponding ordinary
growth function instead. Very soon this will copy the object to a
new chunk; then there will be lots of room available again.
So, each time you use an ordinary growth function, check afterward for
sufficient space using obstack_room. Once the object is copied
to a new chunk, there will be plenty of space again, so the program will
start using the fast growth functions again.
Here is an example:
void
add_string (struct obstack *obstack, char *ptr, size_t len)
{
while (len > 0)
{
if (obstack_room (obstack) > len)
{
/* We have enough room: add everything fast. */
while (len-- > 0)
obstack_1grow_fast (obstack, *ptr++);
}
else
{
/* Not enough room. Add one character slowly,
which may copy to a new chunk and make room. */
obstack_1grow (obstack, *ptr++);
len--;
}
}
}
Here are functions that provide information on the current status of allocation in an obstack. You can use them to learn about an object while still growing it.
Function: void * obstack_base (struct obstack *obstack_ptr)
This function returns the tentative address of the beginning of the currently growing object in obstack_ptr. If you finish the object immediately, it will have that address. If you make it larger first, it may outgrow the current chunk--then its address will change!
If no object is growing, this value says where the next object you allocate will start (once again assuming it fits in the current chunk).
Function: void * obstack_next_free (struct obstack *obstack_ptr)
This function returns the address of the first free byte in the current
chunk of obstack obstack_ptr. This is the end of the currently
growing object. If no object is growing, obstack_next_free
returns the same value as obstack_base.
Function: size_t obstack_object_size (struct obstack *obstack_ptr)
This function returns the size in bytes of the currently growing object. This is equivalent to
obstack_next_free (obstack_ptr) - obstack_base (obstack_ptr)
Each obstack has an alignment boundary; each object allocated in the obstack automatically starts on an address that is a multiple of the specified boundary. By default, this boundary is 4 bytes.
To access an obstack's alignment boundary, use the macro
obstack_alignment_mask, whose function prototype looks like
this:
Macro: int obstack_alignment_mask (struct obstack *obstack_ptr)
The value is a bit mask; a bit that is 1 indicates that the corresponding bit in the address of an object should be 0. The mask value should be one less than a power of 2; the effect is that all object addresses are multiples of that power of 2. The default value of the mask is 3, so that addresses are multiples of 4. A mask value of 0 means an object can start on any multiple of 1 (that is, no alignment is required).
The expansion of the macro obstack_alignment_mask is an lvalue,
so you can alter the mask by assignment. For example, this statement:
obstack_alignment_mask (obstack_ptr) = 0;
has the effect of turning off alignment processing in the specified obstack.
Note that a change in alignment mask does not take effect until
after the next time an object is allocated or finished in the
obstack. If you are not growing an object, you can make the new
alignment mask take effect immediately by calling obstack_finish.
This will finish a zero-length object and then do proper alignment for
the next object.
Obstacks work by allocating space for themselves in large chunks, and then parceling out space in the chunks to satisfy your requests. Chunks are normally 4096 bytes long unless you specify a different chunk size. The chunk size includes 8 bytes of overhead that are not actually used for storing objects. Regardless of the specified size, longer chunks will be allocated when necessary for long objects.
The obstack library allocates chunks by calling the function
obstack_chunk_alloc, which you must define. When a chunk is no
longer needed because you have freed all the objects in it, the obstack
library frees the chunk by calling obstack_chunk_free, which you
must also define.
These two must be defined (as macros) or declared (as functions) in each
source file that uses obstack_init (see section Creating Obstacks).
Most often they are defined as macros like this:
#define obstack_chunk_alloc xmalloc #define obstack_chunk_free free
Note that these are simple macros (no arguments). Macro definitions with
arguments will not work! It is necessary that obstack_chunk_alloc
or obstack_chunk_free, alone, expand into a function name if it is
not itself a function name.
The function that actually implements obstack_chunk_alloc cannot
return "failure" in any fashion, because the obstack library is not
prepared to handle failure. Therefore, malloc itself is not
suitable. If the function cannot obtain space, it should either
terminate the process (see section Program Termination) or do a nonlocal
exit using longjmp (see section Non-Local Exits).
If you allocate chunks with malloc, the chunk size should be a
power of 2. The default chunk size, 4096, was chosen because it is long
enough to satisfy many typical requests on the obstack yet short enough
not to waste too much memory in the portion of the last chunk not yet used.
Macro: size_t obstack_chunk_size (struct obstack *obstack_ptr)
This returns the chunk size of the given obstack.
Since this macro expands to an lvalue, you can specify a new chunk size by assigning it a new value. Doing so does not affect the chunks already allocated, but will change the size of chunks allocated for that particular obstack in the future. It is unlikely to be useful to make the chunk size smaller, but making it larger might improve efficiency if you are allocating many objects whose size is comparable to the chunk size. Here is how to do so cleanly:
if (obstack_chunk_size (obstack_ptr) < new_chunk_size) obstack_chunk_size (obstack_ptr) = new_chunk_size;
Here is a summary of all the functions associated with obstacks. Each
takes the address of an obstack (struct obstack *) as its first
argument.
void obstack_init (struct obstack *obstack_ptr)
void *obstack_alloc (struct obstack *obstack_ptr, size_t size)
void *obstack_copy (struct obstack *obstack_ptr, void *address, size_t size)
void *obstack_copy0 (struct obstack *obstack_ptr, void *address, size_t size)
void obstack_free (struct obstack *obstack_ptr, void *object)
void obstack_blank (struct obstack *obstack_ptr, size_t size)
void obstack_grow (struct obstack *obstack_ptr, void *address, size_t size)
void obstack_grow0 (struct obstack *obstack_ptr, void *address, size_t size)
void obstack_1grow (struct obstack *obstack_ptr, char data_char)
void *obstack_finish (struct obstack *obstack_ptr)
size_t obstack_object_size (struct obstack *obstack_ptr)
void obstack_blank_fast (struct obstack *obstack_ptr, size_t size)
void obstack_1grow_fast (struct obstack *obstack_ptr, char data_char)
size_t obstack_room (struct obstack *obstack_ptr)
int obstack_alignment_mask (struct obstack *obstack_ptr)
size_t obstack_chunk_size (struct obstack *obstack_ptr)
void *obstack_base (struct obstack *obstack_ptr)
void *obstack_next_free (struct obstack *obstack_ptr)
The function alloca supports a kind of half-dynamic allocation in
which blocks are allocated dynamically but freed automatically.
Allocating a block with alloca is an explicit action; you can
allocate as many blocks as you wish, and compute the size at run time. But
all the blocks are freed when you exit the function that alloca was
called from, just as if they were automatic variables declared in that
function. There is no way to free the space explicitly.
The prototype for alloca is in `stdlib.h'. This function is
a BSD extension.
Function: void * alloca (size_t size);
The return value of alloca is the address of a block of size
bytes of storage, allocated in the stack frame of the calling function.
Do not use alloca inside the arguments of a function call--you
will get unpredictable results, because the stack space for the
alloca would appear on the stack in the middle of the space for
the function arguments. An example of what to avoid is foo (x,
alloca (4), y).
alloca Example
As an example of use of alloca, here is a function that opens a file
name made from concatenating two argument strings, and returns a file
descriptor or minus one signifying failure:
int
open2 (char *str1, char *str2, int flags, int mode)
{
char *name = (char *) alloca (strlen (str1) + strlen (str2) + 1);
strcpy (name, str1);
strcat (name, str2);
return open (name, flags, mode);
}
Here is how you would get the same results with malloc and
free:
int
open2 (char *str1, char *str2, int flags, int mode)
{
char *name = (char *) malloc (strlen (str1) + strlen (str2) + 1);
int desc;
if (name == 0)
fatal ("virtual memory exceeded");
strcpy (name, str1);
strcat (name, str2);
desc = open (name, flags, mode);
free (name);
return desc;
}
As you can see, it is simpler with alloca. But alloca has
other, more important advantages, and some disadvantages.
alloca
Here are the reasons why alloca may be preferable to malloc:
alloca wastes very little space and is very fast. (It is
open-coded by the GNU C compiler.)
alloca does not have separate pools for different sizes of
block, space used for any size block can be reused for any other size.
alloca does not cause storage fragmentation.
longjmp (see section Non-Local Exits)
automatically free the space allocated with alloca when they exit
through the function that called alloca. This is the most
important reason to use alloca.
To illustrate this, suppose you have a function
open_or_report_error which returns a descriptor, like
open, if it succeeds, but does not return to its caller if it
fails. If the file cannot be opened, it prints an error message and
jumps out to the command level of your program using longjmp.
Let's change open2 (see section alloca Example) to use this
subroutine:
int
open2 (char *str1, char *str2, int flags, int mode)
{
char *name = (char *) alloca (strlen (str1) + strlen (str2) + 1);
strcpy (name, str1);
strcat (name, str2);
return open_or_report_error (name, flags, mode);
}
Because of the way alloca works, the storage it allocates is
freed even when an error occurs, with no special effort required.
By contrast, the previous definition of open2 (which uses
malloc and free) would develop a storage leak if it were
changed in this way. Even if you are willing to make more changes to
fix it, there is no easy way to do so.
alloca
These are the disadvantages of alloca in comparison with
malloc:
alloca, so it is less
portable. However, a slower emulation of alloca written in C
is available for use on systems with this deficiency.
In GNU C, you can replace most uses of alloca with an array of
variable size. Here is how open2 would look then:
int open2 (char *str1, char *str2, int flags, int mode)
{
char name[strlen (str1) + strlen (str2) + 1];
strcpy (name, str1);
strcat (name, str2);
return open (name, flags, mode);
}
But alloca is not always equivalent to a variable-sized array, for
several reasons:
alloca usually
remains until the end of the function.
alloca within a loop, allocating an
additional block on each iteration. This is impossible with
variable-sized arrays. On the other hand, this is also slightly
unclean.
Note: If you mix use of alloca and variable-sized arrays
within one function, exiting a scope in which a variable-sized array was
declared frees all blocks allocated with alloca during the
execution of that scope.
Any system of dynamic memory allocation has overhead: the amount of space it uses is more than the amount the program asks for. The relocating memory allocator achieves very low overhead by moving blocks in memory as necessary, on its own initiative.
When you allocate a block with malloc, the address of the block
never changes unless you use realloc to change its size. Thus,
you can safely store the address in various places, temporarily or
permanently, as you like. This is not safe when you use the relocating
memory allocator, because any and all relocatable blocks can move
whenever you allocate memory in any fashion. Even calling malloc
or realloc can move the relocatable blocks.
For each relocatable block, you must make a handle---a pointer object in memory, designated to store the address of that block. The relocating allocator knows where each block's handle is, and updates the address stored there whenever it moves the block, so that the handle always points to the block. Each time you access the contents of the block, you should fetch its address anew from the handle.
To call any of the relocating allocator functions from a signal handler is almost certainly incorrect, because the signal could happen at any time and relocate all the blocks. The only way to make this safe is to block the signal around any access to the contents of any relocatable block--not a convenient mode of operation. See section Signal Handling and Nonreentrant Functions.
In the descriptions below, handleptr designates the address of the handle. All the functions are declared in `malloc.h'; all are GNU extensions.
Function: void * r_alloc (void **handleptr, size_t size)
This function allocates a relocatable block of size size. It
stores the block's address in *handleptr and returns
a non-null pointer to indicate success.
If r_alloc can't get the space needed, it stores a null pointer
in *handleptr, and returns a null pointer.
Function: void r_alloc_free (void **handleptr)
This function is the way to free a relocatable block. It frees the
block that *handleptr points to, and stores a null pointer
in *handleptr to show it doesn't point to an allocated
block any more.
Function: void * r_re_alloc (void **handleptr, size_t size)
The function r_re_alloc adjusts the size of the block that
*handleptr points to, making it size bytes long. It
stores the address of the resized block in *handleptr and
returns a non-null pointer to indicate success.
If enough memory is not available, this function returns a null pointer
and does not modify *handleptr.
You can ask for warnings as the program approaches running out of memory
space, by calling memory_warnings. This is a GNU extension
declared in `malloc.h'.
Function: void memory_warnings (void *start, void (*warn_func) (char *))
Call this function to request warnings for nearing exhaustion of virtual memory.
The argument start says where data space begins, in memory. The allocator compares this against the last address used and against the limit of data space, to determine the fraction of available memory in use. If you supply zero for start, then a default value is used which is right in most circumstances.
For warn_func, supply a function that malloc can call to
warn you. It is called with a string (a warning message) as argument.
Normally it ought to display the string for the user to read.
The warnings come when memory becomes 75% full, when it becomes 85% full, and when it becomes 95% full. Above 95% you get another warning each time memory usage increases.
Programs that work with characters and strings often need to classify a character--is it alphabetic, is it a digit, is it whitespace, and so on--and perform case conversion operations on characters. The functions in the header file `ctype.h' are provided for this purpose.
Since the choice of locale and character set can alter the
classifications of particular character codes, all of these functions
are affected by the current locale. (More precisely, they are affected
by the locale currently selected for character classification--the
LC_CTYPE category; see section Categories of Activities that Locales Affect.)
This section explains the library functions for classifying characters.
For example, isalpha is the function to test for an alphabetic
character. It takes one argument, the character to test, and returns a
nonzero integer if the character is alphabetic, and zero otherwise. You
would use it like this:
if (isalpha (c))
printf ("The character `%c' is alphabetic.\n", c);
Each of the functions in this section tests for membership in a
particular class of characters; each has a name starting with `is'.
Each of them takes one argument, which is a character to test, and
returns an int which is treated as a boolean value. The
character argument is passed as an int, and it may be the
constant value EOF instead of a real character.
The attributes of any given character can vary between locales. See section Locales and Internationalization, for more information on locales.
These functions are declared in the header file `ctype.h'.
Returns true if c is a lower-case letter.
Returns true if c is an upper-case letter.
Returns true if c is an alphabetic character (a letter). If
islower or isupper is true of a character, then
isalpha is also true.
In some locales, there may be additional characters for which
isalpha is true--letters which are neither upper case nor lower
case. But in the standard "C" locale, there are no such
additional characters.
Returns true if c is a decimal digit (`0' through `9').
Returns true if c is an alphanumeric character (a letter or
number); in other words, if either isalpha or isdigit is
true of a character, then isalnum is also true.
Function: int isxdigit (int c)
Returns true if c is a hexadecimal digit. Hexadecimal digits include the normal decimal digits `0' through `9' and the letters `A' through `F' and `a' through `f'.
Returns true if c is a punctuation character. This means any printing character that is not alphanumeric or a space character.
Returns true if c is a whitespace character. In the standard
"C" locale, isspace returns true for only the standard
whitespace characters:
' '
'\f'
'\n'
'\r'
'\t'
'\v'
Returns true if c is a blank character; that is, a space or a tab. This function is a GNU extension.
Returns true if c is a graphic character; that is, a character that has a glyph associated with it. The whitespace characters are not considered graphic.
Returns true if c is a printing character. Printing characters include all the graphic characters, plus the space (` ') character.
Returns true if c is a control character (that is, a character that is not a printing character).
Returns true if c is a 7-bit unsigned char value that fits
into the US/UK ASCII character set. This function is a BSD extension
and is also an SVID extension.
This section explains the library functions for performing conversions
such as case mappings on characters. For example, toupper
converts any character to upper case if possible. If the character
can't be converted, toupper returns it unchanged.
These functions take one argument of type int, which is the
character to convert, and return the converted character as an
int. If the conversion is not applicable to the argument given,
the argument is returned unchanged.
Compatibility Note: In pre-ANSI C dialects, instead of
returning the argument unchanged, these functions may fail when the
argument is not suitable for the conversion. Thus for portability, you
may need to write islower(c) ? toupper(c) : c rather than just
toupper(c).
These functions are declared in the header file `ctype.h'.
If c is an upper-case letter, tolower returns the corresponding
lower-case letter. If c is not an upper-case letter,
c is returned unchanged.
If c is a lower-case letter, tolower returns the corresponding
upper-case letter. Otherwise c is returned unchanged.
This function converts c to a 7-bit unsigned char value
that fits into the US/UK ASCII character set, by clearing the high-order
bits. This function is a BSD extension and is also an SVID extension.
Function: int _tolower (int c)
This is identical to tolower, and is provided for compatibility
with the SVID. See section SVID (The System V Interface Description).
Function: int _toupper (int c)
This is identical to toupper, and is provided for compatibility
with the SVID.
Operations on strings (or arrays of characters) are an important part of
many programs. The GNU C library provides an extensive set of string
utility functions, including functions for copying, concatenating,
comparing, and searching strings. Many of these functions can also
operate on arbitrary regions of storage; for example, the memcpy
function can be used to copy the contents of any kind of array.
It's fairly common for beginning C programmers to "reinvent the wheel" by duplicating this functionality in their own code, but it pays to become familiar with the library functions and to make use of them, since this offers benefits in maintenance, efficiency, and portability.
For instance, you could easily compare one string to another in two
lines of C code, but if you use the built-in strcmp function,
you're less likely to make a mistake. And, since these library
functions are typically highly optimized, your program may run faster
too.
This section is a quick summary of string concepts for beginning C programmers. It describes how character strings are represented in C and some common pitfalls. If you are already familiar with this material, you can skip this section.
A string is an array of char objects. But string-valued
variables are usually declared to be pointers of type char *.
Such variables do not include space for the text of a string; that has
to be stored somewhere else--in an array variable, a string constant,
or dynamically allocated memory (see section Memory Allocation). It's up to
you to store the address of the chosen memory space into the pointer
variable. Alternatively you can store a null pointer in the
pointer variable. The null pointer does not point anywhere, so
attempting to reference the string it points to gets an error.
By convention, a null character, '\0', marks the end of a
string. For example, in testing to see whether the char *
variable p points to a null character marking the end of a string,
you can write !*p or *p == '\0'.
A null character is quite different conceptually from a null pointer,
although both are represented by the integer 0.
String literals appear in C program source as strings of
characters between double-quote characters (`"'). In ANSI C,
string literals can also be formed by string concatenation:
"a" "b" is the same as "ab". Modification of string
literals is not allowed by the GNU C compiler, because literals
are placed in read-only storage.
Character arrays that are declared const cannot be modified
either. It's generally good style to declare non-modifiable string
pointers to be of type const char *, since this often allows the
C compiler to detect accidental modifications as well as providing some
amount of documentation about what your program intends to do with the
string.
The amount of memory allocated for the character array may extend past the null character that normally marks the end of the string. In this document, the term allocation size is always used to refer to the total amount of memory allocated for the string, while the term length refers to the number of characters up to (but not including) the terminating null character.
A notorious source of program bugs is trying to put more characters in a string than fit in its allocated size. When writing code that extends strings or moves characters into a pre-allocated array, you should be very careful to keep track of the length of the text and make explicit checks for overflowing the array. Many of the library functions do not do this for you! Remember also that you need to allocate an extra byte to hold the null character that marks the end of the string.
This chapter describes both functions that work on arbitrary arrays or blocks of memory, and functions that are specific to null-terminated arrays of characters.
Functions that operate on arbitrary blocks of memory have names
beginning with `mem' (such as memcpy) and invariably take an
argument which specifies the size (in bytes) of the block of memory to
operate on. The array arguments and return values for these functions
have type void *, and as a matter of style, the elements of these
arrays are referred to as "bytes". You can pass any kind of pointer
to these functions, and the sizeof operator is useful in
computing the value for the size argument.
In contrast, functions that operate specifically on strings have names
beginning with `str' (such as strcpy) and look for a null
character to terminate the string instead of requiring an explicit size
argument to be passed. (Some of these functions accept a specified
maximum length, but they also check for premature termination with a
null character.) The array arguments and return values for these
functions have type char *, and the array elements are referred
to as "characters".
In many cases, there are both `mem' and `str' versions of a function. The one that is more appropriate to use depends on the exact situation. When your program is manipulating arbitrary arrays or blocks of storage, then you should always use the `mem' functions. On the other hand, when you are manipulating null-terminated strings it is usually more convenient to use the `str' functions, unless you already know the length of the string in advance.
You can get the length of a string using the strlen function.
This function is declared in the header file `string.h'.
Function: size_t strlen (const char *s)
The strlen function returns the length of the null-terminated
string s. (In other words, it returns the offset of the terminating
null character within the array.)
For example,
strlen ("hello, world")
=> 12
When applied to a character array, the strlen function returns
the length of the string stored there, not its allocation size. You can
get the allocation size of the character array that holds a string using
the sizeof operator:
char string[32] = "hello, world";
sizeof (string)
=> 32
strlen (string)
=> 12
You can use the functions described in this section to copy the contents of strings and arrays, or to append the contents of one string to another. These functions are declared in the header file `string.h'.
A helpful way to remember the ordering of the arguments to the functions in this section is that it corresponds to an assignment expression, with the destination array specified to the left of the source array. All of these functions return the address of the destination array.
Most of these functions do not work properly if the source and destination arrays overlap. For example, if the beginning of the destination array overlaps the end of the source array, the original contents of that part of the source array may get overwritten before it is copied. Even worse, in the case of the string functions, the null character marking the end of the string may be lost, and the copy function might get stuck in a loop trashing all the memory allocated to your program.
All functions that have problems copying between overlapping arrays are
explicitly identified in this manual. In addition to functions in this
section, there are a few others like sprintf (see section Formatted Output Functions) and scanf (see section Formatted Input Functions).
Function: void * memcpy (void *to, const void *from, size_t size)
The memcpy function copies size bytes from the object
beginning at from into the object beginning at to. The
behavior of this function is undefined if the two arrays to and
from overlap; use memmove instead if overlapping is possible.
The value returned by memcpy is the value of to.
Here is an example of how you might use memcpy to copy the
contents of a struct:
struct foo *old, *new; ... memcpy (new, old, sizeof(struct foo));
Function: void * memmove (void *to, const void *from, size_t size)
memmove copies the size bytes at from into the
size bytes at to, even if those two blocks of space
overlap. In the case of overlap, memmove is careful to copy the
original values of the bytes in the block at from, including those
bytes which also belong to the block at to.
Function: void * memccpy (void *to, const void *from, int c, size_t size)
This function copies no more than size bytes from from to to, stopping if a byte matching c is found. The return value is a pointer into to one byte past where c was copied, or a null pointer if no byte matching c appeared in the first size bytes of from.
Function: void * memset (void *block, int c, size_t size)
This function copies the value of c (converted to an
unsigned char) into each of the first size bytes of the
object beginning at block. It returns the value of block.
Function: char * strcpy (char *to, const char *from)
This copies characters from the string from (up to and including
the terminating null character) into the string to. Like
memcpy, this function has undefined results if the strings
overlap. The return value is the value of to.
Function: char * strncpy (char *to, const char *from, size_t size)
This function is similar to strcpy but always copies exactly
size characters into to.
If the length of from is more than size, then strncpy
copies just the first size characters.
If the length of from is less than size, then strncpy
copies all of from, followed by enough null characters to add up
to size characters in all. This behavior is rarely useful, but it
is specified by the ANSI C standard.
The behavior of strncpy is undefined if the strings overlap.
Using strncpy as opposed to strcpy is a way to avoid bugs
relating to writing past the end of the allocated space for to.
However, it can also make your program much slower in one common case:
copying a string which is probably small into a potentially large buffer.
In this case, size may be large, and when it is, strncpy will
waste a considerable amount of time copying null characters.
Function: char * strdup (const char *s)
This function copies the null-terminated string s into a newly
allocated string. The string is allocated using malloc; see
section Unconstrained Allocation. If malloc cannot allocate space
for the new string, strdup returns a null pointer. Otherwise it
returns a pointer to the new string.
Function: char * stpcpy (char *to, const char *from)
This function is like strcpy, except that it returns a pointer to
the end of the string to (that is, the address of the terminating
null character) rather than the beginning.
For example, this program uses stpcpy to concatenate `foo'
and `bar' to produce `foobar', which it then prints.
#include <string.h>
int
main (void)
{
char *to = buffer;
to = stpcpy (to, "foo");
to = stpcpy (to, "bar");
printf ("%s\n", buffer);
}
This function is not part of the ANSI or POSIX standards, and is not customary on Unix systems, but we did not invent it either. Perhaps it comes from MS-DOG.
Its behavior is undefined if the strings overlap.
Function: char * strcat (char *to, const char *from)
The strcat function is similar to strcpy, except that the
characters from from are concatenated or appended to the end of
to, instead of overwriting it. That is, the first character from
from overwrites the null character marking the end of to.
An equivalent definition for strcat would be:
char *
strcat (char *to, const char *from)
{
strcpy (to + strlen (to), from);
return to;
}
This function has undefined results if the strings overlap.
Function: char * strncat (char *to, const char *from, size_t size)
This function is like strcat except that not more than size
characters from from are appended to the end of to. A
single null character is also always appended to to, so the total
allocated size of to must be at least size + 1 bytes
longer than its initial length.
char *
strncat (char *to, const char *from, size_t size)
{
strncpy (to + strlen (to), from, size);
return to;
}
The behavior of strncat is undefined if the strings overlap.
Here is an example showing the use of strncpy and strncat.
Notice how, in the call to strncat, the size parameter
is computed to avoid overflowing the character array buffer.
#include <string.h>
#include <stdio.h>
#define SIZE 10
static char buffer[SIZE];
main ()
{
strncpy (buffer, "hello", SIZE);
printf ("%s\n", buffer);
strncat (buffer, ", world", SIZE - strlen (buffer) - 1);
printf ("%s\n", buffer);
}
The output produced by this program looks like:
hello hello, wo
Function: void * bcopy (void *from, const void *to, size_t size)
This is a partially obsolete alternative for memmove, derived from
BSD. Note that it is not quite equivalent to memmove, because the
arguments are not in the same order.
Function: void * bzero (void *block, size_t size)
This is a partially obsolete alternative for memset, derived from
BSD. Note that it is not as general as memset, because the only
value it can store is zero.
You can use the functions in this section to perform comparisons on the contents of strings and arrays. As well as checking for equality, these functions can also be used as the ordering functions for sorting operations. See section Searching and Sorting, for an example of this.
Unlike most comparison operations in C, the string comparison functions return a nonzero value if the strings are not equivalent rather than if they are. The sign of the value indicates the relative ordering of the first characters in the strings that are not equivalent: a negative value indicates that the first string is "less" than the second, while a positive value indicates that the first string is "greater".
If you are using these functions only to check for equality, you might find it makes for a cleaner program to hide them behind a macro definition, like this:
#define str_eq(s1,s2) (!strcmp ((s1),(s2)))
All of these functions are declared in the header file `string.h'.
Function: int memcmp (const void *a1, const void *a2, size_t size)
The function memcmp compares the size bytes of memory
beginning at a1 against the size bytes of memory beginning
at a2. The value returned has the same sign as the difference
between the first differing pair of bytes (interpreted as unsigned
char objects, then promoted to int).
If the contents of the two blocks are equal, memcmp returns
0.
On arbitrary arrays, the memcmp function is mostly useful for
testing equality. It usually isn't meaningful to do byte-wise ordering
comparisons on arrays of things other than bytes. For example, a
byte-wise comparison on the bytes that make up floating-point numbers
isn't likely to tell you anything about the relationship between the
values of the floating-point numbers.
You should also be careful about using memcmp to compare objects
that can contain "holes", such as the padding inserted into structure
objects to enforce alignment requirements, extra space at the end of
unions, and extra characters at the ends of strings whose length is less
than their allocated size. The contents of these "holes" are
indeterminate and may cause strange behavior when performing byte-wise
comparisons. For more predictable results, perform an explicit
component-wise comparison.
For example, given a structure type definition like:
struct foo
{
unsigned char tag;
union
{
double f;
long i;
char *p;
} value;
};
you are better off writing a specialized comparison function to compare
struct foo objects instead of comparing them with memcmp.
Function: int strcmp (const char *s1, const char *s2)
The strcmp function compares the string s1 against
s2, returning a value that has the same sign as the difference
between the first differing pair of characters (interpreted as
unsigned char objects, then promoted to int).
If the two strings are equal, strcmp returns 0.
A consequence of the ordering used by strcmp is that if s1
is an initial substring of s2, then s1 is considered to be
"less than" s2.
Function: int strcasecmp (const char *s1, const char *s2)
This function is like strcmp, except that differences in case
are ignored.
strcasecmp is derived from BSD.
Function: int strncasecmp (const char *s1, const char *s2, size_t n)
This function is like strncmp, except that differences in case
are ignored.
strncasecmp is a GNU extension.
Function: int strncmp (const char *s1, const char *s2, size_t size)
This function is the similar to strcmp, except that no more than
size characters are compared. In other words, if the two strings are
the same in their first size characters, the return value is zero.
Here are some examples showing the use of strcmp and strncmp.
These examples assume the use of the ASCII character set. (If some
other character set--say, EBCDIC--is used instead, then the glyphs
are associated with different numeric codes, and the return values
and ordering may differ.)
strcmp ("hello", "hello")
=> 0 /* These two strings are the same. */
strcmp ("hello", "Hello")
=> 32 /* Comparisons are case-sensitive. */
strcmp ("hello", "world")
=> -15 /* The character 'h' comes before 'w'. */
strcmp ("hello", "hello, world")
=> -44 /* Comparing a null character against a comma. */
strncmp ("hello", "hello, world"", 5)
=> 0 /* The initial 5 characters are the same. */
strncmp ("hello, world", "hello, stupid world!!!", 5)
=> 0 /* The initial 5 characters are the same. */
Function: int bcmp (const void *a1, const void *a2, size_t size)
This is an obsolete alias for memcmp, derived from BSD.
In some locales, the conventions for lexicographic ordering differ from the strict numeric ordering of character codes. For example, in Spanish most glyphs with diacritical marks such as accents are not considered distinct letters for the purposes of collation. On the other hand, the two-character sequence `ll' is treated as a single letter that is collated immediately after `l'.
You can use the functions strcoll and strxfrm (declared in
the header file `string.h') to compare strings using a collation
ordering appropriate for the current locale. The locale used by these
functions in particular can be specified by setting the locale for the
LC_COLLATE category; see section Locales and Internationalization.
In the standard C locale, the collation sequence for strcoll is
the same as that for strcmp.
Effectively, the way these functions work is by applying a mapping to transform the characters in a string to a byte sequence that represents the string's position in the collating sequence of the current locale. Comparing two such byte sequences in a simple fashion is equivalent to comparing the strings with the locale's collating sequence.
The function strcoll performs this translation implicitly, in
order to do one comparison. By contrast, strxfrm performs the
mapping explicitly. If you are making multiple comparisons using the
same string or set of strings, it is likely to be more efficient to use
strxfrm to transform all the strings just once, and subsequently
compare the transformed strings with strcmp.
Function: int strcoll (const char *s1, const char *s2)
The strcoll function is similar to strcmp but uses the
collating sequence of the current locale for collation (the
LC_COLLATE locale).
Here is an example of sorting an array of strings, using strcoll
to compare them. The actual sort algorithm is not written here; it
comes from qsort (see section Array Sort Function). The job of the
code shown here is to say how to compare the strings while sorting them.
(Later on in this section, we will show a way to do this more
efficiently using strxfrm.)
/* This is the comparison function used withqsort. */ int compare_elements (char **p1, char **p2) { return strcoll (*p1, *p2); } /* This is the entry point--the function to sort strings using the locale's collating sequence. */ void sort_strings (char **array, int nstrings) { /* Sorttemp_arrayby comparing the strings. */ qsort (array, sizeof (char *), nstrings, compare_elements); }
Function: size_t strxfrm (char *to, const char *from, size_t size)
The function strxfrm transforms string using the collation
transformation determined by the locale currently selected for
collation, and stores the transformed string in the array to. Up
to size characters (including a terminating null character) are
stored.
The behavior is undefined if the strings to and from overlap; see section Copying and Concatenation.
The return value is the length of the entire transformed string. This
value is not affected by the value of size, but if it is greater
than size, it means that the transformed string did not entirely
fit in the array to. In this case, only as much of the string as
actually fits was stored. To get the whole transformed string, call
strxfrm again with a bigger output array.
The transformed string may be longer than the original string, and it may also be shorter.
If size is zero, no characters are stored in to. In this
case, strxfrm simply returns the number of characters that would
be the length of the transformed string. This is useful for determining
what size string to allocate. It does not matter what to is if
size is zero; to may even be a null pointer.
Here is an example of how you can use strxfrm when
you plan to do many comparisons. It does the same thing as the previous
example, but much faster, because it has to transform each string only
once, no matter how many times it is compared with other strings. Even
the time needed to allocate and free storage is much less than the time
we save, when there are many strings.
struct sorter { char *input; char *transformed; };
/* This is the comparison function used with qsort
to sort an array of struct sorter. */
int
compare_elements (struct sorter *p1, struct sorter *p2)
{
return strcmp (p1->transformed, p2->transformed);
}
/* This is the entry point--the function to sort
strings using the locale's collating sequence. */
void
sort_strings_fast (char **array, int nstrings)
{
struct sorter temp_array[nstrings];
int i;
/* Set up temp_array. Each element contains
one input string and its transformed string. */
for (i = 0; i < nstrings; i++)
{
size_t length = strlen (array[i]) * 2;
temp_array[i].input = array[i];
/* Transform array[i].
First try a buffer probably big enough. */
while (1)
{
char *transformed = (char *) xmalloc (length);
if (strxfrm (transformed, array[i], length) < length)
{
temp_array[i].transformed = transformed;
break;
}
/* Try again with a bigger buffer. */
free (transformed);
length *= 2;
}
}
/* Sort temp_array by comparing transformed strings. */
qsort (temp_array, sizeof (struct sorter),
nstrings, compare_elements);
/* Put the elements back in the permanent array
in their sorted order. */
for (i = 0; i < nstrings; i++)
array[i] = temp_array[i].input;
/* Free the strings we allocated. */
for (i = 0; i < nstrings; i++)
free (temp_array[i].transformed);
}
Compatibility Note: The string collation functions are a new feature of ANSI C. Older C dialects have no equivalent feature.
This section describes library functions which perform various kinds of searching operations on strings and arrays. These functions are declared in the header file `string.h'.
Function: void * memchr (const void *block, int c, size_t size)
This function finds the first occurrence of the byte c (converted
to an unsigned char) in the initial size bytes of the
object beginning at block. The return value is a pointer to the
located byte, or a null pointer if no match was found.
Function: char * strchr (const char *string, int c)
The strchr function finds the first occurrence of the character
c (converted to a char) in the null-terminated string
beginning at string. The return value is a pointer to the located
character, or a null pointer if no match was found.
For example,
strchr ("hello, world", 'l')
=> "llo, world"
strchr ("hello, world", '?')
=> NULL
The terminating null character is considered to be part of the string, so you can use this function get a pointer to the end of a string by specifying a null character as the value of the c argument.
Function: char * strrchr (const char *string, int c)
The function strrchr is like strchr, except that it searches
backwards from the end of the string string (instead of forwards
from the front).
For example,
strrchr ("hello, world", 'l')
=> "ld"
Function: char * strstr (const char *haystack, const char *needle)
This is like strchr, except that it searches haystack for a
substring needle rather than just a single character. It
returns a pointer into the string haystack that is the first
character of the substring, or a null pointer if no match was found. If
needle is an empty string, the function returns haystack.
For example,
strstr ("hello, world", "l")
=> "llo, world"
strstr ("hello, world", "wo")
=> "world"
Function: void * memmem (const void *needle, size_t needle_len,
const void *haystack, size_t haystack_len)
This is like strstr, but needle and haystack are byte
arrays rather than null-terminated strings. needle_len is the
length of needle and haystack_len is the length of
haystack.
This function is a GNU extension.
Function: size_t strspn (const char *string, const char *skipset)
The strspn ("string span") function returns the length of the
initial substring of string that consists entirely of characters that
are members of the set specified by the string skipset. The order
of the characters in skipset is not important.
For example,
strspn ("hello, world", "abcdefghijklmnopqrstuvwxyz")
=> 5
Function: size_t strcspn (const char *string, const char *stopset)
The strcspn ("string complement span") function returns the length
of the initial substring of string that consists entirely of characters
that are not members of the set specified by the string stopset.
(In other words, it returns the offset of the first character in string
that is a member of the set stopset.)
For example,
strcspn ("hello, world", " \t\n,.;!?")
=> 5
Function: char * strpbrk (const char *string, const char *stopset)
The strpbrk ("string pointer break") function is related to
strcspn, except that it returns a pointer to the first character
in string that is a member of the set stopset instead of the
length of the initial substring. It returns a null pointer if no such
character from stopset is found.
For example,
strpbrk ("hello, world", " \t\n,.;!?")
=> ", world"
It's fairly common for programs to have a need to do some simple kinds
of lexical analysis and parsing, such as splitting a command string up
into tokens. You can do this with the strtok function, declared
in the header file `string.h'.
Function: char * strtok (char *newstring, const char *delimiters)
A string can be split into tokens by making a series of calls to the
function strtok.
The string to be split up is passed as the newstring argument on
the first call only. The strtok function uses this to set up
some internal state information. Subsequent calls to get additional
tokens from the same string are indicated by passing a null pointer as
the newstring argument. Calling strtok with another
non-null newstring argument reinitializes the state information.
It is guaranteed that no other library function ever calls strtok
behind your back (which would mess up this internal state information).
The delimiters argument is a string that specifies a set of delimiters that may surround the token being extracted. All the initial characters that are members of this set are discarded. The first character that is not a member of this set of delimiters marks the beginning of the next token. The end of the token is found by looking for the next character that is a member of the delimiter set. This character in the original string newstring is overwritten by a null character, and the pointer to the beginning of the token in newstring is returned.
On the next call to strtok, the searching begins at the next
character beyond the one that marked the end of the previous token.
Note that the set of delimiters delimiters do not have to be the
same on every call in a series of calls to strtok.
If the end of the string newstring is reached, or if the remainder of
string consists only of delimiter characters, strtok returns
a null pointer.
Warning: Since strtok alters the string it is parsing,
you always copy the string to a temporary buffer before parsing it with
strtok. If you allow strtok to modify a string that came
from another part of your program, you are asking for trouble; that
string may be part of a data structure that could be used for other
purposes during the parsing, when alteration by strtok makes the
data structure temporarily inaccurate.
The string that you are operating on might even be a constant. Then
when strtok tries to modify it, your program will get a fatal
signal for writing in read-only memory. See section Program Error Signals.
This is a special case of a general principle: if a part of a program does not have as its purpose the modification of a certain data structure, then it is error-prone to modify the data structure temporarily.
The function strtok is not reentrant. See section Signal Handling and Nonreentrant Functions, for
a discussion of where and why reentrancy is important.
Here is a simple example showing the use of strtok.
#include <string.h> #include <stddef.h> ... char string[] = "words separated by spaces -- and, punctuation!"; const char delimiters[] = " .,;:!-"; char *token; ... token = strtok (string, delimiters); /* token => "words" */ token = strtok (NULL, delimiters); /* token => "separated" */ token = strtok (NULL, delimiters); /* token => "by" */ token = strtok (NULL, delimiters); /* token => "spaces" */ token = strtok (NULL, delimiters); /* token => "and" */ token = strtok (NULL, delimiters); /* token => "punctuation" */ token = strtok (NULL, delimiters); /* token => NULL */
A number of languages use character sets that are larger than the range
of values of type char. Japanese and Chinese are probably the
most familiar examples.
The GNU C library includes support for two mechanisms for dealing with extended character sets: multibyte characters and wide characters. This chapter describes how to use these mechanisms, and the functions for converting between them.
The behavior of the functions in this chapter is affected by the current
locale for character classification--the LC_CTYPE category; see
section Categories of Activities that Locales Affect. This choice of locale selects which multibyte
code is used, and also controls the meanings and characteristics of wide
character codes.
You can represent extended characters in either of two ways:
char objects. Their advantage is that many
programs and operating systems can handle occasional multibyte
characters scattered among ordinary ASCII characters, without any
change.
wchar_t,
has a range large enough to hold extended character codes as well as
old-fashioned ASCII codes.
An advantage of wide characters is that each character is a single data object, just like ordinary ASCII characters. There are a few disadvantages:
Typically, you use the multibyte character representation as part of the
external program interface, such as reading or writing text to files.
However, it's usually easier to perform internal manipulations on
strings containing extended characters on arrays of wchar_t
objects, since the uniform representation makes most editing operations
easier. If you do use multibyte characters for files and wide
characters for internal operations, you need to convert between them
when you read and write data.
If your system supports extended characters, then it supports them both as multibyte characters and as wide characters. The library includes functions you can use to convert between the two representations. These functions are described in this chapter.
A computer system can support more than one multibyte character code, and more than one wide character code. The user controls the choice of codes through the current locale for character classification (see section Locales and Internationalization). Each locale specifies a particular multibyte character code and a particular wide character code. The choice of locale influences the behavior of the conversion functions in the library.
Some locales support neither wide characters nor nontrivial multibyte characters. In these locales, the library conversion functions still work, even though what they do is basically trivial.
If you select a new locale for character classification, the internal shift state maintained by these functions can become confused, so it's not a good idea to change the locale while you are in the middle of processing a string.
In the ordinary ASCII code, a sequence of characters is a sequence of bytes, and each character is one byte. This is very simple, but allows for only 256 distinct characters.
In a multibyte character code, a sequence of characters is a sequence of bytes, but each character may occupy one or more consecutive bytes of the sequence.
There are many different ways of designing a multibyte character code; different systems use different codes. To specify a particular code means designating the basic byte sequences--those which represent a single character--and what characters they stand for. A code that a computer can actually use must have a finite number of these basic sequences, and typically none of them is more than a few characters long.
These sequences need not all have the same length. In fact, many of
them are just one byte long. Because the basic ASCII characters in the
range from 0 to 0177 are so important, they stand for
themselves in all multibyte character codes. That is to say, a byte
whose value is 0 through 0177 is always a character in
itself. The characters which are more than one byte must always start
with a byte in the range from 0200 through 0377.
The byte value 0 can be used to terminated a string, just as it
is often used in a string of ASCII characters.
Specifying the basic byte sequences that represent single characters
automatically gives meanings to many longer byte sequences, as more than
one character. For example, if the two byte sequence 0205 049
stands for the Greek letter alpha, then 0205 049 065 must stand
for an alpha followed by an `A' (ASCII code 065), and 0205 049
0205 049 must stand for two alphas in a row.
If any byte sequence can have more than one meaning as a sequence of characters, then the multibyte code is ambiguous--and no good. The codes that systems actually use are all unambiguous.
In most codes, there are certain sequences of bytes that have no meaning as a character or characters. These are called invalid.
The simplest possible multibyte code is a trivial one:
The basic sequences consist of single bytes.
This particular code is equivalent to not using multibyte characters at all. It has no invalid sequences. But it can handle only 256 different characters.
Here is another possible code which can handle 9376 different characters:
The basic sequences consist of
- single bytes with values in the range
0through0237.
- two-byte sequences, in which both of the bytes have values in the range from
0240through0377.
This code or a similar one is used on some systems to represent Japanese
characters. The invalid sequences are those which consist of an odd
number of consecutive bytes in the range from 0240 through
0377.
Here is another multibyte code which can handle more distinct extended characters--in fact, almost thirty million:
The basic sequences consist of
- single bytes with values in the range
0through0177.
- sequences of up to four bytes in which the first byte is in the range from
0200through0237, and the remaining bytes are in the range from0240through0377.
In this code, any sequence that starts with a byte in the range
from 0240 through 0377 is invalid.
And here is another variant which has the advantage that removing the last byte or bytes from a valid character can never produce another valid character. (This property is convenient when you want to search strings for particular characters.)
The basic sequences consist of
- single bytes with values in the range
0through0177.
- two-byte sequences in which the first byte is in the range from
0200through0207, and the second byte is in the range from0240through0377.
- three-byte sequences in which the first byte is in the range from
0210through0217, and the other bytes are in the range from0240through0377.
- four-byte sequences in which the first byte is in the range from
0220through0227, and the other bytes are in the range from0240through0377.
The list of invalid sequences for this code is long and not worth
stating in full; examples of invalid sequences include 0240 and
0220 0300 065.
The number of possible multibyte codes is astronomical. But a given computer system will support at most a few different codes. (One of these codes may allow for thousands of different characters.) Another computer system may support a completely different code. The library facilities described in this chapter are helpful because they package up the knowledge of the details of a particular computer system's multibyte code, so your programs need not know them.
You can use special standard macros to find out the maximum possible
number of bytes in a character in the currently selected multibyte
code with MB_CUR_MAX, and the maximum for any multibyte
code supported on your computer with MB_LEN_MAX.
This is the maximum length of a multibyte character for any supported locale. It is defined in `limits.h'.
This macro expands into a (possibly non-constant) positive integer
expression that is the maximum number of bytes in a multibyte character
in the current locale. The value is never greater than MB_LEN_MAX.
MB_CUR_MAX is defined in `stdlib.h'.
Normally, each basic sequence in a particular character code stands for one character, the same character regardless of context. Some multibyte character codes have a concept of shift state; certain codes, called shift sequences, change to a different shift state, and the meaning of some or all basic sequences varies according to the current shift state. In fact, the set of basic sequences might even be different depending on the current shift state. See section Multibyte Codes Using Shift Sequences, for more information on handling this sort of code.
What happens if you try to pass a string containing multibyte characters to a function that doesn't know about them? Normally, such a function treats a string as a sequence of bytes, and interprets certain byte values specially; all other byte values are "ordinary". As long as a multibyte character doesn't contain any of the special byte values, the function should pass it through as if it were several ordinary characters.
For example, let's figure out what happens if you use multibyte
characters in a file name. The functions such as open and
unlink that operate on file names treat the name as a sequence of
byte values, with `/' as the only special value. Any other byte
values are copied, or compared, in sequence, and all byte values are
treated alike. Thus, you may think of the file name as a sequence of
bytes or as a string containing multibyte characters; the same behavior
makes sense equally either way, provided no multibyte character contains
a `/'.
Wide characters are much simpler than multibyte characters. They
are simply characters with more than eight bits, so that they have room
for more than 256 distinct codes. The wide character data type,
wchar_t, has a range large enough to hold extended character
codes as well as old-fashioned ASCII codes.
An advantage of wide characters is that each character is a single data object, just like ordinary ASCII characters. Wide characters also have some disadvantages:
Wide character values 0 through 0177 are always identical
in meaning to the ASCII character codes. The wide character value zero
is often used to terminate a string of wide characters, just as a single
byte with value zero often terminates a string of ordinary characters.
This is the "wide character" type, an integer type whose range is large enough to represent all distinct values in any extended character set in the supported locales. See section Locales and Internationalization, for more information about locales. This type is defined in the header file `stddef.h'.
If your system supports extended characters, then each extended character has both a wide character code and a corresponding multibyte basic sequence.
In this chapter, the term code is used to refer to a single
extended character object to emphasize the distinction from the
char data type.
The mbstowcs function converts a string of multibyte characters
to a wide character array. The wcstombs function does the
reverse. These functions are declared in the header file
`stdlib.h'.
In most programs, these functions are the only ones you need for conversion between wide strings and multibyte character strings. But they have limitations. If your data is not null-terminated or is not all in core at once, you probably need to use the low-level conversion functions to convert one character at a time. See section Conversion of Extended Characters One by One.
Function: size_t mbstowcs (wchar_t *wstring, const char *string, size_t size)
The mbstowcs ("multibyte string to wide character string")
function converts the null-terminated string of multibyte characters
string to an array of wide character codes, storing not more than
size wide characters into the array beginning at wstring.
The terminating null character counts towards the size, so if size
is less than the actual number of wide characters resulting from
string, no terminating null character is stored.
The conversion of characters from string begins in the initial shift state.
If an invalid multibyte character sequence is found, this function
returns a value of -1. Otherwise, it returns the number of wide
characters stored in the array wstring. This number does not
include the terminating null character, which is present if the number
is less than size.
Here is an example showing how to convert a string of multibyte characters, allocating enough space for the result.
wchar_t *
mbstowcs_alloc (char *string)
{
int size = strlen (string) + 1;
wchar_t *buffer = (wchar_t) xmalloc (size * sizeof (wchar_t));
size = mbstowcs (buffer, string, size);
if (size < 0)
return NULL;
return (wchar_t) xrealloc (buffer, (size + 1) * sizeof (wchar_t));
}
Function: size_t wcstombs (char *string, const wchar_t wstring, size_t size)
The wcstombs ("wide character string to multibyte string")
function converts the null-terminated wide character array wstring
into a string containing multibyte characters, storing not more than
size bytes starting at string, followed by a terminating
null character if there is room. The conversion of characters begins in
the initial shift state.
The terminating null character counts towards the size, so if size is less than or equal to the number of bytes needed in wstring, no terminating null character is stored.
If a code that does not correspond to a valid multibyte character is
found, this function returns a value of -1. Otherwise, the
return value is the number of bytes stored in the array string.
This number does not include the terminating null character, which is
present if the number is less than size.
This section describes how to scan a string containing multibyte
characters, one character at a time. The difficulty in doing this
is to know how many bytes each character contains. Your program
can use mblen to find this out.
Function: int mblen (const char *string, size_t size)
The mblen function with non-null string returns the number
of bytes that make up the multibyte character beginning at string,
never examining more than size bytes. (The idea is to supply
for size the number of bytes of data you have in hand.)
The return value of mblen distinguishes three possibilities: the
first size bytes at string start with valid multibyte
character, they start with an invalid byte sequence or just part of a
character, or string points to an empty string (a null character).
For a valid multibyte character, mblen returns the number of
bytes in that character (always at least 1, and never more than
size). For an invalid byte sequence, mblen returns
-1. For an empty string, it returns 0.
If the multibyte character code uses shift characters, then mblen
maintains and updates a shift state as it scans. If you call
mblen with a null pointer for string, that initializes the
shift state to its standard initial value. It also returns nonzero if
the multibyte character code in use actually has a shift state.
See section Multibyte Codes Using Shift Sequences.
The function mblen is declared in `stdlib.h'.
You can convert multibyte characters one at a time to wide characters
with the mbtowc function. The wctomb function does the
reverse. These functions are declared in `stdlib.h'.
Function: int mbtowc (wchar_t *result, const char *string, size_t size)
The mbtowc ("multibyte to wide character") function when called
with non-null string converts the first multibyte character
beginning at string to its corresponding wide character code. It
stores the result in *result.
mbtowc never examines more than size bytes. (The idea is
to supply for size the number of bytes of data you have in hand.)
mbtowc with non-null string distinguishes three
possibilities: the first size bytes at string start with
valid multibyte character, they start with an invalid byte sequence or
just part of a character, or string points to an empty string (a
null character).
For a valid multibyte character, mbtowc converts it to a wide
character and stores that in *result, and returns the
number of bytes in that character (always at least 1, and never
more than size).
For an invalid byte sequence, mbtowc returns -1. For an
empty string, it returns 0, also storing 0 in
*result.
If the multibyte character code uses shift characters, then
mbtowc maintains and updates a shift state as it scans. If you
call mbtowc with a null pointer for string, that
initializes the shift state to its standard initial value. It also
returns nonzero if the multibyte character code in use actually has a
shift state. See section Multibyte Codes Using Shift Sequences.
Function: int wctomb (char *string, wchar_t wchar)
The wctomb ("wide character to multibyte") function converts
the wide character code wchar to its corresponding multibyte
character sequence, and stores the result in bytes starting at
string. At most MB_CUR_MAX characters are stored.
wctomb with non-null string distinguishes three
possibilities for wchar: a valid wide character code (one that can
be translated to a multibyte character), an invalid code, and 0.
Given a valid code, wctomb converts it to a multibyte character,
storing the bytes starting at string. Then it returns the number
of bytes in that character (always at least 1, and never more
than MB_CUR_MAX).
If wchar is an invalid wide character code, wctomb returns
-1. If wchar is 0, it returns 0, also
storing 0 in *string.
If the multibyte character code uses shift characters, then
wctomb maintains and updates a shift state as it scans. If you
call wctomb with a null pointer for string, that
initializes the shift state to its standard initial value. It also
returns nonzero if the multibyte character code in use actually has a
shift state. See section Multibyte Codes Using Shift Sequences.
Calling this function with a wchar argument of zero when
string is not null has the side-effect of reinitializing the
stored shift state as well as storing the multibyte character
0 and returning 0.
Here is an example that reads multibyte character text from descriptor
input and writes the corresponding wide characters to descriptor
output. We need to convert characters one by one for this
example because mbstowcs is unable to continue past a null
character, and cannot cope with an apparently invalid partial character
by reading more input.
int
file_mbstowcs (int input, int output)
{
char buffer[BUFSIZ + MB_LEN_MAX];
int filled = 0;
int eof = 0;
while (!eof)
{
int nread;
int nwrite;
char *inp = buffer;
wchar_t outbuf[BUFSIZ];
wchar_t *outp = outbuf;
/* Fill up the buffer from the input file. */
nread = read (input, buffer + filled, BUFSIZ);
if (nread < 0) {
perror ("read");
return 0;
}
/* If we reach end of file, make a note to read no more. */
if (nread == 0)
eof = 1;
/* filled is now the number of bytes in buffer. */
filled += nread;
/* Convert those bytes to wide characters--as many as we can. */
while (1)
{
int thislen = mbtowc (outp, inp, filled);
/* Stop converting at invalid character;
this can mean we have read just the first part
of a valid character. */
if (thislen == -1)
break;
/* Treat null character like any other,
but also reset shift state. */
if (thislen == 0) {
thislen = 1;
mbtowc (NULL, NULL, 0);
}
/* Advance past this character. */
inp += thislen;
filled -= thislen;
outp++;
}
/* Write the wide characters we just made. */
nwrite = write (output, outbuf,
(outp - outbuf) * sizeof (wchar_t));
if (nwrite < 0)
{
perror ("write");
return 0;
}
/* See if we have a real invalid character. */
if ((eof && filled > 0) || filled >= MB_CUR_MAX)
{
error ("invalid multibyte character");
return 0;
}
/* If any characters must be carried forward,
put them at the beginning of buffer. */
if (filled > 0)
memcpy (inp, buffer, filled);
}
}
return 1;
}
In some multibyte character codes, the meaning of any particular byte sequence is not fixed; it depends on what other sequences have come earlier in the same string. Typically there are just a few sequences that can change the meaning of other sequences; these few are called shift sequences and we say that they set the shift state for other sequences that follow.
To illustrate shift state and shift sequences, suppose we decide that
the sequence 0200 (just one byte) enters Japanese mode, in which
pairs of bytes in the range from 0240 to 0377 are single
characters, while 0201 enters Latin-1 mode, in which single bytes
in the range from 0240 to 0377 are characters, and
interpreted according to the ISO Latin-1 character set. This is a
multibyte code which has two alternative shift states ("Japanese mode"
and "Latin-1 mode"), and two shift sequences that specify particular
shift states.
When the multibyte character code in use has shift states, then
mblen, mbtowc and wctomb must maintain and update
the current shift state as they scan the string. To make this work
properly, you must follow these rules:
mblen (NULL,
0). This initializes the shift state to its standard initial value.
Here is an example of using mblen following these rules:
void
scan_string (char *s)
{
int length = strlen (s);
/* Initialize shift state. */
mblen (NULL, 0);
while (1)
{
int thischar = mblen (s, length);
/* Deal with end of string and invalid characters. */
if (thischar == 0)
break;
if (thischar == -1)
{
error ("invalid multibyte character");
break;
}
/* Advance past this character. */
s += thischar;
length -= thischar;
}
}
The functions mblen, mbtowc and wctomb are not
reentrant when using a multibyte code that uses a shift state. However,
no other library functions call these functions, so you don't have to
worry that the shift state will be changed mysteriously.
Different countries and cultures have varying conventions for how to communicate. These conventions range from very simple ones, such as the format for representing dates and times, to very complex ones, such as the language spoken.
Internationalization of software means programming it to be able to adapt to the user's favorite conventions. In ANSI C, internationalization works by means of locales. Each locale specifies a collection of conventions, one convention for each purpose. The user chooses a set of conventions by specifying a locale (via environment variables).
All programs inherit the chosen locale as part of their environment. Provided the programs are written to obey the choice of locale, they will follow the conventions preferred by the user.
Each locale specifies conventions for several purposes, including the following:
Some aspects of adapting to the specified locale are handled
automatically by the library subroutines. For example, all your program
needs to do in order to use the collating sequence of the chosen locale
is to use strcoll or strxfrm to compare strings.
Other aspects of locales are beyond the comprehension of the library. For example, the library can't automatically translate your program's output messages into other languages. The only way you can support output in the user's favorite language is to program this more or less by hand. (Eventually, we hope to provide facilities to make this easier.)
This chapter discusses the mechanism by which you can modify the current locale. The effects of the current locale on specific library functions are discussed in more detail in the descriptions of those functions.
The simplest way for the user to choose a locale is to set the
environment variable LANG. This specifies a single locale to use
for all purposes. For example, a user could specify a hypothetical
locale named `espana-castellano' to use the standard conventions of
most of Spain.
The set of locales supported depends on the operating system you are using, and so do their names. We can't make any promises about what locales will exist, except for one standard locale called `C' or `POSIX'.
A user also has the option of specifying different locales for different purposes--in effect, choosing a mixture of two locales.
For example, the user might specify the locale `espana-castellano' for most purposes, but specify the locale `usa-english' for currency formatting. This might make sense if the user is a Spanish-speaking American, working in Spanish, but representing monetary amounts in US dollars.
Note that both locales `espana-castellano' and `usa-english', like all locales, would include conventions for all of the purposes to which locales apply. However, the user can choose to use each locale for a particular subset of those purposes.
The purposes that locales serve are grouped into categories, so
that a user or a program can choose the locale for each category
independently. Here is a table of categories; each name is both an
environment variable that a user can set, and a macro name that you can
use as an argument to setlocale.
LC_COLLATE
strcoll
and strxfrm); see section Collation Functions.
LC_CTYPE
LC_MONETARY
LC_NUMERIC
LC_TIME
LC_ALL
setlocale to set a single locale for all purposes.
LANG
A C program inherits its locale environment variables when it starts up.
This happens automatically. However, these variables do not
automatically control the locale used by the library functions, because
ANSI C says that all programs start by default in the standard `C'
locale. To use the locales specified by the environment, you must call
setlocale. Call it as follows:
setlocale (LC_ALL, "");
to select a locale based on the appropriate environment variables.
You can also use setlocale to specify a particular locale, for
general use or for a specific category.
The symbols in this section are defined in the header file `locale.h'.
Function: char * setlocale (int category, const char *locale)
The function setlocale sets the current locale for
category category to locale.
If category is LC_ALL, this specifies the locale for all
purposes. The other possible values of category specify an
individual purpose (see section Categories of Activities that Locales Affect).
You can also use this function to find out the current locale by passing
a null pointer as the locale argument. In this case,
setlocale returns a string that is the name of the locale
currently selected for category category.
The string returned by setlocale can be overwritten by subsequent
calls, so you should make a copy of the string (see section Copying and Concatenation) if you want to save it past any further calls to
setlocale. (The standard library is guaranteed never to call
setlocale itself.)
You should not modify the string returned by setlocale.
It might be the same string that was passed as an argument in a
previous call to setlocale.
When you read the current locale for category LC_ALL, the value
encodes the entire combination of selected locales for all categories.
In this case, the value is not just a single locale name. In fact, we
don't make any promises about what it looks like. But if you specify
the same "locale name" with LC_ALL in a subsequent call to
setlocale, it restores the same combination of locale selections.
When the locale argument is not a null pointer, the string returned
by setlocale reflects the newly modified locale.
If you specify an empty string for locale, this means to read the appropriate environment variable and use its value to select the locale for category.
If you specify an invalid locale name, setlocale returns a null
pointer and leaves the current locale unchanged.
Here is an example showing how you might use setlocale to
temporarily switch to a new locale.
#include <stddef.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>
void
with_other_locale (char *new_locale,
void (*subroutine) (int),
int argument)
{
char *old_locale, *saved_locale;
/* Get the name of the current locale. */
old_locale = setlocale (LC_ALL, NULL);
/* Copy the name so it won't be clobbered by setlocale. */
saved_locale = strdup (old_locale);
if (old_locale == NULL)
fatal ("Out of memory");
/* Now change the locale and do some stuff with it. */
setlocale (LC_ALL, new_locale);
(*subroutine) (argument);
/* Restore the original locale. */
setlocale (LC_ALL, saved_locale);
free (saved_locale);
}
Portability Note: Some ANSI C systems may define additional locale categories. For portability, assume that any symbol beginning with `LC_' might be defined in `locale.h'.
The only locale names you can count on finding on all operating systems are these three standard ones:
"C"
"POSIX"
""
Defining and installing named locales is normally a responsibility of the system administrator at your site (or the person who installed the GNU C library). Some systems may allow users to create locales, but we don't discuss that here.
If your program needs to use something other than the `C' locale, it will be more portable if you use the whatever locale the user specifies with the environment, rather than trying to specify some non-standard locale explicitly by name. Remember, different machines might have different sets of locales installed.
When you want to format a number or a currency amount using the
conventions of the current locale, you can use the function
localeconv to get the data on how to do it. The function
localeconv is declared in the header file `locale.h'.
Function: struct lconv * localeconv (void)
The localeconv function returns a pointer to a structure whose
components contain information about how numeric and monetary values
should be formatted in the current locale.
You shouldn't modify the structure or its contents. The structure might
be overwritten by subsequent calls to localeconv, or by calls to
setlocale, but no other function in the library overwrites this
value.
This is the data type of the value returned by localeconv.
If a member of the structure struct lconv has type char,
and the value is CHAR_MAX, it means that the current locale has
no value for that parameter.
These are the standard members of struct lconv; there may be
others.
char *decimal_point
char *mon_decimal_point
decimal_point is ".", and the value of
mon_decimal_point is "".
char *thousands_sep
char *mon_thousands_sep
"" (the empty string).
char *grouping
char *mon_grouping
grouping applies to non-monetary quantities
and mon_grouping applies to monetary quantities. Use either
thousands_sep or mon_thousands_sep to separate the digit
groups.
Each string is made up of decimal numbers separated by semicolons. Successive numbers (from left to right) give the sizes of successive groups (from right to left, starting at the decimal point). The last number in the string is used over and over for all the remaining groups.
If the last integer is -1, it means that there is no more
grouping--or, put another way, any remaining digits form one large
group without separators.
For example, if grouping is "4;3;2", the number
123456787654321 should be grouped into `12', `34',
`56', `78', `765', `4321'. This uses a group of 4
digits at the end, preceded by a group of 3 digits, preceded by groups
of 2 digits (as many as needed). With a separator of `,', the
number would be printed as `12,34,56,78,765,4321'.
A value of "3" indicates repeated groups of three digits, as
normally used in the U.S.
In the standard `C' locale, both grouping and
mon_grouping have a value of "". This value specifies no
grouping at all.
char int_frac_digits
char frac_digits
In the standard `C' locale, both of these members have the value
CHAR_MAX, meaning "unspecified". The ANSI standard doesn't say
what to do when you find this the value; we recommend printing no
fractional digits. (This locale also specifies the empty string for
mon_decimal_point, so printing any fractional digits would be
confusing!)
These members of the struct lconv structure specify how to print
the symbol to identify a monetary value--the international analog of
`$' for US dollars.
Each country has two standard currency symbols. The local currency symbol is used commonly within the country, while the international currency symbol is used internationally to refer to that country's currency when it is necessary to indicate the country unambiguously.
For example, many countries use the dollar as their monetary unit, and when dealing with international currencies it's important to specify that one is dealing with (say) Canadian dollars instead of U.S. dollars or Australian dollars. But when the context is known to be Canada, there is no need to make this explicit--dollar amounts are implicitly assumed to be in Canadian dollars.
char *currency_symbol
In the standard `C' locale, this member has a value of ""
(the empty string), meaning "unspecified". The ANSI standard doesn't
say what to do when you find this value; we recommend you simply print
the empty string as you would print any other string found in the
appropriate member.
char *int_curr_symbol
The value of int_curr_symbol should normally consist of a
three-letter abbreviation determined by the international standard
ISO 4217 Codes for the Representation of Currency and Funds,
followed by a one-character separator (often a space).
In the standard `C' locale, this member has a value of ""
(the empty string), meaning "unspecified". We recommend you simply
print the empty string as you would print any other string found in the
appropriate member.
char p_cs_precedes
char n_cs_precedes
1 if the currency_symbol string should
precede the value of a monetary amount, or 0 if the string should
follow the value. The p_cs_precedes member applies to positive
amounts (or zero), and the n_cs_precedes member applies to
negative amounts.
In the standard `C' locale, both of these members have a value of
CHAR_MAX, meaning "unspecified". The ANSI standard doesn't say
what to do when you find this value, but we recommend printing the
currency symbol before the amount. That's right for most countries.
In other words, treat all nonzero values alike in these members.
The POSIX standard says that these two members apply to the
int_curr_symbol as well as the currency_symbol. The ANSI
C standard seems to imply that they should apply only to the
currency_symbol---so the int_curr_symbol should always
preceed the amount.
We can only guess which of these (if either) matches the usual conventions for printing international currency symbols. Our guess is that they should always preceed the amount. If we find out a reliable answer, we will put it here.
char p_sep_by_space
char n_sep_by_space
1 if a space should appear between the
currency_symbol string and the amount, or 0 if no space
should appear. The p_sep_by_space member applies to positive
amounts (or zero), and the n_sep_by_space member applies to
negative amounts.
In the standard `C' locale, both of these members have a value of
CHAR_MAX, meaning "unspecified". The ANSI standard doesn't say
what you should do when you find this value; we suggest you treat it as
one (print a space). In other words, treat all nonzero values alike in
these members.
These members apply only to currency_symbol. When you use
int_curr_symbol, you never print an additional space, because
int_curr_symbol itself contains the appropriate separator.
The POSIX standard says that these two members apply to the
int_curr_symbol as well as the currency_symbol. But an
example in the ANSI C standard clearly implies that they should apply
only to the currency_symbol---that the int_curr_symbol
contains any appropriate separator, so you should never print an
additional space.
Based on what we know now, we recommend you ignore these members when printing international currency symbols, and print no extra space.
These members of the struct lconv structure specify how to print
the sign (if any) in a monetary value.
char *positive_sign
char *negative_sign
In the standard `C' locale, both of these members have a value of
"" (the empty string), meaning "unspecified".
The ANSI standard doesn't say what to do when you find this value; we
recommend printing positive_sign as you find it, even if it is
empty. For a negative value, print negative_sign as you find it
unless both it and positive_sign are empty, in which case print
`-' instead. (Failing to indicate the sign at all seems rather
unreasonable.)
char p_sign_posn
char n_sign_posn
positive_sign or negative_sign.) The possible values are
as follows:
0
1
2
3
4
CHAR_MAX
The ANSI standard doesn't say what you should do when the value is
CHAR_MAX. We recommend you print the sign after the currency
symbol.
It is not clear whether you should let these members apply to the international currency format or not. POSIX says you should, but intuition plus the examples in the ANSI C standard suggest you should not. We hope that someone who knows well the conventions for formatting monetary quantities will tell us what we should recommend.
This chapter describes functions for searching and sorting arrays of arbitrary objects. You pass the appropriate comparison function to be applied as an argument, along with the size of the objects in the array and the total number of elements.
In order to use the sorted array library functions, you have to describe how to compare the elements of the array.
To do this, you supply a comparison function to compare two elements of
the array. The library will call this function, passing as arguments
pointers to two array elements to be compared. Your comparison function
should return a value the way strcmp (see section String/Array Comparison) does: negative if the first argument is "less" than the
second, zero if they are "equal", and positive if the first argument
is "greater".
Here is an example of a comparison function which works with an array of
numbers of type double:
int
compare_doubles (const double *a, const double *b)
{
double temp = *a - *b;
if (temp > 0)
return 1;
else if (temp < 0)
return -1;
else
return 0;
}
The header file `stdlib.h' defines a name for the data type of comparison functions. This is a GNU extension and thus defined only if you request the GNU extensions.
int comparison_fn_t (const void *, const void *);
To search a sorted array for an element matching the key, use the
bsearch function. The prototype for this function is in
the header file `stdlib.h'.
Function: void * bsearch (const void *key, const void *array, size_t count, size_t size, comparison_fn_t compare)
The bsearch function searches the sorted array array for an object
that is equivalent to key. The array contains count elements,
each of which is of size size.
The compare function is used to perform the comparison. This function is called with two pointer arguments and should return an integer less than, equal to, or greater than zero corresponding to whether its first argument is considered less than, equal to, or greater than its second argument. The elements of the array must already be sorted in ascending order according to this comparison function.
The return value is a pointer to the matching array element, or a null pointer if no match is found. If the array contains more than one element that matches, the one that is returned is unspecified.
This function derives its name from the fact that it is implemented using the binary search.
To sort an array using an arbitrary comparison function, use the
qsort function. The prototype for this function is in
`stdlib.h'.
Function: void qsort (void *array, size_t count, size_t size, comparison_fn_t compare)
The qsort function sorts the array array. The array contains count elements, each of which is of size size.
The compare function is used to perform the comparison on the array elements. This function is called with two pointer arguments and should return an integer less than, equal to, or greater than zero corresponding to whether its first argument is considered less than, equal to, or greater than its second argument.
Warning: If two objects compare as equal, their order after sorting is unpredictable. That is to say, the sorting is not stable. This can make a difference when the comparison considers only part of the elements. Two elements with the same sort key may differ in other respects.
If you want the effect of a stable sort, you can get this result by writing the comparison function so that, lacking other reason distinguish between two elements, it compares them by their addresses.
Here is a simple example of sorting an array of doubles in numerical order, using the comparison function defined above (see section Defining the Comparison Function):
{
double *array;
int size;
...
qsort (array, size, sizeof (double), compare_doubles);
}
The qsort function derives its name from the fact that it was
originally implemented using the algorithm "quick sort".
Here is an example showing the use of qsort and bsearch
with an array of structures. The objects in the array are sorted
by comparing their name fields with the strcmp function.
Then, we can look up individual objects based on their names.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* Define an array of critters to sort. */
struct critter
{
char *name;
char *species;
};
struct critter muppets[]=
{
{"Kermit", "frog"},
{"Piggy", "pig"},
{"Gonzo", "whatever"},
{"Fozzie", "bear"},
{"Sam", "eagle"},
{"Robin", "frog"},
{"Animal", "animal"},
{"Camilla", "chicken"},
{"Sweetums", "monster"},
{"Dr. Strangepork", "pig"},
{"Link Hogthrob", "pig"},
{"Zoot", "human"},
{"Dr. Bunsen Honeydew", "human"},
{"Beaker", "human"},
{"Swedish Chef", "human"}};
int count = sizeof (muppets) / sizeof (struct critter);
/* This is the comparison function used for sorting and searching. */
int
critter_cmp (const struct critter *c1, const struct critter *c2)
{
return strcmp (c1->name, c2->name);
}
/* Print information about a critter. */
void
print_critter (const struct critter *c)
{
printf ("%s, the %s\n", c->name, c->species);
}
/* Do the lookup into the sorted array. */
void
find_critter (char *name)
{
struct critter target, *result;
target.name = name;
result = bsearch (&target, muppets, count, sizeof (struct critter),
critter_cmp);
if (result)
print_critter (result);
else
printf ("Couldn't find %s.\n", name);
}
/* Main program. */
int
main (void)
{
int i;
for (i = 0; i < count; i++)
print_critter (&muppets[i]);
printf ("\n");
qsort (muppets, count, sizeof (struct critter), critter_cmp);
for (i = 0; i < count; i++)
print_critter (&muppets[i]);
printf ("\n");
find_critter ("Kermit");
find_critter ("Gonzo");
find_critter ("Janice");
return 0;
}
The output from this program looks like:
Animal, the animal Beaker, the human Camilla, the chicken Dr. Bunsen Honeydew, the human Dr. Strangepork, the pig Fozzie, the bear Gonzo, the whatever Kermit, the frog Link Hogthrob, the pig Piggy, the pig Robin, the frog Sam, the eagle Swedish Chef, the human Sweetums, the monster Zoot, the human Kermit, the frog Gonzo, the whatever Couldn't find Janice.
The GNU C Library provides pattern matching facilities for two kinds of patterns: regular expressions and file-name wildcards.
This section describes how to match a wildcard pattern against a particular string. The result is a yes or no answer: does the string fit the pattern or not. The symbols described here are all declared in `fnmatch.h'.
Function: int fnmatch (const char *pattern, const char *string, int flags)
This function tests whether the string string matches the pattern
pattern. It returns 0 if they do match; otherwise, it
returns the nonzero value FNM_NOMATCH. The arguments
pattern and string are both strings.
The argument flags is a combination of flag bits that alter the details of matching. See below for a list of the defined flags.
In the GNU C Library, fnmatch cannot experience an "error"---it
always returns an answer for whether the match succeeds. However, other
implementations of fnmatch might sometimes report "errors".
They would do so by returning nonzero values that are not equal to
FNM_NOMATCH.
These are the available flags for the flags argument:
FNM_FILE_NAME
FNM_PATHNAME
FNM_FILE_NAME; it comes from POSIX.2. We
don't recommend this name because we don't use the term "pathname" for
file names.
FNM_PERIOD
If you set both FNM_PERIOD and FNM_FILE_NAME, then the
special treatment applies to `.' following `/' as well as
to `.' at the beginning of string.
FNM_NOESCAPE
If you use FNM_NOESCAPE, then `\' is an ordinary character.
FNM_LEADING_DIR
If this flag is set, either `foo*' or `foobar' as a pattern would match the string `foobar/frobozz'.
FNM_CASEFOLD
The archetypal use of wildcards is for matching against the files in a directory, and making a list of all the matches. This is called globbing.
You could do this using fnmatch, by reading the directory entries
one by one and testing each one with fnmatch. But that would be
slow (and complex, since you would have to handle subdirectories by
hand).
The library provides a function glob to make this particular use
of wildcards convenient. glob and the other symbols in this
section are declared in `glob.h'.
glob
The result of globbing is a vector of file names (strings). To return
this vector, glob uses a special data type, glob_t, which
is a structure. You pass glob the address of the structure, and
it fills in the structure's fields to tell you about the results.
This data type holds a pointer to a word vector. More precisely, it records both the address of the word vector and its size.
gl_pathc
gl_pathv
char **.
gl_offs
gl_pathv field. Unlike the other fields, this
is always an input to glob, rather than an output from it.
If you use a nonzero offset, then that many elements at the beginning of
the vector are left empty. (The glob function fills them with
null pointers.)
The gl_offs field is meaningful only if you use the
GLOB_DOOFFS flag. Otherwise, the offset is always zero
regardless of what is in this field, and the first real element comes at
the beginning of the vector.
Function: int glob (const char *pattern, int flags, int (*errfunc) (const char *filename, int error-code), glob_t *vector_ptr)
The function glob does globbing using the pattern pattern
in the current directory. It puts the result in a newly allocated
vector, and stores the size and address of this vector into
*vector-ptr. The argument flags is a combination of
bit flags; see section Flags for Globbing, for details of the flags.
The result of globbing is a sequence of file names. The function
glob allocates a string for each resulting word, then
allocates a vector of type char ** to store the addresses of
these strings. The last element of the vector is a null pointer.
This vector is called the word vector.
To return this vector, glob stores both its address and its
length (number of elements, not counting the terminating null pointer)
into *vector-ptr.
Normally, glob sorts the file names alphabetically before
returning them. You can turn this off with the flag GLOB_NOSORT
if you want to get the information as fast as possible. Usually it's
a good idea to let glob sort them--if you process the files in
alphabetical order, the users will have a feel for the rate of progress
that your application is making.
If glob succeeds, it returns 0. Otherwise, it returns one
of these error codes:
GLOB_ABORTED
GLOB_ERR or your specified errfunc returned a nonzero
value.
GLOB_NOMATCH
GLOB_NOCHECK flag, then you never get this error code, because
that flag tells glob to pretend that the pattern matched
at least one file.
GLOB_NOSPACE
In the event of an error, glob stores information in
*vector-ptr about all the matches it has found so far.
This section describes the flags that you can specify in the
flags argument to glob. Choose the flags you want,
and combine them with the C operator |.
GLOB_APPEND
glob. This way you can effectively expand
several words as if they were concatenated with spaces between them.
In order for appending to work, you must not modify the contents of the
word vector structure between calls to glob. And, if you set
GLOB_DOOFFS in the first call to glob, you must also
set it when you append to the results.
GLOB_DOOFFS
gl_offs field says how many slots to leave.
The blank slots contain null pointers.
GLOB_ERR
glob tries its best to keep
on going despite any errors, reading whatever directories it can.
You can exercise even more control than this by specifying an error-handler
function errfunc when you call glob. If errfunc is
nonzero, then glob doesn't give up right away when it can't read
a directory; instead, it calls errfunc with two arguments, like
this:
(*errfunc) (filename, error-code)
The argument filename is the name of the directory that
glob couldn't open or couldn't read, and error-code is the
errno value that was reported to glob.
If the error handler function returns nonzero, then glob gives up
right away. Otherwise, it continues.
GLOB_MARK
GLOB_NOCHECK
glob returns that there were no
matches.)
GLOB_NOSORT
GLOB_NOESCAPE
If you use GLOB_NOESCAPE, then `\' is an ordinary character.
glob does its work by calling the function fnmatch
repeatedly. It handles the flag GLOB_NOESCAPE by turning on the
FNM_NOESCAPE flag in calls to fnmatch.
The GNU C library supports two interfaces for matching regular expressions. One is the standard POSIX.2 interface, and the other is what the GNU system has had for many years.
Both interfaces are declared in the header file `regex.h'.
If you define _GNU_SOURCE, then the GNU functions, structures
and constants are declared. Otherwise, only the POSIX names are
declared.
Before you can actually match a regular expression, you must compile it. This is not true compilation--it produces a special data structure, not machine instructions. But it is like ordinary compilation in that its purpose is to enable you to "execute" the pattern fast. (See section Matching a Compiled POSIX Regular Expression, for how to use the compiled regular expression for matching.)
There is a special data type for compiled regular expressions:
This type of object holds a compiled regular expression. It is actually a structure. It has just one field that your programs should look at:
re_nsub
There are several other fields, but we don't describe them here, because only the functions in the library should use them.
After you create a regex_t object, you can compile a regular
expression into it by calling regcomp.
Function: int regcomp (regex_t *compiled, const char *pattern, int cflags)
The function regcomp "compiles" a regular expression into a
data structure that you can use with regexec to match against a
string. The compiled regular expression format is designed for
efficient matching. regcomp stores it into *compiled.
It's up to you to allocate an object of type regex_t and pass its
address to regcomp.
The argument cflags lets you specify various options that control the syntax and semantics of regular expressions. See section Flags for POSIX Regular Expressions.
If you use the flag REG_NOSUB, then regcomp omits from
the compiled regular expression the information necessary to record
how subexpressions actually match. In this case, you might as well
pass 0 for the matchptr and nmatch arguments when
you call regexec.
If you don't use REG_NOSUB, then the compiled regular expression
does have the capacity to record how subexpressions match. Also,
regcomp tells you how many subexpressions pattern has, by
storing the number in compiled->re_nsub. You can use that
value to decide how long an array to allocate to hold information about
subexpression matches.
regcomp returns 0 if it succeeds in compiling the regular
expression; otherwise, it returns a nonzero error code (see the table
below). You can use regerror to produce an error message string
describing the reason for a nonzero value; see section POSIX Regexp Matching Cleanup.
Here are the possible nonzero values that regcomp can return:
REG_BADBR
REG_BADPAT
REG_BADRPT
REG_ECOLLATE
REG_ECTYPE
REG_EESCAPE
REG_ESUBREG
REG_EBRACK
REG_EPAREN
REG_EBRACE
REG_ERANGE
REG_ESPACE
regcomp or regexec ran out of memory.
These are the bit flags that you can use in the cflags operand when
compiling a regular expression with regcomp.
REG_EXTENDED
REG_ICASE
REG_NOSUB
REG_NEWLINE
Otherwise, newline acts like any other ordinary character.
Once you have compiled a regular expression, as described in section POSIX Regular Expression Compilation, you can match it against strings using
regexec. A match anywhere inside the string counts as success,
unless the regular expression contains anchor characters (`^' or
`$').
Function: int regexec (regex_t *compiled, char *string, size_t nmatch, regmatch_t matchptr [], int eflags)
This function tries to match the compiled regular expression
*compiled against string.
regexec returns 0 if the regular expression matches;
otherwise, it returns a nonzero value. See the table below for
what nonzero values mean. You can use regerror to produce an
error message string describing the reason for a nonzero value;
see section POSIX Regexp Matching Cleanup.
The argument eflags is a word of bit flags that enable various options.
If you want to get information about what part of string actually
matched the regular expression or its subexpressions, use the arguments
matchptr and nmatch. Otherwise, pass 0 for
nmatch, and NULL for matchptr. See section Subexpressions Match Results.
You must match the regular expression with the same set of current locales that were in effect when you compiled the regular expression.
The function regexec accepts the following flags in the
eflags argument:
REG_NOTBOL
REG_NOTEOL
Here are the possible nonzero values that regexec can return:
REG_NOMATCH
REG_ESPACE
regcomp or regexec ran out of memory.
When regexec matches parenthetical subexpressions of
pattern, it records which parts of string they match. It
returns that information by storing the offsets into an array whose
elements are structures of type regmatch_t. The first element of
the array records the part of the string that matched the entire regular
expression. Each other element of the array records the beginning and
end of the part that matched a single parenthetical subexpression.
This is the data type of the matcharray array that you pass to
regexec. It containes two structure fields, as follows:
rm_so
rm_eo
regoff_t is an alias for another signed integer type.
The fields of regmatch_t have type regoff_t.
The regmatch_t elements correspond to subexpressions
positionally; the first element records where the first subexpression
matched, the second element records the second subexpression, and so on.
The order of the subexpressions is the order in which they begin.
When you call regexec, you specify how long the matchptr
array is, with the nmatch argument. This tells regexec how
many elements to store. If the actual regular expression has more than
nmatch subexpressions, then you won't get offset information about
the rest of them. But this doesn't alter whether the pattern matches a
particular string or not.
If you don't want regexec to return any information about where
the subexpressions matched, you can either supply 0 for
nmatch, or use the flag REG_NOSUB when you compile the
pattern with regcomp.
Sometimes a subexpression matches a substring of no characters. This
happens when `f\(o*\)' matches the string `fum'. (It really
matches just the `f'.) In this case, both of the offsets identify
the point in the string where the null substring was found. In this
example, the offsets are both 1.
Sometimes the entire regular expression can match without using some of
its subexpressions at all--for example, when `ba\(na\)*' matches the
string `ba', the parenthetical subexpression is not used. When
this happens, regexec stores -1 in both fields of the
element for that subexpression.
Sometimes matching the entire regular expression can match a particular
subexpression more than once--for example, when `ba\(na\)*'
matches the string `bananana', the parenthetical subexpression
matches three times. When this happens, regexec usually stores
the offsets of the last part of the string that matched the
subexpression. In the case of `bananana', these offsets are
6 and 8.
But the last match is not always the one that is chosen. It's more
accurate to say that the last opportunity to match is the one
that takes precedence. What this means is that when one subexpression
appears within another, then the results reported for the inner
subexpression reflect whatever happened on the last match of the outer
subexpression. For an example, consider `\(ba\(na\)*s \)' matching
the string `bananas bas '. The last time the inner expression
actually matches is near the end of the first word. But it is
considered again in the second word, and fails to match there.
regexec reports nonuse of the "na" subexpression.
Another place where this rule applies is when `\(ba\(na\)*s
\|nefer\(ti\)* \)*' matches `bananas nefertiti'. The "na"
subexpression does match in the first word, but it doesn't match in the
second word because the other alternative is used there. Once again,
the second repetition of the outer subexpression overrides the first,
and within that second repetition, the "na" subexpression is not used.
So regexec reports nonuse of the "na" subexpression.
When you are finished using a compiled regular expression, you can
free the storage it uses by calling regfree.
Function: void regfree (regex_t *compiled)
Calling regfree frees all the storage that *compiled
points to. This includes various internal fields of the regex_t
structure that aren't documented in this manual.
regfree does not free the object *compiled itself.
You should always free the space in a regex_t structure with
regfree before using the structure to compile another regular
expression.
When regcomp or regexec reports an error, you can use
the function regerror to turn it into an error message string.
Function: size_t regerror (int errcode, regex_t *compiled, char *buffer, size_t length)
This function produces an error message string for the error code
errcode, and stores the string in length bytes of memory
starting at buffer. For the compiled argument, supply the
same compiled regular expression structure that regcomp or
regexec was working with when it got the error. Alternatively,
you can supply NULL for compiled; you will still get a
meaningful error message, but it might not be as detailed.
If the error message can't fit in length bytes (including a
terminating null character), then regerror truncates it.
The string that regerror stores is always null-terminated
even if it has been truncated.
The return value of regerror is the minimum length needed to
store the entire error message. If this is less than length, then
the error message was not truncated, and you can use it. Otherwise, you
should call regerror again with a larger buffer.
char *get_regerror (int errcode, regex_t *compiled)
{
size_t length = regerror (errcode, compiled, NULL, 0);
char *buffer = xmalloc (length);
(void) regerror (errcode, compiled, buffer, length);
return buffer;
}
Word expansion means the process of splitting a string into words and substituting for variables, commands, and wildcards just as the shell does.
For example, when you write `ls -l foo.c', this string is split into three separate words---`ls', `-l' and `foo.c'. This is the most basic function of word expansion.
When you write `ls *.c', this can become many words, because the word `*.c' can be replaced with any number of file names. This is called wildcard expansion, and it is also a part of word expansion.
When you use `echo $PATH' to print your path, you are taking advantage of variable substitution, which is also part of word expansion.
Ordinary programs can perform word expansion just like the shell by
calling the library function wordexp.
When word expansion is applied to a sequence of words, it performs the following transformations in the order shown here:
For the details of these transformations, and how to write the constructs that use them, see The BASH Manual (to appear).
wordexpAll the functions, constants and data types for word expansion are declared in the header file `wordexp.h'.
Word expansion produces a vector of words (strings). To return this
vector, wordexp uses a special data type, wordexp_t, which
is a structure. You pass wordexp the address of the structure,
and it fills in the structure's fields to tell you about the results.
This data type holds a pointer to a word vector. More precisely, it records both the address of the word vector and its size.
we_wordc
we_wordv
char **.
we_offs
we_wordv field. Unlike the other fields, this
is always an input to wordexp, rather than an output from it.
If you use a nonzero offset, then that many elements at the beginning of
the vector are left empty. (The wordexp function fills them with
null pointers.)
The we_offs field is meaningful only if you use the
WRDE_DOOFFS flag. Otherwise, the offset is always zero
regardless of what is in this field, and the first real element comes at
the beginning of the vector.
Function: int wordexp (const char *words, wordexp_t *word-vector-ptr, int flags)
Perform word expansion on the string words, putting the result in
a newly allocated vector, and store the size and address of this vector
into *word-vector-ptr. The argument flags is a
combination of bit flags; see section Flags for Word Expansion, for details of
the flags.
You shouldn't use any of the characters `|&;<>' in the string
words unless they are quoted; likewise for newline. If you use
these characters unquoted, you will get the WRDE_BADCHAR error
code. Don't use parentheses or braces unless they are quoted or part of
a word expansion construct. If you use quotation characters `'"`',
they should come in pairs that balance.
The results of word expansion are a sequence of words. The function
wordexp allocates a string for each resulting word, then
allocates a vector of type char ** to store the addresses of
these strings. The last element of the vector is a null pointer.
This vector is called the word vector.
To return this vector, wordexp stores both its address and its
length (number of elements, not counting the terminating null pointer)
into *word-vector-ptr.
If wordexp succeeds, it returns 0. Otherwise, it returns one
of these error codes:
WRDE_BADCHAR
WRDE_BADVAL
WRDE_UNDEF to forbid such references.
WRDE_CMDSUB
WRDE_NOCMD to forbid command substitution.
WRDE_NOSPACE
wordexp can store part of the results--as much as it could
allocate room for.
WRDE_SYNTAX
Function: void wordfree (wordexp_t *word-vector-ptr)
Free the storage used for the word-strings and vector that
*word-vector-ptr points to. This does not free the
structure *word-vector-ptr itself--only the other
data it points to.
This section describes the flags that you can specify in the
flags argument to wordexp. Choose the flags you want,
and combine them with the C operator |.
WRDE_APPEND
wordexp. This way you can effectively expand
several words as if they were concatenated with spaces between them.
In order for appending to work, you must not modify the contents of the
word vector structure between calls to wordexp. And, if you set
WRDE_DOOFFS in the first call to wordexp, you must also
set it when you append to the results.
WRDE_DOOFFS
we_offs field says how many slots to leave.
The blank slots contain null pointers.
WRDE_NOCMD
WRDE_REUSE
wordexp.
Instead of allocating a new vector of words, this call to wordexp
will use the vector that already exists (making it larger if necessary).
WRDE_SHOWERR
wordexp gives these
commands a standard error stream that discards all output.
WRDE_UNDEF
wordexp Example
Here is an example of using wordexp to expand several strings
and use the results to run a shell command. It also shows the use of
WRDE_APPEND to concatenate the expansions and of wordfree
to free the space allocated by wordexp.
int
expand_and_execute (const char *program, const char *options)
{
wordexp_t result;
pid_t pid
int status, i;
/* Expand the string for the program to run. */
switch (wordexp (program, &result, 0))
{
case 0: /* Successful. */
break;
case WRDE_NOSPACE:
/* If the error was WRDE_NOSPACE,
then perhaps part of the result was allocated. */
wordfree (&result);
default: /* Some other error. */
return -1;
}
/* Expand the strings specified for the arguments. */
for (i = 0; args[i]; i++)
{
if (wordexp (options, &result, WRDE_APPEND))
{
wordfree (&result);
return -1;
}
}
pid = fork ();
if (pid == 0)
{
/* This is the child process. Execute the command. */
execv (result.we_wordv[0], result.we_wordv);
exit (EXIT_FAILURE);
}
else if (pid < 0)
/* The fork failed. Report failure. */
status = -1;
else
/* This is the parent process. Wait for the child to complete. */
if (waitpid (pid, &status, 0) != pid)
status = -1;
wordfree (&result);
return status;
}
In practice, since wordexp is executed by running a subshell, it
would be faster to do this by concatenating the strings with spaces
between them and running that as a shell command using `sh -c'.
Most programs need to do either input (reading data) or output (writing data), or most frequently both, in order to do anything useful. The GNU C library provides such a large selection of input and output functions that the hardest part is often deciding which function is most appropriate!
This chapter introduces concepts and terminology relating to input and output. Other chapters relating to the GNU I/O facilities are:
Before you can read or write the contents of a file, you must establish a connection or communications channel to the file. This process is called opening the file. You can open a file for reading, writing, or both.
The connection to an open file is represented either as a stream or as a file descriptor. You pass this as an argument to the functions that do the actual read or write operations, to tell them which file to operate on. Certain functions expect streams, and others are designed to operate on file descriptors.
When you have finished reading to or writing from the file, you can terminate the connection by closing the file. Once you have closed a stream or file descriptor, you cannot do any more input or output operations on it.
When you want to do input or output to a file, you have a choice of two
basic mechanisms for representing the connection between your program
and the file: file descriptors and streams. File descriptors are
represented as objects of type int, while streams are represented
as FILE * objects.
File descriptors provide a primitive, low-level interface to input and output operations. Both file descriptors and streams can represent a connection to a device (such as a terminal), or a pipe or socket for communicating with another process, as well as a normal file. But, if you want to do control operations that are specific to a particular kind of device, you must use a file descriptor; there are no facilities to use streams in this way. You must also use file descriptors if your program needs to do input or output in special modes, such as nonblocking (or polled) input (see section File Status Flags).
Streams provide a higher-level interface, layered on top of the primitive file descriptor facilities. The stream interface treats all kinds of files pretty much alike--the sole exception being the three styles of buffering that you can choose (see section Stream Buffering).
The main advantage of using the stream interface is that the set of
functions for performing actual input and output operations (as opposed
to control operations) on streams is much richer and more powerful than
the corresponding facilities for file descriptors. The file descriptor
interface provides only simple functions for transferring blocks of
characters, but the stream interface also provides powerful formatted
input and output functions (printf and scanf) as well as
functions for character- and line-oriented input and output.
Since streams are implemented in terms of file descriptors, you can extract the file descriptor from a stream and perform low-level operations directly on the file descriptor. You can also initially open a connection as a file descriptor and then make a stream associated with that file descriptor.
In general, you should stick with using streams rather than file descriptors, unless there is some specific operation you want to do that can only be done on a file descriptor. If you are a beginning programmer and aren't sure what functions to use, we suggest that you concentrate on the formatted input functions (see section Formatted Input) and formatted output functions (see section Formatted Output).
If you are concerned about portability of your programs to systems other than GNU, you should also be aware that file descriptors are not as portable as streams. You can expect any system running ANSI C to support streams, but non-GNU systems may not support file descriptors at all, or may only implement a subset of the GNU functions that operate on file descriptors. Most of the file descriptor functions in the GNU library are included in the POSIX.1 standard, however.
One of the attributes of an open file is its file position that keeps track of where in the file the next character is to be read or written. In the GNU system, the file position is simply an integer representing the number of bytes from the beginning of the file.
The file position is normally set to the beginning of the file when it is opened, and each time a character is read or written, the file position is incremented. In other words, access to the file is normally sequential.
Ordinary files permit read or write operations at any position within
the file. Some other kinds of files may also permit this. Files which
do permit this are sometimes referred to as random-access files.
You can change the file position using the fseek function on a
stream (see section File Positioning) or the lseek function on a file
descriptor (see section Input and Output Primitives). If you try to change the file
position on a file that doesn't support random access, you get an error.
Streams and descriptors that are opened for append access are treated specially for output: output to such files is always appended sequentially to the end of the file, regardless of the file position. But, the file position is still used to control where in the file reading is done.
If you think about it, you'll realize that several programs can read a given file at the same time. In order for each program to be able to read the file at its own pace, each program must have its own file pointer, which is not affected by anything the other programs do.
In fact, each opening of a file creates a separate file position. Thus, if you open a file twice even in the same program, you get two streams or descriptors with independent file positions.
By contrast, if you open a descriptor and then duplicate it to get another descriptor, these two descriptors share the same file position: changing the file position of one descriptor will affect the other.
In order to open a connection to a file, or to perform other operations such as deleting a file, you need some way to refer to the file. Nearly all files have names that are strings--even files which are actually devices such as tape drives or terminals. These strings are called file names. You specify the file name to say which file you want to open or operate on.
This section describes the conventions for file names and how the operating system works with them.
In order to understand the syntax of file names, you need to understand how the file system is organized into a hierarchy of directories.
A directory is a file that contains information to associate other files with names; these associations are called links or directory entries. Sometimes, people speak of "files in a directory", but in reality, a directory only contains pointers to files, not the files themselves.
The name of a file contained in a directory entry is called a file name component. In general, a file name consists of a sequence of one or more such components, separated by the slash character (`/'). A file name which is just one component names a file with respect to its directory. A file name with multiple components names a directory, and then a file in that directory, and so on.
Some other documents, such as the POSIX standard, use the term pathname for what we call a file name, and either filename or pathname component for what this manual calls a file name component. We don't use this terminology because a "path" is something completely different (a list of directories to search), and we think that "pathname" used for something else will confuse users. We always use "file name" and "file name component" (or sometimes just "component", where the context is obvious) in GNU documentation.
You can find more detailed information about operations on directories in section File System Interface.
A file name consists of file name components separated by slash (`/') characters. On the systems that that GNU library supports, multiple successive `/' characters are equivalent to a single `/' character.
The process of determining what file a file name refers to is called file name resolution. This is performed by examining the components that make up a file name in left-to-right order, and locating each successive component in the directory named by the previous component. Of course, each of the files that are referenced as directories must actually exist, be directories instead of regular files, and have the appropriate permissions to be accessible by the process; otherwise the file name resolution fails.
If a file name begins with a `/', the first component in the file name is located in the root directory of the process. Such a file name is called an absolute file name.
Otherwise, the first component in the file name is located in the current working directory (see section Working Directory). This kind of file name is called a relative file name.
The file name components `.' ("dot") and `..' ("dot-dot") have special meanings. Every directory has entries for these file name components. The file name component `.' refers to the directory itself, while the file name component `..' refers to its parent directory (the directory that contains the link for the directory in question).
Here are some examples of file names:
A file name that names a directory may optionally end in a `/'. You can specify a file name of `/' to refer to the root directory, but the empty string is not a meaningful file name. If you want to refer to the current working directory, use a file name of `.' or `./'.
Unlike some other operating systems, the GNU system doesn't have any built-in support for file types (or extensions) or file versions as part of its file name syntax. Many programs and utilities use conventions for file names--for example, files containing C source code usually have names suffixed with `.c'---but there is nothing in the file system itself that enforces this kind of convention.
Functions that accept file name arguments usually detect these
errno error conditions relating to file name syntax. These
errors are referred to throughout this manual as the usual file
name syntax errors.
EACCES
ENAMETOOLONG
PATH_MAX, or when an individual file name component
has a length greater than NAME_MAX. See section Limits on File System Capacity.
In the GNU system, there is no imposed limit on overall file name length, but some file systems may place limits on the length of a component.
ENOENT
ENOTDIR
The rules for the syntax of file names discussed in section File Names, are the rules normally used by the GNU system and by other POSIX systems. However, other operating systems may use other conventions.
There are two reasons why it can be important for you to be aware of file name portability issues:
The ANSI C standard says very little about file name syntax, only that file names are strings. In addition to varying restrictions on the length of file names and what characters can validly appear in a file name, different operating systems use different conventions and syntax for concepts such as structured directories and file types or extensions. Some concepts such as file versions might be supported in some operating systems and not by others.
The POSIX.1 standard allows implementations to put additional restrictions on file name syntax, concerning what characters are permitted in file names and on the length of file name and file name component strings. However, in the GNU system, you do not need to worry about these restrictions; any character except the null character is permitted in a file name string, and there are no limits on the length of file name strings.
This chapter describes the functions for creating streams and performing input and output operations on them. As discussed in section Input/Output Overview, a stream is a fairly abstract, high-level concept representing a communications channel to a file, device, or process.
For historical reasons, the type of the C data structure that represents
a stream is called FILE rather than "stream". Since most of
the library functions deal with objects of type FILE *, sometimes
the term file pointer is also used to mean "stream". This leads
to unfortunate confusion over terminology in many books on C. This
manual, however, is careful to use the terms "file" and "stream"
only in the technical sense.
The FILE type is declared in the header file `stdio.h'.
This is the data type is used to represent stream objects. A
FILE object holds all of the internal state information about the
connection to the associated file, including such things as the file
position indicator and buffering information. Each stream also has
error and end-of-file status indicators that can be tested with the
ferror and feof functions; see section End-Of-File and Errors.
FILE objects are allocated and managed internally by the
input/output library functions. Don't try to create your own objects of
type FILE; let the library do it. Your programs should
deal only with pointers to these objects (that is, FILE * values)
rather than the objects themselves.
When the main function of your program is invoked, it already has
three predefined streams open and available for use. These represent
the "standard" input and output channels that have been established
for the process.
These streams are declared in the header file `stdio.h'.
The standard input stream, which is the normal source of input for the program.
The standard output stream, which is used for normal output from the program.
The standard error stream, which is used for error messages and diagnostics issued by the program.
In the GNU system, you can specify what files or processes correspond to these streams using the pipe and redirection facilities provided by the shell. (The primitives shells use to implement these facilities are described in section File System Interface.) Most other operating systems provide similar mechanisms, but the details of how to use them can vary.
It is probably not a good idea to close any of the standard streams.
But you can use freopen to get te effect of closing one and
reopening it. See section Opening Streams.
Opening a file with the fopen function creates a new stream and
establishes a connection between the stream and a file. This may
involve creating a new file.
Everything described in this section is declared in the header file `stdio.h'.
Function: FILE * fopen (const char *filename, const char *opentype)
The fopen function opens a stream for I/O to the file
filename, and returns a pointer to the stream.
The opentype argument is a string that controls how the file is opened and specifies attributes of the resulting stream. It must begin with one of the following sequences of characters:
As you can see, `+' requests a stream that can do both input and
output. When using such a stream, you must call fflush
(see section Stream Buffering) or a file positioning function such as
fseek (see section File Positioning) when switching from reading to
writing or vice versa. Otherwise, internal buffers might not be emptied
properly.
The GNU C library defines one additional character for use in
opentype: the character `x' insists on creating a new
file--if a file filename already exists, fopen fails
rather than opening it. This is equivalent to the O_EXCL option
to the open function (see section File Status Flags).
The character `b' in opentype has a standard meaning; it requests a binary stream rather than a text stream. But this makes no difference in POSIX systems (including the GNU system). If both `+' and `b' are specified, they can appear in either order. See section Text and Binary Streams.
Any other characters in opentype are simply ignored. They may be meaningful in other systems.
If the open fails, fopen returns a null pointer.
You can have multiple streams (or file descriptors) pointing to the same file open at the same time. If you do only input, this works straightforwardly, but you must be careful if any output streams are included. See section Precautions for Mixing Streams and Descriptors. This is equally true whether the streams are in one program (not usual) or in several programs (which can easily happen). It may be advantageous to use the file locking facilities to avoid simultaneous access. See section File Locks.
The value of this macro is an integer constant expression that
represents the minimum number of streams that the implementation
guarantees can be open simultaneously. The value of this constant is at
least eight, which includes the three standard streams stdin,
stdout, and stderr.
Function: FILE * freopen (const char *filename, const char *opentype, FILE *stream)
This function is like a combination of fclose and fopen.
It first closes the stream referred to by stream, ignoring any
errors that are detected in the process. (Because errors are ignored,
you should not use freopen on an output stream if you have
actually done any output using the stream.) Then the file named by
filename is opened with mode opentype as for fopen,
and associated with the same stream object stream.
If the operation fails, a null pointer is returned; otherwise,
freopen returns stream.
The main use of freopen is to connect a standard stream such as
stdir with a file of your own choice. This is useful in programs
in which use of a standard stream for certain purposes is hard-coded.
When a stream is closed with fclose, the connection between the
stream and the file is cancelled. After you have closed a stream, you
cannot perform any additional operations on it any more.
Function: int fclose (FILE *stream)
This function causes stream to be closed and the connection to
the corresponding file to be broken. Any buffered output is written
and any buffered input is discarded. The fclose function returns
a value of 0 if the file was closed successfully, and EOF
if an error was detected.
It is important to check for errors when you call fclose to close
an output stream, because real, everyday errors can be detected at this
time. For example, when fclose writes the remaining buffered
output, it might get an error because the disk is full. Even if you you
know the buffer is empty, errors can still occur when closing a file if
you are using NFS.
The function fclose is declared in `stdio.h'.
If the main function to your program returns, or if you call the
exit function (see section Normal Termination), all open streams are
automatically closed properly. If your program terminates in any other
manner, such as by calling the abort function (see section Aborting a Program) or from a fatal signal (see section Signal Handling), open streams
might not be closed properly. Buffered output may not be flushed and
files may not be complete. For more information on buffering of
streams, see section Stream Buffering.
This section describes functions for performing character- and
line-oriented output. Largely for historical compatibility, there are
several variants of these functions, but as a matter of style (and for
simplicity!) we suggest you stick with using fputc and
fputs, and perhaps putc and putchar.
These functions are declared in the header file `stdio.h'.
Function: int fputc (int c, FILE *stream)
The fputc function converts the character c to type
unsigned char, and writes it to the stream stream.
EOF is returned if a write error occurs; otherwise the
character c is returned.
Function: int putc (int c, FILE *stream)
This is just like fputc, except that most systems implement it as
a macro, making it faster. One consequence is that it may evaluate the
stream argument more than once.
The putchar function is equivalent to fputc with
stdout as the value of the stream argument.
Function: int fputs (const char *s, FILE *stream)
The function fputs writes the string s to the stream
stream. The terminating null character is not written.
This function does not add a newline character, either.
It outputs only the chars in the string.
This function returns EOF if a write error occurs, and otherwise
a non-negative value.
For example:
fputs ("Are ", stdout);
fputs ("you ", stdout);
fputs ("hungry?\n", stdout);
outputs the text `Are you hungry?' followed by a newline.
Function: int puts (const char *s)
The puts function writes the string s to the stream
stdout followed by a newline. The terminating null character of
the string is not written.
Function: int putw (int w, FILE *stream)
This function writes the word w (that is, an int) to
stream. It is provided for compatibility with SVID, but we
recommend you use fwrite instead (see section Block Input/Output).
This section describes functions for performing character- and
line-oriented input. Again, there are several variants of these
functions, some of which are considered obsolete stylistically. It's
suggested that you stick with fgetc, getline, and maybe
getc, getchar and fgets.
These functions are declared in the header file `stdio.h'.
Function: int fgetc (FILE *stream)
This function reads the next character as an unsigned char from
the stream stream and returns its value, converted to an
int. If an end-of-file condition or read error occurs,
EOF is returned instead.
Function: int getc (FILE *stream)
This is just like fgetc, except that it is permissible (and typical)
for it to be implemented as a macro that evaluates the stream
argument more than once.
The getchar function is equivalent to fgetc with stdin
as the value of the stream argument.
Here is an example of a function that does input using fgetc. It
would work just as well using getc instead, or using
getchar () instead of fgetc (stdin).
int
y_or_n_p (const char *question)
{
fputs (question, stdout);
while (1) {
int c, answer;
/* Write a space to separate answer from question. */
fputc (' ', stdout);
/* Read the first character of the line.
This should be the answer character, but might not be. */
c = tolower (fgetc (stdin));
answer = c;
/* Discard rest of input line. */
while (c != '\n')
c = fgetc (stdin);
/* Obey the answer if it was valid. */
if (answer == 'y')
return 1;
if (answer == 'n')
return 0;
/* Answer was invalid: ask for valid answer. */
fputs ("Please answer y or n:", stdout);
}
}
Function: int getw (FILE *stream)
This function reads a word (that is, an int) from stream.
It's provided for compatibility with SVID. We recommend you use
fread instead (see section Block Input/Output).
Since many programs interpret input on the basis of lines, it's convenient to have functions to read a line of text from a stream.
Standard C has functions to do this, but they aren't very safe: null
characters and even (for gets) long lines can confuse them. So
the GNU library provides the nonstandard getline function that
makes it easy to read lines reliably.
Another GNU extension, getdelim, generalizes getline. It
reads a delimited record, defined as everything through the next
occurrence of a specified delimeter character.
All these functions are declared in `stdio.h'.
Function: ssize_t getline (char **lineptr, size_t *n, FILE *stream)
This function reads an entire line from stream, storing the text
(including the newline and a terminating null character) in a buffer
and storing the buffer address in *lineptr.
Before calling getline, you should place in *lineptr
the address of a buffer *n bytes long. If this buffer is
long enough to hold the line, getline stores the line in this
buffer. Otherwise, getline makes the buffer bigger using
realloc, storing the new buffer address back in
*lineptr and the increased size back in *n.
In either case, when getline returns, *lineptr is
a char * which points to the text of the line.
When getline is successful, it returns the number of characters
read (including the newline, but not including the terminating null).
This value enables you to distinguish null characters that are part of
the line from the null character inserted as a terminator.
This function is a GNU extension, but it is the recommended way to read lines from a stream. The alternative standard functions are unreliable.
If an error occurs or end of file is reached, getline returns
-1.
Function: ssize_t getdelim (char **lineptr, size_t *n, int delimiter, FILE *stream)
This function is like getline except that the character which
tells it to stop reading is not necessarily newline. The argument
delimeter specifies the delimeter character; getdelim keeps
reading until it sees that character (or end of file).
The text is stored in lineptr, including the delimeter character
and a terminating null. Like getline, getdelim makes
lineptr bigger if it isn't big enough.
Function: char * fgets (char *s, int count, FILE *stream)
The fgets function reads characters from the stream stream
up to and including a newline character and stores them in the string
s, adding a null character to mark the end of the string. You
must supply count characters worth of space in s, but the
number of characters read is at most count - 1. The extra
character space is used to hold the null character at the end of the
string.
If the system is already at end of file when you call fgets, then
the contents of the array s are unchanged and a null pointer is
returned. A null pointer is also returned if a read error occurs.
Otherwise, the return value is the pointer s.
Warning: If the input data has a null character, you can't tell.
So don't use fgets unless you know the data cannot contain a null.
Don't use it to read files edited by the user because, if the user inserts
a null character, you should either handle it properly or print a clear
error message. We recommend using getline instead of fgets.
Deprecated function: char * gets (char *s)
The function gets reads characters from the stream stdin
up to the next newline character, and stores them in the string s.
The newline character is discarded (note that this differs from the
behavior of fgets, which copies the newline character into the
string).
Warning: The gets function is very dangerous
because it provides no protection against overflowing the string s.
The GNU library includes it for compatibility only. You should
always use fgets or getline instead.
In parser programs it is often useful to examine the next character in the input stream without removing it from the stream. This is called "peeking ahead" at the input because your program gets a glimpse of the input it will read next.
Using stream I/O, you can peek ahead at input by first reading it and
then unreading it (also called pushing it back on the stream).
Unreading a character makes it available to be input again from the stream,
by the next call to fgetc or other input function on that stream.
Here is a pictorial explanation of unreading. Suppose you have a stream reading a file that contains just six characters, the letters `foobar'. Suppose you have read three characters so far. The situation looks like this:
f o o b a r
^
so the next input character will be `b'.
If instead of reading `b' you unread the letter `o', you get a situation like this:
f o o b a r
|
o--
^
so that the next input characters will be `o' and `b'.
If you unread `9' instead of `o', you get this situation:
f o o b a r
|
9--
^
so that the next input characters will be `9' and `b'.
ungetc To Do Unreadingungetc, because it
reverses the action of fgetc.
Function: int ungetc (int c, FILE *stream)
The ungetc function pushes back the character c onto the
input stream stream. So the next input from stream will
read c before anything else.
The character that you push back doesn't have to be the same as the last
character that was actually read from the stream. In fact, it isn't
necessary to actually read any characters from the stream before
unreading them with ungetc! But that is a strange way to write
a program; usually ungetc is used only to unread a character
that was just read from the same stream.
The GNU C library only supports one character of pushback--in other
words, it does not work to call ungetc twice without doing input
in between. Other systems might let you push back multiple characters;
then reading from the stream retrieves the characters in the reverse
order that they were pushed.
Pushing back characters doesn't alter the file; only the internal
buffering for the stream is affected. If a file positioning function
(such as fseek or rewind; see section File Positioning) is
called, any pending pushed-back characters are discarded.
Unreading a character on a stream that is at end of file clears the end-of-file indicator for the stream, because it makes the character of input available. Reading that character will set the end-of-file indicator again.
Here is an example showing the use of getc and ungetc to
skip over whitespace characters. When this function reaches a
non-whitespace character, it unreads that character to be seen again on
the next read operation on the stream.
#include <stdio.h>
void
skip_whitespace (FILE *stream)
{
int c;
do
/* No need to check for EOF because it is not
isspace, and ungetc ignores EOF. */
c = getc (stream);
while (isspace (c));
ungetc (c, stream);
}
The functions described in this section (printf and related
functions) provide a convenient way to perform formatted output. You
call printf with a format string or template string
that specifies how to format the values of the remaining arguments.
Unless your program is a filter that specifically performs line- or
character-oriented processing, using printf or one of the other
related functions described in this section is usually the easiest and
most concise way to perform output. These functions are especially
useful for printing error messages, tables of data, and the like.
The printf function can be used to print any number of arguments.
The template string argument you supply in a call provides
information not only about the number of additional arguments, but also
about their types and what style should be used for printing them.
Ordinary characters in the template string are simply written to the output stream as-is, while conversion specifications introduced by a `%' character in the template cause subsequent arguments to be formatted and written to the output stream. For example,
int pct = 37;
char filename[] = "foo.txt";
printf ("Processing of `%s' is %d%% finished.\nPlease be patient.\n",
filename, pct);
produces output like
Processing of `foo.txt' is 37% finished. Please be patient.
This example shows the use of the `%d' conversion to specify that
an int argument should be printed in decimal notation, the
`%s' conversion to specify printing of a string argument, and
the `%%' conversion to print a literal `%' character.
There are also conversions for printing an integer argument as an unsigned value in octal, decimal, or hexadecimal radix (`%o', `%u', or `%x', respectively); or as a character value (`%c').
Floating-point numbers can be printed in normal, fixed-point notation using the `%f' conversion or in exponential notation using the `%e' conversion. The `%g' conversion uses either `%e' or `%f' format, depending on what is more appropriate for the magnitude of the particular number.
You can control formatting more precisely by writing modifiers between the `%' and the character that indicates which conversion to apply. These slightly alter the ordinary behavior of the conversion. For example, most conversion specifications permit you to specify a minimum field width and a flag indicating whether you want the result left- or right-justified within the field.
The specific flags and modifiers that are permitted and their interpretation vary depending on the particular conversion. They're all described in more detail in the following sections. Don't worry if this all seems excessively complicated at first; you can almost always get reasonable free-format output without using any of the modifiers at all. The modifiers are mostly used to make the output look "prettier" in tables.
This section provides details about the precise syntax of conversion
specifications that can appear in a printf template
string.
Characters in the template string that are not part of a conversion specification are printed as-is to the output stream. Multibyte character sequences (see section Extended Characters) are permitted in a template string.
The conversion specifications in a printf template string have
the general form:
% flags width [ . precision ] type conversion
For example, in the conversion specifier `%-10.8ld', the `-'
is a flag, `10' specifies the field width, the precision is
`8', the letter `l' is a type modifier, and `d' specifies
the conversion style. (This particular type specifier says to
print a long int argument in decimal notation, with a minimum of
8 digits left-justified in a field at least 10 characters wide.)
In more detail, output conversion specifications consist of an initial `%' character followed in sequence by:
The GNU library's version of printf also allows you to specify a
field width of `*'. This means that the next argument in the
argument list (before the actual value to be printed) is used as the
field width. The value must be an int. Other C library versions may
not recognize this syntax.
The GNU library's version of printf also allows you to specify a
precision of `*'. This means that the next argument in the
argument list (before the actual value to be printed) is used as the
precision. The value must be an int. If you specify `*'
for both the field width and precision, the field width argument
precedes the precision argument. Other C library versions may not
recognize this syntax.
int,
but you can specify `h', `l', or `L' for other integer
types.)
The exact options that are permitted and how they are interpreted vary between the different conversion specifiers. See the descriptions of the individual conversions for information about the particular options that they use.
Here is a table summarizing what all the different conversions do:
scanf for input
(see section Table of Input Conversions).
size_t. See section Integer Conversions, for details.
errno.
See section Other Output Conversions.
If the syntax of a conversion specification is invalid, unpredictable things will happen, so don't do this. If there aren't enough function arguments provided to supply values for all the conversion specifications in the template string, or if the arguments are not of the correct types, the results are unpredictable. If you supply more arguments than conversion specifications, the extra argument values are simply ignored; this is sometimes useful.
This section describes the options for the `%d', `%i', `%o', `%u', `%x', `%X', and `%Z' conversion specifications. These conversions print integers in various formats.
The `%d' and `%i' conversion specifications both print an
int argument as a signed decimal number; while `%o',
`%u', and `%x' print the argument as an unsigned octal,
decimal, or hexadecimal number (respectively). The `%X' conversion
specification is just like `%x' except that it uses the characters
`ABCDEF' as digits instead of `abcdef'. `%Z' is like
`%u' but expects an argument of type size_t.
The following flags are meaningful:
If a precision is supplied, it specifies the minimum number of digits to appear; leading zeros are produced if necessary. If you don't specify a precision, the number is printed with as many digits as it needs. If you convert a value of zero with an explicit precision of zero, then no characters at all are produced.
Without a type modifier, the corresponding argument is treated as an
int (for the signed conversions `%i' and `%d') or
unsigned int (for the unsigned conversions `%o', `%u',
`%x', and `%X'). Recall that since printf and friends
are variadic, any char and short arguments are
automatically converted to int by the default argument
promotions. For arguments of other integer types, you can use these
modifiers:
short int or unsigned
short int, as appropriate. A short argument is converted to an
int or unsigned int by the default argument promotions
anyway, but the `h' modifier says to convert it back to a
short again.
long int or unsigned long
int, as appropriate.
long long int. (This type is
an extension supported by the GNU C compiler. On systems that don't
support extra-long integers, this is the same as long int.)
The modifiers for argument type are not applicable to `%Z', since
the sole purpose of `%Z' is to specify the data type
size_t.
Here is an example. Using the template string:
|%5d|%-5d|%+5d|%+-5d|% 5d|%05d|%5.0d|%5.2d|%d|\n"
to print numbers using the different options for the `%d' conversion gives results like:
| 0|0 | +0|+0 | 0|00000| | 00|0| | 1|1 | +1|+1 | 1|00001| 1| 01|1| | -1|-1 | -1|-1 | -1|-0001| -1| -01|-1| |100000|100000|+100000| 100000|100000|100000|100000|100000|
In particular, notice what happens in the last case where the number is too large to fit in the minimum field width specified.
Here are some more examples showing how unsigned integers print under various format options, using the template string:
"|%5u|%5o|%5x|%5X|%#5o|%#5x|%#5X|%#10.8x|\n"
| 0| 0| 0| 0| 0| 0x0| 0X0|0x00000000| | 1| 1| 1| 1| 01| 0x1| 0X1|0x00000001| |100000|303240|186a0|186A0|0303240|0x186a0|0X186A0|0x000186a0|
This section discusses the conversion specifications for floating-point numbers: the `%f', `%e', `%E', `%g', and `%G' conversions.
The `%f' conversion prints its argument in fixed-point notation,
producing output of the form
[-]ddd.ddd,
where the number of digits following the decimal point is controlled
by the precision you specify.
The `%e' conversion prints its argument in exponential notation,
producing output of the form
[-]d.ddde[+|-]dd.
Again, the number of digits following the decimal point is controlled by
the precision. The exponent always contains at least two digits. The
`%E' conversion is similar but the exponent is marked with the letter
`E' instead of `e'.
The `%g' and `%G' conversions print the argument in the style of `%e' or `%E' (respectively) if the exponent would be less than -4 or greater than or equal to the precision; otherwise they use the `%f' style. Trailing zeros are removed from the fractional portion of the result and a decimal-point character appears only if it is followed by a digit.
The following flags can be used to modify the behavior:
The precision specifies how many digits follow the decimal-point
character for the `%f', `%e', and `%E' conversions. For
these conversions, the default is 6. If the precision is
explicitly 0, this has the rather strange effect of suppressing
the decimal point character entirely! For the `%g' and `%G'
conversions, the precision specifies how many significant digits to
print; if 0 or not specified, it is treated like a value of
1.
Without a type modifier, the floating-point conversions use an argument
of type double. (By the default argument promotions, any
float arguments are automatically converted to double.)
The following type modifier is supported:
long
double.
Here are some examples showing how numbers print using the various floating-point conversions. All of the numbers were printed using this template string:
"|%12.4f|%12.4e|%12.4g|\n"
Here is the output:
| 0.0000| 0.0000e+00| 0| | 1.0000| 1.0000e+00| 1| | -1.0000| -1.0000e+00| -1| | 100.0000| 1.0000e+02| 100| | 1000.0000| 1.0000e+03| 1000| | 10000.0000| 1.0000e+04| 1e+04| | 12345.0000| 1.2345e+04| 1.234e+04| | 100000.0000| 1.0000e+05| 1e+05| | 123456.0000| 1.2346e+05| 1.234e+05|
Notice how the `%g' conversion drops trailing zeros.
This section describes miscellaneous conversions for printf.
The `%c' conversion prints a single character. The int
argument is first converted to an unsigned char. The `-'
flag can be used to specify left-justification in the field, but no
other flags are defined, and no precision or type modifier can be given.
For example:
printf ("%c%c%c%c%c", 'h', 'e', 'l', 'l', 'o');
prints `hello'.
The `%s' conversion prints a string. The corresponding argument
must be of type char *. A precision can be specified to indicate
the maximum number of characters to write; otherwise characters in the
string up to but not including the terminating null character are
written to the output stream. The `-' flag can be used to specify
left-justification in the field, but no other flags or type modifiers
are defined for this conversion. For example:
printf ("%3s%-6s", "no", "where");
prints ` nowhere '.
If you accidentally pass a null pointer as the argument for a `%s' conversion, the GNU library prints it as `(null)'. We think this is more useful than crashing. But it's not good practice to pass a null argument intentionally.
The `%m' conversion prints the string corresponding to the error
code in errno. See section Error Messages. Thus:
fprintf (stderr, "can't open `%s': %m\n", filename);
is equivalent to:
fprintf (stderr, "can't open `%s': %s\n", filename, strerror (errno));
The `%m' conversion is a GNU C library extension.
The `%p' conversion prints a pointer value. The corresponding
argument must be of type void *. In practice, you can use any
type of pointer.
In the GNU system, non-null pointers are printed as unsigned integers, as if a `%#x' conversion were used. Null pointers print as `(nil)'. (Pointers might print differently in other systems.)
For example:
printf ("%p", "testing");
prints `0x' followed by a hexadecimal number--the address of the
string constant "testing". It does not print the word
`testing'.
You can supply the `-' flag with the `%p' conversion to specify left-justification, but no other flags, precision, or type modifiers are defined.
The `%n' conversion is unlike any of the other output conversions.
It uses an argument which must be a pointer to an int, but
instead of printing anything it stores the number of characters printed
so far by this call at that location. The `h' and `l' type
modifiers are permitted to specify that the argument is of type
short int * or long int * instead of int *, but no
flags, field width, or precision are permitted.
For example,
int nchar;
printf ("%d %s%n\n", 3, "bears", &nchar);
prints:
3 bears
and sets nchar to 7, because `3 bears' is seven
characters.
The `%%' conversion prints a literal `%' character. This conversion doesn't use an argument, and no flags, field width, precision, or type modifiers are permitted.
This section describes how to call printf and related functions.
Prototypes for these functions are in the header file `stdio.h'.
Function: int printf (const char *template, ...)
The printf function prints the optional arguments under the
control of the template string template to the stream
stdout. It returns the number of characters printed, or a
negative value if there was an output error.
Function: int fprintf (FILE *stream, const char *template, ...)
This function is just like printf, except that the output is
written to the stream stream instead of stdout.
Function: int sprintf (char *s, const char *template, ...)
This is like printf, except that the output is stored in the character
array s instead of written to a stream. A null character is written
to mark the end of the string.
The sprintf function returns the number of characters stored in
the array s, not including the terminating null character.
The behavior of this function is undefined if copying takes place between objects that overlap--for example, if s is also given as an argument to be printed under control of the `%s' conversion. See section Copying and Concatenation.
Warning: The sprintf function can be dangerous
because it can potentially output more characters than can fit in the
allocation size of the string s. Remember that the field width
given in a conversion specification is only a minimum value.
To avoid this problem, you can use snprintf or asprintf,
described below.
Function: int snprintf (char *s, size_t size, const char *template, ...)
The snprintf function is similar to sprintf, except that
the size argument specifies the maximum number of characters to
produce. The trailing null character is counted towards this limit, so
you should allocate at least size characters for the string s.
The return value is the number of characters stored, not including the terminating null. If this value equals size, then there was not enough space in s for all the output. You should try again with a bigger output string. Here is an example of doing this:
/* Construct a message describing the value of a variable
whose name is name and whose value is value. */
char *
make_message (char *name, char *value)
{
/* Guess we need no more than 100 chars of space. */
int size = 100;
char *buffer = (char *) xmalloc (size);
while (1)
{
/* Try to print in the allocated space. */
int nchars = snprintf (buffer, size,
"value of %s is %s", name, value);
/* If that worked, return the string. */
if (nchars < size)
return buffer;
/* Else try again with twice as much space. */
size *= 2;
buffer = (char *) xrealloc (size, buffer);
}
}
In practice, it is often easier just to use asprintf, below.
The functions in this section do formatted output and place the results in dynamically allocated memory.
Function: int asprintf (char **ptr, const char *template, ...)
This function is similar to sprintf, except that it dynamically
allocates a string (as with malloc; see section Unconstrained Allocation) to hold the output, instead of putting the output in a
buffer you allocate in advance. The ptr argument should be the
address of a char * object, and asprintf stores a pointer
to the newly allocated string at that location.
Here is how to use asprint to get the same result as the
snprintf example, but more easily:
/* Construct a message describing the value of a variable
whose name is name and whose value is value. */
char *
make_message (char *name, char *value)
{
char *result;
asprintf (&result, "value of %s is %s", name, value);
return result;
}
Function: int obstack_printf (struct obstack *obstack, const char *template, ...)
This function is similar to asprintf, except that it uses the
obstack obstack to allocate the space. See section Obstacks.
The characters are written onto the end of the current object.
To get at them, you must finish the object with obstack_finish
(see section Growing Objects).
The functions vprintf and friends are provided so that you can
define your own variadic printf-like functions that make use of
the same internals as the built-in formatted output functions.
The most natural way to define such functions would be to use a language
construct to say, "Call printf and pass this template plus all
of my arguments after the first five." But there is no way to do this
in C, and it would be hard to provide a way, since at the C language
level there is no way to tell how many arguments your function received.
Since that method is impossible, we provide alternative functions, the
vprintf series, which lets you pass a va_list to describe
"all of my arguments after the first five."
Before calling vprintf or the other functions listed in this
section, you must call va_start (see section Variadic Functions) to initialize a pointer to the variable arguments. Then you
can call va_arg to fetch the arguments that you want to handle
yourself. This advances the pointer past those arguments.
Once your va_list pointer is pointing at the argument of your
choice, you are ready to call vprintf. That argument and all
subsequent arguments that were passed to your function are used by
vprintf along with the template that you specified separately.
In some other systems, the va_list pointer may become invalid
after the call to vprintf, so you must not use va_arg
after you call vprintf. Instead, you should call va_end
to retire the pointer from service. However, you can safely call
va_start on another pointer variable and begin fetching the
arguments again through that pointer. Calling vfprintf does
not destroy the argument list of your function, merely the particular
pointer that you passed to it.
The GNU library does not have such restrictions. You can safely continue
to fetch arguments from a va_list pointer after passing it to
vprintf, and va_end is a no-op.
Prototypes for these functions are declared in `stdio.h'.
Function: int vprintf (const char *template, va_list ap)
This function is similar to printf except that, instead of taking
a variable number of arguments directly, it takes an argument list
pointer ap.
Function: int vfprintf (FILE *stream, const char *template, va_list ap)
This is the equivalent of fprintf with the variable argument list
specified directly as for vprintf.
Function: int vsprintf (char *s, const char *template, va_list ap)
This is the equivalent of sprintf with the variable argument list
specified directly as for vprintf.
Function: int vsnprintf (char *s, size_t size, const char *template, va_list ap)
This is the equivalent of snprintf with the variable argument list
specified directly as for vprintf.
Function: int vasprintf (char **ptr, const char *template, va_list ap)
The vasprintf function is the equivalent of asprintf with the
variable argument list specified directly as for vprintf.
Function: int obstack_vprintf (struct obstack *obstack, const char *template, va_list ap)
The obstack_vprintf function is the equivalent of
obstack_printf with the variable argument list specified directly
as for vprintf.
Here's an example showing how you might use vfprintf. This is a
function that prints error messages to the stream stderr, along
with a prefix indicating the name of the program
(see section Error Messages, for a description of
program_invocation_short_name).
#include <stdio.h>
#include <stdarg.h>
void
eprintf (char *template, ...)
{
va_list ap;
extern char *program_invocation_short_name;
fprintf (stderr, "%s: ", program_invocation_short_name);
va_start (ap, count);
vfprintf (stderr, template, ap);
va_end (ap);
}
You could call eprintf like this:
eprintf ("file `%s' does not exist\n", filename);
You can use the function parse_printf_format to obtain
information about the number and types of arguments that are expected by
a given template string. This function permits interpreters that
provide interfaces to printf to avoid passing along invalid
arguments from the user's program, which could cause a crash.
All the symbols described in this section are declared in the header file `printf.h'.
Function: size_t parse_printf_format (const char *template, size_t n, int *argtypes)
This function returns information about the number and types of
arguments expected by the printf template string template.
The information is stored in the array argtypes; each element of
this array describes one argument. This information is encoded using
the various `PA_' macros, listed below.
The n argument specifies the number of elements in the array
argtypes. This is the most elements that
parse_printf_format will try to write.
parse_printf_format returns the total number of arguments required
by template. If this number is greater than n, then the
information returned describes only the first n arguments. If you
want information about more than that many arguments, allocate a bigger
array and call parse_printf_format again.
The argument types are encoded as a combination of a basic type and modifier flag bits.
This macro is a bitmask for the type modifier flag bits. You can write
the expression (argtypes[i] & PA_FLAG_MASK) to extract just the
flag bits for an argument, or (argtypes[i] & ~PA_FLAG_MASK) to
extract just the basic type code.
Here are symbolic constants that represent the basic types; they stand for integer values.
PA_INT
int.
PA_CHAR
int, cast to char.
PA_STRING
char *, a null-terminated string.
PA_POINTER
void *, an arbitrary pointer.
PA_FLOAT
float.
PA_DOUBLE
double.
PA_LAST
PA_LAST. For example, if you have data types `foo'
and `bar' with their own specialized printf conversions,
you could define encodings for these types as:
#define PA_FOO PA_LAST #define PA_BAR (PA_LAST + 1)
Here are the flag bits that modify a basic type. They are combined with the code for the basic type using inclusive-or.
PA_FLAG_PTR
PA_FLAG_SHORT
short. (This corresponds to the `h' type modifier.)
PA_FLAG_LONG
long. (This corresponds to the `l' type modifier.)
PA_FLAG_LONG_LONG
long long. (This corresponds to the `L' type modifier.)
PA_FLAG_LONG_DOUBLE
PA_FLAG_LONG_LONG, used by convention with
a base type of PA_DOUBLE to indicate a type of long double.
Here is an example of decoding argument types for a format string. We
assume this is part of an interpreter which contains arguments of type
NUMBER, CHAR, STRING and STRUCTURE (and
perhaps others which are not valid here).
/* Test whether the nargs specified objects
in the vector args are valid
for the format string format:
if so, return 1.
If not, return 0 after printing an error message. */
int
validate_args (char *format, int nargs, OBJECT *args)
{
int nelts = 20;
int *argtypes;
int nwanted;
/* Get the information about the arguments. */
while (1) {
argtypes = (int *) alloca (nelts * sizeof (int));
nwanted = parse_printf_format (string, nelts, argtypes);
if (nwanted <= nelts)
break;
nelts *= 2;
}
/* Check the number of arguments. */
if (nwanted > nargs) {
error ("too few arguments (at least %d required)", nwanted);
return 0;
}
/* Check the C type wanted for each argument
and see if the object given is suitable. */
for (i = 0; i < nwanted; i++) {
int wanted;
if (argtypes[i] & PA_FLAG_PTR)
wanted = STRUCTURE;
else
switch (argtypes[i] & ~PA_FLAG_MASK) {
case PA_INT:
case PA_FLOAT:
case PA_DOUBLE:
wanted = NUMBER;
break;
case PA_CHAR:
wanted = CHAR;
break;
case PA_STRING:
wanted = STRING;
break;
case PA_POINTER:
wanted = STRUCTURE;
break;
}
if (TYPE (args[i]) != wanted) {
error ("type mismatch for arg number %d", i);
return 0;
}
}
return 1;
}
printf
The GNU C library lets you define your own custom conversion specifiers
for printf template strings, to teach printf clever ways
to print the important data structures of your program.
The way you do this is by registering the conversion with
register_printf_function; see section Registering New Conversions.
One of the arguments you pass to this function is a pointer to a handler
function that produces the actual output; see section Defining the Output Handler, for information on how to write this function.
You can also install a function that just returns information about the number and type of arguments expected by the conversion specifier. See section Parsing a Template String, for information about this.
The facilities of this section are declared in the header file `printf.h'.
Portability Note: The ability to extend the syntax of
printf template strings is a GNU extension. ANSI standard C has
nothing similar.
The function to register a new output conversion is
register_printf_function, declared in `printf.h'.
Function: int register_printf_function (int spec, printf_function handler_function, printf_arginfo_function arginfo_function)
This function defines the conversion specifier character spec.
Thus, if spec is 'q', it defines the conversion `%q'.
The handler_function is the function called by printf and
friends when this conversion appears in a template string.
See section Defining the Output Handler, for information about how to define
a function to pass as this argument. If you specify a null pointer, any
existing handler function for spec is removed.
The arginfo_function is the function called by
parse_printf_format when this conversion appears in a
template string. See section Parsing a Template String, for information
about this.
Normally, you install both functions for a conversion at the same time,
but if you are never going to call parse_printf_format, you do
not need to define an arginfo function.
The return value is 0 on success, and -1 on failure
(which occurs if spec is out of range).
You can redefine the standard output conversions, but this is probably not a good idea because of the potential for confusion. Library routines written by other people could break if you do this.
If you define a meaning for `%q', what if the template contains `%+Sq' or `%-#q'? To implement a sensible meaning for these, the handler when called needs to be able to get the options specified in the template.
Both the handler_function and arginfo_function arguments
to register_printf_function accept an argument of type
struct print_info, which contains information about the options
appearing in an instance of the conversion specifier. This data type
is declared in the header file `printf.h'.
This structure is used to pass information about the options appearing
in an instance of a conversion specifier in a printf template
string to the handler and arginfo functions for that specifier. It
contains the following members:
int prec
-1 if no precision
was specified. If the precision was given as `*', the
printf_info structure passed to the handler function contains the
actual value retrieved from the argument list. But the structure passed
to the arginfo function contains a value of INT_MIN, since the
actual value is not known.
int width
0 if no
width was specified. If the field width was given as `*', the
printf_info structure passed to the handler function contains the
actual value retrieved from the argument list. But the structure passed
to the arginfo function contains a value of INT_MIN, since the
actual value is not known.
char spec
unsigned int is_long_double
unsigned int is_short
unsigned int is_long
unsigned int alt
unsigned int space
unsigned int left
unsigned int showsign
char pad
'0' if the `0' flag was specified, and
' ' otherwise.
Now let's look at how to define the handler and arginfo functions
which are passed as arguments to register_printf_function.
You should define your handler functions with a prototype like:
int function (FILE *stream, const struct printf_info *info,
va_list *ap_pointer)
The stream argument passed to the handler function is the stream to
which it should write output.
The info argument is a pointer to a structure that contains
information about the various options that were included with the
conversion in the template string. You should not modify this structure
inside your handler function. See section Conversion Specifier Options, for
a description of this data structure.
The ap_pointer argument is used to pass the tail of the variable
argument list containing the values to be printed to your handler.
Unlike most other functions that can be passed an explicit variable
argument list, this is a pointer to a va_list, rather than
the va_list itself. Thus, you should fetch arguments by
means of va_arg (type, *ap_pointer).
(Passing a pointer here allows the function that calls your handler
function to update its own va_list variable to account for the
arguments that your handler processes. See section Variadic Functions.)
The return value from your handler function should be the number of
argument values that it processes from the variable argument list. You
can also return a value of -1 to indicate an error.
This is the data type that a handler function should have.
If you are going to use parse_printf_format in your
application, you should also define a function to pass as the
arginfo_function argument for each new conversion you install with
register_printf_function.
You should define these functions with a prototype like:
int function (const struct printf_info *info,
size_t n, int *argtypes)
The return value from the function should be the number of arguments the
conversion expects, up to a maximum of n. The function should
also fill in the argtypes array with information about the types
of each of these arguments. This information is encoded using the
various `PA_' macros. (You will notice that this is the same
calling convention parse_printf_format itself uses.)
Data Type: printf_arginfo_function
This type is used to describe functions that return information about the number and type of arguments used by a conversion specifier.
printf Extension Example
Here is an example showing how to define a printf handler function.
This program defines a data structure called a Widget and
defines the `%W' conversion to print information about Widget *
arguments, including the pointer value and the name stored in the data
structure. The `%W' conversion supports the minimum field width and
left-justification options, but ignores everything else.
#include <stdio.h>
#include <printf.h>
#include <stdarg.h>
typedef struct
{
char *name;
} Widget;
int
print_widget (FILE *stream, const struct printf_info *info, va_list *app)
{
Widget *w;
char *buffer;
int len;
/* Format the output into a string. */
w = va_arg (*app, Widget *);
len = asprintf (&buffer, "<Widget %p: %s>", w, w->name);
if (len == -1)
return -1;
/* Pad to the minimum field width and print to the stream. */
len = fprintf (stream, "%*s",
(info->left ? - info->width : info->width),
buffer);
/* Clean up and return. */
free (buffer);
return len;
}
int
main (void)
{
/* Make a widget to print. */
Widget mywidget;
mywidget.name = "mywidget";
/* Register the print function for widgets. */
register_printf_function ('W', print_widget, NULL); /* No arginfo. */
/* Now print the widget. */
printf ("|%W|\n", &mywidget);
printf ("|%35W|\n", &mywidget);
printf ("|%-35W|\n", &mywidget);
return 0;
}
The output produced by this program looks like:
|<Widget 0xffeffb7c: mywidget>| | <Widget 0xffeffb7c: mywidget>| |<Widget 0xffeffb7c: mywidget> |
The functions described in this section (scanf and related
functions) provide facilities for formatted input analogous to the
formatted output facilities. These functions provide a mechanism for
reading arbitrary values under the control of a format string or
template string.
Calls to scanf are superficially similar to calls to
printf in that arbitrary arguments are read under the control of
a template string. While the syntax of the conversion specifications in
the template is very similar to that for printf, the
interpretation of the template is oriented more towards free-format
input and simple pattern matching, rather than fixed-field formatting.
For example, most scanf conversions skip over any amount of
"white space" (including spaces, tabs, and newlines) in the input
file, and there is no concept of precision for the numeric input
conversions as there is for the corresponding output conversions.
Ordinarily, non-whitespace characters in the template are expected to
match characters in the input stream exactly, but a matching failure is
distinct from an input error on the stream.
Another area of difference between scanf and printf is
that you must remember to supply pointers rather than immediate values
as the optional arguments to scanf; the values that are read are
stored in the objects that the pointers point to. Even experienced
programmers tend to forget this occasionally, so if your program is
getting strange errors that seem to be related to scanf, you
might want to double-check this.
When a matching failure occurs, scanf returns immediately,
leaving the first non-matching character as the next character to be
read from the stream. The normal return value from scanf is the
number of values that were assigned, so you can use this to determine if
a matching error happened before all the expected values were read.
The scanf function is typically used for things like reading in
the contents of tables. For example, here is a function that uses
scanf to initialize an array of double:
void
readarray (double *array, int n)
{
int i;
for (i=0; i<n; i++)
if (scanf (" %lf", &(array[i])) != 1)
invalid_input_error ();
}
The formatted input functions are not used as frequently as the formatted output functions. Partly, this is because it takes some care to use them properly. Another reason is that it is difficult to recover from a matching error.
If you are trying to read input that doesn't match a single, fixed
pattern, you may be better off using a tool such as Bison to generate a
parser, rather than using scanf. For more information about
this, see section 'Bison' in The Bison Reference Manual.
A scanf template string is a string that contains ordinary
multibyte characters interspersed with conversion specifications that
start with `%'.
Any whitespace character (as defined by the isspace function;
see section Classification of Characters) in the template causes any number
of whitespace characters in the input stream to be read and discarded.
The whitespace characters that are matched need not be exactly the same
whitespace characters that appear in the template string. For example,
write ` , ' in the template to recognize a comma with optional
whitespace before and after.
Other characters in the template string that are not part of conversion specifications must match characters in the input stream exactly; if this is not the case, a matching failure occurs.
The conversion specifications in a scanf template string
have the general form:
% flags width type conversion
In more detail, an input conversion specification consists of an initial `%' character followed in sequence by:
scanf finds a conversion
specification that uses this flag, it reads input as directed by the
rest of the conversion specification, but it discards this input, does
not use a pointer argument, and does not increment the count of
successful assignments.
long int
rather than a pointer to an int.
The exact options that are permitted and how they are interpreted vary between the different conversion specifiers. See the descriptions of the individual conversions for information about the particular options that they allow.
Here is a table that summarizes the various conversion specifications:
printf. See section Other Input Conversions.
If the syntax of a conversion specification is invalid, the behavior is undefined. If there aren't enough function arguments provided to supply addresses for all the conversion specifications in the template strings that perform assignments, or if the arguments are not of the correct types, the behavior is also undefined. On the other hand, extra arguments are simply ignored.
This section describes the scanf conversions for reading numeric
values.
The `%d' conversion matches an optionally signed integer in decimal
radix. The syntax that is recognized is the same as that for the
strtol function (see section Parsing of Integers) with the value
10 for the base argument.
The `%i' conversion matches an optionally signed integer in any of
the formats that the C language defines for specifying an integer
constant. The syntax that is recognized is the same as that for the
strtol function (see section Parsing of Integers) with the value
0 for the base argument.
For example, any of the strings `10', `0xa', or `012'
could be read in as integers under the `%i' conversion. Each of
these specifies a number with decimal value 10.
The `%o', `%u', and `%x' conversions match unsigned
integers in octal, decimal, and hexadecimal radices, respectively. The
syntax that is recognized is the same as that for the strtoul
function (see section Parsing of Integers) with the appropriate value
(8, 10, or 16) for the base argument.
The `%X' conversion is identical to the `%x' conversion. They both permit either uppercase or lowercase letters to be used as digits.
The default type of the corresponding argument for the %d and
%i conversions is int *, and unsigned int * for the
other integer conversions. You can use the following type modifiers to
specify other sizes of integer:
short int * or unsigned
short int *.
long int * or unsigned
long int *.
long long int * or unsigned long long int *. (The long long type is an extension supported by the
GNU C compiler. For systems that don't provide extra-long integers, this
is the same as long int.)
All of the `%e', `%f', `%g', `%E', and `%G'
input conversions are interchangeable. They all match an optionally
signed floating point number, in the same syntax as for the
strtod function (see section Parsing of Floats).
For the floating-point input conversions, the default argument type is
float *. (This is different from the corresponding output
conversions, where the default type is double; remember that
float arguments to printf are converted to double
by the default argument promotions, but float * arguments are
not promoted to double *.) You can specify other sizes of float
using these type modifiers:
double *.
long double *.
This section describes the scanf input conversions for reading
string and character values: `%s', `%[', and `%c'.
You have two options for how to receive the input from these conversions:
char *.
Warning: To make a robust program, you must make sure that the input (plus its terminating null) cannot possibly exceed the size of the buffer you provide. In general, the only way to do this is to specify a maximum field width one less than the buffer size. If you provide the buffer, always specify a maximum field width to prevent overflow.
scanf to allocate a big enough buffer, by specifying the
`a' flag character. This is a GNU extension. You should provide
an argument of type char ** for the buffer address to be stored
in. See section Dynamically Allocating String Conversions.
The `%c' conversion is the simplest: it matches a fixed number of characters, always. The maximum field with says how many characters to read; if you don't specify the maximum, the default is 1. This conversion doesn't append a null character to the end of the text it reads. It also does not skip over initial whitespace characters. It reads precisely the next n characters, and fails if it cannot get that many. Since there is always a maximum field width with `%c' (whether specified, or 1 by default), you can always prevent overflow by making the buffer long enough.
The `%s' conversion matches a string of non-whitespace characters. It skips and discards initial whitespace, but stops when it encounters more whitespace after having read something. It stores a null character at the end of the text that it reads.
For example, reading the input:
hello, world
with the conversion `%10c' produces " hello, wo", but
reading the same input with the conversion `%10s' produces
"hello,".
Warning: If you do not specify a field width for `%s', then the number of characters read is limited only by where the next whitespace character appears. This almost certainly means that invalid input can make your program crash--which is a bug.
To read in characters that belong to an arbitrary set of your choice, use the `%[' conversion. You specify the set between the `[' character and a following `]' character, using the same syntax used in regular expressions. As special cases:
The `%[' conversion does not skip over initial whitespace characters.
Here are some examples of `%[' conversions and what they mean:
One more reminder: the `%s' and `%[' conversions are dangerous if you don't specify a maximum width or use the `a' flag, because input too long would overflow whatever buffer you have provided for it. No matter how long your buffer is, a user could supply input that is longer. A well-written program reports invalid input with a comprehensible error message, not with a crash.
A GNU extension to formatted input lets you safely read a string with no
maximum size. Using this feature, you don't supply a buffer; instead,
scanf allocates a buffer big enough to hold the data and gives
you its address. To use this feature, write `a' as a flag
character, as in `%as' or `%a[0-9a-z]'.
The pointer argument you supply for where to store the input should have
type char **. The scanf function allocates a buffer and
stores its address in the word that the argument points to. You should
free the buffer with free when you no longer need it.
Here is an example of using the `a' flag with the `%[...]' conversion specification to read a "variable assignment" of the form `variable = value'.
{
char *variable, *value;
if (2 > scanf ("%a[a-zA-Z0-9] = %a[^\n]\n",
&variable, &value))
{
invalid_input_error ();
return 0;
}
...
}
This section describes the miscellaneous input conversions.
The `%p' conversion is used to read a pointer value. It recognizes
the same syntax as is used by the `%p' output conversion for
printf. The corresponding argument should be of type void **;
that is, the address of a place to store a pointer.
The resulting pointer value is not guaranteed to be valid if it was not originally written during the same program execution that reads it in.
The `%n' conversion produces the number of characters read so far
by this call. The corresponding argument should be of type int *.
This conversion works in the same way as the `%n' conversion for
printf; see section Other Output Conversions, for an example.
The `%n' conversion is the only mechanism for determining the
success of literal matches or conversions with suppressed assignments.
If the `%n' follows the locus of a matching failure, then no value
is stored for it since scanf returns before processing the
`%n'. If you store -1 in that argument slot before calling
scanf, the presence of -1 after scanf indicates an
error occurred before the `%n' was reached.
Finally, the `%%' conversion matches a literal `%' character in the input stream, without using an argument. This conversion does not permit any flags, field width, or type modifier to be specified.
Here are the descriptions of the functions for performing formatted input. Prototypes for these functions are in the header file `stdio.h'.
Function: int scanf (const char *template, ...)
The scanf function reads formatted input from the stream
stdin under the control of the template string template.
The optional arguments are pointers to the places which receive the
resulting values.
The return value is normally the number of successful assignments. If
an end-of-file condition is detected before any matches are performed
(including matches against whitespace and literal characters in the
template), then EOF is returned.
Function: int fscanf (FILE *stream, const char *template, ...)
This function is just like scanf, except that the input is read
from the stream stream instead of stdin.
Function: int sscanf (const char *s, const char *template, ...)
This is like scanf, except that the characters are taken from the
null-terminated string s instead of from a stream. Reaching the
end of the string is treated as an end-of-file condition.
The behavior of this function is undefined if copying takes place between objects that overlap--for example, if s is also given as an argument to receive a string read under control of the `%s' conversion.
The functions vscanf and friends are provided so that you can
define your own variadic scanf-like functions that make use of
the same internals as the built-in formatted output functions.
These functions are analogous to the vprintf series of output
functions. See section Variable Arguments Output Functions, for important
information on how to use them.
Portability Note: The functions listed in this section are GNU extensions.
Function: int vscanf (const char *template, va_list ap)
This function is similar to scanf except that, instead of taking
a variable number of arguments directly, it takes an argument list
pointer ap of type va_list (see section Variadic Functions).
Function: int vfscanf (FILE *stream, const char *template, va_list ap)
This is the equivalent of fscanf with the variable argument list
specified directly as for vscanf.
Function: int vsscanf (const char *s, const char *template, va_list ap)
This is the equivalent of sscanf with the variable argument list
specified directly as for vscanf.
This section describes how to do input and output operations on blocks of data. You can use these functions to read and write binary data, as well as to read and write text in fixed-size blocks instead of by characters or lines.
Binary files are typically used to read and write blocks of data in the same format as is used to represent the data in a running program. In other words, arbitrary blocks of memory--not just character or string objects--can be written to a binary file, and meaningfully read in again by the same program.
Storing data in binary form is often considerably more efficient than using the formatted I/O functions. Also, for floating-point numbers, the binary form avoids possible loss of precision in the conversion process. On the other hand, binary files can't be examined or modified easily using many standard file utilities (such as text editors), and are not portable between different implementations of the language, or different kinds of computers.
These functions are declared in `stdio.h'.
Function: size_t fread (void *data, size_t size, size_t count, FILE *stream)
This function reads up to count objects of size size into the array data, from the stream stream. It returns the number of objects actually read, which might be less than count if a read error occurs or the end of the file is reached. This function returns a value of zero (and doesn't read anything) if either size or count is zero.
If fread encounters end of file in the middle of an object, it
returns the number of complete objects read, and discards the partial
object. Therefore, the stream remains at the actual end of the file.
Function: size_t fwrite (const void *data, size_t size, size_t count, FILE *stream)
This function writes up to count objects of size size from the array data, to the stream stream. The return value is normally count, if the call succeeds. Any other value indicates some sort of error, such as running out of space.
Many of the functions described in this chapter return the value of the
macro EOF to indicate unsuccessful completion of the operation.
Since EOF is used to report both end of file and random errors,
it's often better to use the feof function to check explicitly
for end of file and ferror to check for errors. These functions
check indicators that are part of the internal state of the stream
object, indicators set if the appropriate condition was detected by a
previous I/O operation on that stream.
These symbols are declared in the header file `stdio.h'.
This macro is an integer value that is returned
by a number of functions to indicate an end-of-file condition, or some
other error situation. With the GNU library, EOF is -1.
In other libraries, its value may be some other negative number.
Function: void clearerr (FILE *stream)
This function clears the end-of-file and error indicators for the stream stream.
The file positioning functions (see section File Positioning) also clear the end-of-file indicator for the stream.
Function: int feof (FILE *stream)
The feof function returns nonzero if and only if the end-of-file
indicator for the stream stream is set.
Function: int ferror (FILE *stream)
The ferror function returns nonzero if and only if the error
indicator for the stream stream is set, indicating that an error
has occurred on a previous operation on the stream.
In addition to setting the error indicator associated with the stream,
the functions that operate on streams also set errno in the same
way as the corresponding low-level functions that operate on file
descriptors. For example, all of the functions that perform output to a
stream--such as fputc, printf, and fflush---are
implemented in terms of write, and all of the errno error
conditions defined for write are meaningful for these functions.
For more information about the descriptor-level I/O functions, see
section Low-Level Input/Output.
The GNU system and other POSIX-compatible operating systems organize all files as uniform sequences of characters. However, some other systems make a distinction between files containing text and files containing binary data, and the input and output facilities of ANSI C provide for this distinction. This section tells you how to write programs portable to such systems.
When you open a stream, you can specify either a text stream or a
binary stream. You indicate that you want a binary stream by
specifying the `b' modifier in the opentype argument to
fopen; see section Opening Streams. Without this
option, fopen opens the file as a text stream.
Text and binary streams differ in several ways:
'\n') characters, while a binary stream is
simply a long series of characters. A text stream might on some systems
fail to handle lines more than 254 characters long (including the
terminating newline character).
Since a binary stream is always more capable and more predictable than a text stream, you might wonder what purpose text streams serve. Why not simply always use binary streams? The answer is that on these operating systems, text and binary streams use different file formats, and the only way to read or write "an ordinary file of text" that can work with other text-oriented programs is through a text stream.
In the GNU library, and on all POSIX systems, there is no difference between text streams and binary streams. When you open a stream, you get the same kind of stream regardless of whether you ask for binary. This stream can handle any file content, and has none of the restrictions that text streams sometimes have.
The file position of a stream describes where in the file the stream is currently reading or writing. I/O on the stream advances the file position through the file. In the GNU system, the file position is represented as an integer, which counts the number of bytes from the beginning of the file. See section File Position.
During I/O to an ordinary disk file, you can change the file position whenever you wish, so as to read or write any portion of the file. Some other kinds of files may also permit this. Files which support changing the file position are sometimes referred to as random-access files.
You can use the functions in this section to examine or modify the file position indicator associated with a stream. The symbols listed below are declared in the header file `stdio.h'.
Function: long int ftell (FILE *stream)
This function returns the current file position of the stream stream.
This function can fail if the stream doesn't support file positioning,
or if the file position can't be represented in a long int, and
possibly for other reasons as well. If a failure occurs, a value of
-1 is returned.
Function: int fseek (FILE *stream, long int offset, int whence)
The fseek function is used to change the file position of the
stream stream. The value of whence must be one of the
constants SEEK_SET, SEEK_CUR, or SEEK_END, to
indicate whether the offset is relative to the beginning of the
file, the current file position, or the end of the file, respectively.
This function returns a value of zero if the operation was successful,
and a nonzero value to indicate failure. A successful call also clears
the end-of-file indicator of stream and discards any characters
that were "pushed back" by the use of ungetc.
fseek either flushes any buffered output before setting the file
position or else remembers it so it will be written later in its proper
place in the file.
Portability Note: In non-POSIX systems, ftell and
fseek might work reliably only on binary streams. See section Text and Binary Streams.
The following symbolic constants are defined for use as the whence
argument to fseek. They are also used with the lseek
function (see section Input and Output Primitives) and to specify offsets for file locks
(see section Control Operations on Files).
This is an integer constant which, when used as the whence
argument to the fseek function, specifies that the offset
provided is relative to the beginning of the file.
This is an integer constant which, when used as the whence
argument to the fseek function, specifies that the offset
provided is relative to the current file position.
This is an integer constant which, when used as the whence
argument to the fseek function, specifies that the offset
provided is relative to the end of the file.
Function: void rewind (FILE *stream)
The rewind function positions the stream stream at the
begining of the file. It is equivalent to calling fseek on the
stream with an offset argument of 0L and a
whence argument of SEEK_SET, except that the return
value is discarded and the error indicator for the stream is reset.
These three aliases for the `SEEK_...' constants exist for the sake of compatibility with older BSD systems. They are defined in two different header files: `fcntl.h' and `sys/file.h'.
On the GNU system, the file position is truly a character count. You
can specify any character count value as an argument to fseek and
get reliable results for any random access file. However, some ANSI C
systems do not represent file positions in this way.
On some systems where text streams truly differ from binary streams, it is impossible to represent the file position of a text stream as a count of characters from the beginning of the file. For example, the file position on some systems must encode both a record offset within the file, and a character offset within the record.
As a consequence, if you want your programs to be portable to these systems, you must observe certain rules:
ftell on a text stream has no predictable
relationship to the number of characters you have read so far. The only
thing you can rely on is that you can use it subsequently as the
offset argument to fseek to move back to the same file
position.
fseek on a text stream, either the offset must
either be zero; or whence must be SEEK_SET and the
offset must be the result of an earlier call to ftell on
the same stream.
ungetc
that haven't been read or discarded. See section Unreading.
But even if you observe these rules, you may still have trouble for long
files, because ftell and fseek use a long int value
to represent the file position. This type may not have room to encode
all the file positions in a large file.
So if you do want to support systems with peculiar encodings for the
file positions, it is better to use the functions fgetpos and
fsetpos instead. These functions represent the file position
using the data type fpos_t, whose internal representation varies
from system to system.
These symbols are declared in the header file `stdio.h'.
This is the type of an object that can encode information about the
file position of a stream, for use by the functions fgetpos and
fsetpos.
In the GNU system, fpos_t is equivalent to off_t or
long int. In other systems, it might have a different internal
representation.
Function: int fgetpos (FILE *stream, fpos_t *position)
This function stores the value of the file position indicator for the
stream stream in the fpos_t object pointed to by
position. If successful, fgetpos returns zero; otherwise
it returns a nonzero value and stores an implementation-defined positive
value in errno.
Function: int fsetpos (FILE *stream, const fpos_t position)
This function sets the file position indicator for the stream stream
to the position position, which must have been set by a previous
call to fgetpos on the same stream. If successful, fsetpos
clears the end-of-file indicator on the stream, discards any characters
that were "pushed back" by the use of ungetc, and returns a value
of zero. Otherwise, fsetpos returns a nonzero value and stores
an implementation-defined positive value in errno.
Characters that are written to a stream are normally accumulated and transmitted asynchronously to the file in a block, instead of appearing as soon as they are output by the application program. Similarly, streams often retrieve input from the host environment in blocks rather than on a character-by-character basis. This is called buffering.
If you are writing programs that do interactive input and output using streams, you need to understand how buffering works when you design the user interface to your program. Otherwise, you might find that output (such as progress or prompt messages) doesn't appear when you intended it to, or that input typed by the user is made available by lines instead of by single characters, or other unexpected behavior.
This section deals only with controlling when characters are transmitted between the stream and the file or device, and not with how things like echoing, flow control, and the like are handled on specific classes of devices. For information on common control operations on terminal devices, see section Low-Level Terminal Interface.
You can bypass the stream buffering facilities altogether by using the low-level input and output functions that operate on file descriptors instead. See section Low-Level Input/Output.
There are three different kinds of buffering strategies:
Newly opened streams are normally fully buffered, with one exception: a stream connected to an interactive device such as a terminal is initially line buffered. See section Controlling Which Kind of Buffering, for information on how to select a different kind of buffering.
The use of line buffering for interactive devices implies that output
messages ending in a newline will appear immediately--which is usually
what you want. Output that doesn't end in a newline might or might not
show up immediately, so if you want them to appear immediately, you
should flush buffered output explicitly with fflush, as described
in section Flushing Buffers.
Line buffering is a good default for terminal input as well, because most interactive programs read commands that are normally single lines. The program should be able to execute each line right away. A line buffered stream permits this, whereas a fully buffered stream would always read enough text to fill the buffer before allowing the program to read any of it. Line buffering also fits in with the usual input-editing facilities of most operating systems, which work within a line of input.
Some programs need an unbuffered terminal input stream. These include programs that read single-character commands (like Emacs) and programs that do their own input editing (such as those that use readline). In order to read a character at a time, it is not enough to turn off buffering in the input stream; you must also turn off input editing in the operating system. This requires changing the terminal mode (see section Terminal Modes). If you want to change the terminal modes, you have to do this separately--merely using an unbuffered stream does not change the modes.
Flushing output on a buffered stream means transmitting all accumulated characters to the file. There are many circumstances when buffered output on a stream is flushed automatically:
exit.
See section Normal Termination.
If you want to flush the buffered output at another time, call
fflush, which is declared in the header file `stdio.h'.
Function: int fflush (FILE *stream)
This function causes any buffered output on stream to be delivered
to the file. If stream is a null pointer, then
fflush causes buffered output on all open output streams
to be flushed.
This function returns EOF if a write error occurs, or zero
otherwise.
Compatibility Note: Some brain-damaged operating systems have been known to be so thoroughly fixated on line-oriented input and output that flushing a line buffered stream causes a newline to be written! Fortunately, this "feature" seems to be becoming less common. You do not need to worry about this in the GNU system.
After opening a stream (but before any other operations have been
performed on it), you can explicitly specify what kind of buffering you
want it to have using the setvbuf function.
The facilities listed in this section are declared in the header file `stdio.h'.
Function: int setvbuf (FILE *stream, char *buf, int mode, size_t size)
This function is used to specify that the stream stream should
have the buffering mode mode, which can be either _IOFBF
(for full buffering), _IOLBF (for line buffering), or
_IONBF (for unbuffered input/output).
If you specify a null pointer as the buf argument, then setvbuf
allocates a buffer itself using malloc. This buffer will be freed
when you close the stream.
Otherwise, buf should be a character array that can hold at least
size characters. You should not free the space for this array as
long as the stream remains open and this array remains its buffer. You
should usually either allocate it statically, or malloc
(see section Unconstrained Allocation) the buffer. Using an automatic array
is not a good idea unless you close the file before exiting the block
that declares the array.
While the array remains a stream buffer, the stream I/O functions will use the buffer for their internal purposes. You shouldn't try to access the values in the array directly while the stream is using it for buffering.
The setvbuf function returns zero on success, or a nonzero value
if the value of mode is not valid or if the request could not
be honored.
The value of this macro is an integer constant expression that can be
used as the mode argument to the setvbuf function to
specify that the stream should be fully buffered.
The value of this macro is an integer constant expression that can be
used as the mode argument to the setvbuf function to
specify that the stream should be line buffered.
The value of this macro is an integer constant expression that can be
used as the mode argument to the setvbuf function to
specify that the stream should be unbuffered.
The value of this macro is an integer constant expression that is good
to use for the size argument to setvbuf. This value is
guaranteed to be at least 256.
The value of BUFSIZ is chosen on each system so as to make stream
I/O efficient. So it is a good idea to use BUFSIZ as the size
for the buffer when you call setvbuf.
Actually, you can get an even better value to use for the buffer size
by means of the fstat system call: it is found in the
st_blksize field of the file attributes. See section What the File Attribute Values Mean.
Sometimes people also use BUFSIZ as the allocation size of
buffers used for related purposes, such as strings used to receive a
line of input with fgets (see section Character Input). There is no
particular reason to use BUFSIZ for this instead of any other
integer, except that it might lead to doing I/O in chunks of an
efficient size.
Function: void setbuf (FILE *stream, char *buf)
If buf is a null pointer, the effect of this function is
equivalent to calling setvbuf with a mode argument of
_IONBF. Otherwise, it is equivalent to calling setvbuf
with buf, and a mode of _IOFBF and a size
argument of BUFSIZ.
The setbuf function is provided for compatibility with old code;
use setvbuf in all new programs.
Function: void setbuffer (FILE *stream, char *buf, size_t size)
If buf is a null pointer, this function makes stream unbuffered. Otherwise, it makes stream fully buffered using buf as the buffer. The size argument specifies the length of buf.
This function is provided for compatibility with old BSD code. Use
setvbuf instead.
Function: void setlinebuf (FILE *stream)
This function makes stream be line buffered, and allocates the buffer for you.
This function is provided for compatibility with old BSD code. Use
setvbuf instead.
If you need to use a temporary file in your program, you can use the
tmpfile function to open it. Or you can use the tmpnam
function make a name for a temporary file and then open it in the usual
way with fopen.
These facilities are declared in the header file `stdio.h'.
Function: FILE * tmpfile (void)
This function creates a temporary binary file for update mode, as if by
calling fopen with mode "wb+". The file is deleted
automatically when it is closed or when the program terminates. (On
some other ANSI C systems the file may fail to be deleted if the program
terminates abnormally).
Function: char * tmpnam (char *result)
This function constructs and returns a file name that is a valid file
name and that does not name any existing file. If the result
argument is a null pointer, the return value is a pointer to an internal
static string, which might be modified by subsequent calls. Otherwise,
the result argument should be a pointer to an array of at least
L_tmpnam characters, and the result is written into that array.
It is possible for tmpnam to fail if you call it too many times.
This is because the fixed length of a temporary file name gives room for
only a finite number of different names. If tmpnam fails, it
returns a null pointer.
The value of this macro is an integer constant expression that represents
the minimum allocation size of a string large enough to hold the
file name generated by the tmpnam function.
The macro TMP_MAX is a lower bound for how many temporary names
you can create with tmpnam. You can rely on being able to call
tmpnam at least this many times before it might fail saying you
have made too many temporary file names.
With the GNU library, you can create a very large number of temporary
file names--if you actually create the files, you will probably run out
of disk space before you run out of names. Some other systems have a
fixed, small limit on the number of temporary files. The limit is never
less than 25.
Function: char * tempnam (const char *dir, const char *prefix)
This function generates a unique temporary filename. If prefix is not a null pointer, up to five characters of this string are used as a prefix for the file name.
The directory prefix for the temporary file name is determined by testing each of the following, in sequence. The directory must exist and be writable.
TMPDIR, if it is defined.
P_tmpdir macro.
This function is defined for SVID compatibility.
This macro is the name of the default directory for temporary files.
The GNU library provides ways for you to define additional kinds of streams that do not necessarily correspond to an open file.
One such type of stream takes input from or writes output to a string.
These kinds of streams are used internally to implement the
sprintf and sscanf functions. You can also create such a
stream explicitly, using the functions described in section String Streams.
More generally, you can define streams that do input/output to arbitrary objects using functions supplied by your program. This protocol is discussed in section Programming Your Own Custom Streams.
Portability Note: The facilities described in this section are specific to GNU. Other systems or C implementations might or might not provide equivalent functionality.
The fmemopen and open_memstream functions allow you to do
I/O to a string or memory buffer. These facilities are declared in
`stdio.h'.
Function: FILE * fmemopen (void *buf, size_t size, const char *opentype)
This function opens a stream that allows the access specified by the opentype argument, that reads from or writes to the buffer specified by the argument buf. This array must be at least size bytes long.
If you specify a null pointer as the buf argument, fmemopen
dynamically allocates (as with malloc; see section Unconstrained Allocation) an array size bytes long. This is really only useful
if you are going to write things to the buffer and then read them back
in again, because you have no way of actually getting a pointer to the
buffer (for this, try open_memstream, below). The buffer is
freed when the stream is open.
The argument opentype is the same as in fopen
(See section Opening Streams). If the opentype specifies
append mode, then the initial file position is set to the first null
character in the buffer. Otherwise the initial file position is at the
beginning of the buffer.
When a stream open for writing is flushed or closed, a null character (zero byte) is written at the end of the buffer if it fits. You should add an extra byte to the size argument to account for this. Attempts to write more than size bytes to the buffer result in an error.
For a stream open for reading, null characters (zero bytes) in the buffer do not count as "end of file". Read operations indicate end of file only when the file position advances past size bytes. So, if you want to read characters from a null-terminated string, you should supply the length of the string as the size argument.
Here is an example of using fmemopen to create a stream for
reading from a string:
#include <stdio.h>
static char buffer[] = "foobar";
int
main (void)
{
int ch;
FILE *stream;
stream = fmemopen (buffer, strlen (buffer), "r");
while ((ch = fgetc (stream)) != EOF)
printf ("Got %c\n", ch);
fclose (stream);
return 0;
}
This program produces the following output:
Got f Got o Got o Got b Got a Got r
Function: FILE * open_memstream (char **ptr, size_t *sizeloc)
This function opens a stream for writing to a buffer. The buffer is
allocated dynamically (as with malloc; see section Unconstrained Allocation) and grown as necessary.
When the stream is closed with fclose or flushed with
fflush, the locations ptr and sizeloc are updated to
contain the pointer to the buffer and its size. The values thus stored
remain valid only as long as no further output on the stream takes
place. If you do more output, you must flush the stream again to store
new values before you use them again.
A null character is written at the end of the buffer. This null character is not included in the size value stored at sizeloc.
You can move the stream's file position with fseek (see section File Positioning). Moving the file position past the end of the data
already written fills the intervening space with zeroes.
Here is an example of using open_memstream:
#include <stdio.h>
int
main (void)
{
char *bp;
size_t size;
FILE *stream;
stream = open_memstream (&bp, &size);
fprintf (stream, "hello");
fflush (stream);
printf ("buf = %s, size = %d\n", bp, size);
fprintf (stream, ", world");
fclose (stream);
printf ("buf = %s, size = %d\n", bp, size);
return 0;
}
This program produces the following output:
buf = `hello', size = 5 buf = `hello, world', size = 12
You can open an output stream that puts it data in an obstack. See section Obstacks.
Function: FILE * open_obstack_stream (struct obstack *obstack)
This function opens a stream for writing data into the obstack obstack. This starts an object in the obstack and makes it grow as data is written (see section Growing Objects).
Calling fflush on this stream updates the current size of the
object to match the amount of data that has been written. After a call
to fflush, you can examine the object temporarily.
You can move the file position of an obstack stream with fseek
(see section File Positioning). Moving the file position past the end of
the data written fills the intervening space with zeros.
To make the object permanent, update the obstack with fflush, and
then use obstack_finish to finalize the object and get its address.
The following write to the stream starts a new object in the obstack,
and later writes add to that object until you do another fflush
and obstack_finish.
But how do you find out how long the object is? You can get the length
in bytes by calling obstack_object_size (see section Status of an Obstack), or you can null-terminate the object like this:
obstack_1grow (obstack, 0);
Whichever one you do, you must do it before calling
obstack_finish. (You can do both if you wish.)
Here is a sample function that uses open_obstack_stream:
char *
make_message_string (const char *a, int b)
{
FILE *stream = open_obstack_stream (&message_obstack);
output_task (stream);
fprintf (stream, ": ");
fprintf (stream, a, b);
fprintf (stream, "\n");
fclose (stream);
obstack_1grow (&message_obstack, 0);
return obstack_finish (&message_obstack);
}
This section describes how you can make a stream that gets input from an arbitrary data source or writes output to an arbitrary data sink programmed by you. We call these custom streams.
Inside every custom stream is a special object called the cookie.
This is an object supplied by you which records where to fetch or store
the data read or written. It is up to you to define a data type to use
for the cookie. The stream functions in the library never refer
directly to its contents, and they don't even know what the type is;
they record its address with type void *.
To implement a custom stream, you must specify how to fetch or store the data in the specified place. You do this by defining hook functions to read, write, change "file position", and close the stream. All four of these functions will be passed the stream's cookie so they can tell where to fetch or store the data. The library functions don't know what's inside the cookie, but your functions will know.
When you create a custom stream, you must specify the cookie pointer,
and also the four hook functions stored in a structure of type
struct cookie_io_functions.
These facilities are declared in `stdio.h'.
Data Type: struct cookie_io_functions
This is a structure type that holds the functions that define the communications protocol between the stream and its cookie. It has the following members:
cookie_read_function *read
EOF.
cookie_write_function *write
cookie_seek_function *seek
fseek on this stream return an ESPIPE error.
cookie_close_function *close
Function: FILE * fopencookie (void *cookie, const char *opentype, struct cookie_functions io_functions)
This function actually creates the stream for communicating with the
cookie using the functions in the io_functions argument.
The opentype argument is interpreted as for fopen;
see section Opening Streams. (But note that the "truncate on
open" option is ignored.) The new stream is fully buffered.
The fopencookie function returns the newly created stream, or a null
pointer in case of an error.
Here are more details on how you should define the four hook functions that a custom stream needs.
You should define the function to read data from the cookie as:
ssize_t reader (void *cookie, void *buffer, size_t size)
This is very similar to the read function; see section Input and Output Primitives. Your function should transfer up to size bytes into
the buffer, and return the number of bytes read, or zero to
indicate end-of-file. You can return a value of -1 to indicate
an error.
You should define the function to write data to the cookie as:
ssize_t writer (void *cookie, const void *buffer, size_t size)
This is very similar to the write function; see section Input and Output Primitives. Your function should transfer up to size bytes from
the buffer, and return the number of bytes written. You can return a
value of -1 to indicate an error.
You should define the function to perform seek operations on the cookie as:
int seeker (void *cookie, fpos_t *position, int whence)
For this function, the position and whence arguments are
interpreted as for fgetpos; see section Portable File-Position Functions. In
the GNU library, fpos_t is equivalent to off_t or
long int, and simply represents the number of bytes from the
beginning of the file.
After doing the seek operation, your function should store the resulting
file position relative to the beginning of the file in position.
Your function should return a value of 0 on success and -1
to indicate an error.
You should define the function to do cleanup operations on the cookie appropriate for closing the stream as:
int cleaner (void *cookie)
Your function should return -1 to indicate an error, and 0
otherwise.
Data Type: cookie_read_function
This is the data type that the read function for a custom stream should have. If you declare the function as shown above, this is the type it will have.
Data Type: cookie_write_function
The data type of the write function for a custom stream.
Data Type: cookie_seek_function
The data type of the seek function for a custom stream.
Data Type: cookie_close_function
The data type of the close function for a custom stream.
This chapter describes functions for performing low-level input/output operations on file descriptors. These functions include the primitives for the higher-level I/O functions described in section Input/Output on Streams, as well as functions for performing low-level control operations for which there are no equivalents on streams.
Stream-level I/O is more flexible and usually more convenient; therefore, programmers generally use the descriptor-level functions only when necessary. These are some of the usual reasons:
fileno to get the descriptor
corresponding to a stream.)
This section describes the primitives for opening and closing files
using file descriptors. The open and creat functions are
declared in the header file `fcntl.h', while close is
declared in `unistd.h'.
Function: int open (const char *filename, int flags[, mode_t mode])
The open function creates and returns a new file descriptor
for the file named by filename. Initially, the file position
indicator for the file is at the beginning of the file. The argument
mode is used only when a file is created, but it doesn't hurt
to supply the argument in any case.
The flags argument controls how the file is to be opened. This is a bit mask; you create the value by the bitwise OR of the appropriate parameters (using the `|' operator in C).
The flags argument must include exactly one of these values to specify the file access mode:
O_RDONLY
O_WRONLY
O_RDWR
The flags argument can also include any combination of these flags:
O_APPEND
write operations write the data at the end of
the file, extending it, regardless of the current file position.
O_CREAT
O_EXCL
O_CREAT and O_EXCL are set, then open fails
if the specified file already exists.
O_NOCTTY
O_NONBLOCK
open blocks until
the file is "ready". If O_NONBLOCK is set, open
returns immediately.
The O_NONBLOCK bit also affects read and write: It
permits them to return immediately with a failure status if there is no
input immediately available (read), or if the output can't be
written immediately (write).
O_TRUNC
For more information about these symbolic constants, see section File Status Flags.
The normal return value from open is a non-negative integer file
descriptor. In the case of an error, a value of -1 is returned
instead. In addition to the usual file name syntax errors (see section File Name Errors), the following errno error conditions are defined
for this function:
EACCES
EEXIST
O_CREAT and O_EXCL are set, and the named file already
exists.
EINTR
open operation was interrupted by a signal.
See section Primitives Interrupted by Signals.
EISDIR
EMFILE
ENFILE
ENOENT
O_CREAT is not specified.
ENOSPC
ENXIO
O_NONBLOCK and O_WRONLY are both set in the flags
argument, the file named by filename is a FIFO (see section Pipes and FIFOs), and no process has the file open for reading.
EROFS
O_WRONLY,
O_RDWR, O_CREAT, and O_TRUNC are set in the
flags argument.
The open function is the underlying primitive for the fopen
and freopen functions, that create streams.
Obsolete function: int creat (const char *filename, mode_t mode)
This function is obsolete. The call
creat (filename, mode)
is equivalent to
open (filename, O_WRONLY | O_CREAT | O_TRUNC, mode)
Function: int close (int filedes)
The function close closes the file descriptor filedes.
Closing a file has the following consequences:
The normal return value from close is 0; a value of -1
is returned in case of failure. The following errno error
conditions are defined for this function:
EBADF
EINTR
EINTR properly:
TEMP_FAILURE_RETRY (close (desc));
To close a stream, call fclose (see section Closing Streams) instead
of trying to close its underlying file descriptor with close.
This flushes any buffered output and updates the stream object to
indicate that it is closed.
This section describes the functions for performing primitive input and
output operations on file descriptors: read, write, and
lseek. These functions are declared in the header file
`unistd.h'.
This data type is used to represent the sizes of blocks that can be
read or written in a single operation. It is similar to size_t,
but must be a signed type.
Function: ssize_t read (int filedes, void *buffer, size_t size)
The read function reads up to size bytes from the file
with descriptor filedes, storing the results in the buffer.
(This is not necessarily a character string and there is no terminating
null character added.)
The return value is the number of bytes actually read. This might be less than size; for example, if there aren't that many bytes left in the file or if there aren't that many bytes immediately available. The exact behavior depends on what kind of file it is. Note that reading less than size bytes is not an error.
A value of zero indicates end-of-file (except if the value of the
size argument is also zero). This is not considered an error.
If you keep calling read while at end-of-file, it will keep
returning zero and doing nothing else.
If read returns at least one character, there is no way you can
tell whether end-of-file was reached. But if you did reach the end, the
next read will return zero.
In case of an error, read returns -1. The following
errno error conditions are defined for this function:
EAGAIN
read waits for
some input. But if the O_NONBLOCK flag is set for the file
(see section File Status Flags), read returns immediately without
reading any data, and reports this error.
Compatibility Note: Most versions of BSD Unix use a different
error code for this: EWOULDBLOCK. In the GNU library,
EWOULDBLOCK is an alias for EAGAIN, so it doesn't matter
which name you use.
On some systems, reading a large amount of data from a character special
file can also fail with EAGAIN if the kernel cannot find enough
physical memory to lock down the user's pages. This is limited to
devices that transfer with direct memory access into the user's memory,
which means it does not include terminals, since they always use
separate buffers inside the kernel.
EBADF
EINTR
read was interrupted by a signal while it was waiting for input.
See section Primitives Interrupted by Signals.
EIO
EIO also occurs when a background process tries to read from the
controlling terminal, and the normal action of stopping the process by
sending it a SIGTTIN signal isn't working. This might happen if
signal is being blocked or ignored, or because the process group is
orphaned. See section Job Control, for more information about job control,
and section Signal Handling, for information about signals.
The read function is the underlying primitive for all of the
functions that read from streams, such as fgetc.
Function: ssize_t write (int filedes, const void *buffer, size_t size)
The write function writes up to size bytes from
buffer to the file with descriptor filedes. The data in
buffer is not necessarily a character string and a null character
output like any other character.
The return value is the number of bytes actually written. This is normally the same as size, but might be less (for example, if the physical media being written to fills up).
In the case of an error, write returns -1. The following
errno error conditions are defined for this function:
EAGAIN
write blocks until the write operation is complete.
But if the O_NONBLOCK flag is set for the file (see section Control Operations on Files), it returns immediately without writing any data, and
reports this error. An example of a situation that might cause the
process to block on output is writing to a terminal device that supports
flow control, where output has been suspended by receipt of a STOP
character.
Compatibility Note: Most versions of BSD Unix use a different
error code for this: EWOULDBLOCK. In the GNU library,
EWOULDBLOCK is an alias for EAGAIN, so it doesn't matter
which name you use.
On some systems, writing a large amount of data from a character special
file can also fail with EAGAIN if the kernel cannot find enough
physical memory to lock down the user's pages. This is limited to
devices that transfer with direct memory access into the user's memory,
which means it does not include terminals, since they always use
separate buffers inside the kernel.
EBADF
EFBIG
EINTR
write operation was interrupted by a signal while it was
blocked waiting for completion. See section Primitives Interrupted by Signals.
EIO
EIO also occurs when a background process tries to write to the
controlling terminal, and the normal action of stopping the process by
sending it a SIGTTOU signal isn't working. This might happen if
the signal is being blocked or ignored. See section Job Control, for more
information about job control, and section Signal Handling, for
information about signals.
ENOSPC
EPIPE
SIGPIPE
signal is also sent to the process; see section Signal Handling.
Unless you have arranged to prevent EINTR failures, you should
check errno after each failing call to write, and if the
error was EINTR, you should simply repeat the call.
See section Primitives Interrupted by Signals. The easy way to do this is with the
macro TEMP_FAILURE_RETRY, as follows:
nbytes = TEMP_FAILURE_RETRY (write (desc, buffer, count));
The write function is the underlying primitive for all of the
functions that write to streams, such as fputc.
Just as you can set the file position of a stream with fseek, you
can set the file position of a descriptor with lseek. This
specifies the position in the file for the next read or
write operation. See section File Positioning, for more information
on the file position and what it means.
To read the current file position value from a descriptor, use
lseek (desc, 0, SEEK_CUR).
Function: off_t lseek (int filedes, off_t offset, int whence)
The lseek function is used to change the file position of the
file with descriptor filedes.
The whence argument specifies how the offset should be
interpreted in the same way as for the fseek function, and can be
one of the symbolic constants SEEK_SET, SEEK_CUR, or
SEEK_END.
SEEK_SET
SEEK_CUR
SEEK_END
The return value from lseek is normally the resulting file
position, measured in bytes from the beginning of the file.
You can use this feature together with SEEK_CUR to read the
current file position.
You can set the file position past the current end of the file. This
does not by itself make the file longer; lseek never changes the
file. But subsequent output at that position will extend the file's
size.
If the file position cannot be changed, or the operation is in some way
invalid, lseek returns a value of -1. The following
errno error conditions are defined for this function:
EBADF
EINVAL
ESPIPE
The lseek function is the underlying primitive for the
fseek, ftell and rewind functions, which operate on
streams instead of file descriptors.
You can have multiple descriptors for the same file if you open the file
more than once, or if you duplicate a descriptor with dup.
Descriptors that come from separate calls to open have independent
file positions; using lseek on one descriptor has no effect on the
other. For example,
{
int d1, d2;
char buf[4];
d1 = open ("foo", O_RDONLY);
d2 = open ("foo", O_RDONLY);
lseek (d1, 1024, SEEK_SET);
read (d2, buf, 4);
}
will read the first four characters of the file `foo'. (The error-checking code necessary for a real program has been omitted here for brevity.)
By contrast, descriptors made by duplication share a common file position with the original descriptor that was duplicated. Anything which alters the file position of one of the duplicates, including reading or writing data, affects all of them alike. Thus, for example,
{
int d1, d2, d3;
char buf1[4], buf2[4];
d1 = open ("foo", O_RDONLY);
d2 = dup (d1);
d3 = dup (d2);
lseek (d3, 1024, SEEK_SET);
read (d1, buf1, 4);
read (d2, buf2, 4);
}
will read four characters starting with the 1024'th character of `foo', and then four more characters starting with the 1028'th character.
This is an arithmetic data type used to represent file sizes.
In the GNU system, this is equivalent to fpos_t or long int.
These three aliases for the `SEEK_...' constants exist for the sake of compatibility with older BSD systems. They are defined in two different header files: `fcntl.h' and `sys/file.h'.
L_SET
SEEK_SET.
L_INCR
SEEK_CUR.
L_XTND
SEEK_END.
Given an open file descriptor, you can create a stream for it with the
fdopen function. You can get the underlying file descriptor for
an existing stream with the fileno function. These functions are
declared in the header file `stdio.h'.
Function: FILE * fdopen (int filedes, const char *opentype)
The fdopen function returns a new stream for the file descriptor
filedes.
The opentype argument is interpreted in the same way as for the
fopen function (see section Opening Streams), except that
the `b' option is not permitted; this is because GNU makes no
distinction between text and binary files. Also, "w" and
"w+" do not cause truncation of the file; these have affect only
when opening a file, and in this case the file has already been opened.
You must make sure that the opentype argument matches the actual
mode of the open file descriptor.
The return value is the new stream. If the stream cannot be created (for example, if the modes for the file indicated by the file descriptor do not permit the access specified by the opentype argument), a null pointer is returned instead.
For an example showing the use of the fdopen function,
see section Creating a Pipe.
Function: int fileno (FILE *stream)
This function returns the file descriptor associated with the stream
stream. If an error is detected (for example, if the stream
is not valid) or if stream does not do I/O to a file,
fileno returns -1.
There are also symbolic constants defined in `unistd.h' for the
file descriptors belonging to the standard streams stdin,
stdout, and stderr; see section Standard Streams.
STDIN_FILENO
0, which is the file descriptor for
standard input.
STDOUT_FILENO
1, which is the file descriptor for
standard output.
STDERR_FILENO
2, which is the file descriptor for
standard error output.
You can have multiple file descriptors and streams (let's call both streams and descriptors "channels" for short) connected to the same file, but you must take care to avoid confusion between channels. There are two cases to consider: linked channels that share a single file position value, and independent channels that have their own file positions.
It's best to use just one channel in your program for actual data
transfer to any given file, except when all the access is for input.
For example, if you open a pipe (something you can only do at the file
descriptor level), either do all I/O with the descriptor, or construct a
stream from the descriptor with fdopen and then do all I/O with
the stream.
Channels that come from a single opening share the same file position;
we call them linked channels. Linked channels result when you
make a stream from a descriptor using fdopen, when you get a
descriptor from a stream with fileno, and when you copy a
descriptor with dup or dup2. For files that don't support
random access, such as terminals and pipes, all channels are
effectively linked. On random-access files, all append-type output
streams are effectively linked to each other.
If you have been using a stream for I/O, and you want to do I/O using another channel (either a stream or a descriptor) that is linked to it, you must first clean up the stream that you have been using. See section Cleaning Streams.
Terminating a process, or executing a new program in the process, destroys all the streams in the process. If descriptors linked to these streams persist in other processes, their file positions become undefined as a result. To prevent this, you must clean up the streams before destroying them.
When you open channels (streams or descriptors) separately on a seekable file, each channel has its own file position. These are called independent channels.
The system handles each channel independently. Most of the time, this is quite predictable and natural (especially for input): each channel can read or write sequentially at its own place in the file. The precautions you should take are these:
If you do output to one channel at the end of the file, this will certainly leave the other independent channels positioned somewhere before the new end. If you want them to output at the end, you must set their file positions to end of file, first. (This is not necessary if you use an append-type descriptor or stream; they always output at the current end of the file.) In order to make the end-of-file position accurate, you must clean the output channel you were using, if it is a stream. (This is necessary even if you plan to use an append-type channel next.)
It's impossible for two channels to have separate file pointers for a file that doesn't support random access. Thus, channels for reading or writing such files are always linked, never independent. Append-type channels are also always linked. For these channels, follow the rules for linked channels; see section Linked Channels.
On the GNU system, you can clean up any stream with fclean:
Clean up the stream stream so that its buffer is empty. If stream is doing output, force it out. If stream is doing input, give the data in the buffer back to the system, arranging to reread it.
On other systems, you can use fflush to clean a stream in most
cases.
You can skip the fclean or fflush if you know the stream
is already clean. A stream is clean whenever its buffer is empty. For
example, an unbuffered stream is always clean. An input stream that is
at end-of-file is clean. A line-buffered stream is clean when the last
character output was a newline.
There is one case in which cleaning a stream is impossible on most
systems. This is when the stream is doing input from a file that is not
random-access. Such streams typically read ahead, and when the file is
not random access, there is no way to give back the excess data already
read. When an input stream reads from a random-access file,
fflush does clean the stream, but leaves the file pointer at an
unpredictable place; you must set the file pointer before doing any
further I/O. On the GNU system, using fclean avoids both of
these problems.
Closing an output-only stream also does fflush, so this is a
valid way of cleaning an output stream. On the GNU system, closing an
input stream does fclean.
You need not clean a stream before using its descriptor for control operations such as setting terminal modes; these operations don't affect the file position and are not affected by it. You can use any descriptor for these operations, and all channels are affected simultaneously. However, text already "output" to a stream but still buffered by the stream will be subject to the new terminal modes when subsequently flushed. To make sure "past" output is covered by the terminal settings that were in effect at the time, flush the output streams for that terminal before setting the modes. See section Terminal Modes.
Sometimes a program needs to accept input on multiple input channels whenever input arrives. For example, some workstations may have devices such as a digitizing tablet, function button box, or dial box that are connected via normal asynchronous serial interfaces; good user interface style requires responding immediately to input on any device. Another example is a program that acts as a server to several other processes via pipes or sockets.
You cannot normally use read for this purpose, because this
blocks the program until input is available on one particular file
descriptor; input on other channels won't wake it up. You could set
nonblocking mode and poll each file descriptor in turn, but this is very
inefficient.
A better solution is to use the select function. This blocks the
program until input or output is ready on a specified set of file
descriptors, or until timer expires, whichever comes first. This
facility is declared in the header file `sys/types.h'.
The file descriptor sets for the select function are specified
as fd_set objects. Here is the description of the data type
and some macros for manipulating these objects.
The fd_set data type represents file descriptor sets for the
select function. It is actually a bit array.
The value of this macro is the maximum number of file descriptors that a
fd_set object can hold information about. On systems with a
fixed maximum number, FD_SETSIZE is at least that number. On
some systems, including GNU, there is no absolute limit on the number of
descriptors open, but this macro still has a constant value which
controls the number of bits in an fd_set.
Macro: void FD_ZERO (fd_set *set)
This macro initializes the file descriptor set set to be the empty set.
Macro: void FD_SET (int filedes, fd_set *set)
This macro adds filedes to the file descriptor set set.
Macro: void FD_CLR (int filedes, fd_set *set)
This macro removes filedes from the file descriptor set set.
Macro: int FD_ISSET (int filedes, fd_set *set)
This macro returns a nonzero value (true) if filedes is a member of the the file descriptor set set, and zero (false) otherwise.
Next, here is the description of the select function itself.
Function: int select (int nfds, fd_set *read_fds, fd_set *write_fds, fd_set *except_fds, struct timeval *timeout)
The select function blocks the calling process until there is
activity on any of the specified sets of file descriptors, or until the
timeout period has expired.
The file descriptors specified by the read_fds argument are checked to see if they are ready for reading; the write_fds file descriptors are checked to see if they are ready for writing; and the except_fds file descriptors are checked for exceptional conditions. You can pass a null pointer for any of these arguments if you are not interested in checking for that kind of condition.
"Exceptional conditions" does not mean errors--errors are reported immediately when an erroneous system call is executed, and do not constitute a state of the descriptor. Rather, they include conditions such as the presence of an urgent message on a socket. (See section Sockets, for information on urgent messages.)
The select function checks only the first nfds file
descriptors. The usual thing is to pass FD_SETSIZE as the value
of this argument.
The timeout specifies the maximum time to wait. If you pass a
null pointer for this argument, it means to block indefinitely until one
of the file descriptors is ready. Otherwise, you should provide the
time in struct timeval format; see section High-Resolution Calendar. Specify zero as the time (a struct timeval containing
all zeros) if you want to find out which descriptors are ready without
waiting if none are ready.
The normal return value from select is the total number of ready file
descriptors in all of the sets. Each of the argument sets is overwritten
with information about the descriptors that are ready for the corresponding
operation. Thus, to see if a particular descriptor desc has input,
use FD_ISSET (desc, read_fds) after select returns.
If select returns because the timeout period expires, it returns
a value of zero.
Any signal will cause select to return immediately. So if your
program uses signals, you can't rely on select to keep waiting
for the full time specified. If you want to be sure of waiting for a
particular amount of time, you must check for EINTR and repeat
the select with a newly calculated timeout based on the current
time. See the example below. See also section Primitives Interrupted by Signals.
If an error occurs, select returns -1 and does not modify
the argument file descriptor sets. The following errno error
conditions are defined for this function:
EBADF
EINTR
EINVAL
Portability Note: The select function is a BSD Unix
feature.
Here is an example showing how you can use select to establish a
timeout period for reading from a file descriptor. The input_timeout
function blocks the calling process until input is available on the
file descriptor, or until the timeout period expires.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
int
input_timeout (int filedes, unsigned int seconds)
{
fd_set set;
struct timeval timeout;
/* Initialize the file descriptor set. */
FD_ZERO (&set);
FD_SET (filedes, &set);
/* Initialize the timeout data structure. */
timeout.tv_sec = seconds;
timeout.tv_usec = 0;
/* select returns 0 if timeout, 1 if input available, -1 if error. */
return TEMP_FAILURE_RETRY (select (FD_SETSIZE, &set, NULL, NULL, &timeout));
}
int
main (void)
{
fprintf (stderr, "select returned %d.\n", input_timeout (STDIN_FILENO, 5));
return 0;
}
There is another example showing the use of select to multiplex
input from multiple sockets in section Byte Stream Connection Server Example.
This section describes how you can perform various other operations on
file descriptors, such as inquiring about or setting flags describing
the status of the file descriptor, manipulating record locks, and the
like. All of these operations are performed by the function fcntl.
The second argument to the fcntl function is a command that
specifies which operation to perform. The function and macros that name
various flags that are used with it are declared in the header file
`fcntl.h'. (Many of these flags are also used by the open
function; see section Opening and Closing Files.)
Function: int fcntl (int filedes, int command, ...)
The fcntl function performs the operation specified by
command on the file descriptor filedes. Some commands
require additional arguments to be supplied. These additional arguments
and the return value and error conditions are given in the detailed
descriptions of the individual commands.
Briefly, here is a list of what the various commands are.
F_DUPFD
F_GETFD
F_SETFD
F_GETFL
F_SETFL
F_GETLK
F_SETLK
F_SETLKW
F_SETLK, but wait for completion. See section File Locks.
F_GETOWN
SIGIO signals.
See section Interrupt-Driven Input.
F_SETOWN
SIGIO signals.
See section Interrupt-Driven Input.
You can duplicate a file descriptor, or allocate another file descriptor that refers to the same open file as the original. Duplicate descriptors share one file position and one set of file status flags (see section File Status Flags), but each has its own set of file descriptor flags (see section File Descriptor Flags).
The major use of duplicating a file descriptor is to implement redirection of input or output: that is, to change the file or pipe that a particular file descriptor corresponds to.
You can perform this operation using the fcntl function with the
F_DUPFD command, but there are also convenient functions
dup and dup2 for duplicating descriptors.
The fcntl function and flags are declared in `fcntl.h',
while prototypes for dup and dup2 are in the header file
`unistd.h'.
This function copies descriptor old to the first available
descriptor number (the first number not currently open). It is
equivalent to fcntl (old, F_DUPFD, 0).
Function: int dup2 (int old, int new)
This function copies the descriptor old to descriptor number new.
If old is an invalid descriptor, then dup2 does nothing; it
does not close new. Otherwise, the new duplicate of old
replaces any previous meaning of descriptor new, as if new
were closed first.
If old and new are different numbers, and old is a
valid descriptor number, then dup2 is equivalent to:
close (new); fcntl (old, F_DUPFD, new)
However, dup2 does this atomically; there is no instant in the
middle of calling dup2 at which new is closed and not yet a
duplicate of old.
This macro is used as the command argument to fcntl, to
copy the file descriptor given as the first argument.
The form of the call in this case is:
fcntl (old, F_DUPFD, next_filedes)
The next_filedes argument is of type int and specifies that
the file descriptor returned should be the next available one greater
than or equal to this value.
The return value from fcntl with this command is normally the value
of the new file descriptor. A return value of -1 indicates an
error. The following errno error conditions are defined for
this command:
EBADF
EINVAL
EMFILE
ENFILE is not a possible error code for dup2 because
dup2 does not create a new opening of a file; duplicate
descriptors do not count toward the limit which ENFILE
indicates. EMFILE is possible because it refers to the limit on
distinct descriptor numbers in use in one process.
Here is an example showing how to use dup2 to do redirection.
Typically, redirection of the standard streams (like stdin) is
done by a shell or shell-like program before calling one of the
exec functions (see section Executing a File) to execute a new
program in a child process. When the new program is executed, it
creates and initializes the standard streams to point to the
corresponding file descriptors, before its main function is
invoked.
So, to redirect standard input to a file, the shell could do something like:
pid = fork ();
if (pid == 0)
{
char *filename;
char *program;
int file;
...
file = TEMP_FAILURE_RETRY (open (filename, O_RDONLY));
dup2 (file, STDIN_FILENO);
TEMP_FAILURE_RETRY (close (file));
execv (program, NULL);
}
There is also a more detailed example showing how to implement redirection in the context of a pipeline of processes in section Launching Jobs.
File descriptor flags are miscellaneous attributes of a file descriptor. These flags are associated with particular file descriptors, so that if you have created duplicate file descriptors from a single opening of a file, each descriptor has its own set of flags.
Currently there is just one file descriptor flag: FD_CLOEXEC,
which causes the descriptor to be closed if you use any of the
exec... functions (see section Executing a File).
The symbols in this section are defined in the header file `fcntl.h'.
This macro is used as the command argument to fcntl, to
specify that it should return the file descriptor flags associated
with the filedes argument.
The normal return value from fcntl with this command is a
nonnegative number which can be interpreted as the bitwise OR of the
individual flags (except that currently there is only one flag to use).
In case of an error, fcntl returns -1. The following
errno error conditions are defined for this command:
EBADF
This macro is used as the command argument to fcntl, to
specify that it should set the file descriptor flags associated with the
filedes argument. This requires a third int argument to
specify the new flags, so the form of the call is:
fcntl (filedes, F_SETFD, new_flags)
The normal return value from fcntl with this command is an
unspecified value other than -1, which indicates an error.
The flags and error conditions are the same as for the F_GETFD
command.
The following macro is defined for use as a file descriptor flag with
the fcntl function. The value is an integer constant usable
as a bit mask value.
This flag specifies that the file descriptor should be closed when
an exec function is invoked; see section Executing a File. When
a file descriptor is allocated (as with open or dup),
this bit is initially cleared on the new file descriptor, meaning that
descriptor will survive into the new program after exec.
If you want to modify the file descriptor flags, you should get the
current flags with F_GETFD and modify the value. Don't assume
that the flag listed here is the only ones that are implemented; your
program may be run years from now and more flags may exist then.
For example, here is a function to set or clear the flag FD_CLOEXEC
without altering any other flags:
/* Set theFD_CLOEXECflag of desc if value is nonzero, or clear the flag if value is 0. Return 0 on success, or -1 on error witherrnoset. */ int set_cloexec_flag (int desc, int value) { int oldflags = fcntl (desc, F_GETFD, 0); /* If reading the flags failed, return error indication now. if (oldflags < 0) return oldflags; /* Set just the flag we want to set. */ if (value != 0) oldflags |= FD_CLOEXEC; else oldflags &= ~FD_CLOEXEC; /* Store modified flag word in the descriptor. */ return fcntl (desc, F_SETFD, oldflags); }
File status flags are used to specify attributes of the opening of a file. Unlike the file descriptor flags discussed in section File Descriptor Flags, the file status flags are shared by duplicated file descriptors resulting from a single opening of the file.
The file status flags are initialized by the open function from
the flags argument of the open function. Some of the flags
are meaningful only in open and are not remembered subsequently;
many of the rest cannot subsequently be changed, though you can read
their values by examining the file status flags.
A few file status flags can be changed at any time using fcntl.
These include O_APPEND and O_NONBLOCK.
The symbols in this section are defined in the header file `fcntl.h'.
This macro is used as the command argument to fcntl, to
read the file status flags for the open file with descriptor
filedes.
The normal return value from fcntl with this command is a
nonnegative number which can be interpreted as the bitwise OR of the
individual flags. The flags are encoded like the flags argument
to open (see section Opening and Closing Files), but only the file
access modes and the O_APPEND and O_NONBLOCK flags are
meaningful here. Since the file access modes are not single-bit values,
you can mask off other bits in the returned flags with O_ACCMODE
to compare them.
In case of an error, fcntl returns -1. The following
errno error conditions are defined for this command:
EBADF
This macro is used as the command argument to fcntl, to set
the file status flags for the open file corresponding to the
filedes argument. This command requires a third int
argument to specify the new flags, so the call looks like this:
fcntl (filedes, F_SETFL, new_flags)
You can't change the access mode for the file in this way; that is,
whether the file descriptor was opened for reading or writing. You can
only change the O_APPEND and O_NONBLOCK flags.
The normal return value from fcntl with this command is an
unspecified value other than -1, which indicates an error. The
error conditions are the same as for the F_GETFL command.
The following macros are defined for use in analyzing and constructing file status flag values:
O_APPEND
write operations write the data at the end of the file, extending
it, regardless of the current file position.
O_NONBLOCK
read requests on the file can return immediately with a failure
status if there is no input immediately available, instead of blocking.
Likewise, write requests can also return immediately with a
failure status if the output can't be written immediately.
O_NDELAY
O_NONBLOCK, provided for compatibility with
BSD.
This macro stands for a mask that can be bitwise-ANDed with the file
status flag value to produce a value representing the file access mode.
The mode will be O_RDONLY, O_WRONLY, or O_RDWR.
O_RDONLY
O_WRONLY
O_RDWR
If you want to modify the file status flags, you should get the current
flags with F_GETFL and modify the value. Don't assume that the
flags listed here are the only ones that are implemented; your program
may be run years from now and more flags may exist then. For example,
here is a function to set or clear the flag O_NONBLOCK without
altering any other flags:
/* Set theO_NONBLOCKflag of desc if value is nonzero, or clear the flag if value is 0. Return 0 on success, or -1 on error witherrnoset. */ int set_nonblock_flag (int desc, int value) { int oldflags = fcntl (desc, F_GETFL, 0); /* If reading the flags failed, return error indication now. */ if (oldflags < 0) return oldflags; /* Set just the flag we want to set. */ if (value != 0) oldflags |= O_NONBLOCK; else oldflags &= ~O_NONBLOCK; /* Store modified flag word in the descriptor. */ return fcntl (desc, F_SETFL, oldflags); }
The remaining fcntl commands are used to support record
locking, which permits multiple cooperating programs to prevent each
other from simultaneously accessing parts of a file in error-prone
ways.
An exclusive or write lock gives a process exclusive access for writing to the specified part of the file. While a write lock is in place, no other process can lock that part of the file.
A shared or read lock prohibits any other process from requesting a write lock on the specified part of the file. However, other processes can request read locks.
The read and write functions do not actually check to see
whether there are any locks in place. If you want to implement a
locking protocol for a file shared by multiple processes, your application
must do explicit fcntl calls to request and clear locks at the
appropriate points.
Locks are associated with processes. A process can only have one kind
of lock set for each byte of a given file. When any file descriptor for
that file is closed by the process, all of the locks that process holds
on that file are released, even if the locks were made using other
descriptors that remain open. Likewise, locks are released when a
process exits, and are not inherited by child processes created using
fork (see section Creating a Process).
When making a lock, use a struct flock to specify what kind of
lock and where. This data type and the associated macros for the
fcntl function are declared in the header file `fcntl.h'.
This structure is used with the fcntl function to describe a file
lock. It has these members:
short int l_type
F_RDLCK, F_WRLCK, or
F_UNLCK.
short int l_whence
fseek or
lseek, and specifies what the offset is relative to. Its value
can be one of SEEK_SET, SEEK_CUR, or SEEK_END.
off_t l_start
l_whence member.
off_t l_len
0 is treated specially; it means the region extends to the end of
the file.
pid_t l_pid
fcntl with
the F_GETLK command, but is ignored when making a lock.
This macro is used as the command argument to fcntl, to
specify that it should get information about a lock. This command
requires a third argument of type struct flock * to be passed
to fcntl, so that the form of the call is:
fcntl (filedes, F_GETLK, lockp)
If there is a lock already in place that would block the lock described
by the lockp argument, information about that lock overwrites
*lockp. Existing locks are not reported if they are
compatible with making a new lock as specified. Thus, you should
specify a lock type of F_WRLCK if you want to find out about both
read and write locks, or F_RDLCK if you want to find out about
write locks only.
There might be more than one lock affecting the region specified by the
lockp argument, but fcntl only returns information about
one of them. The l_whence member of the lockp structure is
set to SEEK_SET and the l_start and l_len fields
set to identify the locked region.
If no lock applies, the only change to the lockp structure is to
update the l_type to a value of F_UNLCK.
The normal return value from fcntl with this command is an
unspecified value other than -1, which is reserved to indicate an
error. The following errno error conditions are defined for
this command:
EBADF
EINVAL
This macro is used as the command argument to fcntl, to
specify that it should set or clear a lock. This command requires a
third argument of type struct flock * to be passed to
fcntl, so that the form of the call is:
fcntl (filedes, F_SETLK, lockp)
If the process already has a lock on any part of the region, the old lock
on that part is replaced with the new lock. You can remove a lock
by specifying the a lock type of F_UNLCK.
If the lock cannot be set, fcntl returns immediately with a value
of -1. This function does not block waiting for other processes
to release locks. If fcntl succeeds, it return a value other
than -1.
The following errno error conditions are defined for this
function:
EACCES
EAGAIN
EAGAIN in this case, and other systems
use EACCES; your program should treat them alike, after
F_SETLK.
EBADF
EINVAL
ENOLCK
Well-designed file systems never report this error, because they have no limitation on the number of locks. However, you must still take account of the possibility of this error, as it could result from network access to a file system on another machine.
This macro is used as the command argument to fcntl, to
specify that it should set or clear a lock. It is just like the
F_SETLK command, but causes the process to block (or wait)
until the request can be specified.
This command requires a third argument of type struct flock *, as
for the F_SETLK command.
The fcntl return values and errors are the same as for the
F_SETLK command, but these additional errno error conditions
are defined for this command:
EINTR
EDEADLK
The following macros are defined for use as values for the l_type
member of the flock structure. The values are integer constants.
F_RDLCK
F_WRLCK
F_UNLCK
As an example of a situation where file locking is useful, consider a program that can be run simultaneously by several different users, that logs status information to a common file. One example of such a program might be a game that uses a file to keep track of high scores. Another example might be a program that records usage or accounting information for billing purposes.
Having multiple copies of the program simultaneously writing to the file could cause the contents of the file to become mixed up. But you can prevent this kind of problem by setting a write lock on the file before actually writing to the file.
If the program also needs to read the file and wants to make sure that the contents of the file are in a consistent state, then it can also use a read lock. While the read lock is set, no other process can lock that part of the file for writing.
Remember that file locks are only a voluntary protocol for controlling access to a file. There is still potential for access to the file by programs that don't use the lock protocol.
If you set the FASYNC status flag on a file descriptor
(see section File Status Flags), a SIGIO signal is sent whenever
input or output becomes possible on that file descriptor. The process
or process group to receive the signal can be selected by using the
F_SETOWN command to the fcntl function. If the file
descriptor is a socket, this also selects the recipient of SIGURG
signals that are delivered when out-of-band data arrives on that socket;
see section Out-of-Band Data.
If the file descriptor corresponds to a terminal device, then SIGIO
signals are sent to the foreground process group of the terminal.
See section Job Control.
The symbols in this section are defined in the header file `fcntl.h'.
This macro is used as the command argument to fcntl, to
specify that it should get information about the process or process
group to which SIGIO signals are sent. (For a terminal, this is
actually the foreground process group ID, which you can get using
tcgetpgrp; see section Functions for Controlling Terminal Access.)
The return value is interpreted as a process ID; if negative, its absolute value is the process group ID.
The following errno error condition is defined for this command:
EBADF
This macro is used as the command argument to fcntl, to
specify that it should set the process or process group to which
SIGIO signals are sent. This command requires a third argument
of type pid_t to be passed to fcntl, so that the form of
the call is:
fcntl (filedes, F_SETOWN, pid)
The pid argument should be a process ID. You can also pass a negative number whose absolute value is a process group ID.
The return value from fcntl with this command is -1
in case of error and some other value if successful. The following
errno error conditions are defined for this command:
EBADF
ESRCH
This chapter describes the GNU C library's functions for manipulating files. Unlike the input and output functions described in section Input/Output on Streams and section Low-Level Input/Output, these functions are concerned with operating on the files themselves, rather than on their contents.
Among the facilities described in this chapter are functions for examining or modifying directories, functions for renaming and deleting files, and functions for examining and setting file attributes such as access permissions and modification times.
Each process has associated with it a directory, called its current working directory or simply working directory, that is used in the resolution of relative file names (see section File Name Resolution).
When you log in and begin a new session, your working directory is
initially set to the home directory associated with your login account
in the system user database. You can find any user's home directory
using the getpwuid or getpwnam functions; see section User Database.
Users can change the working directory using shell commands like
cd. The functions described in this section are the primitives
used by those commands and by other programs for examining and changing
the working directory.
Prototypes for these functions are declared in the header file `unistd.h'.
Function: char * getcwd (char *buffer, size_t size)
The getcwd function returns an absolute file name representing
the current working directory, storing it in the character array
buffer that you provide. The size argument is how you tell
the system the allocation size of buffer.
The GNU library version of this function also permits you to specify a
null pointer for the buffer argument. Then getcwd
allocates a buffer automatically, as with malloc
(see section Unconstrained Allocation). If the size is greater than
zero, then the buffer is that large; otherwise, the buffer is as large
as necessary to hold the result.
The return value is buffer on success and a null pointer on failure.
The following errno error conditions are defined for this function:
EINVAL
ERANGE
EACCES
Here is an example showing how you could implement the behavior of GNU's
getcwd (NULL, 0) using only the standard behavior of
getcwd:
char *
gnu_getcwd ()
{
int size = 100;
char *buffer = (char *) xmalloc (size);
while (1)
{
char *value = getcwd (buffer, size);
if (value != 0)
return buffer;
size *= 2;
free (buffer);
buffer = (char *) xmalloc (size);
}
}
See section Examples of malloc, for information about xmalloc, which is
not a library function but is a customary name used in most GNU
software.
Function: char * getwd (char *buffer)
This is similar to getcwd. The GNU library provides getwd
for backwards compatibility with BSD. The buffer should be a
pointer to an array at least PATH_MAX bytes long.
Function: int chdir (const char *filename)
This function is used to set the process's working directory to filename.
The normal, successful return value from chdir is 0. A
value of -1 is returned to indicate an error. The errno
error conditions defined for this function are the usual file name
syntax errors (see section File Name Errors), plus ENOTDIR if the
file filename is not a directory.
The facilities described in this section let you read the contents of a directory file. This is useful if you want your program to list all the files in a directory, perhaps as part of a menu.
The opendir function opens a directory stream whose
elements are directory entries. You use the readdir function on
the directory stream to retrieve these entries, represented as
struct dirent objects. The name of the file for each entry is
stored in the d_name member of this structure. There are obvious
parallels here to the stream facilities for ordinary files, described in
section Input/Output on Streams.
This section describes what you find in a single directory entry, as you might obtain it from a directory stream. All the symbols are declared in the header file `dirent.h'.
This is a structure type used to return information about directory entries. It contains the following fields:
char *d_name
ino_t d_fileno
d_ino.
size_t d_namlen
This structure may contain additional members in the future.
When a file has multiple names, each name has its own directory entry.
The only way you can tell that the directory entries belong to a
single file is that they have the same value for the d_fileno
field.
File attributes such as size, modification times, and the like are part of the file itself, not any particular directory entry. See section File Attributes.
This section describes how to open a directory stream. All the symbols are declared in the header file `dirent.h'.
The DIR data type represents a directory stream.
You shouldn't ever allocate objects of the struct dirent or
DIR data types, since the directory access functions do that for
you. Instead, you refer to these objects using the pointers returned by
the following functions.
Function: DIR * opendir (const char *dirname)
The opendir function opens and returns a directory stream for
reading the directory whose file name is dirname. The stream has
type DIR *.
If unsuccessful, opendir returns a null pointer. In addition to
the usual file name syntax errors (see section File Name Errors), the
following errno error conditions are defined for this function:
EACCES
dirname.
EMFILE
ENFILE
The DIR type is typically implemented using a file descriptor,
and the opendir function in terms of the open function.
See section Low-Level Input/Output. Directory streams and the underlying
file descriptors are closed on exec (see section Executing a File).
This section describes how to read directory entries from a directory stream, and how to close the stream when you are done with it. All the symbols are declared in the header file `dirent.h'.
Function: struct dirent * readdir (DIR *dirstream)
This function reads the next entry from the directory. It normally returns a pointer to a structure containing information about the file. This structure is statically allocated and can be rewritten by a subsequent call.
Portability Note: On some systems, readdir may not
return entries for `.' and `..'. See section File Name Resolution.
If there are no more entries in the directory or an error is detected,
readdir returns a null pointer. The following errno error
conditions are defined for this function:
EBADF
Function: int closedir (DIR *dirstream)
This function closes the directory stream dirstream. It returns
0 on success and -1 on failure.
The following errno error conditions are defined for this
function:
EBADF
Here's a simple program that prints the names of the files in the current working directory:
#include <stddef.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
int
main (void)
{
DIR *dp;
struct dirent *ep;
dp = opendir ("./");
if (dp != NULL)
{
while (ep = readdir (dp))
puts (ep->d_name);
(void) closedir (dp);
}
else
puts ("Couldn't open the directory.");
return 0;
}
The order in which files appear in a directory tends to be fairly random. A more useful program would sort the entries (perhaps by alphabetizing them) before printing them; see section Array Sort Function
This section describes how to reread parts of a directory that you have already read from an open directory stream. All the symbols are declared in the header file `dirent.h'.
Function: void rewinddir (DIR *dirstream)
The rewinddir function is used to reinitialize the directory
stream dirstream, so that if you call readdir it
returns information about the first entry in the directory again. This
function also notices if files have been added or removed to the
directory since it was opened with opendir. (Entries for these
files might or might not be returned by readdir if they were
added or removed since you last called opendir or
rewinddir.)
Function: off_t telldir (DIR *dirstream)
The telldir function returns the file position of the directory
stream dirstream. You can use this value with seekdir to
restore the directory stream to that position.
Function: void seekdir (DIR *dirstream, off_t pos)
The seekdir function sets the file position of the directory
stream dirstream to pos. The value pos must be the
result of a previous call to telldir on this particular stream;
closing and reopening the directory can invalidate values returned by
telldir.
In POSIX systems, one file can have many names at the same time. All of the names are equally real, and no one of them is preferred to the others.
To add a name to a file, use the link function. (The new name is
also called a hard link to the file.) Creating a new link to a
file does not copy the contents of the file; it simply makes a new name
by which the file can be known, in addition to the file's existing name
or names.
One file can have names in several directories, so the the organization of the file system is not a strict hierarchy or tree.
Since a particular file exists within a single file system, all its
names must be in directories in that file system. link reports
an error if you try to make a hard link to the file from another file
system.
The prototype for the link function is declared in the header
file `unistd.h'.
Function: int link (const char *oldname, const char *newname)
The link function makes a new link to the existing file named by
oldname, under the new name newname.
This function returns a value of 0 if it is successful and
-1 on failure. In addition to the usual file name syntax errors
(see section File Name Errors) for both oldname and newname, the
following errno error conditions are defined for this function:
EACCES
EEXIST
EMLINK
LINK_MAX; see
section Limits on File System Capacity.)
Well-designed file systems never report this error, because they permit more links than your disk could possibly hold. However, you must still take account of the possibility of this error, as it could result from network access to a file system on another machine.
ENOENT
ENOSPC
EPERM
EROFS
EXDEV
The GNU system supports soft links or symbolic links. This is a kind of "file" that is essentially a pointer to another file name. Unlike hard links, symbolic links can be made to directories or across file systems with no restrictions. You can also make a symbolic link to a name which is not the name of any file. (Opening this link will fail until a file by that name is created.) Likewise, if the symbolic link points to an existing file which is later deleted, the symbolic link continues to point to the same file name even though the name no longer names any file.
The reason symbolic links work the way they do is that special things
happen when you try to open the link. The open function realizes
you have specified the name of a link, reads the file name contained in
the link, and opens that file name instead. The stat function
likewise operates on the file that the symbolic link points to, instead
of on the link itself. So does link, the function that makes a
hard link.
By contrast, other operations such as deleting or renaming the file
operate on the link itself. The functions readlink and
lstat also refrain from following symbolic links, because
their purpose is to obtain information about the link.
Prototypes for the functions listed in this section are in `unistd.h'.
Function: int symlink (const char *oldname, const char *newname)
The symlink function makes a symbolic link to oldname named
newname.
The normal return value from symlink is 0. A return value
of -1 indicates an error. In addition to the usual file name
syntax errors (see section File Name Errors), the following errno
error conditions are defined for this function:
EEXIST
EROFS
ENOSPC
EIO
Function: int readlink (const char *filename, char *buffer, size_t size)
The readlink function gets the value of the symbolic link
filename. The file name that the link points to is copied into
buffer. This file name string is not null-terminated;
readlink normally returns the number of characters copied. The
size argument specifies the maximum number of characters to copy,
usually the allocation size of buffer.
If the return value equals size, you cannot tell whether or not
there was room to return the entire name. So make a bigger buffer and
call readlink again. Here is an example:
char *
readlink_malloc (char *filename)
{
int size = 100;
while (1)
{
char *buffer = (char *) xmalloc (size);
int nchars = readlink (filename, buffer, size);
if (nchars < size)
return buffer;
free (buffer);
size *= 2;
}
}
A value of -1 is returned in case of error. In addition to the
usual file name syntax errors (see section File Name Errors), the following
errno error conditions are defined for this function:
EINVAL
EIO
You can delete a file with the functions unlink or remove.
(These names are synonymous.)
Deletion actually deletes a file name. If this is the file's only name, then the file is deleted as well. If the file has other names as well (see section Hard Links), it remains accessible under its other names.
Function: int unlink (const char *filename)
The unlink function deletes the file name filename. If
this is a file's sole name, the file itself is also deleted. (Actually,
if any process has the file open when this happens, deletion is
postponed until all processes have closed the file.)
The function unlink is declared in the header file `unistd.h'.
This function returns 0 on successful completion, and -1
on error. In addition to the usual file name syntax errors
(see section File Name Errors), the following errno error conditions are
defined for this function:
EACCESS
EBUSY
ENOENT
EPERM
unlink cannot be used to delete the name of a
directory, or can only be used this way by a privileged user.
To avoid such problems, use rmdir to delete directories.
EROFS
Function: int remove (const char *filename)
The remove function is another name for unlink.
remove is the ANSI C name, whereas unlink is the POSIX.1
name. The name remove is declared in `stdio.h'.
Function: int rmdir (const char *filename)
The rmdir function deletes a directory. The directory must be
empty before it can be removed; in other words, it can only contain
entries for `.' and `..'.
In most other respects, rmdir behaves like unlink. There
are two additional errno error conditions defined for
rmdir:
EEXIST
ENOTEMPTY
These two error codes are synonymous; some systems use one, and some use the other.
The prototype for this function is declared in the header file `unistd.h'.
The rename function is used to change a file's name.
Function: int rename (const char *oldname, const char *newname)
The rename function renames the file name oldname with
newname. The file formerly accessible under the name
oldname is afterward accessible as newname instead. (If the
file had any other names aside from oldname, it continues to have
those names.)
The directory containing the name newname must be on the same file system as the file (as indicated by the name oldname).
One special case for rename is when oldname and
newname are two names for the same file. The consistent way to
handle this case is to delete oldname. However, POSIX says that
in this case rename does nothing and reports success--which is
inconsistent. We don't know what your operating system will do. The
GNU system, when completed, will probably do the right thing (delete
oldname) unless you explicitly request strict POSIX compatibility
"even when it hurts".
If the oldname is not a directory, then any existing file named
newname is removed during the renaming operation. However, if
newname is the name of a directory, rename fails in this
case.
If the oldname is a directory, then either newname must not
exist or it must name a directory that is empty. In the latter case,
the existing directory named newname is deleted first. The name
newname must not specify a subdirectory of the directory
oldname which is being renamed.
One useful feature of rename is that the meaning of the name
newname changes "atomically" from any previously existing file
by that name to its new meaning (the file that was called
oldname). There is no instant at which newname is
nonexistent "in between" the old meaning and the new meaning.
If rename fails, it returns -1. In addition to the usual
file name syntax errors (see section File Name Errors), the following
errno error conditions are defined for this function:
EACCES
EBUSY
EEXIST
ENOTEMPTY
EINVAL
EISDIR
EMLINK
Well-designed file systems never report this error, because they permit more links than your disk could possibly hold. However, you must still take account of the possibility of this error, as it could result from network access to a file system on another machine.
ENOENT
ENOSPC
EROFS
EXDEV
Directories are created with the mkdir function. (There is also
a shell command mkdir which does the same thing.)
Function: int mkdir (const char *filename, mode_t mode)
The mkdir function creates a new, empty directory whose name is
filename.
The argument mode specifies the file permissions for the new directory file. See section The Mode Bits for Access Permission, for more information about this.
A return value of 0 indicates successful completion, and
-1 indicates failure. In addition to the usual file name syntax
errors (see section File Name Errors), the following errno error
conditions are defined for this function:
EACCES
EEXIST
EMLINK
Well-designed file systems never report this error, because they permit more links than your disk could possibly hold. However, you must still take account of the possibility of this error, as it could result from network access to a file system on another machine.
ENOSPC
EROFS
To use this function, your program should include the header file `sys/stat.h'.
When you issue an `ls -l' shell command on a file, it gives you information about the size of the file, who owns it, when it was last modified, and the like. This kind of information is called the file attributes; it is associated with the file itself and not a particular one of its names.
This section contains information about how you can inquire about and modify these attributes of files.
When you read the attributes of a file, they come back in a structure
called struct stat. This section describes the names of the
attributes, their data types, and what they mean. For the functions
to read the attributes of a file, see section Reading the Attributes of a File.
The header file `sys/stat.h' declares all the symbols defined in this section.
The stat structure type is used to return information about the
attributes of a file. It contains at least the following members:
mode_t