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 65*a95e6fecStobiasu /* Expand "@file" arguments in argv. */ 66*a95e6fecStobiasu 67*a95e6fecStobiasu extern void expandargv PARAMS ((int *, char ***)); 68b305b0f1Sespie 690c6d0228Sniklas /* Return the last component of a path name. Note that we can't use a 700c6d0228Sniklas prototype here because the parameter is declared inconsistently 710c6d0228Sniklas across different systems, sometimes as "char *" and sometimes as 720c6d0228Sniklas "const char *" */ 732159047fSniklas 74b55d4692Sfgsch /* HAVE_DECL_* is a three-state macro: undefined, 0 or 1. If it is 75b55d4692Sfgsch undefined, we haven't run the autoconf check so provide the 76b55d4692Sfgsch declaration without arguments. If it is 0, we checked and failed 77b55d4692Sfgsch to find the declaration so provide a fully prototyped one. If it 78b55d4692Sfgsch is 1, we found it so don't provide any declaration at all. */ 79c074d1c9Sdrahn #if !HAVE_DECL_BASENAME 80c074d1c9Sdrahn #if defined (__GNU_LIBRARY__ ) || defined (__linux__) || defined (__FreeBSD__) || defined (__OpenBSD__) || defined(__NetBSD__) || defined (__CYGWIN__) || defined (__CYGWIN32__) || defined (HAVE_DECL_BASENAME) 81b305b0f1Sespie extern char *basename PARAMS ((const char *)); 82b305b0f1Sespie #else 832159047fSniklas extern char *basename (); 84b305b0f1Sespie #endif 85b55d4692Sfgsch #endif 862159047fSniklas 87c074d1c9Sdrahn /* A well-defined basename () that is always compiled in. */ 88c074d1c9Sdrahn 89c074d1c9Sdrahn extern const char *lbasename PARAMS ((const char *)); 90c074d1c9Sdrahn 91c074d1c9Sdrahn /* A well-defined realpath () that is always compiled in. */ 92c074d1c9Sdrahn 93c074d1c9Sdrahn extern char *lrealpath PARAMS ((const char *)); 94c074d1c9Sdrahn 95c074d1c9Sdrahn /* Concatenate an arbitrary number of strings. You must pass NULL as 96c074d1c9Sdrahn the last argument of this function, to terminate the list of 97c074d1c9Sdrahn strings. Allocates memory using xmalloc. */ 982159047fSniklas 99b305b0f1Sespie extern char *concat PARAMS ((const char *, ...)) ATTRIBUTE_MALLOC; 1002159047fSniklas 101c074d1c9Sdrahn /* Concatenate an arbitrary number of strings. You must pass NULL as 102c074d1c9Sdrahn the last argument of this function, to terminate the list of 103c074d1c9Sdrahn strings. Allocates memory using xmalloc. The first argument is 104c074d1c9Sdrahn not one of the strings to be concatenated, but if not NULL is a 105c074d1c9Sdrahn pointer to be freed after the new string is created, similar to the 106c074d1c9Sdrahn way xrealloc works. */ 107c074d1c9Sdrahn 108c074d1c9Sdrahn extern char *reconcat PARAMS ((char *, const char *, ...)) ATTRIBUTE_MALLOC; 109c074d1c9Sdrahn 110c074d1c9Sdrahn /* Determine the length of concatenating an arbitrary number of 111c074d1c9Sdrahn strings. You must pass NULL as the last argument of this function, 112c074d1c9Sdrahn to terminate the list of strings. */ 113c074d1c9Sdrahn 114c074d1c9Sdrahn extern unsigned long concat_length PARAMS ((const char *, ...)); 115c074d1c9Sdrahn 116c074d1c9Sdrahn /* Concatenate an arbitrary number of strings into a SUPPLIED area of 117c074d1c9Sdrahn memory. You must pass NULL as the last argument of this function, 118c074d1c9Sdrahn to terminate the list of strings. The supplied memory is assumed 119c074d1c9Sdrahn to be large enough. */ 120c074d1c9Sdrahn 121c074d1c9Sdrahn extern char *concat_copy PARAMS ((char *, const char *, ...)); 122c074d1c9Sdrahn 123c074d1c9Sdrahn /* Concatenate an arbitrary number of strings into a GLOBAL area of 124c074d1c9Sdrahn memory. You must pass NULL as the last argument of this function, 125c074d1c9Sdrahn to terminate the list of strings. The supplied memory is assumed 126c074d1c9Sdrahn to be large enough. */ 127c074d1c9Sdrahn 128c074d1c9Sdrahn extern char *concat_copy2 PARAMS ((const char *, ...)); 129c074d1c9Sdrahn 130c074d1c9Sdrahn /* This is the global area used by concat_copy2. */ 131c074d1c9Sdrahn 132c074d1c9Sdrahn extern char *libiberty_concat_ptr; 133c074d1c9Sdrahn 134c074d1c9Sdrahn /* Concatenate an arbitrary number of strings. You must pass NULL as 135c074d1c9Sdrahn the last argument of this function, to terminate the list of 136c074d1c9Sdrahn strings. Allocates memory using alloca. The arguments are 137c074d1c9Sdrahn evaluated twice! */ 138c074d1c9Sdrahn #define ACONCAT(ACONCAT_PARAMS) \ 139c074d1c9Sdrahn (libiberty_concat_ptr = alloca (concat_length ACONCAT_PARAMS + 1), \ 140c074d1c9Sdrahn concat_copy2 ACONCAT_PARAMS) 141c074d1c9Sdrahn 1422159047fSniklas /* Check whether two file descriptors refer to the same file. */ 1432159047fSniklas 1442159047fSniklas extern int fdmatch PARAMS ((int fd1, int fd2)); 1452159047fSniklas 146b305b0f1Sespie /* Get the working directory. The result is cached, so don't call 147b305b0f1Sespie chdir() between calls to getpwd(). */ 148b305b0f1Sespie 149b305b0f1Sespie extern char * getpwd PARAMS ((void)); 150b305b0f1Sespie 1512159047fSniklas /* Get the amount of time the process has run, in microseconds. */ 1522159047fSniklas 1532159047fSniklas extern long get_run_time PARAMS ((void)); 1542159047fSniklas 155c074d1c9Sdrahn /* Generate a relocated path to some installation directory. Allocates 156c074d1c9Sdrahn return value using malloc. */ 157c074d1c9Sdrahn 158c074d1c9Sdrahn extern char *make_relative_prefix PARAMS ((const char *, const char *, 159c074d1c9Sdrahn const char *)); 160c074d1c9Sdrahn 161b305b0f1Sespie /* Choose a temporary directory to use for scratch files. */ 162c88b1d6cSniklas 163b305b0f1Sespie extern char *choose_temp_base PARAMS ((void)) ATTRIBUTE_MALLOC; 164c88b1d6cSniklas 165b305b0f1Sespie /* Return a temporary file name or NULL if unable to create one. */ 166b305b0f1Sespie 167b305b0f1Sespie extern char *make_temp_file PARAMS ((const char *)) ATTRIBUTE_MALLOC; 168bc1ed23cSespie 1692159047fSniklas /* Allocate memory filled with spaces. Allocates using malloc. */ 1702159047fSniklas 1712159047fSniklas extern const char *spaces PARAMS ((int count)); 1722159047fSniklas 1732159047fSniklas /* Return the maximum error number for which strerror will return a 1742159047fSniklas string. */ 1752159047fSniklas 1762159047fSniklas extern int errno_max PARAMS ((void)); 1772159047fSniklas 1782159047fSniklas /* Return the name of an errno value (e.g., strerrno (EINVAL) returns 1792159047fSniklas "EINVAL"). */ 1802159047fSniklas 1812159047fSniklas extern const char *strerrno PARAMS ((int)); 1822159047fSniklas 1832159047fSniklas /* Given the name of an errno value, return the value. */ 1842159047fSniklas 1852159047fSniklas extern int strtoerrno PARAMS ((const char *)); 1862159047fSniklas 1872159047fSniklas /* ANSI's strerror(), but more robust. */ 1882159047fSniklas 1892159047fSniklas extern char *xstrerror PARAMS ((int)); 1902159047fSniklas 1912159047fSniklas /* Return the maximum signal number for which strsignal will return a 1922159047fSniklas string. */ 1932159047fSniklas 1942159047fSniklas extern int signo_max PARAMS ((void)); 1952159047fSniklas 1962159047fSniklas /* Return a signal message string for a signal number 1972159047fSniklas (e.g., strsignal (SIGHUP) returns something like "Hangup"). */ 1982159047fSniklas /* This is commented out as it can conflict with one in system headers. 1992159047fSniklas We still document its existence though. */ 2002159047fSniklas 2012159047fSniklas /*extern const char *strsignal PARAMS ((int));*/ 2022159047fSniklas 2032159047fSniklas /* Return the name of a signal number (e.g., strsigno (SIGHUP) returns 2042159047fSniklas "SIGHUP"). */ 2052159047fSniklas 2062159047fSniklas extern const char *strsigno PARAMS ((int)); 2072159047fSniklas 2082159047fSniklas /* Given the name of a signal, return its number. */ 2092159047fSniklas 2102159047fSniklas extern int strtosigno PARAMS ((const char *)); 2112159047fSniklas 2122159047fSniklas /* Register a function to be run by xexit. Returns 0 on success. */ 2132159047fSniklas 2142159047fSniklas extern int xatexit PARAMS ((void (*fn) (void))); 2152159047fSniklas 2162159047fSniklas /* Exit, calling all the functions registered with xatexit. */ 2172159047fSniklas 218b305b0f1Sespie extern void xexit PARAMS ((int status)) ATTRIBUTE_NORETURN; 2192159047fSniklas 2202159047fSniklas /* Set the program name used by xmalloc. */ 2212159047fSniklas 2222159047fSniklas extern void xmalloc_set_program_name PARAMS ((const char *)); 2232159047fSniklas 224b55d4692Sfgsch /* Report an allocation failure. */ 225b55d4692Sfgsch extern void xmalloc_failed PARAMS ((size_t)) ATTRIBUTE_NORETURN; 226b55d4692Sfgsch 2272159047fSniklas /* Allocate memory without fail. If malloc fails, this will print a 2282159047fSniklas message to stderr (using the name set by xmalloc_set_program_name, 229e93f7393Sniklas if any) and then call xexit. */ 2302159047fSniklas 231b305b0f1Sespie extern PTR xmalloc PARAMS ((size_t)) ATTRIBUTE_MALLOC; 2322159047fSniklas 233b305b0f1Sespie /* Reallocate memory without fail. This works like xmalloc. Note, 234b305b0f1Sespie realloc type functions are not suitable for attribute malloc since 235b305b0f1Sespie they may return the same address across multiple calls. */ 2362159047fSniklas 237e93f7393Sniklas extern PTR xrealloc PARAMS ((PTR, size_t)); 2380c6d0228Sniklas 239b305b0f1Sespie /* Allocate memory without fail and set it to zero. This works like 240b305b0f1Sespie xmalloc. */ 241b305b0f1Sespie 242b305b0f1Sespie extern PTR xcalloc PARAMS ((size_t, size_t)) ATTRIBUTE_MALLOC; 243b305b0f1Sespie 244c88b1d6cSniklas /* Copy a string into a memory buffer without fail. */ 245c88b1d6cSniklas 246b305b0f1Sespie extern char *xstrdup PARAMS ((const char *)) ATTRIBUTE_MALLOC; 247b305b0f1Sespie 248b305b0f1Sespie /* Copy an existing memory buffer to a new memory buffer without fail. */ 249b305b0f1Sespie 250b305b0f1Sespie extern PTR xmemdup PARAMS ((const PTR, size_t, size_t)) ATTRIBUTE_MALLOC; 251c88b1d6cSniklas 252c074d1c9Sdrahn /* Physical memory routines. Return values are in BYTES. */ 253c074d1c9Sdrahn extern double physmem_total PARAMS ((void)); 254c074d1c9Sdrahn extern double physmem_available PARAMS ((void)); 255c074d1c9Sdrahn 2562159047fSniklas /* hex character manipulation routines */ 2572159047fSniklas 2582159047fSniklas #define _hex_array_size 256 2592159047fSniklas #define _hex_bad 99 260007c2a45Smiod extern const unsigned char _hex_value[_hex_array_size]; 2612159047fSniklas extern void hex_init PARAMS ((void)); 2622159047fSniklas #define hex_p(c) (hex_value (c) != _hex_bad) 2632159047fSniklas /* If you change this, note well: Some code relies on side effects in 2642159047fSniklas the argument being performed exactly once. */ 265007c2a45Smiod #define hex_value(c) ((unsigned int) _hex_value[(unsigned char) (c)]) 2662159047fSniklas 267b305b0f1Sespie /* Definitions used by the pexecute routine. */ 268b305b0f1Sespie 269b305b0f1Sespie #define PEXECUTE_FIRST 1 270b305b0f1Sespie #define PEXECUTE_LAST 2 271b305b0f1Sespie #define PEXECUTE_ONE (PEXECUTE_FIRST + PEXECUTE_LAST) 272b305b0f1Sespie #define PEXECUTE_SEARCH 4 273b305b0f1Sespie #define PEXECUTE_VERBOSE 8 274b305b0f1Sespie 275b305b0f1Sespie /* Execute a program. */ 276b305b0f1Sespie 277b305b0f1Sespie extern int pexecute PARAMS ((const char *, char * const *, const char *, 278b305b0f1Sespie const char *, char **, char **, int)); 279b305b0f1Sespie 280b305b0f1Sespie /* Wait for pexecute to finish. */ 281b305b0f1Sespie 282b305b0f1Sespie extern int pwait PARAMS ((int, int *, int)); 283b305b0f1Sespie 284c074d1c9Sdrahn #if !HAVE_DECL_ASPRINTF 285b305b0f1Sespie /* Like sprintf but provides a pointer to malloc'd storage, which must 286b305b0f1Sespie be freed by the caller. */ 287b305b0f1Sespie 288b305b0f1Sespie extern int asprintf PARAMS ((char **, const char *, ...)) ATTRIBUTE_PRINTF_2; 289c074d1c9Sdrahn #endif 290b305b0f1Sespie 291c074d1c9Sdrahn #if !HAVE_DECL_VASPRINTF 292b305b0f1Sespie /* Like vsprintf but provides a pointer to malloc'd storage, which 293b305b0f1Sespie must be freed by the caller. */ 294b305b0f1Sespie 295b305b0f1Sespie extern int vasprintf PARAMS ((char **, const char *, va_list)) 296b305b0f1Sespie ATTRIBUTE_PRINTF(2,0); 297c074d1c9Sdrahn #endif 298b305b0f1Sespie 299b55d4692Sfgsch #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0])) 300b55d4692Sfgsch 301c074d1c9Sdrahn /* Drastically simplified alloca configurator. If we're using GCC, 302c074d1c9Sdrahn we use __builtin_alloca; otherwise we use the C alloca. The C 303c074d1c9Sdrahn alloca is always available. You can override GCC by defining 304c074d1c9Sdrahn USE_C_ALLOCA yourself. The canonical autoconf macro C_ALLOCA is 305c074d1c9Sdrahn also set/unset as it is often used to indicate whether code needs 306c074d1c9Sdrahn to call alloca(0). */ 307c074d1c9Sdrahn extern PTR C_alloca PARAMS ((size_t)) ATTRIBUTE_MALLOC; 308c074d1c9Sdrahn #undef alloca 309c074d1c9Sdrahn #if GCC_VERSION >= 2000 && !defined USE_C_ALLOCA 310c074d1c9Sdrahn # define alloca(x) __builtin_alloca(x) 311c074d1c9Sdrahn # undef C_ALLOCA 312c074d1c9Sdrahn # define ASTRDUP(X) \ 313c074d1c9Sdrahn (__extension__ ({ const char *const libiberty_optr = (X); \ 314c074d1c9Sdrahn const unsigned long libiberty_len = strlen (libiberty_optr) + 1; \ 315c074d1c9Sdrahn char *const libiberty_nptr = alloca (libiberty_len); \ 316c074d1c9Sdrahn (char *) memcpy (libiberty_nptr, libiberty_optr, libiberty_len); })) 317c074d1c9Sdrahn #else 318c074d1c9Sdrahn # define alloca(x) C_alloca(x) 319c074d1c9Sdrahn # undef USE_C_ALLOCA 320c074d1c9Sdrahn # define USE_C_ALLOCA 1 321c074d1c9Sdrahn # undef C_ALLOCA 322c074d1c9Sdrahn # define C_ALLOCA 1 323c074d1c9Sdrahn extern const char *libiberty_optr; 324c074d1c9Sdrahn extern char *libiberty_nptr; 325c074d1c9Sdrahn extern unsigned long libiberty_len; 326c074d1c9Sdrahn # define ASTRDUP(X) \ 327c074d1c9Sdrahn (libiberty_optr = (X), \ 328c074d1c9Sdrahn libiberty_len = strlen (libiberty_optr) + 1, \ 329c074d1c9Sdrahn libiberty_nptr = alloca (libiberty_len), \ 330c074d1c9Sdrahn (char *) memcpy (libiberty_nptr, libiberty_optr, libiberty_len)) 331c074d1c9Sdrahn #endif 332c074d1c9Sdrahn 333b305b0f1Sespie #ifdef __cplusplus 334b305b0f1Sespie } 335b305b0f1Sespie #endif 336b305b0f1Sespie 337b305b0f1Sespie 3382159047fSniklas #endif /* ! defined (LIBIBERTY_H) */ 339