Blame manual/string.texi

Packit 6c4009
@node String and Array Utilities, Character Set Handling, Character Handling, Top
Packit 6c4009
@c %MENU% Utilities for copying and comparing strings and arrays
Packit 6c4009
@chapter String and Array Utilities
Packit 6c4009
Packit 6c4009
Operations on strings (null-terminated byte sequences) are an important part of
Packit 6c4009
many programs.  @Theglibc{} provides an extensive set of string
Packit 6c4009
utility functions, including functions for copying, concatenating,
Packit 6c4009
comparing, and searching strings.  Many of these functions can also
Packit 6c4009
operate on arbitrary regions of storage; for example, the @code{memcpy}
Packit 6c4009
function can be used to copy the contents of any kind of array.
Packit 6c4009
Packit 6c4009
It's fairly common for beginning C programmers to ``reinvent the wheel''
Packit 6c4009
by duplicating this functionality in their own code, but it pays to
Packit 6c4009
become familiar with the library functions and to make use of them,
Packit 6c4009
since this offers benefits in maintenance, efficiency, and portability.
Packit 6c4009
Packit 6c4009
For instance, you could easily compare one string to another in two
Packit 6c4009
lines of C code, but if you use the built-in @code{strcmp} function,
Packit 6c4009
you're less likely to make a mistake.  And, since these library
Packit 6c4009
functions are typically highly optimized, your program may run faster
Packit 6c4009
too.
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Representation of Strings::   Introduction to basic concepts.
Packit 6c4009
* String/Array Conventions::    Whether to use a string function or an
Packit 6c4009
				 arbitrary array function.
Packit 6c4009
* String Length::               Determining the length of a string.
Packit 6c4009
* Copying Strings and Arrays::  Functions to copy strings and arrays.
Packit 6c4009
* Concatenating Strings::       Functions to concatenate strings while copying.
Packit 6c4009
* Truncating Strings::          Functions to truncate strings while copying.
Packit 6c4009
* String/Array Comparison::     Functions for byte-wise and character-wise
Packit 6c4009
				 comparison.
Packit 6c4009
* Collation Functions::         Functions for collating strings.
Packit 6c4009
* Search Functions::            Searching for a specific element or substring.
Packit 6c4009
* Finding Tokens in a String::  Splitting a string into tokens by looking
Packit 6c4009
				 for delimiters.
Packit 6c4009
* Erasing Sensitive Data::      Clearing memory which contains sensitive
Packit 6c4009
                                 data, after it's no longer needed.
Packit 6c4009
* Shuffling Bytes::             Or how to flash-cook a string.
Packit 6c4009
* Obfuscating Data::            Reversibly obscuring data from casual view.
Packit 6c4009
* Encode Binary Data::          Encoding and Decoding of Binary Data.
Packit 6c4009
* Argz and Envz Vectors::       Null-separated string vectors.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node Representation of Strings
Packit 6c4009
@section Representation of Strings
Packit 6c4009
@cindex string, representation of
Packit 6c4009
Packit 6c4009
This section is a quick summary of string concepts for beginning C
Packit 6c4009
programmers.  It describes how strings are represented in C
Packit 6c4009
and some common pitfalls.  If you are already familiar with this
Packit 6c4009
material, you can skip this section.
Packit 6c4009
Packit 6c4009
@cindex string
Packit 6c4009
A @dfn{string} is a null-terminated array of bytes of type @code{char},
Packit 6c4009
including the terminating null byte.  String-valued
Packit 6c4009
variables are usually declared to be pointers of type @code{char *}.
Packit 6c4009
Such variables do not include space for the text of a string; that has
Packit 6c4009
to be stored somewhere else---in an array variable, a string constant,
Packit 6c4009
or dynamically allocated memory (@pxref{Memory Allocation}).  It's up to
Packit 6c4009
you to store the address of the chosen memory space into the pointer
Packit 6c4009
variable.  Alternatively you can store a @dfn{null pointer} in the
Packit 6c4009
pointer variable.  The null pointer does not point anywhere, so
Packit 6c4009
attempting to reference the string it points to gets an error.
Packit 6c4009
Packit 6c4009
@cindex multibyte character
Packit 6c4009
@cindex multibyte string
Packit 6c4009
@cindex wide string
Packit 6c4009
A @dfn{multibyte character} is a sequence of one or more bytes that
Packit 6c4009
represents a single character using the locale's encoding scheme; a
Packit 6c4009
null byte always represents the null character.  A @dfn{multibyte
Packit 6c4009
string} is a string that consists entirely of multibyte
Packit 6c4009
characters.  In contrast, a @dfn{wide string} is a null-terminated
Packit 6c4009
sequence of @code{wchar_t} objects.  A wide-string variable is usually
Packit 6c4009
declared to be a pointer of type @code{wchar_t *}, by analogy with
Packit 6c4009
string variables and @code{char *}.  @xref{Extended Char Intro}.
Packit 6c4009
Packit 6c4009
@cindex null byte
Packit 6c4009
@cindex null wide character
Packit 6c4009
By convention, the @dfn{null byte}, @code{'\0'},
Packit 6c4009
marks the end of a string and the @dfn{null wide character},
Packit 6c4009
@code{L'\0'}, marks the end of a wide string.  For example, in
Packit 6c4009
testing to see whether the @code{char *} variable @var{p} points to a
Packit 6c4009
null byte marking the end of a string, you can write
Packit 6c4009
@code{!*@var{p}} or @code{*@var{p} == '\0'}.
Packit 6c4009
Packit 6c4009
A null byte is quite different conceptually from a null pointer,
Packit 6c4009
although both are represented by the integer constant @code{0}.
Packit 6c4009
Packit 6c4009
@cindex string literal
Packit 6c4009
A @dfn{string literal} appears in C program source as a multibyte
Packit 6c4009
string between double-quote characters (@samp{"}).  If the
Packit 6c4009
initial double-quote character is immediately preceded by a capital
Packit 6c4009
@samp{L} (ell) character (as in @code{L"foo"}), it is a wide string
Packit 6c4009
literal.  String literals can also contribute to @dfn{string
Packit 6c4009
concatenation}: @code{"a" "b"} is the same as @code{"ab"}.
Packit 6c4009
For wide strings one can use either
Packit 6c4009
@code{L"a" L"b"} or @code{L"a" "b"}.  Modification of string literals is
Packit 6c4009
not allowed by the GNU C compiler, because literals are placed in
Packit 6c4009
read-only storage.
Packit 6c4009
Packit 6c4009
Arrays that are declared @code{const} cannot be modified
Packit 6c4009
either.  It's generally good style to declare non-modifiable string
Packit 6c4009
pointers to be of type @code{const char *}, since this often allows the
Packit 6c4009
C compiler to detect accidental modifications as well as providing some
Packit 6c4009
amount of documentation about what your program intends to do with the
Packit 6c4009
string.
Packit 6c4009
Packit 6c4009
The amount of memory allocated for a byte array may extend past the null byte
Packit 6c4009
that marks the end of the string that the array contains.  In this
Packit 6c4009
document, the term @dfn{allocated size} is always used to refer to the
Packit 6c4009
total amount of memory allocated for an array, while the term
Packit 6c4009
@dfn{length} refers to the number of bytes up to (but not including)
Packit 6c4009
the terminating null byte.  Wide strings are similar, except their
Packit 6c4009
sizes and lengths count wide characters, not bytes.
Packit 6c4009
@cindex length of string
Packit 6c4009
@cindex allocation size of string
Packit 6c4009
@cindex size of string
Packit 6c4009
@cindex string length
Packit 6c4009
@cindex string allocation
Packit 6c4009
Packit 6c4009
A notorious source of program bugs is trying to put more bytes into a
Packit 6c4009
string than fit in its allocated size.  When writing code that extends
Packit 6c4009
strings or moves bytes into a pre-allocated array, you should be
Packit 6c4009
very careful to keep track of the length of the text and make explicit
Packit 6c4009
checks for overflowing the array.  Many of the library functions
Packit 6c4009
@emph{do not} do this for you!  Remember also that you need to allocate
Packit 6c4009
an extra byte to hold the null byte that marks the end of the
Packit 6c4009
string.
Packit 6c4009
Packit 6c4009
@cindex single-byte string
Packit 6c4009
@cindex multibyte string
Packit 6c4009
Originally strings were sequences of bytes where each byte represented a
Packit 6c4009
single character.  This is still true today if the strings are encoded
Packit 6c4009
using a single-byte character encoding.  Things are different if the
Packit 6c4009
strings are encoded using a multibyte encoding (for more information on
Packit 6c4009
encodings see @ref{Extended Char Intro}).  There is no difference in
Packit 6c4009
the programming interface for these two kind of strings; the programmer
Packit 6c4009
has to be aware of this and interpret the byte sequences accordingly.
Packit 6c4009
Packit 6c4009
But since there is no separate interface taking care of these
Packit 6c4009
differences the byte-based string functions are sometimes hard to use.
Packit 6c4009
Since the count parameters of these functions specify bytes a call to
Packit 6c4009
@code{memcpy} could cut a multibyte character in the middle and put an
Packit 6c4009
incomplete (and therefore unusable) byte sequence in the target buffer.
Packit 6c4009
Packit 6c4009
@cindex wide string
Packit 6c4009
To avoid these problems later versions of the @w{ISO C} standard
Packit 6c4009
introduce a second set of functions which are operating on @dfn{wide
Packit 6c4009
characters} (@pxref{Extended Char Intro}).  These functions don't have
Packit 6c4009
the problems the single-byte versions have since every wide character is
Packit 6c4009
a legal, interpretable value.  This does not mean that cutting wide
Packit 6c4009
strings at arbitrary points is without problems.  It normally
Packit 6c4009
is for alphabet-based languages (except for non-normalized text) but
Packit 6c4009
languages based on syllables still have the problem that more than one
Packit 6c4009
wide character is necessary to complete a logical unit.  This is a
Packit 6c4009
higher level problem which the @w{C library} functions are not designed
Packit 6c4009
to solve.  But it is at least good that no invalid byte sequences can be
Packit 6c4009
created.  Also, the higher level functions can also much more easily operate
Packit 6c4009
on wide characters than on multibyte characters so that a common strategy
Packit 6c4009
is to use wide characters internally whenever text is more than simply
Packit 6c4009
copied.
Packit 6c4009
Packit 6c4009
The remaining of this chapter will discuss the functions for handling
Packit 6c4009
wide strings in parallel with the discussion of
Packit 6c4009
strings since there is almost always an exact equivalent
Packit 6c4009
available.
Packit 6c4009
Packit 6c4009
@node String/Array Conventions
Packit 6c4009
@section String and Array Conventions
Packit 6c4009
Packit 6c4009
This chapter describes both functions that work on arbitrary arrays or
Packit 6c4009
blocks of memory, and functions that are specific to strings and wide
Packit 6c4009
strings.
Packit 6c4009
Packit 6c4009
Functions that operate on arbitrary blocks of memory have names
Packit 6c4009
beginning with @samp{mem} and @samp{wmem} (such as @code{memcpy} and
Packit 6c4009
@code{wmemcpy}) and invariably take an argument which specifies the size
Packit 6c4009
(in bytes and wide characters respectively) of the block of memory to
Packit 6c4009
operate on.  The array arguments and return values for these functions
Packit 6c4009
have type @code{void *} or @code{wchar_t}.  As a matter of style, the
Packit 6c4009
elements of the arrays used with the @samp{mem} functions are referred
Packit 6c4009
to as ``bytes''.  You can pass any kind of pointer to these functions,
Packit 6c4009
and the @code{sizeof} operator is useful in computing the value for the
Packit 6c4009
size argument.  Parameters to the @samp{wmem} functions must be of type
Packit 6c4009
@code{wchar_t *}.  These functions are not really usable with anything
Packit 6c4009
but arrays of this type.
Packit 6c4009
Packit 6c4009
In contrast, functions that operate specifically on strings and wide
Packit 6c4009
strings have names beginning with @samp{str} and @samp{wcs}
Packit 6c4009
respectively (such as @code{strcpy} and @code{wcscpy}) and look for a
Packit 6c4009
terminating null byte or null wide character instead of requiring an explicit
Packit 6c4009
size argument to be passed.  (Some of these functions accept a specified
Packit 6c4009
maximum length, but they also check for premature termination.)
Packit 6c4009
The array arguments and return values for these
Packit 6c4009
functions have type @code{char *} and @code{wchar_t *} respectively, and
Packit 6c4009
the array elements are referred to as ``bytes'' and ``wide
Packit 6c4009
characters''.
Packit 6c4009
Packit 6c4009
In many cases, there are both @samp{mem} and @samp{str}/@samp{wcs}
Packit 6c4009
versions of a function.  The one that is more appropriate to use depends
Packit 6c4009
on the exact situation.  When your program is manipulating arbitrary
Packit 6c4009
arrays or blocks of storage, then you should always use the @samp{mem}
Packit 6c4009
functions.  On the other hand, when you are manipulating
Packit 6c4009
strings it is usually more convenient to use the @samp{str}/@samp{wcs}
Packit 6c4009
functions, unless you already know the length of the string in advance.
Packit 6c4009
The @samp{wmem} functions should be used for wide character arrays with
Packit 6c4009
known size.
Packit 6c4009
Packit 6c4009
@cindex wint_t
Packit 6c4009
@cindex parameter promotion
Packit 6c4009
Some of the memory and string functions take single characters as
Packit 6c4009
arguments.  Since a value of type @code{char} is automatically promoted
Packit 6c4009
into a value of type @code{int} when used as a parameter, the functions
Packit 6c4009
are declared with @code{int} as the type of the parameter in question.
Packit 6c4009
In case of the wide character functions the situation is similar: the
Packit 6c4009
parameter type for a single wide character is @code{wint_t} and not
Packit 6c4009
@code{wchar_t}.  This would for many implementations not be necessary
Packit 6c4009
since @code{wchar_t} is large enough to not be automatically
Packit 6c4009
promoted, but since the @w{ISO C} standard does not require such a
Packit 6c4009
choice of types the @code{wint_t} type is used.
Packit 6c4009
Packit 6c4009
@node String Length
Packit 6c4009
@section String Length
Packit 6c4009
Packit 6c4009
You can get the length of a string using the @code{strlen} function.
Packit 6c4009
This function is declared in the header file @file{string.h}.
Packit 6c4009
@pindex string.h
Packit 6c4009
Packit 6c4009
@deftypefun size_t strlen (const char *@var{s})
Packit 6c4009
@standards{ISO, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{strlen} function returns the length of the
Packit 6c4009
string @var{s} in bytes.  (In other words, it returns the offset of the
Packit 6c4009
terminating null byte within the array.)
Packit 6c4009
Packit 6c4009
For example,
Packit 6c4009
@smallexample
Packit 6c4009
strlen ("hello, world")
Packit 6c4009
    @result{} 12
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
When applied to an array, the @code{strlen} function returns
Packit 6c4009
the length of the string stored there, not its allocated size.  You can
Packit 6c4009
get the allocated size of the array that holds a string using
Packit 6c4009
the @code{sizeof} operator:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
char string[32] = "hello, world";
Packit 6c4009
sizeof (string)
Packit 6c4009
    @result{} 32
Packit 6c4009
strlen (string)
Packit 6c4009
    @result{} 12
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
But beware, this will not work unless @var{string} is the
Packit 6c4009
array itself, not a pointer to it.  For example:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
char string[32] = "hello, world";
Packit 6c4009
char *ptr = string;
Packit 6c4009
sizeof (string)
Packit 6c4009
    @result{} 32
Packit 6c4009
sizeof (ptr)
Packit 6c4009
    @result{} 4  /* @r{(on a machine with 4 byte pointers)} */
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
This is an easy mistake to make when you are working with functions that
Packit 6c4009
take string arguments; those arguments are always pointers, not arrays.
Packit 6c4009
Packit 6c4009
It must also be noted that for multibyte encoded strings the return
Packit 6c4009
value does not have to correspond to the number of characters in the
Packit 6c4009
string.  To get this value the string can be converted to wide
Packit 6c4009
characters and @code{wcslen} can be used or something like the following
Packit 6c4009
code can be used:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
/* @r{The input is in @code{string}.}
Packit 6c4009
   @r{The length is expected in @code{n}.}  */
Packit 6c4009
@{
Packit 6c4009
  mbstate_t t;
Packit 6c4009
  char *scopy = string;
Packit 6c4009
  /* In initial state.  */
Packit 6c4009
  memset (&t, '\0', sizeof (t));
Packit 6c4009
  /* Determine number of characters.  */
Packit 6c4009
  n = mbsrtowcs (NULL, &scopy, strlen (scopy), &t);
Packit 6c4009
@}
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
This is cumbersome to do so if the number of characters (as opposed to
Packit 6c4009
bytes) is needed often it is better to work with wide characters.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
The wide character equivalent is declared in @file{wchar.h}.
Packit 6c4009
Packit 6c4009
@deftypefun size_t wcslen (const wchar_t *@var{ws})
Packit 6c4009
@standards{ISO, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{wcslen} function is the wide character equivalent to
Packit 6c4009
@code{strlen}.  The return value is the number of wide characters in the
Packit 6c4009
wide string pointed to by @var{ws} (this is also the offset of
Packit 6c4009
the terminating null wide character of @var{ws}).
Packit 6c4009
Packit 6c4009
Since there are no multi wide character sequences making up one wide
Packit 6c4009
character the return value is not only the offset in the array, it is
Packit 6c4009
also the number of wide characters.
Packit 6c4009
Packit 6c4009
This function was introduced in @w{Amendment 1} to @w{ISO C90}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun size_t strnlen (const char *@var{s}, size_t @var{maxlen})
Packit 6c4009
@standards{GNU, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
If the array @var{s} of size @var{maxlen} contains a null byte,
Packit 6c4009
the @code{strnlen} function returns the length of the string @var{s} in
Packit 6c4009
bytes.  Otherwise it
Packit 6c4009
returns @var{maxlen}.  Therefore this function is equivalent to
Packit 6c4009
@code{(strlen (@var{s}) < @var{maxlen} ? strlen (@var{s}) : @var{maxlen})}
Packit 6c4009
but it
Packit 6c4009
is more efficient and works even if @var{s} is not null-terminated so
Packit 6c4009
long as @var{maxlen} does not exceed the size of @var{s}'s array.
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
char string[32] = "hello, world";
Packit 6c4009
strnlen (string, 32)
Packit 6c4009
    @result{} 12
Packit 6c4009
strnlen (string, 5)
Packit 6c4009
    @result{} 5
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
This function is a GNU extension and is declared in @file{string.h}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun size_t wcsnlen (const wchar_t *@var{ws}, size_t @var{maxlen})
Packit 6c4009
@standards{GNU, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{wcsnlen} is the wide character equivalent to @code{strnlen}.  The
Packit 6c4009
@var{maxlen} parameter specifies the maximum number of wide characters.
Packit 6c4009
Packit 6c4009
This function is a GNU extension and is declared in @file{wchar.h}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@node Copying Strings and Arrays
Packit 6c4009
@section Copying Strings and Arrays
Packit 6c4009
Packit 6c4009
You can use the functions described in this section to copy the contents
Packit 6c4009
of strings, wide strings, and arrays.  The @samp{str} and @samp{mem}
Packit 6c4009
functions are declared in @file{string.h} while the @samp{w} functions
Packit 6c4009
are declared in @file{wchar.h}.
Packit 6c4009
@pindex string.h
Packit 6c4009
@pindex wchar.h
Packit 6c4009
@cindex copying strings and arrays
Packit 6c4009
@cindex string copy functions
Packit 6c4009
@cindex array copy functions
Packit 6c4009
@cindex concatenating strings
Packit 6c4009
@cindex string concatenation functions
Packit 6c4009
Packit 6c4009
A helpful way to remember the ordering of the arguments to the functions
Packit 6c4009
in this section is that it corresponds to an assignment expression, with
Packit 6c4009
the destination array specified to the left of the source array.  Most
Packit 6c4009
of these functions return the address of the destination array; a few
Packit 6c4009
return the address of the destination's terminating null, or of just
Packit 6c4009
past the destination.
Packit 6c4009
Packit 6c4009
Most of these functions do not work properly if the source and
Packit 6c4009
destination arrays overlap.  For example, if the beginning of the
Packit 6c4009
destination array overlaps the end of the source array, the original
Packit 6c4009
contents of that part of the source array may get overwritten before it
Packit 6c4009
is copied.  Even worse, in the case of the string functions, the null
Packit 6c4009
byte marking the end of the string may be lost, and the copy
Packit 6c4009
function might get stuck in a loop trashing all the memory allocated to
Packit 6c4009
your program.
Packit 6c4009
Packit 6c4009
All functions that have problems copying between overlapping arrays are
Packit 6c4009
explicitly identified in this manual.  In addition to functions in this
Packit 6c4009
section, there are a few others like @code{sprintf} (@pxref{Formatted
Packit 6c4009
Output Functions}) and @code{scanf} (@pxref{Formatted Input
Packit 6c4009
Functions}).
Packit 6c4009
Packit 6c4009
@deftypefun {void *} memcpy (void *restrict @var{to}, const void *restrict @var{from}, size_t @var{size})
Packit 6c4009
@standards{ISO, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{memcpy} function copies @var{size} bytes from the object
Packit 6c4009
beginning at @var{from} into the object beginning at @var{to}.  The
Packit 6c4009
behavior of this function is undefined if the two arrays @var{to} and
Packit 6c4009
@var{from} overlap; use @code{memmove} instead if overlapping is possible.
Packit 6c4009
Packit 6c4009
The value returned by @code{memcpy} is the value of @var{to}.
Packit 6c4009
Packit 6c4009
Here is an example of how you might use @code{memcpy} to copy the
Packit 6c4009
contents of an array:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
struct foo *oldarray, *newarray;
Packit 6c4009
int arraysize;
Packit 6c4009
@dots{}
Packit 6c4009
memcpy (new, old, arraysize * sizeof (struct foo));
Packit 6c4009
@end smallexample
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {wchar_t *} wmemcpy (wchar_t *restrict @var{wto}, const wchar_t *restrict @var{wfrom}, size_t @var{size})
Packit 6c4009
@standards{ISO, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{wmemcpy} function copies @var{size} wide characters from the object
Packit 6c4009
beginning at @var{wfrom} into the object beginning at @var{wto}.  The
Packit 6c4009
behavior of this function is undefined if the two arrays @var{wto} and
Packit 6c4009
@var{wfrom} overlap; use @code{wmemmove} instead if overlapping is possible.
Packit 6c4009
Packit 6c4009
The following is a possible implementation of @code{wmemcpy} but there
Packit 6c4009
are more optimizations possible.
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
wchar_t *
Packit 6c4009
wmemcpy (wchar_t *restrict wto, const wchar_t *restrict wfrom,
Packit 6c4009
         size_t size)
Packit 6c4009
@{
Packit 6c4009
  return (wchar_t *) memcpy (wto, wfrom, size * sizeof (wchar_t));
Packit 6c4009
@}
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
The value returned by @code{wmemcpy} is the value of @var{wto}.
Packit 6c4009
Packit 6c4009
This function was introduced in @w{Amendment 1} to @w{ISO C90}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {void *} mempcpy (void *restrict @var{to}, const void *restrict @var{from}, size_t @var{size})
Packit 6c4009
@standards{GNU, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{mempcpy} function is nearly identical to the @code{memcpy}
Packit 6c4009
function.  It copies @var{size} bytes from the object beginning at
Packit 6c4009
@code{from} into the object pointed to by @var{to}.  But instead of
Packit 6c4009
returning the value of @var{to} it returns a pointer to the byte
Packit 6c4009
following the last written byte in the object beginning at @var{to}.
Packit 6c4009
I.e., the value is @code{((void *) ((char *) @var{to} + @var{size}))}.
Packit 6c4009
Packit 6c4009
This function is useful in situations where a number of objects shall be
Packit 6c4009
copied to consecutive memory positions.
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
void *
Packit 6c4009
combine (void *o1, size_t s1, void *o2, size_t s2)
Packit 6c4009
@{
Packit 6c4009
  void *result = malloc (s1 + s2);
Packit 6c4009
  if (result != NULL)
Packit 6c4009
    mempcpy (mempcpy (result, o1, s1), o2, s2);
Packit 6c4009
  return result;
Packit 6c4009
@}
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
This function is a GNU extension.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {wchar_t *} wmempcpy (wchar_t *restrict @var{wto}, const wchar_t *restrict @var{wfrom}, size_t @var{size})
Packit 6c4009
@standards{GNU, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{wmempcpy} function is nearly identical to the @code{wmemcpy}
Packit 6c4009
function.  It copies @var{size} wide characters from the object
Packit 6c4009
beginning at @code{wfrom} into the object pointed to by @var{wto}.  But
Packit 6c4009
instead of returning the value of @var{wto} it returns a pointer to the
Packit 6c4009
wide character following the last written wide character in the object
Packit 6c4009
beginning at @var{wto}.  I.e., the value is @code{@var{wto} + @var{size}}.
Packit 6c4009
Packit 6c4009
This function is useful in situations where a number of objects shall be
Packit 6c4009
copied to consecutive memory positions.
Packit 6c4009
Packit 6c4009
The following is a possible implementation of @code{wmemcpy} but there
Packit 6c4009
are more optimizations possible.
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
wchar_t *
Packit 6c4009
wmempcpy (wchar_t *restrict wto, const wchar_t *restrict wfrom,
Packit 6c4009
          size_t size)
Packit 6c4009
@{
Packit 6c4009
  return (wchar_t *) mempcpy (wto, wfrom, size * sizeof (wchar_t));
Packit 6c4009
@}
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
This function is a GNU extension.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {void *} memmove (void *@var{to}, const void *@var{from}, size_t @var{size})
Packit 6c4009
@standards{ISO, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{memmove} copies the @var{size} bytes at @var{from} into the
Packit 6c4009
@var{size} bytes at @var{to}, even if those two blocks of space
Packit 6c4009
overlap.  In the case of overlap, @code{memmove} is careful to copy the
Packit 6c4009
original values of the bytes in the block at @var{from}, including those
Packit 6c4009
bytes which also belong to the block at @var{to}.
Packit 6c4009
Packit 6c4009
The value returned by @code{memmove} is the value of @var{to}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {wchar_t *} wmemmove (wchar_t *@var{wto}, const wchar_t *@var{wfrom}, size_t @var{size})
Packit 6c4009
@standards{ISO, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{wmemmove} copies the @var{size} wide characters at @var{wfrom}
Packit 6c4009
into the @var{size} wide characters at @var{wto}, even if those two
Packit 6c4009
blocks of space overlap.  In the case of overlap, @code{wmemmove} is
Packit 6c4009
careful to copy the original values of the wide characters in the block
Packit 6c4009
at @var{wfrom}, including those wide characters which also belong to the
Packit 6c4009
block at @var{wto}.
Packit 6c4009
Packit 6c4009
The following is a possible implementation of @code{wmemcpy} but there
Packit 6c4009
are more optimizations possible.
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
wchar_t *
Packit 6c4009
wmempcpy (wchar_t *restrict wto, const wchar_t *restrict wfrom,
Packit 6c4009
          size_t size)
Packit 6c4009
@{
Packit 6c4009
  return (wchar_t *) mempcpy (wto, wfrom, size * sizeof (wchar_t));
Packit 6c4009
@}
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
The value returned by @code{wmemmove} is the value of @var{wto}.
Packit 6c4009
Packit 6c4009
This function is a GNU extension.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {void *} memccpy (void *restrict @var{to}, const void *restrict @var{from}, int @var{c}, size_t @var{size})
Packit 6c4009
@standards{SVID, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This function copies no more than @var{size} bytes from @var{from} to
Packit 6c4009
@var{to}, stopping if a byte matching @var{c} is found.  The return
Packit 6c4009
value is a pointer into @var{to} one byte past where @var{c} was copied,
Packit 6c4009
or a null pointer if no byte matching @var{c} appeared in the first
Packit 6c4009
@var{size} bytes of @var{from}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {void *} memset (void *@var{block}, int @var{c}, size_t @var{size})
Packit 6c4009
@standards{ISO, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This function copies the value of @var{c} (converted to an
Packit 6c4009
@code{unsigned char}) into each of the first @var{size} bytes of the
Packit 6c4009
object beginning at @var{block}.  It returns the value of @var{block}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {wchar_t *} wmemset (wchar_t *@var{block}, wchar_t @var{wc}, size_t @var{size})
Packit 6c4009
@standards{ISO, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This function copies the value of @var{wc} into each of the first
Packit 6c4009
@var{size} wide characters of the object beginning at @var{block}.  It
Packit 6c4009
returns the value of @var{block}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {char *} strcpy (char *restrict @var{to}, const char *restrict @var{from})
Packit 6c4009
@standards{ISO, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This copies bytes from the string @var{from} (up to and including
Packit 6c4009
the terminating null byte) into the string @var{to}.  Like
Packit 6c4009
@code{memcpy}, this function has undefined results if the strings
Packit 6c4009
overlap.  The return value is the value of @var{to}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {wchar_t *} wcscpy (wchar_t *restrict @var{wto}, const wchar_t *restrict @var{wfrom})
Packit 6c4009
@standards{ISO, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This copies wide characters from the wide string @var{wfrom} (up to and
Packit 6c4009
including the terminating null wide character) into the string
Packit 6c4009
@var{wto}.  Like @code{wmemcpy}, this function has undefined results if
Packit 6c4009
the strings overlap.  The return value is the value of @var{wto}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {char *} strdup (const char *@var{s})
Packit 6c4009
@standards{SVID, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit 6c4009
This function copies the string @var{s} into a newly
Packit 6c4009
allocated string.  The string is allocated using @code{malloc}; see
Packit 6c4009
@ref{Unconstrained Allocation}.  If @code{malloc} cannot allocate space
Packit 6c4009
for the new string, @code{strdup} returns a null pointer.  Otherwise it
Packit 6c4009
returns a pointer to the new string.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {wchar_t *} wcsdup (const wchar_t *@var{ws})
Packit 6c4009
@standards{GNU, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit 6c4009
This function copies the wide string @var{ws}
Packit 6c4009
into a newly allocated string.  The string is allocated using
Packit 6c4009
@code{malloc}; see @ref{Unconstrained Allocation}.  If @code{malloc}
Packit 6c4009
cannot allocate space for the new string, @code{wcsdup} returns a null
Packit 6c4009
pointer.  Otherwise it returns a pointer to the new wide string.
Packit 6c4009
Packit 6c4009
This function is a GNU extension.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {char *} stpcpy (char *restrict @var{to}, const char *restrict @var{from})
Packit 6c4009
@standards{Unknown origin, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This function is like @code{strcpy}, except that it returns a pointer to
Packit 6c4009
the end of the string @var{to} (that is, the address of the terminating
Packit 6c4009
null byte @code{to + strlen (from)}) rather than the beginning.
Packit 6c4009
Packit 6c4009
For example, this program uses @code{stpcpy} to concatenate @samp{foo}
Packit 6c4009
and @samp{bar} to produce @samp{foobar}, which it then prints.
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
@include stpcpy.c.texi
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
This function is part of POSIX.1-2008 and later editions, but was
Packit 6c4009
available in @theglibc{} and other systems as an extension long before
Packit 6c4009
it was standardized.
Packit 6c4009
Packit 6c4009
Its behavior is undefined if the strings overlap.  The function is
Packit 6c4009
declared in @file{string.h}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {wchar_t *} wcpcpy (wchar_t *restrict @var{wto}, const wchar_t *restrict @var{wfrom})
Packit 6c4009
@standards{GNU, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This function is like @code{wcscpy}, except that it returns a pointer to
Packit 6c4009
the end of the string @var{wto} (that is, the address of the terminating
Packit 6c4009
null wide character @code{wto + wcslen (wfrom)}) rather than the beginning.
Packit 6c4009
Packit 6c4009
This function is not part of ISO or POSIX but was found useful while
Packit 6c4009
developing @theglibc{} itself.
Packit 6c4009
Packit 6c4009
The behavior of @code{wcpcpy} is undefined if the strings overlap.
Packit 6c4009
Packit 6c4009
@code{wcpcpy} is a GNU extension and is declared in @file{wchar.h}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefn {Macro} {char *} strdupa (const char *@var{s})
Packit 6c4009
@standards{GNU, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This macro is similar to @code{strdup} but allocates the new string
Packit 6c4009
using @code{alloca} instead of @code{malloc} (@pxref{Variable Size
Packit 6c4009
Automatic}).  This means of course the returned string has the same
Packit 6c4009
limitations as any block of memory allocated using @code{alloca}.
Packit 6c4009
Packit 6c4009
For obvious reasons @code{strdupa} is implemented only as a macro;
Packit 6c4009
you cannot get the address of this function.  Despite this limitation
Packit 6c4009
it is a useful function.  The following code shows a situation where
Packit 6c4009
using @code{malloc} would be a lot more expensive.
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
@include strdupa.c.texi
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
Please note that calling @code{strtok} using @var{path} directly is
Packit 6c4009
invalid.  It is also not allowed to call @code{strdupa} in the argument
Packit 6c4009
list of @code{strtok} since @code{strdupa} uses @code{alloca}
Packit 6c4009
(@pxref{Variable Size Automatic}) can interfere with the parameter
Packit 6c4009
passing.
Packit 6c4009
Packit 6c4009
This function is only available if GNU CC is used.
Packit 6c4009
@end deftypefn
Packit 6c4009
Packit 6c4009
@deftypefun void bcopy (const void *@var{from}, void *@var{to}, size_t @var{size})
Packit 6c4009
@standards{BSD, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This is a partially obsolete alternative for @code{memmove}, derived from
Packit 6c4009
BSD.  Note that it is not quite equivalent to @code{memmove}, because the
Packit 6c4009
arguments are not in the same order and there is no return value.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun void bzero (void *@var{block}, size_t @var{size})
Packit 6c4009
@standards{BSD, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This is a partially obsolete alternative for @code{memset}, derived from
Packit 6c4009
BSD.  Note that it is not as general as @code{memset}, because the only
Packit 6c4009
value it can store is zero.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@node Concatenating Strings
Packit 6c4009
@section Concatenating Strings
Packit 6c4009
@pindex string.h
Packit 6c4009
@pindex wchar.h
Packit 6c4009
@cindex concatenating strings
Packit 6c4009
@cindex string concatenation functions
Packit 6c4009
Packit 6c4009
The functions described in this section concatenate the contents of a
Packit 6c4009
string or wide string to another.  They follow the string-copying
Packit 6c4009
functions in their conventions.  @xref{Copying Strings and Arrays}.
Packit 6c4009
@samp{strcat} is declared in the header file @file{string.h} while
Packit 6c4009
@samp{wcscat} is declared in @file{wchar.h}.
Packit 6c4009
Packit 6c4009
@deftypefun {char *} strcat (char *restrict @var{to}, const char *restrict @var{from})
Packit 6c4009
@standards{ISO, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{strcat} function is similar to @code{strcpy}, except that the
Packit 6c4009
bytes from @var{from} are concatenated or appended to the end of
Packit 6c4009
@var{to}, instead of overwriting it.  That is, the first byte from
Packit 6c4009
@var{from} overwrites the null byte marking the end of @var{to}.
Packit 6c4009
Packit 6c4009
An equivalent definition for @code{strcat} would be:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
char *
Packit 6c4009
strcat (char *restrict to, const char *restrict from)
Packit 6c4009
@{
Packit 6c4009
  strcpy (to + strlen (to), from);
Packit 6c4009
  return to;
Packit 6c4009
@}
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
This function has undefined results if the strings overlap.
Packit 6c4009
Packit 6c4009
As noted below, this function has significant performance issues.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {wchar_t *} wcscat (wchar_t *restrict @var{wto}, const wchar_t *restrict @var{wfrom})
Packit 6c4009
@standards{ISO, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{wcscat} function is similar to @code{wcscpy}, except that the
Packit 6c4009
wide characters from @var{wfrom} are concatenated or appended to the end of
Packit 6c4009
@var{wto}, instead of overwriting it.  That is, the first wide character from
Packit 6c4009
@var{wfrom} overwrites the null wide character marking the end of @var{wto}.
Packit 6c4009
Packit 6c4009
An equivalent definition for @code{wcscat} would be:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
wchar_t *
Packit 6c4009
wcscat (wchar_t *wto, const wchar_t *wfrom)
Packit 6c4009
@{
Packit 6c4009
  wcscpy (wto + wcslen (wto), wfrom);
Packit 6c4009
  return wto;
Packit 6c4009
@}
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
This function has undefined results if the strings overlap.
Packit 6c4009
Packit 6c4009
As noted below, this function has significant performance issues.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Programmers using the @code{strcat} or @code{wcscat} function (or the
Packit 6c4009
@code{strncat} or @code{wcsncat} functions defined in
Packit 6c4009
a later section, for that matter)
Packit 6c4009
can easily be recognized as lazy and reckless.  In almost all situations
Packit 6c4009
the lengths of the participating strings are known (it better should be
Packit 6c4009
since how can one otherwise ensure the allocated size of the buffer is
Packit 6c4009
sufficient?)  Or at least, one could know them if one keeps track of the
Packit 6c4009
results of the various function calls.  But then it is very inefficient
Packit 6c4009
to use @code{strcat}/@code{wcscat}.  A lot of time is wasted finding the
Packit 6c4009
end of the destination string so that the actual copying can start.
Packit 6c4009
This is a common example:
Packit 6c4009
Packit 6c4009
@cindex va_copy
Packit 6c4009
@smallexample
Packit 6c4009
/* @r{This function concatenates arbitrarily many strings.  The last}
Packit 6c4009
   @r{parameter must be @code{NULL}.}  */
Packit 6c4009
char *
Packit 6c4009
concat (const char *str, @dots{})
Packit 6c4009
@{
Packit 6c4009
  va_list ap, ap2;
Packit 6c4009
  size_t total = 1;
Packit 6c4009
  const char *s;
Packit 6c4009
  char *result;
Packit 6c4009
Packit 6c4009
  va_start (ap, str);
Packit 6c4009
  va_copy (ap2, ap);
Packit 6c4009
Packit 6c4009
  /* @r{Determine how much space we need.}  */
Packit 6c4009
  for (s = str; s != NULL; s = va_arg (ap, const char *))
Packit 6c4009
    total += strlen (s);
Packit 6c4009
Packit 6c4009
  va_end (ap);
Packit 6c4009
Packit 6c4009
  result = (char *) malloc (total);
Packit 6c4009
  if (result != NULL)
Packit 6c4009
    @{
Packit 6c4009
      result[0] = '\0';
Packit 6c4009
Packit 6c4009
      /* @r{Copy the strings.}  */
Packit 6c4009
      for (s = str; s != NULL; s = va_arg (ap2, const char *))
Packit 6c4009
        strcat (result, s);
Packit 6c4009
    @}
Packit 6c4009
Packit 6c4009
  va_end (ap2);
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
@}
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
This looks quite simple, especially the second loop where the strings
Packit 6c4009
are actually copied.  But these innocent lines hide a major performance
Packit 6c4009
penalty.  Just imagine that ten strings of 100 bytes each have to be
Packit 6c4009
concatenated.  For the second string we search the already stored 100
Packit 6c4009
bytes for the end of the string so that we can append the next string.
Packit 6c4009
For all strings in total the comparisons necessary to find the end of
Packit 6c4009
the intermediate results sums up to 5500!  If we combine the copying
Packit 6c4009
with the search for the allocation we can write this function more
Packit 6c4009
efficiently:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
char *
Packit 6c4009
concat (const char *str, @dots{})
Packit 6c4009
@{
Packit 6c4009
  va_list ap;
Packit 6c4009
  size_t allocated = 100;
Packit 6c4009
  char *result = (char *) malloc (allocated);
Packit 6c4009
Packit 6c4009
  if (result != NULL)
Packit 6c4009
    @{
Packit 6c4009
      char *newp;
Packit 6c4009
      char *wp;
Packit 6c4009
      const char *s;
Packit 6c4009
Packit 6c4009
      va_start (ap, str);
Packit 6c4009
Packit 6c4009
      wp = result;
Packit 6c4009
      for (s = str; s != NULL; s = va_arg (ap, const char *))
Packit 6c4009
        @{
Packit 6c4009
          size_t len = strlen (s);
Packit 6c4009
Packit 6c4009
          /* @r{Resize the allocated memory if necessary.}  */
Packit 6c4009
          if (wp + len + 1 > result + allocated)
Packit 6c4009
            @{
Packit 6c4009
              allocated = (allocated + len) * 2;
Packit 6c4009
              newp = (char *) realloc (result, allocated);
Packit 6c4009
              if (newp == NULL)
Packit 6c4009
                @{
Packit 6c4009
                  free (result);
Packit 6c4009
                  return NULL;
Packit 6c4009
                @}
Packit 6c4009
              wp = newp + (wp - result);
Packit 6c4009
              result = newp;
Packit 6c4009
            @}
Packit 6c4009
Packit 6c4009
          wp = mempcpy (wp, s, len);
Packit 6c4009
        @}
Packit 6c4009
Packit 6c4009
      /* @r{Terminate the result string.}  */
Packit 6c4009
      *wp++ = '\0';
Packit 6c4009
Packit 6c4009
      /* @r{Resize memory to the optimal size.}  */
Packit 6c4009
      newp = realloc (result, wp - result);
Packit 6c4009
      if (newp != NULL)
Packit 6c4009
        result = newp;
Packit 6c4009
Packit 6c4009
      va_end (ap);
Packit 6c4009
    @}
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
@}
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
With a bit more knowledge about the input strings one could fine-tune
Packit 6c4009
the memory allocation.  The difference we are pointing to here is that
Packit 6c4009
we don't use @code{strcat} anymore.  We always keep track of the length
Packit 6c4009
of the current intermediate result so we can save ourselves the search for the
Packit 6c4009
end of the string and use @code{mempcpy}.  Please note that we also
Packit 6c4009
don't use @code{stpcpy} which might seem more natural since we are handling
Packit 6c4009
strings.  But this is not necessary since we already know the
Packit 6c4009
length of the string and therefore can use the faster memory copying
Packit 6c4009
function.  The example would work for wide characters the same way.
Packit 6c4009
Packit 6c4009
Whenever a programmer feels the need to use @code{strcat} she or he
Packit 6c4009
should think twice and look through the program to see whether the code cannot
Packit 6c4009
be rewritten to take advantage of already calculated results.  Again: it
Packit 6c4009
is almost always unnecessary to use @code{strcat}.
Packit 6c4009
Packit 6c4009
@node Truncating Strings
Packit 6c4009
@section Truncating Strings while Copying
Packit 6c4009
@cindex truncating strings
Packit 6c4009
@cindex string truncation
Packit 6c4009
Packit 6c4009
The functions described in this section copy or concatenate the
Packit 6c4009
possibly-truncated contents of a string or array to another, and
Packit 6c4009
similarly for wide strings.  They follow the string-copying functions
Packit 6c4009
in their header conventions.  @xref{Copying Strings and Arrays}.  The
Packit 6c4009
@samp{str} functions are declared in the header file @file{string.h}
Packit 6c4009
and the @samp{wc} functions are declared in the file @file{wchar.h}.
Packit 6c4009
Packit 6c4009
@deftypefun {char *} strncpy (char *restrict @var{to}, const char *restrict @var{from}, size_t @var{size})
Packit 6c4009
@standards{C90, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This function is similar to @code{strcpy} but always copies exactly
Packit 6c4009
@var{size} bytes into @var{to}.
Packit 6c4009
Packit 6c4009
If @var{from} does not contain a null byte in its first @var{size}
Packit 6c4009
bytes, @code{strncpy} copies just the first @var{size} bytes.  In this
Packit 6c4009
case no null terminator is written into @var{to}.
Packit 6c4009
Packit 6c4009
Otherwise @var{from} must be a string with length less than
Packit 6c4009
@var{size}.  In this case @code{strncpy} copies all of @var{from},
Packit 6c4009
followed by enough null bytes to add up to @var{size} bytes in all.
Packit 6c4009
Packit 6c4009
The behavior of @code{strncpy} is undefined if the strings overlap.
Packit 6c4009
Packit 6c4009
This function was designed for now-rarely-used arrays consisting of
Packit 6c4009
non-null bytes followed by zero or more null bytes.  It needs to set
Packit 6c4009
all @var{size} bytes of the destination, even when @var{size} is much
Packit 6c4009
greater than the length of @var{from}.  As noted below, this function
Packit 6c4009
is generally a poor choice for processing text.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {wchar_t *} wcsncpy (wchar_t *restrict @var{wto}, const wchar_t *restrict @var{wfrom}, size_t @var{size})
Packit 6c4009
@standards{ISO, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This function is similar to @code{wcscpy} but always copies exactly
Packit 6c4009
@var{size} wide characters into @var{wto}.
Packit 6c4009
Packit 6c4009
If @var{wfrom} does not contain a null wide character in its first
Packit 6c4009
@var{size} wide characters, then @code{wcsncpy} copies just the first
Packit 6c4009
@var{size} wide characters.  In this case no null terminator is
Packit 6c4009
written into @var{wto}.
Packit 6c4009
Packit 6c4009
Otherwise @var{wfrom} must be a wide string with length less than
Packit 6c4009
@var{size}.  In this case @code{wcsncpy} copies all of @var{wfrom},
Packit 6c4009
followed by enough null wide characters to add up to @var{size} wide
Packit 6c4009
characters in all.
Packit 6c4009
Packit 6c4009
The behavior of @code{wcsncpy} is undefined if the strings overlap.
Packit 6c4009
Packit 6c4009
This function is the wide-character counterpart of @code{strncpy} and
Packit 6c4009
suffers from most of the problems that @code{strncpy} does.  For
Packit 6c4009
example, as noted below, this function is generally a poor choice for
Packit 6c4009
processing text.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {char *} strndup (const char *@var{s}, size_t @var{size})
Packit 6c4009
@standards{GNU, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit 6c4009
This function is similar to @code{strdup} but always copies at most
Packit 6c4009
@var{size} bytes into the newly allocated string.
Packit 6c4009
Packit 6c4009
If the length of @var{s} is more than @var{size}, then @code{strndup}
Packit 6c4009
copies just the first @var{size} bytes and adds a closing null byte.
Packit 6c4009
Otherwise all bytes are copied and the string is terminated.
Packit 6c4009
Packit 6c4009
This function differs from @code{strncpy} in that it always terminates
Packit 6c4009
the destination string.
Packit 6c4009
Packit 6c4009
As noted below, this function is generally a poor choice for
Packit 6c4009
processing text.
Packit 6c4009
Packit 6c4009
@code{strndup} is a GNU extension.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefn {Macro} {char *} strndupa (const char *@var{s}, size_t @var{size})
Packit 6c4009
@standards{GNU, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This function is similar to @code{strndup} but like @code{strdupa} it
Packit 6c4009
allocates the new string using @code{alloca} @pxref{Variable Size
Packit 6c4009
Automatic}.  The same advantages and limitations of @code{strdupa} are
Packit 6c4009
valid for @code{strndupa}, too.
Packit 6c4009
Packit 6c4009
This function is implemented only as a macro, just like @code{strdupa}.
Packit 6c4009
Just as @code{strdupa} this macro also must not be used inside the
Packit 6c4009
parameter list in a function call.
Packit 6c4009
Packit 6c4009
As noted below, this function is generally a poor choice for
Packit 6c4009
processing text.
Packit 6c4009
Packit 6c4009
@code{strndupa} is only available if GNU CC is used.
Packit 6c4009
@end deftypefn
Packit 6c4009
Packit 6c4009
@deftypefun {char *} stpncpy (char *restrict @var{to}, const char *restrict @var{from}, size_t @var{size})
Packit 6c4009
@standards{GNU, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This function is similar to @code{stpcpy} but copies always exactly
Packit 6c4009
@var{size} bytes into @var{to}.
Packit 6c4009
Packit 6c4009
If the length of @var{from} is more than @var{size}, then @code{stpncpy}
Packit 6c4009
copies just the first @var{size} bytes and returns a pointer to the
Packit 6c4009
byte directly following the one which was copied last.  Note that in
Packit 6c4009
this case there is no null terminator written into @var{to}.
Packit 6c4009
Packit 6c4009
If the length of @var{from} is less than @var{size}, then @code{stpncpy}
Packit 6c4009
copies all of @var{from}, followed by enough null bytes to add up
Packit 6c4009
to @var{size} bytes in all.  This behavior is rarely useful, but it
Packit 6c4009
is implemented to be useful in contexts where this behavior of the
Packit 6c4009
@code{strncpy} is used.  @code{stpncpy} returns a pointer to the
Packit 6c4009
@emph{first} written null byte.
Packit 6c4009
Packit 6c4009
This function is not part of ISO or POSIX but was found useful while
Packit 6c4009
developing @theglibc{} itself.
Packit 6c4009
Packit 6c4009
Its behavior is undefined if the strings overlap.  The function is
Packit 6c4009
declared in @file{string.h}.
Packit 6c4009
Packit 6c4009
As noted below, this function is generally a poor choice for
Packit 6c4009
processing text.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {wchar_t *} wcpncpy (wchar_t *restrict @var{wto}, const wchar_t *restrict @var{wfrom}, size_t @var{size})
Packit 6c4009
@standards{GNU, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This function is similar to @code{wcpcpy} but copies always exactly
Packit 6c4009
@var{wsize} wide characters into @var{wto}.
Packit 6c4009
Packit 6c4009
If the length of @var{wfrom} is more than @var{size}, then
Packit 6c4009
@code{wcpncpy} copies just the first @var{size} wide characters and
Packit 6c4009
returns a pointer to the wide character directly following the last
Packit 6c4009
non-null wide character which was copied last.  Note that in this case
Packit 6c4009
there is no null terminator written into @var{wto}.
Packit 6c4009
Packit 6c4009
If the length of @var{wfrom} is less than @var{size}, then @code{wcpncpy}
Packit 6c4009
copies all of @var{wfrom}, followed by enough null wide characters to add up
Packit 6c4009
to @var{size} wide characters in all.  This behavior is rarely useful, but it
Packit 6c4009
is implemented to be useful in contexts where this behavior of the
Packit 6c4009
@code{wcsncpy} is used.  @code{wcpncpy} returns a pointer to the
Packit 6c4009
@emph{first} written null wide character.
Packit 6c4009
Packit 6c4009
This function is not part of ISO or POSIX but was found useful while
Packit 6c4009
developing @theglibc{} itself.
Packit 6c4009
Packit 6c4009
Its behavior is undefined if the strings overlap.
Packit 6c4009
Packit 6c4009
As noted below, this function is generally a poor choice for
Packit 6c4009
processing text.
Packit 6c4009
Packit 6c4009
@code{wcpncpy} is a GNU extension.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {char *} strncat (char *restrict @var{to}, const char *restrict @var{from}, size_t @var{size})
Packit 6c4009
@standards{ISO, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This function is like @code{strcat} except that not more than @var{size}
Packit 6c4009
bytes from @var{from} are appended to the end of @var{to}, and
Packit 6c4009
@var{from} need not be null-terminated.  A single null byte is also
Packit 6c4009
always appended to @var{to}, so the total
Packit 6c4009
allocated size of @var{to} must be at least @code{@var{size} + 1} bytes
Packit 6c4009
longer than its initial length.
Packit 6c4009
Packit 6c4009
The @code{strncat} function could be implemented like this:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
@group
Packit 6c4009
char *
Packit 6c4009
strncat (char *to, const char *from, size_t size)
Packit 6c4009
@{
Packit 6c4009
  size_t len = strlen (to);
Packit 6c4009
  memcpy (to + len, from, strnlen (from, size));
Packit 6c4009
  to[len + strnlen (from, size)] = '\0';
Packit 6c4009
  return to;
Packit 6c4009
@}
Packit 6c4009
@end group
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
The behavior of @code{strncat} is undefined if the strings overlap.
Packit 6c4009
Packit 6c4009
As a companion to @code{strncpy}, @code{strncat} was designed for
Packit 6c4009
now-rarely-used arrays consisting of non-null bytes followed by zero
Packit 6c4009
or more null bytes.  As noted below, this function is generally a poor
Packit 6c4009
choice for processing text.  Also, this function has significant
Packit 6c4009
performance issues.  @xref{Concatenating Strings}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {wchar_t *} wcsncat (wchar_t *restrict @var{wto}, const wchar_t *restrict @var{wfrom}, size_t @var{size})
Packit 6c4009
@standards{ISO, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This function is like @code{wcscat} except that not more than @var{size}
Packit 6c4009
wide characters from @var{from} are appended to the end of @var{to},
Packit 6c4009
and @var{from} need not be null-terminated.  A single null wide
Packit 6c4009
character is also always appended to @var{to}, so the total allocated
Packit 6c4009
size of @var{to} must be at least @code{wcsnlen (@var{wfrom},
Packit 6c4009
@var{size}) + 1} wide characters longer than its initial length.
Packit 6c4009
Packit 6c4009
The @code{wcsncat} function could be implemented like this:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
@group
Packit 6c4009
wchar_t *
Packit 6c4009
wcsncat (wchar_t *restrict wto, const wchar_t *restrict wfrom,
Packit 6c4009
         size_t size)
Packit 6c4009
@{
Packit 6c4009
  size_t len = wcslen (wto);
Packit 6c4009
  memcpy (wto + len, wfrom, wcsnlen (wfrom, size) * sizeof (wchar_t));
Packit 6c4009
  wto[len + wcsnlen (wfrom, size)] = L'\0';
Packit 6c4009
  return wto;
Packit 6c4009
@}
Packit 6c4009
@end group
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
The behavior of @code{wcsncat} is undefined if the strings overlap.
Packit 6c4009
Packit 6c4009
As noted below, this function is generally a poor choice for
Packit 6c4009
processing text.  Also, this function has significant performance
Packit 6c4009
issues.  @xref{Concatenating Strings}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Because these functions can abruptly truncate strings or wide strings,
Packit 6c4009
they are generally poor choices for processing text.  When coping or
Packit 6c4009
concatening multibyte strings, they can truncate within a multibyte
Packit 6c4009
character so that the result is not a valid multibyte string.  When
Packit 6c4009
combining or concatenating multibyte or wide strings, they may
Packit 6c4009
truncate the output after a combining character, resulting in a
Packit 6c4009
corrupted grapheme.  They can cause bugs even when processing
Packit 6c4009
single-byte strings: for example, when calculating an ASCII-only user
Packit 6c4009
name, a truncated name can identify the wrong user.
Packit 6c4009
Packit 6c4009
Although some buffer overruns can be prevented by manually replacing
Packit 6c4009
calls to copying functions with calls to truncation functions, there
Packit 6c4009
are often easier and safer automatic techniques that cause buffer
Packit 6c4009
overruns to reliably terminate a program, such as GCC's
Packit 6c4009
@option{-fcheck-pointer-bounds} and @option{-fsanitize=address}
Packit 6c4009
options.  @xref{Debugging Options,, Options for Debugging Your Program
Packit 6c4009
or GCC, gcc, Using GCC}.  Because truncation functions can mask
Packit 6c4009
application bugs that would otherwise be caught by the automatic
Packit 6c4009
techniques, these functions should be used only when the application's
Packit 6c4009
underlying logic requires truncation.
Packit 6c4009
Packit 6c4009
@strong{Note:} GNU programs should not truncate strings or wide
Packit 6c4009
strings to fit arbitrary size limits.  @xref{Semantics, , Writing
Packit 6c4009
Robust Programs, standards, The GNU Coding Standards}.  Instead of
Packit 6c4009
string-truncation functions, it is usually better to use dynamic
Packit 6c4009
memory allocation (@pxref{Unconstrained Allocation}) and functions
Packit 6c4009
such as @code{strdup} or @code{asprintf} to construct strings.
Packit 6c4009
Packit 6c4009
@node String/Array Comparison
Packit 6c4009
@section String/Array Comparison
Packit 6c4009
@cindex comparing strings and arrays
Packit 6c4009
@cindex string comparison functions
Packit 6c4009
@cindex array comparison functions
Packit 6c4009
@cindex predicates on strings
Packit 6c4009
@cindex predicates on arrays
Packit 6c4009
Packit 6c4009
You can use the functions in this section to perform comparisons on the
Packit 6c4009
contents of strings and arrays.  As well as checking for equality, these
Packit 6c4009
functions can also be used as the ordering functions for sorting
Packit 6c4009
operations.  @xref{Searching and Sorting}, for an example of this.
Packit 6c4009
Packit 6c4009
Unlike most comparison operations in C, the string comparison functions
Packit 6c4009
return a nonzero value if the strings are @emph{not} equivalent rather
Packit 6c4009
than if they are.  The sign of the value indicates the relative ordering
Packit 6c4009
of the first part of the strings that are not equivalent:  a
Packit 6c4009
negative value indicates that the first string is ``less'' than the
Packit 6c4009
second, while a positive value indicates that the first string is
Packit 6c4009
``greater''.
Packit 6c4009
Packit 6c4009
The most common use of these functions is to check only for equality.
Packit 6c4009
This is canonically done with an expression like @w{@samp{! strcmp (s1, s2)}}.
Packit 6c4009
Packit 6c4009
All of these functions are declared in the header file @file{string.h}.
Packit 6c4009
@pindex string.h
Packit 6c4009
Packit 6c4009
@deftypefun int memcmp (const void *@var{a1}, const void *@var{a2}, size_t @var{size})
Packit 6c4009
@standards{ISO, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The function @code{memcmp} compares the @var{size} bytes of memory
Packit 6c4009
beginning at @var{a1} against the @var{size} bytes of memory beginning
Packit 6c4009
at @var{a2}.  The value returned has the same sign as the difference
Packit 6c4009
between the first differing pair of bytes (interpreted as @code{unsigned
Packit 6c4009
char} objects, then promoted to @code{int}).
Packit 6c4009
Packit 6c4009
If the contents of the two blocks are equal, @code{memcmp} returns
Packit 6c4009
@code{0}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int wmemcmp (const wchar_t *@var{a1}, const wchar_t *@var{a2}, size_t @var{size})
Packit 6c4009
@standards{ISO, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The function @code{wmemcmp} compares the @var{size} wide characters
Packit 6c4009
beginning at @var{a1} against the @var{size} wide characters beginning
Packit 6c4009
at @var{a2}.  The value returned is smaller than or larger than zero
Packit 6c4009
depending on whether the first differing wide character is @var{a1} is
Packit 6c4009
smaller or larger than the corresponding wide character in @var{a2}.
Packit 6c4009
Packit 6c4009
If the contents of the two blocks are equal, @code{wmemcmp} returns
Packit 6c4009
@code{0}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
On arbitrary arrays, the @code{memcmp} function is mostly useful for
Packit 6c4009
testing equality.  It usually isn't meaningful to do byte-wise ordering
Packit 6c4009
comparisons on arrays of things other than bytes.  For example, a
Packit 6c4009
byte-wise comparison on the bytes that make up floating-point numbers
Packit 6c4009
isn't likely to tell you anything about the relationship between the
Packit 6c4009
values of the floating-point numbers.
Packit 6c4009
Packit 6c4009
@code{wmemcmp} is really only useful to compare arrays of type
Packit 6c4009
@code{wchar_t} since the function looks at @code{sizeof (wchar_t)} bytes
Packit 6c4009
at a time and this number of bytes is system dependent.
Packit 6c4009
Packit 6c4009
You should also be careful about using @code{memcmp} to compare objects
Packit 6c4009
that can contain ``holes'', such as the padding inserted into structure
Packit 6c4009
objects to enforce alignment requirements, extra space at the end of
Packit 6c4009
unions, and extra bytes at the ends of strings whose length is less
Packit 6c4009
than their allocated size.  The contents of these ``holes'' are
Packit 6c4009
indeterminate and may cause strange behavior when performing byte-wise
Packit 6c4009
comparisons.  For more predictable results, perform an explicit
Packit 6c4009
component-wise comparison.
Packit 6c4009
Packit 6c4009
For example, given a structure type definition like:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
struct foo
Packit 6c4009
  @{
Packit 6c4009
    unsigned char tag;
Packit 6c4009
    union
Packit 6c4009
      @{
Packit 6c4009
        double f;
Packit 6c4009
        long i;
Packit 6c4009
        char *p;
Packit 6c4009
      @} value;
Packit 6c4009
  @};
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
you are better off writing a specialized comparison function to compare
Packit 6c4009
@code{struct foo} objects instead of comparing them with @code{memcmp}.
Packit 6c4009
Packit 6c4009
@deftypefun int strcmp (const char *@var{s1}, const char *@var{s2})
Packit 6c4009
@standards{ISO, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{strcmp} function compares the string @var{s1} against
Packit 6c4009
@var{s2}, returning a value that has the same sign as the difference
Packit 6c4009
between the first differing pair of bytes (interpreted as
Packit 6c4009
@code{unsigned char} objects, then promoted to @code{int}).
Packit 6c4009
Packit 6c4009
If the two strings are equal, @code{strcmp} returns @code{0}.
Packit 6c4009
Packit 6c4009
A consequence of the ordering used by @code{strcmp} is that if @var{s1}
Packit 6c4009
is an initial substring of @var{s2}, then @var{s1} is considered to be
Packit 6c4009
``less than'' @var{s2}.
Packit 6c4009
Packit 6c4009
@code{strcmp} does not take sorting conventions of the language the
Packit 6c4009
strings are written in into account.  To get that one has to use
Packit 6c4009
@code{strcoll}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int wcscmp (const wchar_t *@var{ws1}, const wchar_t *@var{ws2})
Packit 6c4009
@standards{ISO, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
Packit 6c4009
The @code{wcscmp} function compares the wide string @var{ws1}
Packit 6c4009
against @var{ws2}.  The value returned is smaller than or larger than zero
Packit 6c4009
depending on whether the first differing wide character is @var{ws1} is
Packit 6c4009
smaller or larger than the corresponding wide character in @var{ws2}.
Packit 6c4009
Packit 6c4009
If the two strings are equal, @code{wcscmp} returns @code{0}.
Packit 6c4009
Packit 6c4009
A consequence of the ordering used by @code{wcscmp} is that if @var{ws1}
Packit 6c4009
is an initial substring of @var{ws2}, then @var{ws1} is considered to be
Packit 6c4009
``less than'' @var{ws2}.
Packit 6c4009
Packit 6c4009
@code{wcscmp} does not take sorting conventions of the language the
Packit 6c4009
strings are written in into account.  To get that one has to use
Packit 6c4009
@code{wcscoll}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int strcasecmp (const char *@var{s1}, const char *@var{s2})
Packit 6c4009
@standards{BSD, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
Packit 6c4009
@c Although this calls tolower multiple times, it's a macro, and
Packit 6c4009
@c strcasecmp is optimized so that the locale pointer is read only once.
Packit 6c4009
@c There are some asm implementations too, for which the single-read
Packit 6c4009
@c from locale TLS pointers also applies.
Packit 6c4009
This function is like @code{strcmp}, except that differences in case are
Packit 6c4009
ignored, and its arguments must be multibyte strings.
Packit 6c4009
How uppercase and lowercase characters are related is
Packit 6c4009
determined by the currently selected locale.  In the standard @code{"C"}
Packit 6c4009
locale the characters @"A and @"a do not match but in a locale which
Packit 6c4009
regards these characters as parts of the alphabet they do match.
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
@code{strcasecmp} is derived from BSD.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int wcscasecmp (const wchar_t *@var{ws1}, const wchar_t *@var{ws2})
Packit 6c4009
@standards{GNU, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
Packit 6c4009
@c Since towlower is not a macro, the locale object may be read multiple
Packit 6c4009
@c times.
Packit 6c4009
This function is like @code{wcscmp}, except that differences in case are
Packit 6c4009
ignored.  How uppercase and lowercase characters are related is
Packit 6c4009
determined by the currently selected locale.  In the standard @code{"C"}
Packit 6c4009
locale the characters @"A and @"a do not match but in a locale which
Packit 6c4009
regards these characters as parts of the alphabet they do match.
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
@code{wcscasecmp} is a GNU extension.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int strncmp (const char *@var{s1}, const char *@var{s2}, size_t @var{size})
Packit 6c4009
@standards{ISO, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This function is the similar to @code{strcmp}, except that no more than
Packit 6c4009
@var{size} bytes are compared.  In other words, if the two
Packit 6c4009
strings are the same in their first @var{size} bytes, the
Packit 6c4009
return value is zero.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int wcsncmp (const wchar_t *@var{ws1}, const wchar_t *@var{ws2}, size_t @var{size})
Packit 6c4009
@standards{ISO, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This function is similar to @code{wcscmp}, except that no more than
Packit 6c4009
@var{size} wide characters are compared.  In other words, if the two
Packit 6c4009
strings are the same in their first @var{size} wide characters, the
Packit 6c4009
return value is zero.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int strncasecmp (const char *@var{s1}, const char *@var{s2}, size_t @var{n})
Packit 6c4009
@standards{BSD, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
Packit 6c4009
This function is like @code{strncmp}, except that differences in case
Packit 6c4009
are ignored, and the compared parts of the arguments should consist of
Packit 6c4009
valid multibyte characters.
Packit 6c4009
Like @code{strcasecmp}, it is locale dependent how
Packit 6c4009
uppercase and lowercase characters are related.
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
@code{strncasecmp} is a GNU extension.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int wcsncasecmp (const wchar_t *@var{ws1}, const wchar_t *@var{s2}, size_t @var{n})
Packit 6c4009
@standards{GNU, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
Packit 6c4009
This function is like @code{wcsncmp}, except that differences in case
Packit 6c4009
are ignored.  Like @code{wcscasecmp}, it is locale dependent how
Packit 6c4009
uppercase and lowercase characters are related.
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
@code{wcsncasecmp} is a GNU extension.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Here are some examples showing the use of @code{strcmp} and
Packit 6c4009
@code{strncmp} (equivalent examples can be constructed for the wide
Packit 6c4009
character functions).  These examples assume the use of the ASCII
Packit 6c4009
character set.  (If some other character set---say, EBCDIC---is used
Packit 6c4009
instead, then the glyphs are associated with different numeric codes,
Packit 6c4009
and the return values and ordering may differ.)
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
strcmp ("hello", "hello")
Packit 6c4009
    @result{} 0    /* @r{These two strings are the same.} */
Packit 6c4009
strcmp ("hello", "Hello")
Packit 6c4009
    @result{} 32   /* @r{Comparisons are case-sensitive.} */
Packit 6c4009
strcmp ("hello", "world")
Packit 6c4009
    @result{} -15  /* @r{The byte @code{'h'} comes before @code{'w'}.} */
Packit 6c4009
strcmp ("hello", "hello, world")
Packit 6c4009
    @result{} -44  /* @r{Comparing a null byte against a comma.} */
Packit 6c4009
strncmp ("hello", "hello, world", 5)
Packit 6c4009
    @result{} 0    /* @r{The initial 5 bytes are the same.} */
Packit 6c4009
strncmp ("hello, world", "hello, stupid world!!!", 5)
Packit 6c4009
    @result{} 0    /* @r{The initial 5 bytes are the same.} */
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@deftypefun int strverscmp (const char *@var{s1}, const char *@var{s2})
Packit 6c4009
@standards{GNU, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
Packit 6c4009
@c Calls isdigit multiple times, locale may change in between.
Packit 6c4009
The @code{strverscmp} function compares the string @var{s1} against
Packit 6c4009
@var{s2}, considering them as holding indices/version numbers.  The
Packit 6c4009
return value follows the same conventions as found in the
Packit 6c4009
@code{strcmp} function.  In fact, if @var{s1} and @var{s2} contain no
Packit 6c4009
digits, @code{strverscmp} behaves like @code{strcmp}
Packit 6c4009
(in the sense that the sign of the result is the same).
Packit 6c4009
Packit 6c4009
The comparison algorithm which the @code{strverscmp} function implements
Packit 6c4009
differs slightly from other version-comparison algorithms.  The
Packit 6c4009
implementation is based on a finite-state machine, whose behavior is
Packit 6c4009
approximated below.
Packit 6c4009
Packit 6c4009
@itemize @bullet
Packit 6c4009
@item
Packit 6c4009
The input strings are each split into sequences of non-digits and
Packit 6c4009
digits.  These sequences can be empty at the beginning and end of the
Packit 6c4009
string.  Digits are determined by the @code{isdigit} function and are
Packit 6c4009
thus subject to the current locale.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
Comparison starts with a (possibly empty) non-digit sequence.  The first
Packit 6c4009
non-equal sequences of non-digits or digits determines the outcome of
Packit 6c4009
the comparison.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
Corresponding non-digit sequences in both strings are compared
Packit 6c4009
lexicographically if their lengths are equal.  If the lengths differ,
Packit 6c4009
the shorter non-digit sequence is extended with the input string
Packit 6c4009
character immediately following it (which may be the null terminator),
Packit 6c4009
the other sequence is truncated to be of the same (extended) length, and
Packit 6c4009
these two sequences are compared lexicographically.  In the last case,
Packit 6c4009
the sequence comparison determines the result of the function because
Packit 6c4009
the extension character (or some character before it) is necessarily
Packit 6c4009
different from the character at the same offset in the other input
Packit 6c4009
string.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
For two sequences of digits, the number of leading zeros is counted (which
Packit 6c4009
can be zero).  If the count differs, the string with more leading zeros
Packit 6c4009
in the digit sequence is considered smaller than the other string.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
If the two sequences of digits have no leading zeros, they are compared
Packit 6c4009
as integers, that is, the string with the longer digit sequence is
Packit 6c4009
deemed larger, and if both sequences are of equal length, they are
Packit 6c4009
compared lexicographically.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
If both digit sequences start with a zero and have an equal number of
Packit 6c4009
leading zeros, they are compared lexicographically if their lengths are
Packit 6c4009
the same.  If the lengths differ, the shorter sequence is extended with
Packit 6c4009
the following character in its input string, and the other sequence is
Packit 6c4009
truncated to the same length, and both sequences are compared
Packit 6c4009
lexicographically (similar to the non-digit sequence case above).
Packit 6c4009
@end itemize
Packit 6c4009
Packit 6c4009
The treatment of leading zeros and the tie-breaking extension characters
Packit 6c4009
(which in effect propagate across non-digit/digit sequence boundaries)
Packit 6c4009
differs from other version-comparison algorithms.
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
strverscmp ("no digit", "no digit")
Packit 6c4009
    @result{} 0    /* @r{same behavior as strcmp.} */
Packit 6c4009
strverscmp ("item#99", "item#100")
Packit 6c4009
    @result{} <0   /* @r{same prefix, but 99 < 100.} */
Packit 6c4009
strverscmp ("alpha1", "alpha001")
Packit 6c4009
    @result{} >0   /* @r{different number of leading zeros (0 and 2).} */
Packit 6c4009
strverscmp ("part1_f012", "part1_f01")
Packit 6c4009
    @result{} >0   /* @r{lexicographical comparison with leading zeros.} */
Packit 6c4009
strverscmp ("foo.009", "foo.0")
Packit 6c4009
    @result{} <0   /* @r{different number of leading zeros (2 and 1).} */
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@code{strverscmp} is a GNU extension.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int bcmp (const void *@var{a1}, const void *@var{a2}, size_t @var{size})
Packit 6c4009
@standards{BSD, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This is an obsolete alias for @code{memcmp}, derived from BSD.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@node Collation Functions
Packit 6c4009
@section Collation Functions
Packit 6c4009
Packit 6c4009
@cindex collating strings
Packit 6c4009
@cindex string collation functions
Packit 6c4009
Packit 6c4009
In some locales, the conventions for lexicographic ordering differ from
Packit 6c4009
the strict numeric ordering of character codes.  For example, in Spanish
Packit 6c4009
most glyphs with diacritical marks such as accents are not considered
Packit 6c4009
distinct letters for the purposes of collation.  On the other hand, the
Packit 6c4009
two-character sequence @samp{ll} is treated as a single letter that is
Packit 6c4009
collated immediately after @samp{l}.
Packit 6c4009
Packit 6c4009
You can use the functions @code{strcoll} and @code{strxfrm} (declared in
Packit 6c4009
the headers file @file{string.h}) and @code{wcscoll} and @code{wcsxfrm}
Packit 6c4009
(declared in the headers file @file{wchar}) to compare strings using a
Packit 6c4009
collation ordering appropriate for the current locale.  The locale used
Packit 6c4009
by these functions in particular can be specified by setting the locale
Packit 6c4009
for the @code{LC_COLLATE} category; see @ref{Locales}.
Packit 6c4009
@pindex string.h
Packit 6c4009
@pindex wchar.h
Packit 6c4009
Packit 6c4009
In the standard C locale, the collation sequence for @code{strcoll} is
Packit 6c4009
the same as that for @code{strcmp}.  Similarly, @code{wcscoll} and
Packit 6c4009
@code{wcscmp} are the same in this situation.
Packit 6c4009
Packit 6c4009
Effectively, the way these functions work is by applying a mapping to
Packit 6c4009
transform the characters in a multibyte string to a byte
Packit 6c4009
sequence that represents
Packit 6c4009
the string's position in the collating sequence of the current locale.
Packit 6c4009
Comparing two such byte sequences in a simple fashion is equivalent to
Packit 6c4009
comparing the strings with the locale's collating sequence.
Packit 6c4009
Packit 6c4009
The functions @code{strcoll} and @code{wcscoll} perform this translation
Packit 6c4009
implicitly, in order to do one comparison.  By contrast, @code{strxfrm}
Packit 6c4009
and @code{wcsxfrm} perform the mapping explicitly.  If you are making
Packit 6c4009
multiple comparisons using the same string or set of strings, it is
Packit 6c4009
likely to be more efficient to use @code{strxfrm} or @code{wcsxfrm} to
Packit 6c4009
transform all the strings just once, and subsequently compare the
Packit 6c4009
transformed strings with @code{strcmp} or @code{wcscmp}.
Packit 6c4009
Packit 6c4009
@deftypefun int strcoll (const char *@var{s1}, const char *@var{s2})
Packit 6c4009
@standards{ISO, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit 6c4009
@c Calls strcoll_l with the current locale, which dereferences only the
Packit 6c4009
@c LC_COLLATE data pointer.
Packit 6c4009
The @code{strcoll} function is similar to @code{strcmp} but uses the
Packit 6c4009
collating sequence of the current locale for collation (the
Packit 6c4009
@code{LC_COLLATE} locale).  The arguments are multibyte strings.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int wcscoll (const wchar_t *@var{ws1}, const wchar_t *@var{ws2})
Packit 6c4009
@standards{ISO, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit 6c4009
@c Same as strcoll, but calling wcscoll_l.
Packit 6c4009
The @code{wcscoll} function is similar to @code{wcscmp} but uses the
Packit 6c4009
collating sequence of the current locale for collation (the
Packit 6c4009
@code{LC_COLLATE} locale).
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Here is an example of sorting an array of strings, using @code{strcoll}
Packit 6c4009
to compare them.  The actual sort algorithm is not written here; it
Packit 6c4009
comes from @code{qsort} (@pxref{Array Sort Function}).  The job of the
Packit 6c4009
code shown here is to say how to compare the strings while sorting them.
Packit 6c4009
(Later on in this section, we will show a way to do this more
Packit 6c4009
efficiently using @code{strxfrm}.)
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
/* @r{This is the comparison function used with @code{qsort}.} */
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
compare_elements (const void *v1, const void *v2)
Packit 6c4009
@{
Packit 6c4009
  char * const *p1 = v1;
Packit 6c4009
  char * const *p2 = v2;
Packit 6c4009
Packit 6c4009
  return strcoll (*p1, *p2);
Packit 6c4009
@}
Packit 6c4009
Packit 6c4009
/* @r{This is the entry point---the function to sort}
Packit 6c4009
   @r{strings using the locale's collating sequence.} */
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
sort_strings (char **array, int nstrings)
Packit 6c4009
@{
Packit 6c4009
  /* @r{Sort @code{temp_array} by comparing the strings.} */
Packit 6c4009
  qsort (array, nstrings,
Packit 6c4009
         sizeof (char *), compare_elements);
Packit 6c4009
@}
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@cindex converting string to collation order
Packit 6c4009
@deftypefun size_t strxfrm (char *restrict @var{to}, const char *restrict @var{from}, size_t @var{size})
Packit 6c4009
@standards{ISO, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit 6c4009
The function @code{strxfrm} transforms the multibyte string
Packit 6c4009
@var{from} using the
Packit 6c4009
collation transformation determined by the locale currently selected for
Packit 6c4009
collation, and stores the transformed string in the array @var{to}.  Up
Packit 6c4009
to @var{size} bytes (including a terminating null byte) are
Packit 6c4009
stored.
Packit 6c4009
Packit 6c4009
The behavior is undefined if the strings @var{to} and @var{from}
Packit 6c4009
overlap; see @ref{Copying Strings and Arrays}.
Packit 6c4009
Packit 6c4009
The return value is the length of the entire transformed string.  This
Packit 6c4009
value is not affected by the value of @var{size}, but if it is greater
Packit 6c4009
or equal than @var{size}, it means that the transformed string did not
Packit 6c4009
entirely fit in the array @var{to}.  In this case, only as much of the
Packit 6c4009
string as actually fits was stored.  To get the whole transformed
Packit 6c4009
string, call @code{strxfrm} again with a bigger output array.
Packit 6c4009
Packit 6c4009
The transformed string may be longer than the original string, and it
Packit 6c4009
may also be shorter.
Packit 6c4009
Packit 6c4009
If @var{size} is zero, no bytes are stored in @var{to}.  In this
Packit 6c4009
case, @code{strxfrm} simply returns the number of bytes that would
Packit 6c4009
be the length of the transformed string.  This is useful for determining
Packit 6c4009
what size the allocated array should be.  It does not matter what
Packit 6c4009
@var{to} is if @var{size} is zero; @var{to} may even be a null pointer.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun size_t wcsxfrm (wchar_t *restrict @var{wto}, const wchar_t *@var{wfrom}, size_t @var{size})
Packit 6c4009
@standards{ISO, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit 6c4009
The function @code{wcsxfrm} transforms wide string @var{wfrom}
Packit 6c4009
using the collation transformation determined by the locale currently
Packit 6c4009
selected for collation, and stores the transformed string in the array
Packit 6c4009
@var{wto}.  Up to @var{size} wide characters (including a terminating null
Packit 6c4009
wide character) are stored.
Packit 6c4009
Packit 6c4009
The behavior is undefined if the strings @var{wto} and @var{wfrom}
Packit 6c4009
overlap; see @ref{Copying Strings and Arrays}.
Packit 6c4009
Packit 6c4009
The return value is the length of the entire transformed wide
Packit 6c4009
string.  This value is not affected by the value of @var{size}, but if
Packit 6c4009
it is greater or equal than @var{size}, it means that the transformed
Packit 6c4009
wide string did not entirely fit in the array @var{wto}.  In
Packit 6c4009
this case, only as much of the wide string as actually fits
Packit 6c4009
was stored.  To get the whole transformed wide string, call
Packit 6c4009
@code{wcsxfrm} again with a bigger output array.
Packit 6c4009
Packit 6c4009
The transformed wide string may be longer than the original
Packit 6c4009
wide string, and it may also be shorter.
Packit 6c4009
Packit 6c4009
If @var{size} is zero, no wide characters are stored in @var{to}.  In this
Packit 6c4009
case, @code{wcsxfrm} simply returns the number of wide characters that
Packit 6c4009
would be the length of the transformed wide string.  This is
Packit 6c4009
useful for determining what size the allocated array should be (remember
Packit 6c4009
to multiply with @code{sizeof (wchar_t)}).  It does not matter what
Packit 6c4009
@var{wto} is if @var{size} is zero; @var{wto} may even be a null pointer.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Here is an example of how you can use @code{strxfrm} when
Packit 6c4009
you plan to do many comparisons.  It does the same thing as the previous
Packit 6c4009
example, but much faster, because it has to transform each string only
Packit 6c4009
once, no matter how many times it is compared with other strings.  Even
Packit 6c4009
the time needed to allocate and free storage is much less than the time
Packit 6c4009
we save, when there are many strings.
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
struct sorter @{ char *input; char *transformed; @};
Packit 6c4009
Packit 6c4009
/* @r{This is the comparison function used with @code{qsort}}
Packit 6c4009
   @r{to sort an array of @code{struct sorter}.} */
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
compare_elements (const void *v1, const void *v2)
Packit 6c4009
@{
Packit 6c4009
  const struct sorter *p1 = v1;
Packit 6c4009
  const struct sorter *p2 = v2;
Packit 6c4009
Packit 6c4009
  return strcmp (p1->transformed, p2->transformed);
Packit 6c4009
@}
Packit 6c4009
Packit 6c4009
/* @r{This is the entry point---the function to sort}
Packit 6c4009
   @r{strings using the locale's collating sequence.} */
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
sort_strings_fast (char **array, int nstrings)
Packit 6c4009
@{
Packit 6c4009
  struct sorter temp_array[nstrings];
Packit 6c4009
  int i;
Packit 6c4009
Packit 6c4009
  /* @r{Set up @code{temp_array}.  Each element contains}
Packit 6c4009
     @r{one input string and its transformed string.} */
Packit 6c4009
  for (i = 0; i < nstrings; i++)
Packit 6c4009
    @{
Packit 6c4009
      size_t length = strlen (array[i]) * 2;
Packit 6c4009
      char *transformed;
Packit 6c4009
      size_t transformed_length;
Packit 6c4009
Packit 6c4009
      temp_array[i].input = array[i];
Packit 6c4009
Packit 6c4009
      /* @r{First try a buffer perhaps big enough.}  */
Packit 6c4009
      transformed = (char *) xmalloc (length);
Packit 6c4009
Packit 6c4009
      /* @r{Transform @code{array[i]}.}  */
Packit 6c4009
      transformed_length = strxfrm (transformed, array[i], length);
Packit 6c4009
Packit 6c4009
      /* @r{If the buffer was not large enough, resize it}
Packit 6c4009
         @r{and try again.}  */
Packit 6c4009
      if (transformed_length >= length)
Packit 6c4009
        @{
Packit 6c4009
          /* @r{Allocate the needed space. +1 for terminating}
Packit 6c4009
             @r{@code{'\0'} byte.}  */
Packit 6c4009
          transformed = (char *) xrealloc (transformed,
Packit 6c4009
                                           transformed_length + 1);
Packit 6c4009
Packit 6c4009
          /* @r{The return value is not interesting because we know}
Packit 6c4009
             @r{how long the transformed string is.}  */
Packit 6c4009
          (void) strxfrm (transformed, array[i],
Packit 6c4009
                          transformed_length + 1);
Packit 6c4009
        @}
Packit 6c4009
Packit 6c4009
      temp_array[i].transformed = transformed;
Packit 6c4009
    @}
Packit 6c4009
Packit 6c4009
  /* @r{Sort @code{temp_array} by comparing transformed strings.} */
Packit 6c4009
  qsort (temp_array, nstrings,
Packit 6c4009
         sizeof (struct sorter), compare_elements);
Packit 6c4009
Packit 6c4009
  /* @r{Put the elements back in the permanent array}
Packit 6c4009
     @r{in their sorted order.} */
Packit 6c4009
  for (i = 0; i < nstrings; i++)
Packit 6c4009
    array[i] = temp_array[i].input;
Packit 6c4009
Packit 6c4009
  /* @r{Free the strings we allocated.} */
Packit 6c4009
  for (i = 0; i < nstrings; i++)
Packit 6c4009
    free (temp_array[i].transformed);
Packit 6c4009
@}
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
The interesting part of this code for the wide character version would
Packit 6c4009
look like this:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
void
Packit 6c4009
sort_strings_fast (wchar_t **array, int nstrings)
Packit 6c4009
@{
Packit 6c4009
  @dots{}
Packit 6c4009
      /* @r{Transform @code{array[i]}.}  */
Packit 6c4009
      transformed_length = wcsxfrm (transformed, array[i], length);
Packit 6c4009
Packit 6c4009
      /* @r{If the buffer was not large enough, resize it}
Packit 6c4009
         @r{and try again.}  */
Packit 6c4009
      if (transformed_length >= length)
Packit 6c4009
        @{
Packit 6c4009
          /* @r{Allocate the needed space. +1 for terminating}
Packit 6c4009
             @r{@code{L'\0'} wide character.}  */
Packit 6c4009
          transformed = (wchar_t *) xrealloc (transformed,
Packit 6c4009
                                              (transformed_length + 1)
Packit 6c4009
                                              * sizeof (wchar_t));
Packit 6c4009
Packit 6c4009
          /* @r{The return value is not interesting because we know}
Packit 6c4009
             @r{how long the transformed string is.}  */
Packit 6c4009
          (void) wcsxfrm (transformed, array[i],
Packit 6c4009
                          transformed_length + 1);
Packit 6c4009
        @}
Packit 6c4009
  @dots{}
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
Note the additional multiplication with @code{sizeof (wchar_t)} in the
Packit 6c4009
@code{realloc} call.
Packit 6c4009
Packit 6c4009
@strong{Compatibility Note:} The string collation functions are a new
Packit 6c4009
feature of @w{ISO C90}.  Older C dialects have no equivalent feature.
Packit 6c4009
The wide character versions were introduced in @w{Amendment 1} to @w{ISO
Packit 6c4009
C90}.
Packit 6c4009
Packit 6c4009
@node Search Functions
Packit 6c4009
@section Search Functions
Packit 6c4009
Packit 6c4009
This section describes library functions which perform various kinds
Packit 6c4009
of searching operations on strings and arrays.  These functions are
Packit 6c4009
declared in the header file @file{string.h}.
Packit 6c4009
@pindex string.h
Packit 6c4009
@cindex search functions (for strings)
Packit 6c4009
@cindex string search functions
Packit 6c4009
Packit 6c4009
@deftypefun {void *} memchr (const void *@var{block}, int @var{c}, size_t @var{size})
Packit 6c4009
@standards{ISO, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This function finds the first occurrence of the byte @var{c} (converted
Packit 6c4009
to an @code{unsigned char}) in the initial @var{size} bytes of the
Packit 6c4009
object beginning at @var{block}.  The return value is a pointer to the
Packit 6c4009
located byte, or a null pointer if no match was found.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {wchar_t *} wmemchr (const wchar_t *@var{block}, wchar_t @var{wc}, size_t @var{size})
Packit 6c4009
@standards{ISO, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This function finds the first occurrence of the wide character @var{wc}
Packit 6c4009
in the initial @var{size} wide characters of the object beginning at
Packit 6c4009
@var{block}.  The return value is a pointer to the located wide
Packit 6c4009
character, or a null pointer if no match was found.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {void *} rawmemchr (const void *@var{block}, int @var{c})
Packit 6c4009
@standards{GNU, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
Often the @code{memchr} function is used with the knowledge that the
Packit 6c4009
byte @var{c} is available in the memory block specified by the
Packit 6c4009
parameters.  But this means that the @var{size} parameter is not really
Packit 6c4009
needed and that the tests performed with it at runtime (to check whether
Packit 6c4009
the end of the block is reached) are not needed.
Packit 6c4009
Packit 6c4009
The @code{rawmemchr} function exists for just this situation which is
Packit 6c4009
surprisingly frequent.  The interface is similar to @code{memchr} except
Packit 6c4009
that the @var{size} parameter is missing.  The function will look beyond
Packit 6c4009
the end of the block pointed to by @var{block} in case the programmer
Packit 6c4009
made an error in assuming that the byte @var{c} is present in the block.
Packit 6c4009
In this case the result is unspecified.  Otherwise the return value is a
Packit 6c4009
pointer to the located byte.
Packit 6c4009
Packit 6c4009
This function is of special interest when looking for the end of a
Packit 6c4009
string.  Since all strings are terminated by a null byte a call like
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
   rawmemchr (str, '\0')
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
will never go beyond the end of the string.
Packit 6c4009
Packit 6c4009
This function is a GNU extension.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {void *} memrchr (const void *@var{block}, int @var{c}, size_t @var{size})
Packit 6c4009
@standards{GNU, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The function @code{memrchr} is like @code{memchr}, except that it searches
Packit 6c4009
backwards from the end of the block defined by @var{block} and @var{size}
Packit 6c4009
(instead of forwards from the front).
Packit 6c4009
Packit 6c4009
This function is a GNU extension.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {char *} strchr (const char *@var{string}, int @var{c})
Packit 6c4009
@standards{ISO, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{strchr} function finds the first occurrence of the byte
Packit 6c4009
@var{c} (converted to a @code{char}) in the string
Packit 6c4009
beginning at @var{string}.  The return value is a pointer to the located
Packit 6c4009
byte, or a null pointer if no match was found.
Packit 6c4009
Packit 6c4009
For example,
Packit 6c4009
@smallexample
Packit 6c4009
strchr ("hello, world", 'l')
Packit 6c4009
    @result{} "llo, world"
Packit 6c4009
strchr ("hello, world", '?')
Packit 6c4009
    @result{} NULL
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
The terminating null byte is considered to be part of the string,
Packit 6c4009
so you can use this function get a pointer to the end of a string by
Packit 6c4009
specifying zero as the value of the @var{c} argument.
Packit 6c4009
Packit 6c4009
When @code{strchr} returns a null pointer, it does not let you know
Packit 6c4009
the position of the terminating null byte it has found.  If you
Packit 6c4009
need that information, it is better (but less portable) to use
Packit 6c4009
@code{strchrnul} than to search for it a second time.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {wchar_t *} wcschr (const wchar_t *@var{wstring}, int @var{wc})
Packit 6c4009
@standards{ISO, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{wcschr} function finds the first occurrence of the wide
Packit 6c4009
character @var{wc} in the wide string
Packit 6c4009
beginning at @var{wstring}.  The return value is a pointer to the
Packit 6c4009
located wide character, or a null pointer if no match was found.
Packit 6c4009
Packit 6c4009
The terminating null wide character is considered to be part of the wide
Packit 6c4009
string, so you can use this function get a pointer to the end
Packit 6c4009
of a wide string by specifying a null wide character as the
Packit 6c4009
value of the @var{wc} argument.  It would be better (but less portable)
Packit 6c4009
to use @code{wcschrnul} in this case, though.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {char *} strchrnul (const char *@var{string}, int @var{c})
Packit 6c4009
@standards{GNU, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{strchrnul} is the same as @code{strchr} except that if it does
Packit 6c4009
not find the byte, it returns a pointer to string's terminating
Packit 6c4009
null byte rather than a null pointer.
Packit 6c4009
Packit 6c4009
This function is a GNU extension.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {wchar_t *} wcschrnul (const wchar_t *@var{wstring}, wchar_t @var{wc})
Packit 6c4009
@standards{GNU, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{wcschrnul} is the same as @code{wcschr} except that if it does not
Packit 6c4009
find the wide character, it returns a pointer to the wide string's
Packit 6c4009
terminating null wide character rather than a null pointer.
Packit 6c4009
Packit 6c4009
This function is a GNU extension.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
One useful, but unusual, use of the @code{strchr}
Packit 6c4009
function is when one wants to have a pointer pointing to the null byte
Packit 6c4009
terminating a string.  This is often written in this way:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
  s += strlen (s);
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
This is almost optimal but the addition operation duplicated a bit of
Packit 6c4009
the work already done in the @code{strlen} function.  A better solution
Packit 6c4009
is this:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
  s = strchr (s, '\0');
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
There is no restriction on the second parameter of @code{strchr} so it
Packit 6c4009
could very well also be zero.  Those readers thinking very
Packit 6c4009
hard about this might now point out that the @code{strchr} function is
Packit 6c4009
more expensive than the @code{strlen} function since we have two abort
Packit 6c4009
criteria.  This is right.  But in @theglibc{} the implementation of
Packit 6c4009
@code{strchr} is optimized in a special way so that @code{strchr}
Packit 6c4009
actually is faster.
Packit 6c4009
Packit 6c4009
@deftypefun {char *} strrchr (const char *@var{string}, int @var{c})
Packit 6c4009
@standards{ISO, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The function @code{strrchr} is like @code{strchr}, except that it searches
Packit 6c4009
backwards from the end of the string @var{string} (instead of forwards
Packit 6c4009
from the front).
Packit 6c4009
Packit 6c4009
For example,
Packit 6c4009
@smallexample
Packit 6c4009
strrchr ("hello, world", 'l')
Packit 6c4009
    @result{} "ld"
Packit 6c4009
@end smallexample
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {wchar_t *} wcsrchr (const wchar_t *@var{wstring}, wchar_t @var{c})
Packit 6c4009
@standards{ISO, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The function @code{wcsrchr} is like @code{wcschr}, except that it searches
Packit 6c4009
backwards from the end of the string @var{wstring} (instead of forwards
Packit 6c4009
from the front).
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {char *} strstr (const char *@var{haystack}, const char *@var{needle})
Packit 6c4009
@standards{ISO, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This is like @code{strchr}, except that it searches @var{haystack} for a
Packit 6c4009
substring @var{needle} rather than just a single byte.  It
Packit 6c4009
returns a pointer into the string @var{haystack} that is the first
Packit 6c4009
byte of the substring, or a null pointer if no match was found.  If
Packit 6c4009
@var{needle} is an empty string, the function returns @var{haystack}.
Packit 6c4009
Packit 6c4009
For example,
Packit 6c4009
@smallexample
Packit 6c4009
strstr ("hello, world", "l")
Packit 6c4009
    @result{} "llo, world"
Packit 6c4009
strstr ("hello, world", "wo")
Packit 6c4009
    @result{} "world"
Packit 6c4009
@end smallexample
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {wchar_t *} wcsstr (const wchar_t *@var{haystack}, const wchar_t *@var{needle})
Packit 6c4009
@standards{ISO, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This is like @code{wcschr}, except that it searches @var{haystack} for a
Packit 6c4009
substring @var{needle} rather than just a single wide character.  It
Packit 6c4009
returns a pointer into the string @var{haystack} that is the first wide
Packit 6c4009
character of the substring, or a null pointer if no match was found.  If
Packit 6c4009
@var{needle} is an empty string, the function returns @var{haystack}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {wchar_t *} wcswcs (const wchar_t *@var{haystack}, const wchar_t *@var{needle})
Packit 6c4009
@standards{XPG, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{wcswcs} is a deprecated alias for @code{wcsstr}.  This is the
Packit 6c4009
name originally used in the X/Open Portability Guide before the
Packit 6c4009
@w{Amendment 1} to @w{ISO C90} was published.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@deftypefun {char *} strcasestr (const char *@var{haystack}, const char *@var{needle})
Packit 6c4009
@standards{GNU, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
Packit 6c4009
@c There may be multiple calls of strncasecmp, each accessing the locale
Packit 6c4009
@c object independently.
Packit 6c4009
This is like @code{strstr}, except that it ignores case in searching for
Packit 6c4009
the substring.   Like @code{strcasecmp}, it is locale dependent how
Packit 6c4009
uppercase and lowercase characters are related, and arguments are
Packit 6c4009
multibyte strings.
Packit 6c4009
Packit 6c4009
Packit 6c4009
For example,
Packit 6c4009
@smallexample
Packit 6c4009
strcasestr ("hello, world", "L")
Packit 6c4009
    @result{} "llo, world"
Packit 6c4009
strcasestr ("hello, World", "wo")
Packit 6c4009
    @result{} "World"
Packit 6c4009
@end smallexample
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@deftypefun {void *} memmem (const void *@var{haystack}, size_t @var{haystack-len},@*const void *@var{needle}, size_t @var{needle-len})
Packit 6c4009
@standards{GNU, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This is like @code{strstr}, but @var{needle} and @var{haystack} are byte
Packit 6c4009
arrays rather than strings.  @var{needle-len} is the
Packit 6c4009
length of @var{needle} and @var{haystack-len} is the length of
Packit 6c4009
@var{haystack}.@refill
Packit 6c4009
Packit 6c4009
This function is a GNU extension.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun size_t strspn (const char *@var{string}, const char *@var{skipset})
Packit 6c4009
@standards{ISO, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{strspn} (``string span'') function returns the length of the
Packit 6c4009
initial substring of @var{string} that consists entirely of bytes that
Packit 6c4009
are members of the set specified by the string @var{skipset}.  The order
Packit 6c4009
of the bytes in @var{skipset} is not important.
Packit 6c4009
Packit 6c4009
For example,
Packit 6c4009
@smallexample
Packit 6c4009
strspn ("hello, world", "abcdefghijklmnopqrstuvwxyz")
Packit 6c4009
    @result{} 5
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
In a multibyte string, characters consisting of
Packit 6c4009
more than one byte are not treated as single entities.  Each byte is treated
Packit 6c4009
separately.  The function is not locale-dependent.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun size_t wcsspn (const wchar_t *@var{wstring}, const wchar_t *@var{skipset})
Packit 6c4009
@standards{ISO, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{wcsspn} (``wide character string span'') function returns the
Packit 6c4009
length of the initial substring of @var{wstring} that consists entirely
Packit 6c4009
of wide characters that are members of the set specified by the string
Packit 6c4009
@var{skipset}.  The order of the wide characters in @var{skipset} is not
Packit 6c4009
important.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun size_t strcspn (const char *@var{string}, const char *@var{stopset})
Packit 6c4009
@standards{ISO, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{strcspn} (``string complement span'') function returns the length
Packit 6c4009
of the initial substring of @var{string} that consists entirely of bytes
Packit 6c4009
that are @emph{not} members of the set specified by the string @var{stopset}.
Packit 6c4009
(In other words, it returns the offset of the first byte in @var{string}
Packit 6c4009
that is a member of the set @var{stopset}.)
Packit 6c4009
Packit 6c4009
For example,
Packit 6c4009
@smallexample
Packit 6c4009
strcspn ("hello, world", " \t\n,.;!?")
Packit 6c4009
    @result{} 5
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
In a multibyte string, characters consisting of
Packit 6c4009
more than one byte are not treated as a single entities.  Each byte is treated
Packit 6c4009
separately.  The function is not locale-dependent.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun size_t wcscspn (const wchar_t *@var{wstring}, const wchar_t *@var{stopset})
Packit 6c4009
@standards{ISO, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{wcscspn} (``wide character string complement span'') function
Packit 6c4009
returns the length of the initial substring of @var{wstring} that
Packit 6c4009
consists entirely of wide characters that are @emph{not} members of the
Packit 6c4009
set specified by the string @var{stopset}.  (In other words, it returns
Packit 6c4009
the offset of the first wide character in @var{string} that is a member of
Packit 6c4009
the set @var{stopset}.)
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {char *} strpbrk (const char *@var{string}, const char *@var{stopset})
Packit 6c4009
@standards{ISO, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{strpbrk} (``string pointer break'') function is related to
Packit 6c4009
@code{strcspn}, except that it returns a pointer to the first byte
Packit 6c4009
in @var{string} that is a member of the set @var{stopset} instead of the
Packit 6c4009
length of the initial substring.  It returns a null pointer if no such
Packit 6c4009
byte from @var{stopset} is found.
Packit 6c4009
Packit 6c4009
@c @group  Invalid outside the example.
Packit 6c4009
For example,
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
strpbrk ("hello, world", " \t\n,.;!?")
Packit 6c4009
    @result{} ", world"
Packit 6c4009
@end smallexample
Packit 6c4009
@c @end group
Packit 6c4009
Packit 6c4009
In a multibyte string, characters consisting of
Packit 6c4009
more than one byte are not treated as single entities.  Each byte is treated
Packit 6c4009
separately.  The function is not locale-dependent.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {wchar_t *} wcspbrk (const wchar_t *@var{wstring}, const wchar_t *@var{stopset})
Packit 6c4009
@standards{ISO, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{wcspbrk} (``wide character string pointer break'') function is
Packit 6c4009
related to @code{wcscspn}, except that it returns a pointer to the first
Packit 6c4009
wide character in @var{wstring} that is a member of the set
Packit 6c4009
@var{stopset} instead of the length of the initial substring.  It
Packit 6c4009
returns a null pointer if no such wide character from @var{stopset} is found.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@subsection Compatibility String Search Functions
Packit 6c4009
Packit 6c4009
@deftypefun {char *} index (const char *@var{string}, int @var{c})
Packit 6c4009
@standards{BSD, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{index} is another name for @code{strchr}; they are exactly the same.
Packit 6c4009
New code should always use @code{strchr} since this name is defined in
Packit 6c4009
@w{ISO C} while @code{index} is a BSD invention which never was available
Packit 6c4009
on @w{System V} derived systems.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {char *} rindex (const char *@var{string}, int @var{c})
Packit 6c4009
@standards{BSD, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@code{rindex} is another name for @code{strrchr}; they are exactly the same.
Packit 6c4009
New code should always use @code{strrchr} since this name is defined in
Packit 6c4009
@w{ISO C} while @code{rindex} is a BSD invention which never was available
Packit 6c4009
on @w{System V} derived systems.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@node Finding Tokens in a String
Packit 6c4009
@section Finding Tokens in a String
Packit 6c4009
Packit 6c4009
@cindex tokenizing strings
Packit 6c4009
@cindex breaking a string into tokens
Packit 6c4009
@cindex parsing tokens from a string
Packit 6c4009
It's fairly common for programs to have a need to do some simple kinds
Packit 6c4009
of lexical analysis and parsing, such as splitting a command string up
Packit 6c4009
into tokens.  You can do this with the @code{strtok} function, declared
Packit 6c4009
in the header file @file{string.h}.
Packit 6c4009
@pindex string.h
Packit 6c4009
Packit 6c4009
@deftypefun {char *} strtok (char *restrict @var{newstring}, const char *restrict @var{delimiters})
Packit 6c4009
@standards{ISO, string.h}
Packit 6c4009
@safety{@prelim{}@mtunsafe{@mtasurace{:strtok}}@asunsafe{}@acsafe{}}
Packit 6c4009
A string can be split into tokens by making a series of calls to the
Packit 6c4009
function @code{strtok}.
Packit 6c4009
Packit 6c4009
The string to be split up is passed as the @var{newstring} argument on
Packit 6c4009
the first call only.  The @code{strtok} function uses this to set up
Packit 6c4009
some internal state information.  Subsequent calls to get additional
Packit 6c4009
tokens from the same string are indicated by passing a null pointer as
Packit 6c4009
the @var{newstring} argument.  Calling @code{strtok} with another
Packit 6c4009
non-null @var{newstring} argument reinitializes the state information.
Packit 6c4009
It is guaranteed that no other library function ever calls @code{strtok}
Packit 6c4009
behind your back (which would mess up this internal state information).
Packit 6c4009
Packit 6c4009
The @var{delimiters} argument is a string that specifies a set of delimiters
Packit 6c4009
that may surround the token being extracted.  All the initial bytes
Packit 6c4009
that are members of this set are discarded.  The first byte that is
Packit 6c4009
@emph{not} a member of this set of delimiters marks the beginning of the
Packit 6c4009
next token.  The end of the token is found by looking for the next
Packit 6c4009
byte that is a member of the delimiter set.  This byte in the
Packit 6c4009
original string @var{newstring} is overwritten by a null byte, and the
Packit 6c4009
pointer to the beginning of the token in @var{newstring} is returned.
Packit 6c4009
Packit 6c4009
On the next call to @code{strtok}, the searching begins at the next
Packit 6c4009
byte beyond the one that marked the end of the previous token.
Packit 6c4009
Note that the set of delimiters @var{delimiters} do not have to be the
Packit 6c4009
same on every call in a series of calls to @code{strtok}.
Packit 6c4009
Packit 6c4009
If the end of the string @var{newstring} is reached, or if the remainder of
Packit 6c4009
string consists only of delimiter bytes, @code{strtok} returns
Packit 6c4009
a null pointer.
Packit 6c4009
Packit 6c4009
In a multibyte string, characters consisting of
Packit 6c4009
more than one byte are not treated as single entities.  Each byte is treated
Packit 6c4009
separately.  The function is not locale-dependent.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {wchar_t *} wcstok (wchar_t *@var{newstring}, const wchar_t *@var{delimiters}, wchar_t **@var{save_ptr})
Packit 6c4009
@standards{ISO, wchar.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
A string can be split into tokens by making a series of calls to the
Packit 6c4009
function @code{wcstok}.
Packit 6c4009
Packit 6c4009
The string to be split up is passed as the @var{newstring} argument on
Packit 6c4009
the first call only.  The @code{wcstok} function uses this to set up
Packit 6c4009
some internal state information.  Subsequent calls to get additional
Packit 6c4009
tokens from the same wide string are indicated by passing a
Packit 6c4009
null pointer as the @var{newstring} argument, which causes the pointer
Packit 6c4009
previously stored in @var{save_ptr} to be used instead.
Packit 6c4009
Packit 6c4009
The @var{delimiters} argument is a wide string that specifies
Packit 6c4009
a set of delimiters that may surround the token being extracted.  All
Packit 6c4009
the initial wide characters that are members of this set are discarded.
Packit 6c4009
The first wide character that is @emph{not} a member of this set of
Packit 6c4009
delimiters marks the beginning of the next token.  The end of the token
Packit 6c4009
is found by looking for the next wide character that is a member of the
Packit 6c4009
delimiter set.  This wide character in the original wide
Packit 6c4009
string @var{newstring} is overwritten by a null wide character, the
Packit 6c4009
pointer past the overwritten wide character is saved in @var{save_ptr},
Packit 6c4009
and the pointer to the beginning of the token in @var{newstring} is
Packit 6c4009
returned.
Packit 6c4009
Packit 6c4009
On the next call to @code{wcstok}, the searching begins at the next
Packit 6c4009
wide character beyond the one that marked the end of the previous token.
Packit 6c4009
Note that the set of delimiters @var{delimiters} do not have to be the
Packit 6c4009
same on every call in a series of calls to @code{wcstok}.
Packit 6c4009
Packit 6c4009
If the end of the wide string @var{newstring} is reached, or
Packit 6c4009
if the remainder of string consists only of delimiter wide characters,
Packit 6c4009
@code{wcstok} returns a null pointer.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@strong{Warning:} Since @code{strtok} and @code{wcstok} alter the string
Packit 6c4009
they is parsing, you should always copy the string to a temporary buffer
Packit 6c4009
before parsing it with @code{strtok}/@code{wcstok} (@pxref{Copying Strings
Packit 6c4009
and Arrays}).  If you allow @code{strtok} or @code{wcstok} to modify
Packit 6c4009
a string that came from another part of your program, you are asking for
Packit 6c4009
trouble; that string might be used for other purposes after
Packit 6c4009
@code{strtok} or @code{wcstok} has modified it, and it would not have
Packit 6c4009
the expected value.
Packit 6c4009
Packit 6c4009
The string that you are operating on might even be a constant.  Then
Packit 6c4009
when @code{strtok} or @code{wcstok} tries to modify it, your program
Packit 6c4009
will get a fatal signal for writing in read-only memory.  @xref{Program
Packit 6c4009
Error Signals}.  Even if the operation of @code{strtok} or @code{wcstok}
Packit 6c4009
would not require a modification of the string (e.g., if there is
Packit 6c4009
exactly one token) the string can (and in the @glibcadj{} case will) be
Packit 6c4009
modified.
Packit 6c4009
Packit 6c4009
This is a special case of a general principle: if a part of a program
Packit 6c4009
does not have as its purpose the modification of a certain data
Packit 6c4009
structure, then it is error-prone to modify the data structure
Packit 6c4009
temporarily.
Packit 6c4009
Packit 6c4009
The function @code{strtok} is not reentrant, whereas @code{wcstok} is.
Packit 6c4009
@xref{Nonreentrancy}, for a discussion of where and why reentrancy is
Packit 6c4009
important.
Packit 6c4009
Packit 6c4009
Here is a simple example showing the use of @code{strtok}.
Packit 6c4009
Packit 6c4009
@comment Yes, this example has been tested.
Packit 6c4009
@smallexample
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <stddef.h>
Packit 6c4009
Packit 6c4009
@dots{}
Packit 6c4009
Packit 6c4009
const char string[] = "words separated by spaces -- and, punctuation!";
Packit 6c4009
const char delimiters[] = " .,;:!-";
Packit 6c4009
char *token, *cp;
Packit 6c4009
Packit 6c4009
@dots{}
Packit 6c4009
Packit 6c4009
cp = strdupa (string);                /* Make writable copy.  */
Packit 6c4009
token = strtok (cp, delimiters);      /* token => "words" */
Packit 6c4009
token = strtok (NULL, delimiters);    /* token => "separated" */
Packit 6c4009
token = strtok (NULL, delimiters);    /* token => "by" */
Packit 6c4009
token = strtok (NULL, delimiters);    /* token => "spaces" */
Packit 6c4009
token = strtok (NULL, delimiters);    /* token => "and" */
Packit 6c4009
token = strtok (NULL, delimiters);    /* token => "punctuation" */
Packit 6c4009
token = strtok (NULL, delimiters);    /* token => NULL */
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@Theglibc{} contains two more functions for tokenizing a string
Packit 6c4009
which overcome the limitation of non-reentrancy.  They are not
Packit 6c4009
available available for wide strings.
Packit 6c4009
Packit 6c4009
@deftypefun {char *} strtok_r (char *@var{newstring}, const char *@var{delimiters}, char **@var{save_ptr})
Packit 6c4009
@standards{POSIX, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
Just like @code{strtok}, this function splits the string into several
Packit 6c4009
tokens which can be accessed by successive calls to @code{strtok_r}.
Packit 6c4009
The difference is that, as in @code{wcstok}, the information about the
Packit 6c4009
next token is stored in the space pointed to by the third argument,
Packit 6c4009
@var{save_ptr}, which is a pointer to a string pointer.  Calling
Packit 6c4009
@code{strtok_r} with a null pointer for @var{newstring} and leaving
Packit 6c4009
@var{save_ptr} between the calls unchanged does the job without
Packit 6c4009
hindering reentrancy.
Packit 6c4009
Packit 6c4009
This function is defined in POSIX.1 and can be found on many systems
Packit 6c4009
which support multi-threading.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {char *} strsep (char **@var{string_ptr}, const char *@var{delimiter})
Packit 6c4009
@standards{BSD, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This function has a similar functionality as @code{strtok_r} with the
Packit 6c4009
@var{newstring} argument replaced by the @var{save_ptr} argument.  The
Packit 6c4009
initialization of the moving pointer has to be done by the user.
Packit 6c4009
Successive calls to @code{strsep} move the pointer along the tokens
Packit 6c4009
separated by @var{delimiter}, returning the address of the next token
Packit 6c4009
and updating @var{string_ptr} to point to the beginning of the next
Packit 6c4009
token.
Packit 6c4009
Packit 6c4009
One difference between @code{strsep} and @code{strtok_r} is that if the
Packit 6c4009
input string contains more than one byte from @var{delimiter} in a
Packit 6c4009
row @code{strsep} returns an empty string for each pair of bytes
Packit 6c4009
from @var{delimiter}.  This means that a program normally should test
Packit 6c4009
for @code{strsep} returning an empty string before processing it.
Packit 6c4009
Packit 6c4009
This function was introduced in 4.3BSD and therefore is widely available.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Here is how the above example looks like when @code{strsep} is used.
Packit 6c4009
Packit 6c4009
@comment Yes, this example has been tested.
Packit 6c4009
@smallexample
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <stddef.h>
Packit 6c4009
Packit 6c4009
@dots{}
Packit 6c4009
Packit 6c4009
const char string[] = "words separated by spaces -- and, punctuation!";
Packit 6c4009
const char delimiters[] = " .,;:!-";
Packit 6c4009
char *running;
Packit 6c4009
char *token;
Packit 6c4009
Packit 6c4009
@dots{}
Packit 6c4009
Packit 6c4009
running = strdupa (string);
Packit 6c4009
token = strsep (&running, delimiters);    /* token => "words" */
Packit 6c4009
token = strsep (&running, delimiters);    /* token => "separated" */
Packit 6c4009
token = strsep (&running, delimiters);    /* token => "by" */
Packit 6c4009
token = strsep (&running, delimiters);    /* token => "spaces" */
Packit 6c4009
token = strsep (&running, delimiters);    /* token => "" */
Packit 6c4009
token = strsep (&running, delimiters);    /* token => "" */
Packit 6c4009
token = strsep (&running, delimiters);    /* token => "" */
Packit 6c4009
token = strsep (&running, delimiters);    /* token => "and" */
Packit 6c4009
token = strsep (&running, delimiters);    /* token => "" */
Packit 6c4009
token = strsep (&running, delimiters);    /* token => "punctuation" */
Packit 6c4009
token = strsep (&running, delimiters);    /* token => "" */
Packit 6c4009
token = strsep (&running, delimiters);    /* token => NULL */
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@deftypefun {char *} basename (const char *@var{filename})
Packit 6c4009
@standards{GNU, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The GNU version of the @code{basename} function returns the last
Packit 6c4009
component of the path in @var{filename}.  This function is the preferred
Packit 6c4009
usage, since it does not modify the argument, @var{filename}, and
Packit 6c4009
respects trailing slashes.  The prototype for @code{basename} can be
Packit 6c4009
found in @file{string.h}.  Note, this function is overridden by the XPG
Packit 6c4009
version, if @file{libgen.h} is included.
Packit 6c4009
Packit 6c4009
Example of using GNU @code{basename}:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (int argc, char *argv[])
Packit 6c4009
@{
Packit 6c4009
  char *prog = basename (argv[0]);
Packit 6c4009
Packit 6c4009
  if (argc < 2)
Packit 6c4009
    @{
Packit 6c4009
      fprintf (stderr, "Usage %s <arg>\n", prog);
Packit 6c4009
      exit (1);
Packit 6c4009
    @}
Packit 6c4009
Packit 6c4009
  @dots{}
Packit 6c4009
@}
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@strong{Portability Note:} This function may produce different results
Packit 6c4009
on different systems.
Packit 6c4009
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {char *} basename (char *@var{path})
Packit 6c4009
@standards{XPG, libgen.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
This is the standard XPG defined @code{basename}.  It is similar in
Packit 6c4009
spirit to the GNU version, but may modify the @var{path} by removing
Packit 6c4009
trailing '/' bytes.  If the @var{path} is made up entirely of '/'
Packit 6c4009
bytes, then "/" will be returned.  Also, if @var{path} is
Packit 6c4009
@code{NULL} or an empty string, then "." is returned.  The prototype for
Packit 6c4009
the XPG version can be found in @file{libgen.h}.
Packit 6c4009
Packit 6c4009
Example of using XPG @code{basename}:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
#include <libgen.h>
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (int argc, char *argv[])
Packit 6c4009
@{
Packit 6c4009
  char *prog;
Packit 6c4009
  char *path = strdupa (argv[0]);
Packit 6c4009
Packit 6c4009
  prog = basename (path);
Packit 6c4009
Packit 6c4009
  if (argc < 2)
Packit 6c4009
    @{
Packit 6c4009
      fprintf (stderr, "Usage %s <arg>\n", prog);
Packit 6c4009
      exit (1);
Packit 6c4009
    @}
Packit 6c4009
Packit 6c4009
  @dots{}
Packit 6c4009
Packit 6c4009
@}
Packit 6c4009
@end smallexample
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {char *} dirname (char *@var{path})
Packit 6c4009
@standards{XPG, libgen.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{dirname} function is the compliment to the XPG version of
Packit 6c4009
@code{basename}.  It returns the parent directory of the file specified
Packit 6c4009
by @var{path}.  If @var{path} is @code{NULL}, an empty string, or
Packit 6c4009
contains no '/' bytes, then "." is returned.  The prototype for this
Packit 6c4009
function can be found in @file{libgen.h}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@node Erasing Sensitive Data
Packit 6c4009
@section Erasing Sensitive Data
Packit 6c4009
Packit 6c4009
Sensitive data, such as cryptographic keys, should be erased from
Packit 6c4009
memory after use, to reduce the risk that a bug will expose it to the
Packit 6c4009
outside world.  However, compiler optimizations may determine that an
Packit 6c4009
erasure operation is ``unnecessary,'' and remove it from the generated
Packit 6c4009
code, because no @emph{correct} program could access the variable or
Packit 6c4009
heap object containing the sensitive data after it's deallocated.
Packit 6c4009
Since erasure is a precaution against bugs, this optimization is
Packit 6c4009
inappropriate.
Packit 6c4009
Packit 6c4009
The function @code{explicit_bzero} erases a block of memory, and
Packit 6c4009
guarantees that the compiler will not remove the erasure as
Packit 6c4009
``unnecessary.''
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
@group
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
extern void encrypt (const char *key, const char *in,
Packit 6c4009
                     char *out, size_t n);
Packit 6c4009
extern void genkey (const char *phrase, char *key);
Packit 6c4009
Packit 6c4009
void encrypt_with_phrase (const char *phrase, const char *in,
Packit 6c4009
                          char *out, size_t n)
Packit 6c4009
@{
Packit 6c4009
  char key[16];
Packit 6c4009
  genkey (phrase, key);
Packit 6c4009
  encrypt (key, in, out, n);
Packit 6c4009
  explicit_bzero (key, 16);
Packit 6c4009
@}
Packit 6c4009
@end group
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
In this example, if @code{memset}, @code{bzero}, or a hand-written
Packit 6c4009
loop had been used, the compiler might remove them as ``unnecessary.''
Packit 6c4009
Packit 6c4009
@strong{Warning:} @code{explicit_bzero} does not guarantee that
Packit 6c4009
sensitive data is @emph{completely} erased from the computer's memory.
Packit 6c4009
There may be copies in temporary storage areas, such as registers and
Packit 6c4009
``scratch'' stack space; since these are invisible to the source code,
Packit 6c4009
a library function cannot erase them.
Packit 6c4009
Packit 6c4009
Also, @code{explicit_bzero} only operates on RAM.  If a sensitive data
Packit 6c4009
object never needs to have its address taken other than to call
Packit 6c4009
@code{explicit_bzero}, it might be stored entirely in CPU registers
Packit 6c4009
@emph{until} the call to @code{explicit_bzero}.  Then it will be
Packit 6c4009
copied into RAM, the copy will be erased, and the original will remain
Packit 6c4009
intact.  Data in RAM is more likely to be exposed by a bug than data
Packit 6c4009
in registers, so this creates a brief window where the data is at
Packit 6c4009
greater risk of exposure than it would have been if the program didn't
Packit 6c4009
try to erase it at all.
Packit 6c4009
Packit 6c4009
Declaring sensitive variables as @code{volatile} will make both the
Packit 6c4009
above problems @emph{worse}; a @code{volatile} variable will be stored
Packit 6c4009
in memory for its entire lifetime, and the compiler will make
Packit 6c4009
@emph{more} copies of it than it would otherwise have.  Attempting to
Packit 6c4009
erase a normal variable ``by hand'' through a
Packit 6c4009
@code{volatile}-qualified pointer doesn't work at all---because the
Packit 6c4009
variable itself is not @code{volatile}, some compilers will ignore the
Packit 6c4009
qualification on the pointer and remove the erasure anyway.
Packit 6c4009
Packit 6c4009
Having said all that, in most situations, using @code{explicit_bzero}
Packit 6c4009
is better than not using it.  At present, the only way to do a more
Packit 6c4009
thorough job is to write the entire sensitive operation in assembly
Packit 6c4009
language.  We anticipate that future compilers will recognize calls to
Packit 6c4009
@code{explicit_bzero} and take appropriate steps to erase all the
Packit 6c4009
copies of the affected data, whereever they may be.
Packit 6c4009
Packit 6c4009
@deftypefun void explicit_bzero (void *@var{block}, size_t @var{len})
Packit 6c4009
@standards{BSD, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
Packit 6c4009
@code{explicit_bzero} writes zero into @var{len} bytes of memory
Packit 6c4009
beginning at @var{block}, just as @code{bzero} would.  The zeroes are
Packit 6c4009
always written, even if the compiler could determine that this is
Packit 6c4009
``unnecessary'' because no correct program could read them back.
Packit 6c4009
Packit 6c4009
@strong{Note:} The @emph{only} optimization that @code{explicit_bzero}
Packit 6c4009
disables is removal of ``unnecessary'' writes to memory.  The compiler
Packit 6c4009
can perform all the other optimizations that it could for a call to
Packit 6c4009
@code{memset}.  For instance, it may replace the function call with
Packit 6c4009
inline memory writes, and it may assume that @var{block} cannot be a
Packit 6c4009
null pointer.
Packit 6c4009
Packit 6c4009
@strong{Portability Note:} This function first appeared in OpenBSD 5.5
Packit 6c4009
and has not been standardized.  Other systems may provide the same
Packit 6c4009
functionality under a different name, such as @code{explicit_memset},
Packit 6c4009
@code{memset_s}, or @code{SecureZeroMemory}.
Packit 6c4009
Packit 6c4009
@Theglibc{} declares this function in @file{string.h}, but on other
Packit 6c4009
systems it may be in @file{strings.h} instead.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node Shuffling Bytes
Packit 6c4009
@section Shuffling Bytes
Packit 6c4009
Packit 6c4009
The function below addresses the perennial programming quandary: ``How do
Packit 6c4009
I take good data in string form and painlessly turn it into garbage?''
Packit 6c4009
This is not a difficult thing to code for oneself, but the authors of
Packit 6c4009
@theglibc{} wish to make it as convenient as possible.
Packit 6c4009
Packit 6c4009
To @emph{erase} data, use @code{explicit_bzero} (@pxref{Erasing
Packit 6c4009
Sensitive Data}); to obfuscate it reversibly, use @code{memfrob}
Packit 6c4009
(@pxref{Obfuscating Data}).
Packit 6c4009
Packit 6c4009
@deftypefun {char *} strfry (char *@var{string})
Packit 6c4009
@standards{GNU, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c Calls initstate_r, time, getpid, strlen, and random_r.
Packit 6c4009
Packit 6c4009
@code{strfry} performs an in-place shuffle on @var{string}.  Each
Packit 6c4009
character is swapped to a position selected at random, within the
Packit 6c4009
portion of the string starting with the character's original position.
Packit 6c4009
(This is the Fisher-Yates algorithm for unbiased shuffling.)
Packit 6c4009
Packit 6c4009
Calling @code{strfry} will not disturb any of the random number
Packit 6c4009
generators that have global state (@pxref{Pseudo-Random Numbers}).
Packit 6c4009
Packit 6c4009
The return value of @code{strfry} is always @var{string}.
Packit 6c4009
Packit 6c4009
@strong{Portability Note:}  This function is unique to @theglibc{}.
Packit 6c4009
It is declared in @file{string.h}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node Obfuscating Data
Packit 6c4009
@section Obfuscating Data
Packit 6c4009
@cindex Rot13
Packit 6c4009
Packit 6c4009
The @code{memfrob} function reversibly obfuscates an array of binary
Packit 6c4009
data.  This is not true encryption; the obfuscated data still bears a
Packit 6c4009
clear relationship to the original, and no secret key is required to
Packit 6c4009
undo the obfuscation.  It is analogous to the ``Rot13'' cipher used on
Packit 6c4009
Usenet for obscuring offensive jokes, spoilers for works of fiction,
Packit 6c4009
and so on, but it can be applied to arbitrary binary data.
Packit 6c4009
Packit 6c4009
Programs that need true encryption---a transformation that completely
Packit 6c4009
obscures the original and cannot be reversed without knowledge of a
Packit 6c4009
secret key---should use a dedicated cryptography library, such as
Packit 6c4009
@uref{https://www.gnu.org/software/libgcrypt/,,libgcrypt}.
Packit 6c4009
Packit 6c4009
Programs that need to @emph{destroy} data should use
Packit 6c4009
@code{explicit_bzero} (@pxref{Erasing Sensitive Data}), or possibly
Packit 6c4009
@code{strfry} (@pxref{Shuffling Bytes}).
Packit 6c4009
Packit 6c4009
@deftypefun {void *} memfrob (void *@var{mem}, size_t @var{length})
Packit 6c4009
@standards{GNU, string.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
Packit 6c4009
The function @code{memfrob} obfuscates @var{length} bytes of data
Packit 6c4009
beginning at @var{mem}, in place.  Each byte is bitwise xor-ed with
Packit 6c4009
the binary pattern 00101010 (hexadecimal 0x2A).  The return value is
Packit 6c4009
always @var{mem}.
Packit 6c4009
Packit 6c4009
@code{memfrob} a second time on the same data returns it to
Packit 6c4009
its original state.
Packit 6c4009
Packit 6c4009
@strong{Portability Note:}  This function is unique to @theglibc{}.
Packit 6c4009
It is declared in @file{string.h}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@node Encode Binary Data
Packit 6c4009
@section Encode Binary Data
Packit 6c4009
Packit 6c4009
To store or transfer binary data in environments which only support text
Packit 6c4009
one has to encode the binary data by mapping the input bytes to
Packit 6c4009
bytes in the range allowed for storing or transferring.  SVID
Packit 6c4009
systems (and nowadays XPG compliant systems) provide minimal support for
Packit 6c4009
this task.
Packit 6c4009
Packit 6c4009
@deftypefun {char *} l64a (long int @var{n})
Packit 6c4009
@standards{XPG, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtunsafe{@mtasurace{:l64a}}@asunsafe{}@acsafe{}}
Packit 6c4009
This function encodes a 32-bit input value using bytes from the
Packit 6c4009
basic character set.  It returns a pointer to a 7 byte buffer which
Packit 6c4009
contains an encoded version of @var{n}.  To encode a series of bytes the
Packit 6c4009
user must copy the returned string to a destination buffer.  It returns
Packit 6c4009
the empty string if @var{n} is zero, which is somewhat bizarre but
Packit 6c4009
mandated by the standard.@*
Packit 6c4009
@strong{Warning:} Since a static buffer is used this function should not
Packit 6c4009
be used in multi-threaded programs.  There is no thread-safe alternative
Packit 6c4009
to this function in the C library.@*
Packit 6c4009
@strong{Compatibility Note:} The XPG standard states that the return
Packit 6c4009
value of @code{l64a} is undefined if @var{n} is negative.  In the GNU
Packit 6c4009
implementation, @code{l64a} treats its argument as unsigned, so it will
Packit 6c4009
return a sensible encoding for any nonzero @var{n}; however, portable
Packit 6c4009
programs should not rely on this.
Packit 6c4009
Packit 6c4009
To encode a large buffer @code{l64a} must be called in a loop, once for
Packit 6c4009
each 32-bit word of the buffer.  For example, one could do something
Packit 6c4009
like this:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
char *
Packit 6c4009
encode (const void *buf, size_t len)
Packit 6c4009
@{
Packit 6c4009
  /* @r{We know in advance how long the buffer has to be.} */
Packit 6c4009
  unsigned char *in = (unsigned char *) buf;
Packit 6c4009
  char *out = malloc (6 + ((len + 3) / 4) * 6 + 1);
Packit 6c4009
  char *cp = out, *p;
Packit 6c4009
Packit 6c4009
  /* @r{Encode the length.} */
Packit 6c4009
  /* @r{Using `htonl' is necessary so that the data can be}
Packit 6c4009
     @r{decoded even on machines with different byte order.}
Packit 6c4009
     @r{`l64a' can return a string shorter than 6 bytes, so }
Packit 6c4009
     @r{we pad it with encoding of 0 (}'.'@r{) at the end by }
Packit 6c4009
     @r{hand.} */
Packit 6c4009
Packit 6c4009
  p = stpcpy (cp, l64a (htonl (len)));
Packit 6c4009
  cp = mempcpy (p, "......", 6 - (p - cp));
Packit 6c4009
Packit 6c4009
  while (len > 3)
Packit 6c4009
    @{
Packit 6c4009
      unsigned long int n = *in++;
Packit 6c4009
      n = (n << 8) | *in++;
Packit 6c4009
      n = (n << 8) | *in++;
Packit 6c4009
      n = (n << 8) | *in++;
Packit 6c4009
      len -= 4;
Packit 6c4009
      p = stpcpy (cp, l64a (htonl (n)));
Packit 6c4009
      cp = mempcpy (p, "......", 6 - (p - cp));
Packit 6c4009
    @}
Packit 6c4009
  if (len > 0)
Packit 6c4009
    @{
Packit 6c4009
      unsigned long int n = *in++;
Packit 6c4009
      if (--len > 0)
Packit 6c4009
        @{
Packit 6c4009
          n = (n << 8) | *in++;
Packit 6c4009
          if (--len > 0)
Packit 6c4009
            n = (n << 8) | *in;
Packit 6c4009
        @}
Packit 6c4009
      cp = stpcpy (cp, l64a (htonl (n)));
Packit 6c4009
    @}
Packit 6c4009
  *cp = '\0';
Packit 6c4009
  return out;
Packit 6c4009
@}
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
It is strange that the library does not provide the complete
Packit 6c4009
functionality needed but so be it.
Packit 6c4009
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
To decode data produced with @code{l64a} the following function should be
Packit 6c4009
used.
Packit 6c4009
Packit 6c4009
@deftypefun {long int} a64l (const char *@var{string})
Packit 6c4009
@standards{XPG, stdlib.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The parameter @var{string} should contain a string which was produced by
Packit 6c4009
a call to @code{l64a}.  The function processes at least 6 bytes of
Packit 6c4009
this string, and decodes the bytes it finds according to the table
Packit 6c4009
below.  It stops decoding when it finds a byte not in the table,
Packit 6c4009
rather like @code{atoi}; if you have a buffer which has been broken into
Packit 6c4009
lines, you must be careful to skip over the end-of-line bytes.
Packit 6c4009
Packit 6c4009
The decoded number is returned as a @code{long int} value.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
The @code{l64a} and @code{a64l} functions use a base 64 encoding, in
Packit 6c4009
which each byte of an encoded string represents six bits of an
Packit 6c4009
input word.  These symbols are used for the base 64 digits:
Packit 6c4009
Packit 6c4009
@multitable {xxxxx} {xxx} {xxx} {xxx} {xxx} {xxx} {xxx} {xxx} {xxx}
Packit 6c4009
@item              @tab 0 @tab 1 @tab 2 @tab 3 @tab 4 @tab 5 @tab 6 @tab 7
Packit 6c4009
@item       0      @tab @code{.} @tab @code{/} @tab @code{0} @tab @code{1}
Packit 6c4009
                   @tab @code{2} @tab @code{3} @tab @code{4} @tab @code{5}
Packit 6c4009
@item       8      @tab @code{6} @tab @code{7} @tab @code{8} @tab @code{9}
Packit 6c4009
                   @tab @code{A} @tab @code{B} @tab @code{C} @tab @code{D}
Packit 6c4009
@item       16     @tab @code{E} @tab @code{F} @tab @code{G} @tab @code{H}
Packit 6c4009
                   @tab @code{I} @tab @code{J} @tab @code{K} @tab @code{L}
Packit 6c4009
@item       24     @tab @code{M} @tab @code{N} @tab @code{O} @tab @code{P}
Packit 6c4009
                   @tab @code{Q} @tab @code{R} @tab @code{S} @tab @code{T}
Packit 6c4009
@item       32     @tab @code{U} @tab @code{V} @tab @code{W} @tab @code{X}
Packit 6c4009
                   @tab @code{Y} @tab @code{Z} @tab @code{a} @tab @code{b}
Packit 6c4009
@item       40     @tab @code{c} @tab @code{d} @tab @code{e} @tab @code{f}
Packit 6c4009
                   @tab @code{g} @tab @code{h} @tab @code{i} @tab @code{j}
Packit 6c4009
@item       48     @tab @code{k} @tab @code{l} @tab @code{m} @tab @code{n}
Packit 6c4009
                   @tab @code{o} @tab @code{p} @tab @code{q} @tab @code{r}
Packit 6c4009
@item       56     @tab @code{s} @tab @code{t} @tab @code{u} @tab @code{v}
Packit 6c4009
                   @tab @code{w} @tab @code{x} @tab @code{y} @tab @code{z}
Packit 6c4009
@end multitable
Packit 6c4009
Packit 6c4009
This encoding scheme is not standard.  There are some other encoding
Packit 6c4009
methods which are much more widely used (UU encoding, MIME encoding).
Packit 6c4009
Generally, it is better to use one of these encodings.
Packit 6c4009
Packit 6c4009
@node Argz and Envz Vectors
Packit 6c4009
@section Argz and Envz Vectors
Packit 6c4009
Packit 6c4009
@cindex argz vectors (string vectors)
Packit 6c4009
@cindex string vectors, null-byte separated
Packit 6c4009
@cindex argument vectors, null-byte separated
Packit 6c4009
@dfn{argz vectors} are vectors of strings in a contiguous block of
Packit 6c4009
memory, each element separated from its neighbors by null bytes
Packit 6c4009
(@code{'\0'}).
Packit 6c4009
Packit 6c4009
@cindex envz vectors (environment vectors)
Packit 6c4009
@cindex environment vectors, null-byte separated
Packit 6c4009
@dfn{Envz vectors} are an extension of argz vectors where each element is a
Packit 6c4009
name-value pair, separated by a @code{'='} byte (as in a Unix
Packit 6c4009
environment).
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Argz Functions::              Operations on argz vectors.
Packit 6c4009
* Envz Functions::              Additional operations on environment vectors.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node Argz Functions, Envz Functions, , Argz and Envz Vectors
Packit 6c4009
@subsection Argz Functions
Packit 6c4009
Packit 6c4009
Each argz vector is represented by a pointer to the first element, of
Packit 6c4009
type @code{char *}, and a size, of type @code{size_t}, both of which can
Packit 6c4009
be initialized to @code{0} to represent an empty argz vector.  All argz
Packit 6c4009
functions accept either a pointer and a size argument, or pointers to
Packit 6c4009
them, if they will be modified.
Packit 6c4009
Packit 6c4009
The argz functions use @code{malloc}/@code{realloc} to allocate/grow
Packit 6c4009
argz vectors, and so any argz vector created using these functions may
Packit 6c4009
be freed by using @code{free}; conversely, any argz function that may
Packit 6c4009
grow a string expects that string to have been allocated using
Packit 6c4009
@code{malloc} (those argz functions that only examine their arguments or
Packit 6c4009
modify them in place will work on any sort of memory).
Packit 6c4009
@xref{Unconstrained Allocation}.
Packit 6c4009
Packit 6c4009
All argz functions that do memory allocation have a return type of
Packit 6c4009
@code{error_t}, and return @code{0} for success, and @code{ENOMEM} if an
Packit 6c4009
allocation error occurs.
Packit 6c4009
Packit 6c4009
@pindex argz.h
Packit 6c4009
These functions are declared in the standard include file @file{argz.h}.
Packit 6c4009
Packit 6c4009
@deftypefun {error_t} argz_create (char *const @var{argv}[], char **@var{argz}, size_t *@var{argz_len})
Packit 6c4009
@standards{GNU, argz.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit 6c4009
The @code{argz_create} function converts the Unix-style argument vector
Packit 6c4009
@var{argv} (a vector of pointers to normal C strings, terminated by
Packit 6c4009
@code{(char *)0}; @pxref{Program Arguments}) into an argz vector with
Packit 6c4009
the same elements, which is returned in @var{argz} and @var{argz_len}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {error_t} argz_create_sep (const char *@var{string}, int @var{sep}, char **@var{argz}, size_t *@var{argz_len})
Packit 6c4009
@standards{GNU, argz.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit 6c4009
The @code{argz_create_sep} function converts the string
Packit 6c4009
@var{string} into an argz vector (returned in @var{argz} and
Packit 6c4009
@var{argz_len}) by splitting it into elements at every occurrence of the
Packit 6c4009
byte @var{sep}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {size_t} argz_count (const char *@var{argz}, size_t @var{argz_len})
Packit 6c4009
@standards{GNU, argz.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
Returns the number of elements in the argz vector @var{argz} and
Packit 6c4009
@var{argz_len}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {void} argz_extract (const char *@var{argz}, size_t @var{argz_len}, char **@var{argv})
Packit 6c4009
@standards{GNU, argz.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{argz_extract} function converts the argz vector @var{argz} and
Packit 6c4009
@var{argz_len} into a Unix-style argument vector stored in @var{argv},
Packit 6c4009
by putting pointers to every element in @var{argz} into successive
Packit 6c4009
positions in @var{argv}, followed by a terminator of @code{0}.
Packit 6c4009
@var{Argv} must be pre-allocated with enough space to hold all the
Packit 6c4009
elements in @var{argz} plus the terminating @code{(char *)0}
Packit 6c4009
(@code{(argz_count (@var{argz}, @var{argz_len}) + 1) * sizeof (char *)}
Packit 6c4009
bytes should be enough).  Note that the string pointers stored into
Packit 6c4009
@var{argv} point into @var{argz}---they are not copies---and so
Packit 6c4009
@var{argz} must be copied if it will be changed while @var{argv} is
Packit 6c4009
still active.  This function is useful for passing the elements in
Packit 6c4009
@var{argz} to an exec function (@pxref{Executing a File}).
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {void} argz_stringify (char *@var{argz}, size_t @var{len}, int @var{sep})
Packit 6c4009
@standards{GNU, argz.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{argz_stringify} converts @var{argz} into a normal string with
Packit 6c4009
the elements separated by the byte @var{sep}, by replacing each
Packit 6c4009
@code{'\0'} inside @var{argz} (except the last one, which terminates the
Packit 6c4009
string) with @var{sep}.  This is handy for printing @var{argz} in a
Packit 6c4009
readable manner.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {error_t} argz_add (char **@var{argz}, size_t *@var{argz_len}, const char *@var{str})
Packit 6c4009
@standards{GNU, argz.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit 6c4009
@c Calls strlen and argz_append.
Packit 6c4009
The @code{argz_add} function adds the string @var{str} to the end of the
Packit 6c4009
argz vector @code{*@var{argz}}, and updates @code{*@var{argz}} and
Packit 6c4009
@code{*@var{argz_len}} accordingly.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {error_t} argz_add_sep (char **@var{argz}, size_t *@var{argz_len}, const char *@var{str}, int @var{delim})
Packit 6c4009
@standards{GNU, argz.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit 6c4009
The @code{argz_add_sep} function is similar to @code{argz_add}, but
Packit 6c4009
@var{str} is split into separate elements in the result at occurrences of
Packit 6c4009
the byte @var{delim}.  This is useful, for instance, for
Packit 6c4009
adding the components of a Unix search path to an argz vector, by using
Packit 6c4009
a value of @code{':'} for @var{delim}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {error_t} argz_append (char **@var{argz}, size_t *@var{argz_len}, const char *@var{buf}, size_t @var{buf_len})
Packit 6c4009
@standards{GNU, argz.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit 6c4009
The @code{argz_append} function appends @var{buf_len} bytes starting at
Packit 6c4009
@var{buf} to the argz vector @code{*@var{argz}}, reallocating
Packit 6c4009
@code{*@var{argz}} to accommodate it, and adding @var{buf_len} to
Packit 6c4009
@code{*@var{argz_len}}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {void} argz_delete (char **@var{argz}, size_t *@var{argz_len}, char *@var{entry})
Packit 6c4009
@standards{GNU, argz.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit 6c4009
@c Calls free if no argument is left.
Packit 6c4009
If @var{entry} points to the beginning of one of the elements in the
Packit 6c4009
argz vector @code{*@var{argz}}, the @code{argz_delete} function will
Packit 6c4009
remove this entry and reallocate @code{*@var{argz}}, modifying
Packit 6c4009
@code{*@var{argz}} and @code{*@var{argz_len}} accordingly.  Note that as
Packit 6c4009
destructive argz functions usually reallocate their argz argument,
Packit 6c4009
pointers into argz vectors such as @var{entry} will then become invalid.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {error_t} argz_insert (char **@var{argz}, size_t *@var{argz_len}, char *@var{before}, const char *@var{entry})
Packit 6c4009
@standards{GNU, argz.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit 6c4009
@c Calls argz_add or realloc and memmove.
Packit 6c4009
The @code{argz_insert} function inserts the string @var{entry} into the
Packit 6c4009
argz vector @code{*@var{argz}} at a point just before the existing
Packit 6c4009
element pointed to by @var{before}, reallocating @code{*@var{argz}} and
Packit 6c4009
updating @code{*@var{argz}} and @code{*@var{argz_len}}.  If @var{before}
Packit 6c4009
is @code{0}, @var{entry} is added to the end instead (as if by
Packit 6c4009
@code{argz_add}).  Since the first element is in fact the same as
Packit 6c4009
@code{*@var{argz}}, passing in @code{*@var{argz}} as the value of
Packit 6c4009
@var{before} will result in @var{entry} being inserted at the beginning.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {char *} argz_next (const char *@var{argz}, size_t @var{argz_len}, const char *@var{entry})
Packit 6c4009
@standards{GNU, argz.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{argz_next} function provides a convenient way of iterating
Packit 6c4009
over the elements in the argz vector @var{argz}.  It returns a pointer
Packit 6c4009
to the next element in @var{argz} after the element @var{entry}, or
Packit 6c4009
@code{0} if there are no elements following @var{entry}.  If @var{entry}
Packit 6c4009
is @code{0}, the first element of @var{argz} is returned.
Packit 6c4009
Packit 6c4009
This behavior suggests two styles of iteration:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
    char *entry = 0;
Packit 6c4009
    while ((entry = argz_next (@var{argz}, @var{argz_len}, entry)))
Packit 6c4009
      @var{action};
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
(the double parentheses are necessary to make some C compilers shut up
Packit 6c4009
about what they consider a questionable @code{while}-test) and:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
    char *entry;
Packit 6c4009
    for (entry = @var{argz};
Packit 6c4009
         entry;
Packit 6c4009
         entry = argz_next (@var{argz}, @var{argz_len}, entry))
Packit 6c4009
      @var{action};
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
Note that the latter depends on @var{argz} having a value of @code{0} if
Packit 6c4009
it is empty (rather than a pointer to an empty block of memory); this
Packit 6c4009
invariant is maintained for argz vectors created by the functions here.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun error_t argz_replace (@w{char **@var{argz}, size_t *@var{argz_len}}, @w{const char *@var{str}, const char *@var{with}}, @w{unsigned *@var{replace_count}})
Packit 6c4009
@standards{GNU, argz.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit 6c4009
Replace any occurrences of the string @var{str} in @var{argz} with
Packit 6c4009
@var{with}, reallocating @var{argz} as necessary.  If
Packit 6c4009
@var{replace_count} is non-zero, @code{*@var{replace_count}} will be
Packit 6c4009
incremented by the number of replacements performed.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@node Envz Functions, , Argz Functions, Argz and Envz Vectors
Packit 6c4009
@subsection Envz Functions
Packit 6c4009
Packit 6c4009
Envz vectors are just argz vectors with additional constraints on the form
Packit 6c4009
of each element; as such, argz functions can also be used on them, where it
Packit 6c4009
makes sense.
Packit 6c4009
Packit 6c4009
Each element in an envz vector is a name-value pair, separated by a @code{'='}
Packit 6c4009
byte; if multiple @code{'='} bytes are present in an element, those
Packit 6c4009
after the first are considered part of the value, and treated like all other
Packit 6c4009
non-@code{'\0'} bytes.
Packit 6c4009
Packit 6c4009
If @emph{no} @code{'='} bytes are present in an element, that element is
Packit 6c4009
considered the name of a ``null'' entry, as distinct from an entry with an
Packit 6c4009
empty value: @code{envz_get} will return @code{0} if given the name of null
Packit 6c4009
entry, whereas an entry with an empty value would result in a value of
Packit 6c4009
@code{""}; @code{envz_entry} will still find such entries, however.  Null
Packit 6c4009
entries can be removed with the @code{envz_strip} function.
Packit 6c4009
Packit 6c4009
As with argz functions, envz functions that may allocate memory (and thus
Packit 6c4009
fail) have a return type of @code{error_t}, and return either @code{0} or
Packit 6c4009
@code{ENOMEM}.
Packit 6c4009
Packit 6c4009
@pindex envz.h
Packit 6c4009
These functions are declared in the standard include file @file{envz.h}.
Packit 6c4009
Packit 6c4009
@deftypefun {char *} envz_entry (const char *@var{envz}, size_t @var{envz_len}, const char *@var{name})
Packit 6c4009
@standards{GNU, envz.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{envz_entry} function finds the entry in @var{envz} with the name
Packit 6c4009
@var{name}, and returns a pointer to the whole entry---that is, the argz
Packit 6c4009
element which begins with @var{name} followed by a @code{'='} byte.  If
Packit 6c4009
there is no entry with that name, @code{0} is returned.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {char *} envz_get (const char *@var{envz}, size_t @var{envz_len}, const char *@var{name})
Packit 6c4009
@standards{GNU, envz.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{envz_get} function finds the entry in @var{envz} with the name
Packit 6c4009
@var{name} (like @code{envz_entry}), and returns a pointer to the value
Packit 6c4009
portion of that entry (following the @code{'='}).  If there is no entry with
Packit 6c4009
that name (or only a null entry), @code{0} is returned.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {error_t} envz_add (char **@var{envz}, size_t *@var{envz_len}, const char *@var{name}, const char *@var{value})
Packit 6c4009
@standards{GNU, envz.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit 6c4009
@c Calls envz_remove, which calls enz_entry and argz_delete, and then
Packit 6c4009
@c argz_add or equivalent code that reallocs and appends name=value.
Packit 6c4009
The @code{envz_add} function adds an entry to @code{*@var{envz}}
Packit 6c4009
(updating @code{*@var{envz}} and @code{*@var{envz_len}}) with the name
Packit 6c4009
@var{name}, and value @var{value}.  If an entry with the same name
Packit 6c4009
already exists in @var{envz}, it is removed first.  If @var{value} is
Packit 6c4009
@code{0}, then the new entry will be the special null type of entry
Packit 6c4009
(mentioned above).
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {error_t} envz_merge (char **@var{envz}, size_t *@var{envz_len}, const char *@var{envz2}, size_t @var{envz2_len}, int @var{override})
Packit 6c4009
@standards{GNU, envz.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit 6c4009
The @code{envz_merge} function adds each entry in @var{envz2} to @var{envz},
Packit 6c4009
as if with @code{envz_add}, updating @code{*@var{envz}} and
Packit 6c4009
@code{*@var{envz_len}}.  If @var{override} is true, then values in @var{envz2}
Packit 6c4009
will supersede those with the same name in @var{envz}, otherwise not.
Packit 6c4009
Packit 6c4009
Null entries are treated just like other entries in this respect, so a null
Packit 6c4009
entry in @var{envz} can prevent an entry of the same name in @var{envz2} from
Packit 6c4009
being added to @var{envz}, if @var{override} is false.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {void} envz_strip (char **@var{envz}, size_t *@var{envz_len})
Packit 6c4009
@standards{GNU, envz.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{envz_strip} function removes any null entries from @var{envz},
Packit 6c4009
updating @code{*@var{envz}} and @code{*@var{envz_len}}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {void} envz_remove (char **@var{envz}, size_t *@var{envz_len}, const char *@var{name})
Packit 6c4009
@standards{GNU, envz.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit 6c4009
The @code{envz_remove} function removes an entry named @var{name} from
Packit 6c4009
@var{envz}, updating @code{*@var{envz}} and @code{*@var{envz_len}}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@c FIXME this are undocumented:
Packit 6c4009
@c strcasecmp_l @safety{@mtsafe{}@assafe{}@acsafe{}} see strcasecmp