* libiberty.h (ffs): Declare, if necessary.
[deliverable/binutils-gdb.git] / include / libiberty.h
CommitLineData
252b5132 1/* Function declarations for libiberty.
4f1d9bd8 2
1d619e59 3 Copyright 2001, 2002 Free Software Foundation, Inc.
4f1d9bd8 4
526c3e12
NC
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.
11
4f1d9bd8
NC
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)
15 any later version.
16
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.
21
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.
26
252b5132
RH
27 Written by Cygnus Support, 1994.
28
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. */
34
35#ifndef LIBIBERTY_H
36#define LIBIBERTY_H
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42#include "ansidecl.h"
43
b13291a9
DD
44/* Get a definition for size_t. */
45#include <stddef.h>
46/* Get a definition for va_list. */
47#include <stdarg.h>
b13291a9 48
252b5132
RH
49/* Build an argument vector from a string. Allocates memory using
50 malloc. Use freeargv to free the vector. */
51
9334f9c6 52extern char **buildargv (const char *) ATTRIBUTE_MALLOC;
252b5132
RH
53
54/* Free a vector returned by buildargv. */
55
9334f9c6 56extern void freeargv (char **);
252b5132
RH
57
58/* Duplicate an argument vector. Allocates memory using malloc. Use
59 freeargv to free the vector. */
60
9334f9c6 61extern char **dupargv (char **) ATTRIBUTE_MALLOC;
252b5132
RH
62
63
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
67 "const char *" */
68
79c6de76
L
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. */
931f285f 74#if !HAVE_DECL_BASENAME
79fadcb0 75#if defined (__GNU_LIBRARY__ ) || defined (__linux__) || defined (__FreeBSD__) || defined (__OpenBSD__) || defined(__NetBSD__) || defined (__CYGWIN__) || defined (__CYGWIN32__) || defined (__MINGW32__) || defined (HAVE_DECL_BASENAME)
9334f9c6 76extern char *basename (const char *);
252b5132
RH
77#else
78extern char *basename ();
931f285f 79#endif
252b5132
RH
80#endif
81
8aa30e60
DD
82/* A well-defined basename () that is always compiled in. */
83
9334f9c6 84extern const char *lbasename (const char *);
8aa30e60 85
e2803db9
DJ
86/* A well-defined realpath () that is always compiled in. */
87
9334f9c6 88extern char *lrealpath (const char *);
e2803db9 89
38bfaea8
DD
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. */
252b5132 93
9334f9c6 94extern char *concat (const char *, ...) ATTRIBUTE_MALLOC ATTRIBUTE_SENTINEL;
252b5132 95
99ee3a8f
DD
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. */
102
9334f9c6 103extern char *reconcat (char *, const char *, ...) ATTRIBUTE_MALLOC ATTRIBUTE_SENTINEL;
99ee3a8f 104
54c20242 105/* Determine the length of concatenating an arbitrary number of
38bfaea8
DD
106 strings. You must pass NULL as the last argument of this function,
107 to terminate the list of strings. */
54c20242 108
9334f9c6 109extern unsigned long concat_length (const char *, ...) ATTRIBUTE_SENTINEL;
54c20242
DD
110
111/* Concatenate an arbitrary number of strings into a SUPPLIED area of
38bfaea8
DD
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. */
54c20242 115
9334f9c6 116extern char *concat_copy (char *, const char *, ...) ATTRIBUTE_SENTINEL;
54c20242
DD
117
118/* Concatenate an arbitrary number of strings into a GLOBAL area of
38bfaea8
DD
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. */
54c20242 122
9334f9c6 123extern char *concat_copy2 (const char *, ...) ATTRIBUTE_SENTINEL;
54c20242
DD
124
125/* This is the global area used by concat_copy2. */
126
127extern char *libiberty_concat_ptr;
128
38bfaea8
DD
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
132 evaluated twice! */
54c20242
DD
133#define ACONCAT(ACONCAT_PARAMS) \
134 (libiberty_concat_ptr = alloca (concat_length ACONCAT_PARAMS + 1), \
135 concat_copy2 ACONCAT_PARAMS)
136
252b5132
RH
137/* Check whether two file descriptors refer to the same file. */
138
9334f9c6 139extern int fdmatch (int fd1, int fd2);
252b5132 140
3d0dfe26
MM
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
145extern int ffs(int);
146#endif
147
cc89ffe6
ILT
148/* Get the working directory. The result is cached, so don't call
149 chdir() between calls to getpwd(). */
150
9334f9c6 151extern char * getpwd (void);
cc89ffe6 152
8ec32723
DD
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. */
156#ifdef __MINGW32__
157/* Forward declaration to avoid #include <sys/time.h>. */
158struct timeval;
9334f9c6 159extern int gettimeofday (struct timeval *, void *);
8ec32723
DD
160#endif
161
252b5132
RH
162/* Get the amount of time the process has run, in microseconds. */
163
9334f9c6 164extern long get_run_time (void);
252b5132 165
2a80c0a4
DD
166/* Generate a relocated path to some installation directory. Allocates
167 return value using malloc. */
168
9334f9c6
DD
169extern char *make_relative_prefix (const char *, const char *,
170 const char *) ATTRIBUTE_MALLOC;
2a80c0a4 171
252b5132
RH
172/* Choose a temporary directory to use for scratch files. */
173
9334f9c6 174extern char *choose_temp_base (void) ATTRIBUTE_MALLOC;
cc89ffe6
ILT
175
176/* Return a temporary file name or NULL if unable to create one. */
177
9334f9c6 178extern char *make_temp_file (const char *) ATTRIBUTE_MALLOC;
252b5132 179
190eb137
DD
180/* Remove a link to a file unless it is special. */
181
9334f9c6 182extern int unlink_if_ordinary (const char *);
190eb137 183
252b5132
RH
184/* Allocate memory filled with spaces. Allocates using malloc. */
185
9334f9c6 186extern const char *spaces (int count);
252b5132
RH
187
188/* Return the maximum error number for which strerror will return a
189 string. */
190
9334f9c6 191extern int errno_max (void);
252b5132
RH
192
193/* Return the name of an errno value (e.g., strerrno (EINVAL) returns
194 "EINVAL"). */
195
9334f9c6 196extern const char *strerrno (int);
252b5132
RH
197
198/* Given the name of an errno value, return the value. */
199
9334f9c6 200extern int strtoerrno (const char *);
252b5132
RH
201
202/* ANSI's strerror(), but more robust. */
203
9334f9c6 204extern char *xstrerror (int);
252b5132
RH
205
206/* Return the maximum signal number for which strsignal will return a
207 string. */
208
9334f9c6 209extern int signo_max (void);
252b5132
RH
210
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. */
215
9334f9c6 216/*extern const char *strsignal (int);*/
252b5132
RH
217
218/* Return the name of a signal number (e.g., strsigno (SIGHUP) returns
219 "SIGHUP"). */
220
9334f9c6 221extern const char *strsigno (int);
252b5132
RH
222
223/* Given the name of a signal, return its number. */
224
9334f9c6 225extern int strtosigno (const char *);
252b5132
RH
226
227/* Register a function to be run by xexit. Returns 0 on success. */
228
9334f9c6 229extern int xatexit (void (*fn) (void));
252b5132
RH
230
231/* Exit, calling all the functions registered with xatexit. */
232
9334f9c6 233extern void xexit (int status) ATTRIBUTE_NORETURN;
252b5132
RH
234
235/* Set the program name used by xmalloc. */
236
9334f9c6 237extern void xmalloc_set_program_name (const char *);
252b5132 238
b13291a9 239/* Report an allocation failure. */
9334f9c6 240extern void xmalloc_failed (size_t) ATTRIBUTE_NORETURN;
b13291a9 241
252b5132
RH
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. */
245
9334f9c6 246extern PTR xmalloc (size_t) ATTRIBUTE_MALLOC;
252b5132 247
cc89ffe6
ILT
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. */
252b5132 251
9334f9c6 252extern PTR xrealloc (PTR, size_t);
252b5132
RH
253
254/* Allocate memory without fail and set it to zero. This works like
255 xmalloc. */
256
9334f9c6 257extern PTR xcalloc (size_t, size_t) ATTRIBUTE_MALLOC;
252b5132
RH
258
259/* Copy a string into a memory buffer without fail. */
260
9334f9c6 261extern char *xstrdup (const char *) ATTRIBUTE_MALLOC;
cc89ffe6 262
0fad4bdb
DD
263/* Copy at most N characters from string into a buffer without fail. */
264
9334f9c6 265extern char *xstrndup (const char *, size_t) ATTRIBUTE_MALLOC;
0fad4bdb 266
cc89ffe6
ILT
267/* Copy an existing memory buffer to a new memory buffer without fail. */
268
9334f9c6 269extern PTR xmemdup (const PTR, size_t, size_t) ATTRIBUTE_MALLOC;
252b5132 270
638ceb1a 271/* Physical memory routines. Return values are in BYTES. */
9334f9c6
DD
272extern double physmem_total (void);
273extern double physmem_available (void);
4938384a 274
d5b4094f
DD
275
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[]. */
281
282/* Scalar allocators. */
283
284#define XNEW(T) ((T *) xmalloc (sizeof (T)))
285#define XCNEW(T) ((T *) xcalloc (1, sizeof (T)))
deaa6723 286#define XDELETE(P) free ((void*) (P))
d5b4094f
DD
287
288/* Array allocators. */
289
290#define XNEWVEC(T, N) ((T *) xmalloc (sizeof (T) * (N)))
291#define XCNEWVEC(T, N) ((T *) xcalloc ((N), sizeof (T)))
deaa6723
DD
292#define XRESIZEVEC(T, P, N) ((T *) xrealloc ((void *) (P), sizeof (T) * (N)))
293#define XDELETEVEC(P) free ((void*) (P))
d5b4094f
DD
294
295/* Allocators for variable-sized structures and raw buffers. */
296
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)))
300
301/* Type-safe obstack allocator. */
302
303#define XOBNEW(O, T) ((T *) obstack_alloc ((O), sizeof (T)))
304
305
252b5132
RH
306/* hex character manipulation routines */
307
308#define _hex_array_size 256
309#define _hex_bad 99
e4f79046 310extern const unsigned char _hex_value[_hex_array_size];
9334f9c6 311extern void hex_init (void);
252b5132
RH
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. */
e4f79046 315#define hex_value(c) ((unsigned int) _hex_value[(unsigned char) (c)])
252b5132
RH
316
317/* Definitions used by the pexecute routine. */
318
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
324
325/* Execute a program. */
326
9334f9c6
DD
327extern int pexecute (const char *, char * const *, const char *,
328 const char *, char **, char **, int);
252b5132
RH
329
330/* Wait for pexecute to finish. */
331
9334f9c6 332extern int pwait (int, int *, int);
252b5132 333
bb7eb039 334#if !HAVE_DECL_ASPRINTF
cc89ffe6
ILT
335/* Like sprintf but provides a pointer to malloc'd storage, which must
336 be freed by the caller. */
337
9334f9c6 338extern int asprintf (char **, const char *, ...) ATTRIBUTE_PRINTF_2;
bb7eb039 339#endif
cc89ffe6 340
bb7eb039 341#if !HAVE_DECL_VASPRINTF
cc89ffe6
ILT
342/* Like vsprintf but provides a pointer to malloc'd storage, which
343 must be freed by the caller. */
344
9334f9c6 345extern int vasprintf (char **, const char *, va_list)
cc89ffe6 346 ATTRIBUTE_PRINTF(2,0);
bb7eb039 347#endif
cc89ffe6 348
b18903cb
NC
349#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
350
30673bf5
DD
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
129e8d96
DD
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). */
9334f9c6 357extern PTR C_alloca (size_t) ATTRIBUTE_MALLOC;
30673bf5
DD
358#undef alloca
359#if GCC_VERSION >= 2000 && !defined USE_C_ALLOCA
360# define alloca(x) __builtin_alloca(x)
129e8d96 361# undef C_ALLOCA
54c20242
DD
362# define ASTRDUP(X) \
363 (__extension__ ({ const char *const libiberty_optr = (X); \
364 const unsigned long libiberty_len = strlen (libiberty_optr) + 1; \
7ab9a76e 365 char *const libiberty_nptr = (char *const) alloca (libiberty_len); \
54c20242 366 (char *) memcpy (libiberty_nptr, libiberty_optr, libiberty_len); }))
30673bf5
DD
367#else
368# define alloca(x) C_alloca(x)
369# undef USE_C_ALLOCA
370# define USE_C_ALLOCA 1
129e8d96
DD
371# undef C_ALLOCA
372# define C_ALLOCA 1
54c20242
DD
373extern const char *libiberty_optr;
374extern char *libiberty_nptr;
375extern unsigned long libiberty_len;
376# define ASTRDUP(X) \
377 (libiberty_optr = (X), \
378 libiberty_len = strlen (libiberty_optr) + 1, \
7ab9a76e 379 libiberty_nptr = (char *) alloca (libiberty_len), \
54c20242 380 (char *) memcpy (libiberty_nptr, libiberty_optr, libiberty_len))
30673bf5
DD
381#endif
382
252b5132
RH
383#ifdef __cplusplus
384}
385#endif
386
387
388#endif /* ! defined (LIBIBERTY_H) */
This page took 0.301482 seconds and 4 git commands to generate.