1 /* Function declarations for libiberty.
3 Copyright 2001, 2002 Free Software Foundation, Inc.
5 Note - certain prototypes declared in this header file are for
6 functions whoes implementation copyright does not belong to the
7 FSF. Those prototypes are present in this file for reference
8 purposes only and their presence in this file should not construed
9 as an indication of ownership by the FSF of the implementation of
10 those functions in any way or form whatsoever.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2, or (at your option)
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place - Suite 330,
25 Boston, MA 02111-1307, USA.
27 Written by Cygnus Support, 1994.
29 The libiberty library provides a number of functions which are
30 missing on some operating systems. We do not declare those here,
31 to avoid conflicts with the system header files on operating
32 systems that do support those functions. In this file we only
33 declare those functions which are specific to libiberty. */
44 /* Get a definition for size_t. */
46 /* Get a definition for va_list. */
49 /* Build an argument vector from a string. Allocates memory using
50 malloc. Use freeargv to free the vector. */
52 extern char **buildargv (const char *) ATTRIBUTE_MALLOC;
54 /* Free a vector returned by buildargv. */
56 extern void freeargv (char **);
58 /* Duplicate an argument vector. Allocates memory using malloc. Use
59 freeargv to free the vector. */
61 extern char **dupargv (char **) ATTRIBUTE_MALLOC;
64 /* Return the last component of a path name. Note that we can't use a
65 prototype here because the parameter is declared inconsistently
66 across different systems, sometimes as "char *" and sometimes as
69 /* HAVE_DECL_* is a three-state macro: undefined, 0 or 1. If it is
70 undefined, we haven't run the autoconf check so provide the
71 declaration without arguments. If it is 0, we checked and failed
72 to find the declaration so provide a fully prototyped one. If it
73 is 1, we found it so don't provide any declaration at all. */
74 #if !HAVE_DECL_BASENAME
75 #if defined (__GNU_LIBRARY__ ) || defined (__linux__) || defined (__FreeBSD__) || defined (__OpenBSD__) || defined(__NetBSD__) || defined (__CYGWIN__) || defined (__CYGWIN32__) || defined (__MINGW32__) || defined (HAVE_DECL_BASENAME)
76 extern char *basename (const char *);
78 extern char *basename ();
82 /* A well-defined basename () that is always compiled in. */
84 extern const char *lbasename (const char *);
86 /* A well-defined realpath () that is always compiled in. */
88 extern char *lrealpath (const char *);
90 /* Concatenate an arbitrary number of strings. You must pass NULL as
91 the last argument of this function, to terminate the list of
92 strings. Allocates memory using xmalloc. */
94 extern char *concat (const char *, ...) ATTRIBUTE_MALLOC ATTRIBUTE_SENTINEL;
96 /* Concatenate an arbitrary number of strings. You must pass NULL as
97 the last argument of this function, to terminate the list of
98 strings. Allocates memory using xmalloc. The first argument is
99 not one of the strings to be concatenated, but if not NULL is a
100 pointer to be freed after the new string is created, similar to the
101 way xrealloc works. */
103 extern char *reconcat (char *, const char *, ...) ATTRIBUTE_MALLOC ATTRIBUTE_SENTINEL;
105 /* Determine the length of concatenating an arbitrary number of
106 strings. You must pass NULL as the last argument of this function,
107 to terminate the list of strings. */
109 extern unsigned long concat_length (const char *, ...) ATTRIBUTE_SENTINEL;
111 /* Concatenate an arbitrary number of strings into a SUPPLIED area of
112 memory. You must pass NULL as the last argument of this function,
113 to terminate the list of strings. The supplied memory is assumed
114 to be large enough. */
116 extern char *concat_copy (char *, const char *, ...) ATTRIBUTE_SENTINEL;
118 /* Concatenate an arbitrary number of strings into a GLOBAL area of
119 memory. You must pass NULL as the last argument of this function,
120 to terminate the list of strings. The supplied memory is assumed
121 to be large enough. */
123 extern char *concat_copy2 (const char *, ...) ATTRIBUTE_SENTINEL;
125 /* This is the global area used by concat_copy2. */
127 extern char *libiberty_concat_ptr;
129 /* Concatenate an arbitrary number of strings. You must pass NULL as
130 the last argument of this function, to terminate the list of
131 strings. Allocates memory using alloca. The arguments are
133 #define ACONCAT(ACONCAT_PARAMS) \
134 (libiberty_concat_ptr = alloca (concat_length ACONCAT_PARAMS + 1), \
135 concat_copy2 ACONCAT_PARAMS)
137 /* Check whether two file descriptors refer to the same file. */
139 extern int fdmatch (int fd1, int fd2);
141 /* Get the working directory. The result is cached, so don't call
142 chdir() between calls to getpwd(). */
144 extern char * getpwd (void);
146 /* Get the current time. */
147 /* Prototypes vary from system to system, so we only provide a
148 prototype on systems where we know that we need it. */
150 /* Forward declaration to avoid #include <sys/time.h>. */
152 extern int gettimeofday (struct timeval *, void *);
155 /* Get the amount of time the process has run, in microseconds. */
157 extern long get_run_time (void);
159 /* Generate a relocated path to some installation directory. Allocates
160 return value using malloc. */
162 extern char *make_relative_prefix (const char *, const char *,
163 const char *) ATTRIBUTE_MALLOC;
165 /* Choose a temporary directory to use for scratch files. */
167 extern char *choose_temp_base (void) ATTRIBUTE_MALLOC;
169 /* Return a temporary file name or NULL if unable to create one. */
171 extern char *make_temp_file (const char *) ATTRIBUTE_MALLOC;
173 /* Remove a link to a file unless it is special. */
175 extern int unlink_if_ordinary (const char *);
177 /* Allocate memory filled with spaces. Allocates using malloc. */
179 extern const char *spaces (int count);
181 /* Return the maximum error number for which strerror will return a
184 extern int errno_max (void);
186 /* Return the name of an errno value (e.g., strerrno (EINVAL) returns
189 extern const char *strerrno (int);
191 /* Given the name of an errno value, return the value. */
193 extern int strtoerrno (const char *);
195 /* ANSI's strerror(), but more robust. */
197 extern char *xstrerror (int);
199 /* Return the maximum signal number for which strsignal will return a
202 extern int signo_max (void);
204 /* Return a signal message string for a signal number
205 (e.g., strsignal (SIGHUP) returns something like "Hangup"). */
206 /* This is commented out as it can conflict with one in system headers.
207 We still document its existence though. */
209 /*extern const char *strsignal (int);*/
211 /* Return the name of a signal number (e.g., strsigno (SIGHUP) returns
214 extern const char *strsigno (int);
216 /* Given the name of a signal, return its number. */
218 extern int strtosigno (const char *);
220 /* Register a function to be run by xexit. Returns 0 on success. */
222 extern int xatexit (void (*fn) (void));
224 /* Exit, calling all the functions registered with xatexit. */
226 extern void xexit (int status) ATTRIBUTE_NORETURN;
228 /* Set the program name used by xmalloc. */
230 extern void xmalloc_set_program_name (const char *);
232 /* Report an allocation failure. */
233 extern void xmalloc_failed (size_t) ATTRIBUTE_NORETURN;
235 /* Allocate memory without fail. If malloc fails, this will print a
236 message to stderr (using the name set by xmalloc_set_program_name,
237 if any) and then call xexit. */
239 extern PTR xmalloc (size_t) ATTRIBUTE_MALLOC;
241 /* Reallocate memory without fail. This works like xmalloc. Note,
242 realloc type functions are not suitable for attribute malloc since
243 they may return the same address across multiple calls. */
245 extern PTR xrealloc (PTR, size_t);
247 /* Allocate memory without fail and set it to zero. This works like
250 extern PTR xcalloc (size_t, size_t) ATTRIBUTE_MALLOC;
252 /* Copy a string into a memory buffer without fail. */
254 extern char *xstrdup (const char *) ATTRIBUTE_MALLOC;
256 /* Copy at most N characters from string into a buffer without fail. */
258 extern char *xstrndup (const char *, size_t) ATTRIBUTE_MALLOC;
260 /* Copy an existing memory buffer to a new memory buffer without fail. */
262 extern PTR xmemdup (const PTR, size_t, size_t) ATTRIBUTE_MALLOC;
264 /* Physical memory routines. Return values are in BYTES. */
265 extern double physmem_total (void);
266 extern double physmem_available (void);
269 /* These macros provide a K&R/C89/C++-friendly way of allocating structures
270 with nice encapsulation. The XDELETE*() macros are technically
271 superfluous, but provided here for symmetry. Using them consistently
272 makes it easier to update client code to use different allocators such
273 as new/delete and new[]/delete[]. */
275 /* Scalar allocators. */
277 #define XNEW(T) ((T *) xmalloc (sizeof (T)))
278 #define XCNEW(T) ((T *) xcalloc (1, sizeof (T)))
279 #define XDELETE(P) free ((void*) (P))
281 /* Array allocators. */
283 #define XNEWVEC(T, N) ((T *) xmalloc (sizeof (T) * (N)))
284 #define XCNEWVEC(T, N) ((T *) xcalloc ((N), sizeof (T)))
285 #define XRESIZEVEC(T, P, N) ((T *) xrealloc ((void *) (P), sizeof (T) * (N)))
286 #define XDELETEVEC(P) free ((void*) (P))
288 /* Allocators for variable-sized structures and raw buffers. */
290 #define XNEWVAR(T, S) ((T *) xmalloc ((S)))
291 #define XCNEWVAR(T, S) ((T *) xcalloc (1, (S)))
292 #define XRESIZEVAR(T, P, S) ((T *) xrealloc ((P), (S)))
294 /* Type-safe obstack allocator. */
296 #define XOBNEW(O, T) ((T *) obstack_alloc ((O), sizeof (T)))
299 /* hex character manipulation routines */
301 #define _hex_array_size 256
303 extern const unsigned char _hex_value[_hex_array_size];
304 extern void hex_init (void);
305 #define hex_p(c) (hex_value (c) != _hex_bad)
306 /* If you change this, note well: Some code relies on side effects in
307 the argument being performed exactly once. */
308 #define hex_value(c) ((unsigned int) _hex_value[(unsigned char) (c)])
310 /* Definitions used by the pexecute routine. */
312 #define PEXECUTE_FIRST 1
313 #define PEXECUTE_LAST 2
314 #define PEXECUTE_ONE (PEXECUTE_FIRST + PEXECUTE_LAST)
315 #define PEXECUTE_SEARCH 4
316 #define PEXECUTE_VERBOSE 8
318 /* Execute a program. */
320 extern int pexecute (const char *, char * const *, const char *,
321 const char *, char **, char **, int);
323 /* Wait for pexecute to finish. */
325 extern int pwait (int, int *, int);
327 #if !HAVE_DECL_ASPRINTF
328 /* Like sprintf but provides a pointer to malloc'd storage, which must
329 be freed by the caller. */
331 extern int asprintf (char **, const char *, ...) ATTRIBUTE_PRINTF_2;
334 #if !HAVE_DECL_VASPRINTF
335 /* Like vsprintf but provides a pointer to malloc'd storage, which
336 must be freed by the caller. */
338 extern int vasprintf (char **, const char *, va_list)
339 ATTRIBUTE_PRINTF(2,0);
342 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
344 /* Drastically simplified alloca configurator. If we're using GCC,
345 we use __builtin_alloca; otherwise we use the C alloca. The C
346 alloca is always available. You can override GCC by defining
347 USE_C_ALLOCA yourself. The canonical autoconf macro C_ALLOCA is
348 also set/unset as it is often used to indicate whether code needs
349 to call alloca(0). */
350 extern PTR C_alloca (size_t) ATTRIBUTE_MALLOC;
352 #if GCC_VERSION >= 2000 && !defined USE_C_ALLOCA
353 # define alloca(x) __builtin_alloca(x)
355 # define ASTRDUP(X) \
356 (__extension__ ({ const char *const libiberty_optr = (X); \
357 const unsigned long libiberty_len = strlen (libiberty_optr) + 1; \
358 char *const libiberty_nptr = (char *const) alloca (libiberty_len); \
359 (char *) memcpy (libiberty_nptr, libiberty_optr, libiberty_len); }))
361 # define alloca(x) C_alloca(x)
363 # define USE_C_ALLOCA 1
366 extern const char *libiberty_optr;
367 extern char *libiberty_nptr;
368 extern unsigned long libiberty_len;
369 # define ASTRDUP(X) \
370 (libiberty_optr = (X), \
371 libiberty_len = strlen (libiberty_optr) + 1, \
372 libiberty_nptr = (char *) alloca (libiberty_len), \
373 (char *) memcpy (libiberty_nptr, libiberty_optr, libiberty_len))
381 #endif /* ! defined (LIBIBERTY_H) */