12159047fSniklas /* Function declarations for libiberty.
2b55d4692Sfgsch 
3c074d1c9Sdrahn    Copyright 2001, 2002 Free Software Foundation, Inc.
4b55d4692Sfgsch 
5b55d4692Sfgsch    Note - certain prototypes declared in this header file are for
6b55d4692Sfgsch    functions whoes implementation copyright does not belong to the
7b55d4692Sfgsch    FSF.  Those prototypes are present in this file for reference
8b55d4692Sfgsch    purposes only and their presence in this file should not construed
9b55d4692Sfgsch    as an indication of ownership by the FSF of the implementation of
10b55d4692Sfgsch    those functions in any way or form whatsoever.
11b55d4692Sfgsch 
12b55d4692Sfgsch    This program is free software; you can redistribute it and/or modify
13b55d4692Sfgsch    it under the terms of the GNU General Public License as published by
14b55d4692Sfgsch    the Free Software Foundation; either version 2, or (at your option)
15b55d4692Sfgsch    any later version.
16b55d4692Sfgsch 
17b55d4692Sfgsch    This program is distributed in the hope that it will be useful,
18b55d4692Sfgsch    but WITHOUT ANY WARRANTY; without even the implied warranty of
19b55d4692Sfgsch    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20b55d4692Sfgsch    GNU General Public License for more details.
21b55d4692Sfgsch 
22b55d4692Sfgsch    You should have received a copy of the GNU General Public License
23b55d4692Sfgsch    along with this program; if not, write to the Free Software
24b55d4692Sfgsch    Foundation, Inc., 59 Temple Place - Suite 330,
25b55d4692Sfgsch    Boston, MA 02111-1307, USA.
26b55d4692Sfgsch 
272159047fSniklas    Written by Cygnus Support, 1994.
282159047fSniklas 
292159047fSniklas    The libiberty library provides a number of functions which are
302159047fSniklas    missing on some operating systems.  We do not declare those here,
312159047fSniklas    to avoid conflicts with the system header files on operating
322159047fSniklas    systems that do support those functions.  In this file we only
332159047fSniklas    declare those functions which are specific to libiberty.  */
342159047fSniklas 
352159047fSniklas #ifndef LIBIBERTY_H
362159047fSniklas #define LIBIBERTY_H
372159047fSniklas 
38b305b0f1Sespie #ifdef __cplusplus
39b305b0f1Sespie extern "C" {
40b305b0f1Sespie #endif
41b305b0f1Sespie 
422159047fSniklas #include "ansidecl.h"
432159047fSniklas 
44b55d4692Sfgsch #ifdef ANSI_PROTOTYPES
45b55d4692Sfgsch /* Get a definition for size_t.  */
46b55d4692Sfgsch #include <stddef.h>
47b55d4692Sfgsch /* Get a definition for va_list.  */
48b55d4692Sfgsch #include <stdarg.h>
49b55d4692Sfgsch #endif
50b55d4692Sfgsch 
512159047fSniklas /* Build an argument vector from a string.  Allocates memory using
522159047fSniklas    malloc.  Use freeargv to free the vector.  */
532159047fSniklas 
54c074d1c9Sdrahn extern char **buildargv PARAMS ((const char *)) ATTRIBUTE_MALLOC;
552159047fSniklas 
562159047fSniklas /* Free a vector returned by buildargv.  */
572159047fSniklas 
582159047fSniklas extern void freeargv PARAMS ((char **));
592159047fSniklas 
60b305b0f1Sespie /* Duplicate an argument vector. Allocates memory using malloc.  Use
61b305b0f1Sespie    freeargv to free the vector.  */
62b305b0f1Sespie 
63b305b0f1Sespie extern char **dupargv PARAMS ((char **)) ATTRIBUTE_MALLOC;
64b305b0f1Sespie 
65b305b0f1Sespie 
660c6d0228Sniklas /* Return the last component of a path name.  Note that we can't use a
670c6d0228Sniklas    prototype here because the parameter is declared inconsistently
680c6d0228Sniklas    across different systems, sometimes as "char *" and sometimes as
690c6d0228Sniklas    "const char *" */
702159047fSniklas 
71b55d4692Sfgsch /* HAVE_DECL_* is a three-state macro: undefined, 0 or 1.  If it is
72b55d4692Sfgsch    undefined, we haven't run the autoconf check so provide the
73b55d4692Sfgsch    declaration without arguments.  If it is 0, we checked and failed
74b55d4692Sfgsch    to find the declaration so provide a fully prototyped one.  If it
75b55d4692Sfgsch    is 1, we found it so don't provide any declaration at all.  */
76c074d1c9Sdrahn #if !HAVE_DECL_BASENAME
77c074d1c9Sdrahn #if defined (__GNU_LIBRARY__ ) || defined (__linux__) || defined (__FreeBSD__) || defined (__OpenBSD__) || defined(__NetBSD__) || defined (__CYGWIN__) || defined (__CYGWIN32__) || defined (HAVE_DECL_BASENAME)
78b305b0f1Sespie extern char *basename PARAMS ((const char *));
79b305b0f1Sespie #else
802159047fSniklas extern char *basename ();
81b305b0f1Sespie #endif
82b55d4692Sfgsch #endif
832159047fSniklas 
84c074d1c9Sdrahn /* A well-defined basename () that is always compiled in.  */
85c074d1c9Sdrahn 
86c074d1c9Sdrahn extern const char *lbasename PARAMS ((const char *));
87c074d1c9Sdrahn 
88c074d1c9Sdrahn /* A well-defined realpath () that is always compiled in.  */
89c074d1c9Sdrahn 
90c074d1c9Sdrahn extern char *lrealpath PARAMS ((const char *));
91c074d1c9Sdrahn 
92c074d1c9Sdrahn /* Concatenate an arbitrary number of strings.  You must pass NULL as
93c074d1c9Sdrahn    the last argument of this function, to terminate the list of
94c074d1c9Sdrahn    strings.  Allocates memory using xmalloc.  */
952159047fSniklas 
96b305b0f1Sespie extern char *concat PARAMS ((const char *, ...)) ATTRIBUTE_MALLOC;
972159047fSniklas 
98c074d1c9Sdrahn /* Concatenate an arbitrary number of strings.  You must pass NULL as
99c074d1c9Sdrahn    the last argument of this function, to terminate the list of
100c074d1c9Sdrahn    strings.  Allocates memory using xmalloc.  The first argument is
101c074d1c9Sdrahn    not one of the strings to be concatenated, but if not NULL is a
102c074d1c9Sdrahn    pointer to be freed after the new string is created, similar to the
103c074d1c9Sdrahn    way xrealloc works.  */
104c074d1c9Sdrahn 
105c074d1c9Sdrahn extern char *reconcat PARAMS ((char *, const char *, ...)) ATTRIBUTE_MALLOC;
106c074d1c9Sdrahn 
107c074d1c9Sdrahn /* Determine the length of concatenating an arbitrary number of
108c074d1c9Sdrahn    strings.  You must pass NULL as the last argument of this function,
109c074d1c9Sdrahn    to terminate the list of strings.  */
110c074d1c9Sdrahn 
111c074d1c9Sdrahn extern unsigned long concat_length PARAMS ((const char *, ...));
112c074d1c9Sdrahn 
113c074d1c9Sdrahn /* Concatenate an arbitrary number of strings into a SUPPLIED area of
114c074d1c9Sdrahn    memory.  You must pass NULL as the last argument of this function,
115c074d1c9Sdrahn    to terminate the list of strings.  The supplied memory is assumed
116c074d1c9Sdrahn    to be large enough.  */
117c074d1c9Sdrahn 
118c074d1c9Sdrahn extern char *concat_copy PARAMS ((char *, const char *, ...));
119c074d1c9Sdrahn 
120c074d1c9Sdrahn /* Concatenate an arbitrary number of strings into a GLOBAL area of
121c074d1c9Sdrahn    memory.  You must pass NULL as the last argument of this function,
122c074d1c9Sdrahn    to terminate the list of strings.  The supplied memory is assumed
123c074d1c9Sdrahn    to be large enough.  */
124c074d1c9Sdrahn 
125c074d1c9Sdrahn extern char *concat_copy2 PARAMS ((const char *, ...));
126c074d1c9Sdrahn 
127c074d1c9Sdrahn /* This is the global area used by concat_copy2.  */
128c074d1c9Sdrahn 
129c074d1c9Sdrahn extern char *libiberty_concat_ptr;
130c074d1c9Sdrahn 
131c074d1c9Sdrahn /* Concatenate an arbitrary number of strings.  You must pass NULL as
132c074d1c9Sdrahn    the last argument of this function, to terminate the list of
133c074d1c9Sdrahn    strings.  Allocates memory using alloca.  The arguments are
134c074d1c9Sdrahn    evaluated twice!  */
135c074d1c9Sdrahn #define ACONCAT(ACONCAT_PARAMS) \
136c074d1c9Sdrahn   (libiberty_concat_ptr = alloca (concat_length ACONCAT_PARAMS + 1), \
137c074d1c9Sdrahn    concat_copy2 ACONCAT_PARAMS)
138c074d1c9Sdrahn 
1392159047fSniklas /* Check whether two file descriptors refer to the same file.  */
1402159047fSniklas 
1412159047fSniklas extern int fdmatch PARAMS ((int fd1, int fd2));
1422159047fSniklas 
143b305b0f1Sespie /* Get the working directory.  The result is cached, so don't call
144b305b0f1Sespie    chdir() between calls to getpwd().  */
145b305b0f1Sespie 
146b305b0f1Sespie extern char * getpwd PARAMS ((void));
147b305b0f1Sespie 
1482159047fSniklas /* Get the amount of time the process has run, in microseconds.  */
1492159047fSniklas 
1502159047fSniklas extern long get_run_time PARAMS ((void));
1512159047fSniklas 
152c074d1c9Sdrahn /* Generate a relocated path to some installation directory.  Allocates
153c074d1c9Sdrahn    return value using malloc.  */
154c074d1c9Sdrahn 
155c074d1c9Sdrahn extern char *make_relative_prefix PARAMS ((const char *, const char *,
156c074d1c9Sdrahn 					   const char *));
157c074d1c9Sdrahn 
158b305b0f1Sespie /* Choose a temporary directory to use for scratch files.  */
159c88b1d6cSniklas 
160b305b0f1Sespie extern char *choose_temp_base PARAMS ((void)) ATTRIBUTE_MALLOC;
161c88b1d6cSniklas 
162b305b0f1Sespie /* Return a temporary file name or NULL if unable to create one.  */
163b305b0f1Sespie 
164b305b0f1Sespie extern char *make_temp_file PARAMS ((const char *)) ATTRIBUTE_MALLOC;
165bc1ed23cSespie 
1662159047fSniklas /* Allocate memory filled with spaces.  Allocates using malloc.  */
1672159047fSniklas 
1682159047fSniklas extern const char *spaces PARAMS ((int count));
1692159047fSniklas 
1702159047fSniklas /* Return the maximum error number for which strerror will return a
1712159047fSniklas    string.  */
1722159047fSniklas 
1732159047fSniklas extern int errno_max PARAMS ((void));
1742159047fSniklas 
1752159047fSniklas /* Return the name of an errno value (e.g., strerrno (EINVAL) returns
1762159047fSniklas    "EINVAL").  */
1772159047fSniklas 
1782159047fSniklas extern const char *strerrno PARAMS ((int));
1792159047fSniklas 
1802159047fSniklas /* Given the name of an errno value, return the value.  */
1812159047fSniklas 
1822159047fSniklas extern int strtoerrno PARAMS ((const char *));
1832159047fSniklas 
1842159047fSniklas /* ANSI's strerror(), but more robust.  */
1852159047fSniklas 
1862159047fSniklas extern char *xstrerror PARAMS ((int));
1872159047fSniklas 
1882159047fSniklas /* Return the maximum signal number for which strsignal will return a
1892159047fSniklas    string.  */
1902159047fSniklas 
1912159047fSniklas extern int signo_max PARAMS ((void));
1922159047fSniklas 
1932159047fSniklas /* Return a signal message string for a signal number
1942159047fSniklas    (e.g., strsignal (SIGHUP) returns something like "Hangup").  */
1952159047fSniklas /* This is commented out as it can conflict with one in system headers.
1962159047fSniklas    We still document its existence though.  */
1972159047fSniklas 
1982159047fSniklas /*extern const char *strsignal PARAMS ((int));*/
1992159047fSniklas 
2002159047fSniklas /* Return the name of a signal number (e.g., strsigno (SIGHUP) returns
2012159047fSniklas    "SIGHUP").  */
2022159047fSniklas 
2032159047fSniklas extern const char *strsigno PARAMS ((int));
2042159047fSniklas 
2052159047fSniklas /* Given the name of a signal, return its number.  */
2062159047fSniklas 
2072159047fSniklas extern int strtosigno PARAMS ((const char *));
2082159047fSniklas 
2092159047fSniklas /* Register a function to be run by xexit.  Returns 0 on success.  */
2102159047fSniklas 
2112159047fSniklas extern int xatexit PARAMS ((void (*fn) (void)));
2122159047fSniklas 
2132159047fSniklas /* Exit, calling all the functions registered with xatexit.  */
2142159047fSniklas 
215b305b0f1Sespie extern void xexit PARAMS ((int status)) ATTRIBUTE_NORETURN;
2162159047fSniklas 
2172159047fSniklas /* Set the program name used by xmalloc.  */
2182159047fSniklas 
2192159047fSniklas extern void xmalloc_set_program_name PARAMS ((const char *));
2202159047fSniklas 
221b55d4692Sfgsch /* Report an allocation failure.  */
222b55d4692Sfgsch extern void xmalloc_failed PARAMS ((size_t)) ATTRIBUTE_NORETURN;
223b55d4692Sfgsch 
2242159047fSniklas /* Allocate memory without fail.  If malloc fails, this will print a
2252159047fSniklas    message to stderr (using the name set by xmalloc_set_program_name,
226e93f7393Sniklas    if any) and then call xexit.  */
2272159047fSniklas 
228b305b0f1Sespie extern PTR xmalloc PARAMS ((size_t)) ATTRIBUTE_MALLOC;
2292159047fSniklas 
230b305b0f1Sespie /* Reallocate memory without fail.  This works like xmalloc.  Note,
231b305b0f1Sespie    realloc type functions are not suitable for attribute malloc since
232b305b0f1Sespie    they may return the same address across multiple calls. */
2332159047fSniklas 
234e93f7393Sniklas extern PTR xrealloc PARAMS ((PTR, size_t));
2350c6d0228Sniklas 
236b305b0f1Sespie /* Allocate memory without fail and set it to zero.  This works like
237b305b0f1Sespie    xmalloc.  */
238b305b0f1Sespie 
239b305b0f1Sespie extern PTR xcalloc PARAMS ((size_t, size_t)) ATTRIBUTE_MALLOC;
240b305b0f1Sespie 
241c88b1d6cSniklas /* Copy a string into a memory buffer without fail.  */
242c88b1d6cSniklas 
243b305b0f1Sespie extern char *xstrdup PARAMS ((const char *)) ATTRIBUTE_MALLOC;
244b305b0f1Sespie 
245b305b0f1Sespie /* Copy an existing memory buffer to a new memory buffer without fail.  */
246b305b0f1Sespie 
247b305b0f1Sespie extern PTR xmemdup PARAMS ((const PTR, size_t, size_t)) ATTRIBUTE_MALLOC;
248c88b1d6cSniklas 
249c074d1c9Sdrahn /* Physical memory routines.  Return values are in BYTES.  */
250c074d1c9Sdrahn extern double physmem_total PARAMS ((void));
251c074d1c9Sdrahn extern double physmem_available PARAMS ((void));
252c074d1c9Sdrahn 
2532159047fSniklas /* hex character manipulation routines */
2542159047fSniklas 
2552159047fSniklas #define _hex_array_size 256
2562159047fSniklas #define _hex_bad	99
257*007c2a45Smiod extern const unsigned char _hex_value[_hex_array_size];
2582159047fSniklas extern void hex_init PARAMS ((void));
2592159047fSniklas #define hex_p(c)	(hex_value (c) != _hex_bad)
2602159047fSniklas /* If you change this, note well: Some code relies on side effects in
2612159047fSniklas    the argument being performed exactly once.  */
262*007c2a45Smiod #define hex_value(c)	((unsigned int) _hex_value[(unsigned char) (c)])
2632159047fSniklas 
264b305b0f1Sespie /* Definitions used by the pexecute routine.  */
265b305b0f1Sespie 
266b305b0f1Sespie #define PEXECUTE_FIRST   1
267b305b0f1Sespie #define PEXECUTE_LAST    2
268b305b0f1Sespie #define PEXECUTE_ONE     (PEXECUTE_FIRST + PEXECUTE_LAST)
269b305b0f1Sespie #define PEXECUTE_SEARCH  4
270b305b0f1Sespie #define PEXECUTE_VERBOSE 8
271b305b0f1Sespie 
272b305b0f1Sespie /* Execute a program.  */
273b305b0f1Sespie 
274b305b0f1Sespie extern int pexecute PARAMS ((const char *, char * const *, const char *,
275b305b0f1Sespie 			    const char *, char **, char **, int));
276b305b0f1Sespie 
277b305b0f1Sespie /* Wait for pexecute to finish.  */
278b305b0f1Sespie 
279b305b0f1Sespie extern int pwait PARAMS ((int, int *, int));
280b305b0f1Sespie 
281c074d1c9Sdrahn #if !HAVE_DECL_ASPRINTF
282b305b0f1Sespie /* Like sprintf but provides a pointer to malloc'd storage, which must
283b305b0f1Sespie    be freed by the caller.  */
284b305b0f1Sespie 
285b305b0f1Sespie extern int asprintf PARAMS ((char **, const char *, ...)) ATTRIBUTE_PRINTF_2;
286c074d1c9Sdrahn #endif
287b305b0f1Sespie 
288c074d1c9Sdrahn #if !HAVE_DECL_VASPRINTF
289b305b0f1Sespie /* Like vsprintf but provides a pointer to malloc'd storage, which
290b305b0f1Sespie    must be freed by the caller.  */
291b305b0f1Sespie 
292b305b0f1Sespie extern int vasprintf PARAMS ((char **, const char *, va_list))
293b305b0f1Sespie   ATTRIBUTE_PRINTF(2,0);
294c074d1c9Sdrahn #endif
295b305b0f1Sespie 
296b55d4692Sfgsch #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
297b55d4692Sfgsch 
298c074d1c9Sdrahn /* Drastically simplified alloca configurator.  If we're using GCC,
299c074d1c9Sdrahn    we use __builtin_alloca; otherwise we use the C alloca.  The C
300c074d1c9Sdrahn    alloca is always available.  You can override GCC by defining
301c074d1c9Sdrahn    USE_C_ALLOCA yourself.  The canonical autoconf macro C_ALLOCA is
302c074d1c9Sdrahn    also set/unset as it is often used to indicate whether code needs
303c074d1c9Sdrahn    to call alloca(0).  */
304c074d1c9Sdrahn extern PTR C_alloca PARAMS ((size_t)) ATTRIBUTE_MALLOC;
305c074d1c9Sdrahn #undef alloca
306c074d1c9Sdrahn #if GCC_VERSION >= 2000 && !defined USE_C_ALLOCA
307c074d1c9Sdrahn # define alloca(x) __builtin_alloca(x)
308c074d1c9Sdrahn # undef C_ALLOCA
309c074d1c9Sdrahn # define ASTRDUP(X) \
310c074d1c9Sdrahn   (__extension__ ({ const char *const libiberty_optr = (X); \
311c074d1c9Sdrahn    const unsigned long libiberty_len = strlen (libiberty_optr) + 1; \
312c074d1c9Sdrahn    char *const libiberty_nptr = alloca (libiberty_len); \
313c074d1c9Sdrahn    (char *) memcpy (libiberty_nptr, libiberty_optr, libiberty_len); }))
314c074d1c9Sdrahn #else
315c074d1c9Sdrahn # define alloca(x) C_alloca(x)
316c074d1c9Sdrahn # undef USE_C_ALLOCA
317c074d1c9Sdrahn # define USE_C_ALLOCA 1
318c074d1c9Sdrahn # undef C_ALLOCA
319c074d1c9Sdrahn # define C_ALLOCA 1
320c074d1c9Sdrahn extern const char *libiberty_optr;
321c074d1c9Sdrahn extern char *libiberty_nptr;
322c074d1c9Sdrahn extern unsigned long libiberty_len;
323c074d1c9Sdrahn # define ASTRDUP(X) \
324c074d1c9Sdrahn   (libiberty_optr = (X), \
325c074d1c9Sdrahn    libiberty_len = strlen (libiberty_optr) + 1, \
326c074d1c9Sdrahn    libiberty_nptr = alloca (libiberty_len), \
327c074d1c9Sdrahn    (char *) memcpy (libiberty_nptr, libiberty_optr, libiberty_len))
328c074d1c9Sdrahn #endif
329c074d1c9Sdrahn 
330b305b0f1Sespie #ifdef __cplusplus
331b305b0f1Sespie }
332b305b0f1Sespie #endif
333b305b0f1Sespie 
334b305b0f1Sespie 
3352159047fSniklas #endif /* ! defined (LIBIBERTY_H) */
336