This macro indicates the basic character set and encoding used by the host: more precisely, the encoding used for character constants in preprocessor `#if' statements (the C "execution character set"). It is defined by safe-ctype.h, and will be an integer constant with one of the following values:
HOST_CHARSET_UNKNOWN
- The host character set is unknown - that is, not one of the next two possibilities.
HOST_CHARSET_ASCII
- The host character set is ASCII.
HOST_CHARSET_EBCDIC
- The host character set is some variant of EBCDIC. (Only one of the nineteen EBCDIC varying characters is tested; exercise caution.)
This function allocates memory which will be automatically reclaimed after the procedure exits. The
libiberty
implementation does not free the memory immediately but will do so eventually during subsequent calls to this function. Memory is allocated usingxmalloc
under normal circumstances.The header file alloca-conf.h can be used in conjunction with the GNU Autoconf test
AC_FUNC_ALLOCA
to test for and properly make available this function. TheAC_FUNC_ALLOCA
test requires that client code use a block of preprocessor code to be safe (see the Autoconf manual for more); this header incorporates that logic and more, including the possibility of a GCC built-in function.
Like
sprintf
, but instead of passing a pointer to a buffer, you pass a pointer to a pointer. This function will compute the size of the buffer needed, allocate memory withmalloc
, and store a pointer to the allocated memory in*
resptr. The value returned is the same assprintf
would return. If memory could not be allocated, minus one is returned andNULL
is stored in*
resptr.
Returns a pointer to the last component of pathname name. Behavior is undefined if the pathname ends in a directory separator.
Compares the first count bytes of two areas of memory. Returns zero if they are the same, nonzero otherwise. Returns zero if count is zero. A nonzero result only indicates a difference, it does not indicate any sorting order (say, by having a positive result mean x sorts before y).
Copies length bytes from memory region in to region out. The use of
bcopy
is deprecated in new programs.
Performs a search over an array of nmemb elements pointed to by base for a member that matches the object pointed to by key. The size of each member is specified by size. The array contents should be sorted in ascending order according to the compar comparison function. This routine should take two arguments pointing to the key and to an array member, in that order, and should return an integer less than, equal to, or greater than zero if the key object is respectively less than, matching, or greater than the array member.
Given a pointer to a string, parse the string extracting fields separated by whitespace and optionally enclosed within either single or double quotes (which are stripped off), and build a vector of pointers to copies of the string for each field. The input string remains unchanged. The last element of the vector is followed by a
NULL
element.All of the memory for the pointer array and copies of the string is obtained from
malloc
. All of the memory can be returned to the system with the single function callfreeargv
, which takes the returned result ofbuildargv
, as it's argument.Returns a pointer to the argument vector if successful. Returns
NULL
if sp isNULL
or if there is insufficient memory to complete building the argument vector.If the input is a null string (as opposed to a
NULL
pointer), then buildarg returns an argument vector that has one arg, a null string.
Zeros count bytes starting at mem. Use of this function is deprecated in favor of
memset
.
Uses
malloc
to allocate storage for nelem objects of elsize bytes each, then zeros the memory.
Return a prefix for temporary file names or
NULL
if unable to find one. The current directory is chosen if all else fails so the program is exited if a temporary directory can't be found (mktemp
fails). The buffer for the result is obtained withxmalloc
.This function is provided for backwards compatability only. Its use is not recommended.
Returns a pointer to a directory path suitable for creating temporary files in.
Returns an approximation of the CPU time used by the process as a
clock_t
; divide this number by `CLOCKS_PER_SEC' to get the number of seconds used.
NULL
)Concatenate zero or more of strings and return the result in freshly
xmalloc
ed memory. ReturnsNULL
if insufficient memory is available. The argument list is terminated by the firstNULL
pointer encountered. Pointers to empty strings are ignored.
Duplicate an argument vector. Simply scans through vector, duplicating each argument until the terminating
NULL
is found. Returns a pointer to the argument vector if successful. ReturnsNULL
if there is insufficient memory to complete building the argument vector.
Returns the maximum
errno
value for which a corresponding symbolic name or message is available. Note that in the case where we use thesys_errlist
supplied by the system, it is possible for there to be more symbolic names than messages, or vice versa. In fact, the manual page forperror(3C)
explicitly warns that one should check the size of the table (sys_nerr
) before indexing it, since new error codes may be added to the system before they are added to the table. Thussys_nerr
might be smaller than value implied by the largesterrno
value defined in<errno.h>
.We return the maximum value that can be used to obtain a meaningful symbolic name or message.
Check to see if two open file descriptors refer to the same file. This is useful, for example, when we have an open file descriptor for an unnamed file, and the name of a file that we believe to correspond to that fd. This can happen when we are exec'd with an already open file (
stdout
for example) or from the SVR4 /proc calls that return open file descriptors for mapped address spaces. All we have to do is open the file by name and check the two file descriptors for a match, which is done by comparing major and minor device numbers and inode numbers.
Opens and returns a
FILE
pointer viafdopen
. If the operating system supports it, ensure that the stream is setup to avoid any multi-threaded locking. Otherwise return theFILE
pointer unchanged.
Find the first (least significant) bit set in valu. Bits are numbered from right to left, starting with bit 1 (corresponding to the value 1). If valu is zero, zero is returned.
Matches string against pattern, returning zero if it matches,
FNM_NOMATCH
if not. pattern may contain the wildcards?
to match any one character,*
to match any zero or more characters, or a set of alternate characters in square brackets, like `[a-gt8]', which match one character (a
throughg
, ort
, or8
, in this example) if that one character is in the set. A set may be inverted (i.e., match anything except what's in the set) by giving^
or!
as the first character in the set. To include those characters in the set, list them as anything other than the first character of the set. To include a dash in the set, list it last in the set. A backslash character makes the following character not special, so for example you could match against a literal asterisk with `\*'. To match a literal backslash, use `\\'.
flags
controls various aspects of the matching process, and is a boolean OR of zero or more of the following values (defined in<fnmatch.h>
):
FNM_PATHNAME
FNM_FILE_NAME
- string is assumed to be a path name. No wildcard will ever match
/
.FNM_NOESCAPE
- Do not interpret backslashes as quoting the following special character.
FNM_PERIOD
- A leading period (at the beginning of string, or if
FNM_PATHNAME
after a slash) is not matched by*
or?
but must be matched explicitly.FNM_LEADING_DIR
- Means that string also matches pattern if some initial part of string matches, and is followed by
/
and zero or more characters. For example, `foo*' would match either `foobar' or `foobar/grill'.FNM_CASEFOLD
- Ignores case when performing the comparison.
Opens and returns a
FILE
pointer viafopen
. If the operating system supports it, ensure that the stream is setup to avoid any multi-threaded locking. Otherwise return theFILE
pointer unchanged.
Free an argument vector that was built using
buildargv
. Simply scans through vector, freeing the memory for each argument until the terminatingNULL
is found, and then frees vector itself.
Opens and returns a
FILE
pointer viafreopen
. If the operating system supports it, ensure that the stream is setup to avoid any multi-threaded locking. Otherwise return theFILE
pointer unchanged.
Returns the time used so far, in microseconds. If possible, this is the time used by this process, else it is the elapsed time since the process started.
Copy the absolute pathname for the current working directory into pathname, which is assumed to point to a buffer of at least len bytes, and return a pointer to the buffer. If the current directory's path doesn't fit in len characters, the result is
NULL
anderrno
is set. If pathname is a null pointer,getcwd
will obtain len bytes of space usingmalloc
.
Returns the number of bytes in a page of memory. This is the granularity of many of the system memory management routines. No guarantee is made as to whether or not it is the same as the basic memory management hardware page size.
Returns the current working directory. This implementation caches the result on the assumption that the process will not call
chdir
between calls togetpwd
.
Initializes the array mapping the current character set to corresponding hex values. This function must be called before any call to
hex_p
orhex_value
. If you fail to call it, a default ASCII-based table will normally be used on ASCII systems.
Evaluates to non-zero if the given character is a valid hex character, or zero if it is not. Note that the value you pass will be cast to
unsigned char
within the macro.
Returns the numeric equivalent of the given character when interpreted as a hexidecimal digit. The result is undefined if you pass an invalid hex digit. Note that the value you pass will be cast to
unsigned char
within the macro.The
hex_value
macro returnsunsigned int
, rather than signedint
, to make it easier to use in parsing addresses from hex dump files: a signedint
would be sign-extended when converted to a wider unsigned type — likebfd_vma
, on some systems.
Returns a pointer to the first occurrence of the character c in the string s, or
NULL
if not found. The use ofindex
is deprecated in new programs in favor ofstrchr
.
Routines to manipulate queues built from doubly linked lists. The
insque
routine inserts elem in the queue immediately after pred. Theremque
routine removes elem from its containing queue. These routines expect to be passed pointers to structures which have as their first members a forward pointer and a back pointer, like this prototype (although no prototype is provided):struct qelem { struct qelem *q_forw; struct qelem *q_back; char q_data[]; };
These twelve macros are defined by safe-ctype.h. Each has the same meaning as the corresponding macro (with name in lowercase) defined by the standard header ctype.h. For example,
ISALPHA
returns true for alphabetic characters and false for others. However, there are two differences between these macros and those provided by ctype.h:
- These macros are guaranteed to have well-defined behavior for all values representable by
signed char
andunsigned char
, and forEOF
.- These macros ignore the current locale; they are true for these fixed sets of characters:
ALPHA
A-Za-z ALNUM
A-Za-z0-9 BLANK
space tab CNTRL
DIGIT
0-9 GRAPH
ALNUM || PUNCT
LOWER
a-z GRAPH ||
spacePUNCT
`~!@#$%^&*()_-=+[{]}\|;:'",<.>/? SPACE
space tab \n \r \f \v UPPER
A-Z XDIGIT
0-9A-Fa-f Note that, if the host character set is ASCII or a superset thereof, all these macros will return false for all values of
char
outside the range of 7-bit ASCII. In particular, both ISPRINT and ISCNTRL return false for characters with numeric values from 128 to 255.
These six macros are defined by safe-ctype.h and provide additional character classes which are useful when doing lexical analysis of C or similar languages. They are true for the following sets of characters:
IDNUM
A-Za-z0-9_ IDST
A-Za-z_ VSPACE
\r \n NVSPACE
space tab \f \v \0 SPACE_OR_NUL
VSPACE || NVSPACE
ISOBASIC
VSPACE || NVSPACE || PRINT
Given a pointer to a string containing a typical pathname (`/usr/src/cmd/ls/ls.c' for example), returns a pointer to the last component of the pathname (`ls.c' in this case). The returned pointer is guaranteed to lie within the original string. This latter fact is not true of many vendor C libraries, which return special strings or modify the passed strings for particular input.
In particular, the empty string returns the same empty string, and a path ending in
/
returns the empty string after it.
Given a pointer to a string containing a pathname, returns a canonical version of the filename. Symlinks will be resolved, and “.” and “..” components will be simplified. The returned value will be allocated using
malloc
, orNULL
will be returned on a memory allocation error.
Given three paths progname, bin_prefix, prefix, return the path that is in the same position relative to progname's directory as prefix is relative to bin_prefix. That is, a string starting with the directory portion of progname, followed by a relative pathname of the difference between bin_prefix and prefix.
If progname does not contain any directory separators,
make_relative_prefix
will search PATH to find a program named progname. Also, if progname is a symbolic link, the symbolic link will be resolved.For example, if bin_prefix is
/alpha/beta/gamma/gcc/delta
, prefix is/alpha/beta/gamma/omega/
, and progname is/red/green/blue/gcc
, then this function will return/red/green/blue/../../omega/
.The return value is normally allocated via
malloc
. If no relative prefix can be found, returnNULL
.
Return a temporary file name (as a string) or
NULL
if unable to create one. suffix is a suffix to append to the file name. The string ismalloc
ed, and the temporary file has been created.
This function searches memory starting at
*
s for the character c. The search only ends with the first occurrence of c, or after length characters; in particular, a null character does not terminate the search. If the character c is found within length characters of*
s, a pointer to the character is returned. If c is not found, thenNULL
is returned.
Compares the first count bytes of two areas of memory. Returns zero if they are the same, a value less than zero if x is lexically less than y, or a value greater than zero if x is lexically greater than y. Note that lexical order is determined as if comparing unsigned char arrays.
Copies length bytes from memory region in to region out. Returns a pointer to out.
Copies count bytes from memory area from to memory area to, returning a pointer to to.
Copies length bytes from memory region in to region out. Returns a pointer to out + length.
Sets the first count bytes of s to the constant byte c, returning a pointer to s.
Generate a unique temporary file name from template. template has the form:
path/ccXXXXXXsuffixsuffix_len tells us how long suffix is (it can be zero length). The last six characters of template before suffix must be `XXXXXX'; they are replaced with a string that makes the filename unique. Returns a file descriptor open on the file for reading and writing.
Executes a program.
program and argv are the arguments to
execv
/execvp
.this_pname is name of the calling program (i.e.,
argv[0]
).temp_base is the path name, sans suffix, of a temporary file to use if needed. This is currently only needed for MS-DOS ports that don't use
go32
(do any still exist?). Ports that don't need it can passNULL
.(flags
& PEXECUTE_SEARCH
) is non-zero if PATH should be searched (??? It's not clear that GCC passes this flag correctly). (flags& PEXECUTE_FIRST
) is nonzero for the first process in chain. (flags& PEXECUTE_FIRST
) is nonzero for the last process in chain. The first/last flags could be simplified to only mark the last of a chain of processes but that requires the caller to always mark the last one (and not give up early if some error occurs). It's more robust to require the caller to mark both ends of the chain.The result is the pid on systems like Unix where we
fork
/exec
and on systems like WIN32 and OS/2 where we usespawn
. It is up to the caller to wait for the child.The result is the
WEXITSTATUS
on systems like MS-DOS where wespawn
and wait for the child here.Upon failure, errmsg_fmt and errmsg_arg are set to the text of the error message with an optional argument (if not needed, errmsg_arg is set to
NULL
), and −1 is returned.errno
is available to the caller to use.
Print message to the standard error, followed by a colon, followed by the description of the signal specified by signo, followed by a newline.
Uses
setenv
orunsetenv
to put string into the environment or remove it. If string is of the form `name=value' the string is added; if no `=' is present the name is unset/removed.
Waits for a program started by
pexecute
to finish.pid is the process id of the task to wait for. status is the `status' argument to wait. flags is currently unused (allows future enhancement without breaking upward compatibility). Pass 0 for now.
The result is the pid of the child reaped, or -1 for failure (
errno
says why).On systems that don't support waiting for a particular child, pid is ignored. On systems like MS-DOS that don't really multitask
pwait
is just a mechanism to provide a consistent interface for the caller.
Random number functions.
random
returns a random number in the range 0 toLONG_MAX
.srandom
initializes the random number generator to some starting point determined by seed (else, the values returned byrandom
are always the same for each run of the program).initstate
andsetstate
allow fine-grained control over the state of the random number generator.
NULL
)Same as
concat
, except that if optr is notNULL
it is freed after the string is created. This is intended to be useful when you're extending an existing string or building up a string in a loop:str = reconcat (str, "pre-", str, NULL);
Renames a file from old to new. If new already exists, it is removed.
Returns a pointer to the last occurrence of the character c in the string s, or
NULL
if not found. The use ofrindex
is deprecated in new programs in favor ofstrrchr
.
setenv
adds name to the environment with value value. If the name was already present in the environment, the new value will be stored only if overwrite is nonzero. The companionunsetenv
function removes name from the environment. This implementation is not safe for multithreaded code.
Returns the maximum signal value for which a corresponding symbolic name or message is available. Note that in the case where we use the
sys_siglist
supplied by the system, it is possible for there to be more symbolic names than messages, or vice versa. In fact, the manual page forpsignal(3b)
explicitly warns that one should check the size of the table (NSIG
) before indexing it, since new signal codes may be added to the system before they are added to the table. ThusNSIG
might be smaller than value implied by the largest signo value defined in<signal.h>
.We return the maximum value that can be used to obtain a meaningful symbolic name or message.
Sets the signal mask to the one provided in set and returns the old mask (which, for libiberty's implementation, will always be the value
1
).
This function is similar to sprintf, but it will print at most n characters. On error the return value is -1, otherwise it returns the number of characters that would have been printed had n been sufficiently large, regardless of the actual value of n. Note some pre-C99 system libraries do not implement this correctly so users cannot generally rely on the return value if the system version of this function is used.
Returns a pointer to a memory region filled with the specified number of spaces and null terminated. The returned pointer is valid until at least the next call.
Copies the string src into dst. Returns a pointer to dst + strlen(src).
Copies the string src into dst, copying exactly len and padding with zeros if necessary. If len < strlen(src) then return dst + len, otherwise returns dst + strlen(src).
Returns a pointer to the first occurrence of the character c in the string s, or
NULL
if not found. If c is itself the null character, the results are undefined.
Returns a pointer to a copy of s in memory obtained from
malloc
, orNULL
if insufficient memory was available.
Given an error number returned from a system call (typically returned in
errno
), returns a pointer to a string containing the symbolic name of that error number, as found in<errno.h>
.If the supplied error number is within the valid range of indices for symbolic names, but no name is available for the particular error number, then returns the string `Error num', where num is the error number.
If the supplied error number is not within the range of valid indices, then returns
NULL
.The contents of the location pointed to are only guaranteed to be valid until the next call to
strerrno
.
Maps an
errno
number to an error message string, the contents of which are implementation defined. On systems which have the external variablessys_nerr
andsys_errlist
, these strings will be the same as the ones used byperror
.If the supplied error number is within the valid range of indices for the
sys_errlist
, but no message is available for the particular error number, then returns the string `Error num', where num is the error number.If the supplied error number is not a valid index into
sys_errlist
, returnsNULL
.The returned string is only guaranteed to be valid only until the next call to
strerror
.
Compares the first n bytes of two strings, returning a value as
strcmp
.
Returns a pointer to the last occurrence of the character c in the string s, or
NULL
if not found. If c is itself the null character, the results are undefined.
Maps an signal number to an signal message string, the contents of which are implementation defined. On systems which have the external variable
sys_siglist
, these strings will be the same as the ones used bypsignal()
.If the supplied signal number is within the valid range of indices for the
sys_siglist
, but no message is available for the particular signal number, then returns the string `Signal num', where num is the signal number.If the supplied signal number is not a valid index into
sys_siglist
, returnsNULL
.The returned string is only guaranteed to be valid only until the next call to
strsignal
.
Given an signal number, returns a pointer to a string containing the symbolic name of that signal number, as found in
<signal.h>
.If the supplied signal number is within the valid range of indices for symbolic names, but no name is available for the particular signal number, then returns the string `Signal num', where num is the signal number.
If the supplied signal number is not within the range of valid indices, then returns
NULL
.The contents of the location pointed to are only guaranteed to be valid until the next call to
strsigno
.
This function searches for the substring sub in the string string, not including the terminating null characters. A pointer to the first occurrence of sub is returned, or
NULL
if the substring is absent. If sub points to a string with zero length, the function returns string.
This ISO C function converts the initial portion of string to a
double
. If endptr is notNULL
, a pointer to the character after the last character used in the conversion is stored in the location referenced by endptr. If no conversion is performed, zero is returned and the value of string is stored in the location referenced by endptr.
Given the symbolic name of a error number (e.g.,
EACCES
), map it to an errno value. If no translation is found, returns 0.
The
strtol
function converts the string in string to a long integer value according to the given base, which must be between 2 and 36 inclusive, or be the special value 0. If base is 0,strtol
will look for the prefixes0
and0x
to indicate bases 8 and 16, respectively, else default to base 10. When the base is 16 (either explicitly or implicitly), a prefix of0x
is allowed. The handling of endptr is as that ofstrtod
above. Thestrtoul
function is the same, except that the converted value is unsigned.
Given the symbolic name of a signal, map it to a signal number. If no translation is found, returns 0.
This function attempts to create a name for a temporary file, which will be a valid file name yet not exist when
tmpnam
checks for it. s must point to a buffer of at leastL_tmpnam
bytes, or beNULL
. Use of this function creates a security risk, and it must not be used in new projects. Usemkstemp
instead.
If the OS supports it, ensure that the standard I/O streams,
stdin
,stdout
andstderr
are setup to avoid any multi-threaded locking. Otherwise do nothing.
If the OS supports it, ensure that the supplied stream is setup to avoid any multi-threaded locking. Otherwise leave the
FILE
pointer unchanged. If the stream isNULL
do nothing.
Like
vsprintf
, but instead of passing a pointer to a buffer, you pass a pointer to a pointer. This function will compute the size of the buffer needed, allocate memory withmalloc
, and store a pointer to the allocated memory in*
resptr. The value returned is the same asvsprintf
would return. If memory could not be allocated, minus one is returned andNULL
is stored in*
resptr.
These functions are the same as
printf
,fprintf
, andsprintf
, respectively, except that they are called with ava_list
instead of a variable number of arguments. Note that they do not callva_end
; this is the application's responsibility. Inlibiberty
they are implemented in terms of the nonstandard but common function_doprnt
.
This function is similar to vsprintf, but it will print at most n characters. On error the return value is -1, otherwise it returns the number of characters that would have been printed had n been sufficiently large, regardless of the actual value of n. Note some pre-C99 system libraries do not implement this correctly so users cannot generally rely on the return value if the system version of this function is used.
This is a wrapper around the
wait
function. Any “special” values of pid depend on your implementation ofwait
, as does the return value. The third argument is unused inlibiberty
.
Behaves as the standard
atexit
function, but with no limit on the number of registered functions. Returns 0 on success, or −1 on failure. If you usexatexit
to register functions, you must usexexit
to terminate your program.
Allocate memory without fail, and set it to zero. This routine functions like
calloc
, but will behave the same asxmalloc
if memory cannot be found.
Terminates the program. If any functions have been registered with the
xatexit
replacement function, they will be called first. Termination is handled via the system's normalexit
call.
Allocate memory without fail. If
malloc
fails, this will print a message tostderr
(using the name set byxmalloc_set_program_name
, if any) and then callxexit
. Note that it is therefore safe for a program to contain#define malloc xmalloc
in its source.
This function is not meant to be called by client code, and is listed here for completeness only. If any of the allocation routines fail, this function will be called to print an error message and terminate execution.
You can use this to set the name of the program used by
xmalloc_failed
when printing a failure message.
Duplicates a region of memory without fail. First, alloc_size bytes are allocated, then copy_size bytes from input are copied into it, and the new memory is returned. If fewer bytes are copied than were allocated, the remaining memory is zeroed.
Reallocate memory without fail. This routine functions like
realloc
, but will behave the same asxmalloc
if memory cannot be found.