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 /* Return the position of the first bit set in the argument. */
142 /* Prototypes vary from system to system, so we only provide a
143 prototype on systems where we know that we need it. */
144 #if defined (HAVE_DECL_FFS) && !HAVE_DECL_FFS
148 /* Get the working directory. The result is cached, so don't call
149 chdir() between calls to getpwd(). */
151 extern char * getpwd (void);
153 /* Get the current time. */
154 /* Prototypes vary from system to system, so we only provide a
155 prototype on systems where we know that we need it. */
157 /* Forward declaration to avoid #include <sys/time.h>. */
159 extern int gettimeofday (struct timeval *, void *);
162 /* Get the amount of time the process has run, in microseconds. */
164 extern long get_run_time (void);
166 /* Generate a relocated path to some installation directory. Allocates
167 return value using malloc. */
169 extern char *make_relative_prefix (const char *, const char *,
170 const char *) ATTRIBUTE_MALLOC;
172 /* Choose a temporary directory to use for scratch files. */
174 extern char *choose_temp_base (void) ATTRIBUTE_MALLOC;
176 /* Return a temporary file name or NULL if unable to create one. */
178 extern char *make_temp_file (const char *) ATTRIBUTE_MALLOC;
180 /* Remove a link to a file unless it is special. */
182 extern int unlink_if_ordinary (const char *);
184 /* Allocate memory filled with spaces. Allocates using malloc. */
186 extern const char *spaces (int count);
188 /* Return the maximum error number for which strerror will return a
191 extern int errno_max (void);
193 /* Return the name of an errno value (e.g., strerrno (EINVAL) returns
196 extern const char *strerrno (int);
198 /* Given the name of an errno value, return the value. */
200 extern int strtoerrno (const char *);
202 /* ANSI's strerror(), but more robust. */
204 extern char *xstrerror (int);
206 /* Return the maximum signal number for which strsignal will return a
209 extern int signo_max (void);
211 /* Return a signal message string for a signal number
212 (e.g., strsignal (SIGHUP) returns something like "Hangup"). */
213 /* This is commented out as it can conflict with one in system headers.
214 We still document its existence though. */
216 /*extern const char *strsignal (int);*/
218 /* Return the name of a signal number (e.g., strsigno (SIGHUP) returns
221 extern const char *strsigno (int);
223 /* Given the name of a signal, return its number. */
225 extern int strtosigno (const char *);
227 /* Register a function to be run by xexit. Returns 0 on success. */
229 extern int xatexit (void (*fn) (void));
231 /* Exit, calling all the functions registered with xatexit. */
233 extern void xexit (int status) ATTRIBUTE_NORETURN;
235 /* Set the program name used by xmalloc. */
237 extern void xmalloc_set_program_name (const char *);
239 /* Report an allocation failure. */
240 extern void xmalloc_failed (size_t) ATTRIBUTE_NORETURN;
242 /* Allocate memory without fail. If malloc fails, this will print a
243 message to stderr (using the name set by xmalloc_set_program_name,
244 if any) and then call xexit. */
246 extern PTR xmalloc (size_t) ATTRIBUTE_MALLOC;
248 /* Reallocate memory without fail. This works like xmalloc. Note,
249 realloc type functions are not suitable for attribute malloc since
250 they may return the same address across multiple calls. */
252 extern PTR xrealloc (PTR, size_t);
254 /* Allocate memory without fail and set it to zero. This works like
257 extern PTR xcalloc (size_t, size_t) ATTRIBUTE_MALLOC;
259 /* Copy a string into a memory buffer without fail. */
261 extern char *xstrdup (const char *) ATTRIBUTE_MALLOC;
263 /* Copy at most N characters from string into a buffer without fail. */
265 extern char *xstrndup (const char *, size_t) ATTRIBUTE_MALLOC;
267 /* Copy an existing memory buffer to a new memory buffer without fail. */
269 extern PTR xmemdup (const PTR, size_t, size_t) ATTRIBUTE_MALLOC;
271 /* Physical memory routines. Return values are in BYTES. */
272 extern double physmem_total (void);
273 extern double physmem_available (void);
276 /* These macros provide a K&R/C89/C++-friendly way of allocating structures
277 with nice encapsulation. The XDELETE*() macros are technically
278 superfluous, but provided here for symmetry. Using them consistently
279 makes it easier to update client code to use different allocators such
280 as new/delete and new[]/delete[]. */
282 /* Scalar allocators. */
284 #define XNEW(T) ((T *) xmalloc (sizeof (T)))
285 #define XCNEW(T) ((T *) xcalloc (1, sizeof (T)))
286 #define XDELETE(P) free ((void*) (P))
288 /* Array allocators. */
290 #define XNEWVEC(T, N) ((T *) xmalloc (sizeof (T) * (N)))
291 #define XCNEWVEC(T, N) ((T *) xcalloc ((N), sizeof (T)))
292 #define XRESIZEVEC(T, P, N) ((T *) xrealloc ((void *) (P), sizeof (T) * (N)))
293 #define XDELETEVEC(P) free ((void*) (P))
295 /* Allocators for variable-sized structures and raw buffers. */
297 #define XNEWVAR(T, S) ((T *) xmalloc ((S)))
298 #define XCNEWVAR(T, S) ((T *) xcalloc (1, (S)))
299 #define XRESIZEVAR(T, P, S) ((T *) xrealloc ((P), (S)))
301 /* Type-safe obstack allocator. */
303 #define XOBNEW(O, T) ((T *) obstack_alloc ((O), sizeof (T)))
306 /* hex character manipulation routines */
308 #define _hex_array_size 256
310 extern const unsigned char _hex_value[_hex_array_size];
311 extern void hex_init (void);
312 #define hex_p(c) (hex_value (c) != _hex_bad)
313 /* If you change this, note well: Some code relies on side effects in
314 the argument being performed exactly once. */
315 #define hex_value(c) ((unsigned int) _hex_value[(unsigned char) (c)])
317 /* Definitions used by the pexecute routine. */
319 #define PEXECUTE_FIRST 1
320 #define PEXECUTE_LAST 2
321 #define PEXECUTE_ONE (PEXECUTE_FIRST + PEXECUTE_LAST)
322 #define PEXECUTE_SEARCH 4
323 #define PEXECUTE_VERBOSE 8
325 /* Execute a program. */
327 extern int pexecute (const char *, char * const *, const char *,
328 const char *, char **, char **, int);
330 /* Wait for pexecute to finish. */
332 extern int pwait (int, int *, int);
334 #if !HAVE_DECL_ASPRINTF
335 /* Like sprintf but provides a pointer to malloc'd storage, which must
336 be freed by the caller. */
338 extern int asprintf (char **, const char *, ...) ATTRIBUTE_PRINTF_2;
341 #if !HAVE_DECL_VASPRINTF
342 /* Like vsprintf but provides a pointer to malloc'd storage, which
343 must be freed by the caller. */
345 extern int vasprintf (char **, const char *, va_list)
346 ATTRIBUTE_PRINTF(2,0);
349 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
351 /* Drastically simplified alloca configurator. If we're using GCC,
352 we use __builtin_alloca; otherwise we use the C alloca. The C
353 alloca is always available. You can override GCC by defining
354 USE_C_ALLOCA yourself. The canonical autoconf macro C_ALLOCA is
355 also set/unset as it is often used to indicate whether code needs
356 to call alloca(0). */
357 extern PTR C_alloca (size_t) ATTRIBUTE_MALLOC;
359 #if GCC_VERSION >= 2000 && !defined USE_C_ALLOCA
360 # define alloca(x) __builtin_alloca(x)
362 # define ASTRDUP(X) \
363 (__extension__ ({ const char *const libiberty_optr = (X); \
364 const unsigned long libiberty_len = strlen (libiberty_optr) + 1; \
365 char *const libiberty_nptr = (char *const) alloca (libiberty_len); \
366 (char *) memcpy (libiberty_nptr, libiberty_optr, libiberty_len); }))
368 # define alloca(x) C_alloca(x)
370 # define USE_C_ALLOCA 1
373 extern const char *libiberty_optr;
374 extern char *libiberty_nptr;
375 extern unsigned long libiberty_len;
376 # define ASTRDUP(X) \
377 (libiberty_optr = (X), \
378 libiberty_len = strlen (libiberty_optr) + 1, \
379 libiberty_nptr = (char *) alloca (libiberty_len), \
380 (char *) memcpy (libiberty_nptr, libiberty_optr, libiberty_len))
388 #endif /* ! defined (LIBIBERTY_H) */