]> git.ipfire.org Git - thirdparty/gcc.git/blame - include/libiberty.h
libiberty.h (xstrndup): Declare.
[thirdparty/gcc.git] / include / libiberty.h
CommitLineData
6599da04 1/* Function declarations for libiberty.
01f537ab 2
b05c4e59 3 Copyright 2001, 2002 Free Software Foundation, Inc.
01f537ab
NC
4
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
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
6599da04
JM
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
5a4917e5
JL
38#ifdef __cplusplus
39extern "C" {
40#endif
41
6599da04
JM
42#include "ansidecl.h"
43
d1209685
ZW
44#ifdef ANSI_PROTOTYPES
45/* Get a definition for size_t. */
46#include <stddef.h>
47/* Get a definition for va_list. */
48#include <stdarg.h>
49#endif
50
6599da04
JM
51/* Build an argument vector from a string. Allocates memory using
52 malloc. Use freeargv to free the vector. */
53
0be6abca 54extern char **buildargv PARAMS ((const char *)) ATTRIBUTE_MALLOC;
6599da04
JM
55
56/* Free a vector returned by buildargv. */
57
58extern void freeargv PARAMS ((char **));
59
5a4917e5
JL
60/* Duplicate an argument vector. Allocates memory using malloc. Use
61 freeargv to free the vector. */
62
d9465687 63extern char **dupargv PARAMS ((char **)) ATTRIBUTE_MALLOC;
5a4917e5
JL
64
65
6599da04
JM
66/* Return the last component of a path name. Note that we can't use a
67 prototype here because the parameter is declared inconsistently
68 across different systems, sometimes as "char *" and sometimes as
69 "const char *" */
70
f31e826b
KG
71/* HAVE_DECL_* is a three-state macro: undefined, 0 or 1. If it is
72 undefined, we haven't run the autoconf check so provide the
73 declaration without arguments. If it is 0, we checked and failed
74 to find the declaration so provide a fully prototyped one. If it
75 is 1, we found it so don't provide any declaration at all. */
66443ad2 76#if !HAVE_DECL_BASENAME
ff06bf4b 77#if defined (__GNU_LIBRARY__ ) || defined (__linux__) || defined (__FreeBSD__) || defined (__OpenBSD__) || defined(__NetBSD__) || defined (__CYGWIN__) || defined (__CYGWIN32__) || defined (__MINGW32__) || defined (HAVE_DECL_BASENAME)
6599da04
JM
78extern char *basename PARAMS ((const char *));
79#else
80extern char *basename ();
66443ad2 81#endif
6599da04
JM
82#endif
83
2b757d51
NB
84/* A well-defined basename () that is always compiled in. */
85
0ef5547d 86extern const char *lbasename PARAMS ((const char *));
2b757d51 87
378ca31e
DJ
88/* A well-defined realpath () that is always compiled in. */
89
90extern char *lrealpath PARAMS ((const char *));
91
69ee340e
KG
92/* Concatenate an arbitrary number of strings. You must pass NULL as
93 the last argument of this function, to terminate the list of
94 strings. Allocates memory using xmalloc. */
6599da04 95
3d091dac 96extern char *concat PARAMS ((const char *, ...)) ATTRIBUTE_MALLOC ATTRIBUTE_SENTINEL;
6599da04 97
ad43d46f
KG
98/* Concatenate an arbitrary number of strings. You must pass NULL as
99 the last argument of this function, to terminate the list of
100 strings. Allocates memory using xmalloc. The first argument is
101 not one of the strings to be concatenated, but if not NULL is a
102 pointer to be freed after the new string is created, similar to the
103 way xrealloc works. */
104
3d091dac 105extern char *reconcat PARAMS ((char *, const char *, ...)) ATTRIBUTE_MALLOC ATTRIBUTE_SENTINEL;
ad43d46f 106
c793eea7 107/* Determine the length of concatenating an arbitrary number of
69ee340e
KG
108 strings. You must pass NULL as the last argument of this function,
109 to terminate the list of strings. */
c793eea7 110
3d091dac 111extern unsigned long concat_length PARAMS ((const char *, ...)) ATTRIBUTE_SENTINEL;
c793eea7
KG
112
113/* Concatenate an arbitrary number of strings into a SUPPLIED area of
69ee340e
KG
114 memory. You must pass NULL as the last argument of this function,
115 to terminate the list of strings. The supplied memory is assumed
116 to be large enough. */
c793eea7 117
3d091dac 118extern char *concat_copy PARAMS ((char *, const char *, ...)) ATTRIBUTE_SENTINEL;
c793eea7
KG
119
120/* Concatenate an arbitrary number of strings into a GLOBAL area of
69ee340e
KG
121 memory. You must pass NULL as the last argument of this function,
122 to terminate the list of strings. The supplied memory is assumed
123 to be large enough. */
c793eea7 124
3d091dac 125extern char *concat_copy2 PARAMS ((const char *, ...)) ATTRIBUTE_SENTINEL;
c793eea7
KG
126
127/* This is the global area used by concat_copy2. */
128
129extern char *libiberty_concat_ptr;
130
69ee340e
KG
131/* Concatenate an arbitrary number of strings. You must pass NULL as
132 the last argument of this function, to terminate the list of
133 strings. Allocates memory using alloca. The arguments are
134 evaluated twice! */
c793eea7
KG
135#define ACONCAT(ACONCAT_PARAMS) \
136 (libiberty_concat_ptr = alloca (concat_length ACONCAT_PARAMS + 1), \
137 concat_copy2 ACONCAT_PARAMS)
138
6599da04
JM
139/* Check whether two file descriptors refer to the same file. */
140
141extern int fdmatch PARAMS ((int fd1, int fd2));
142
25c29e1e
KG
143/* Get the working directory. The result is cached, so don't call
144 chdir() between calls to getpwd(). */
145
146extern char * getpwd PARAMS ((void));
147
17049f0b
MM
148/* Get the current time. */
149/* Prototypes vary from system to system, so we only provide a
150 prototype on systems where we know that we need it. */
151#ifdef __MINGW32__
152/* Forward declaration to avoid #include <sys/time.h>. */
153struct timeval;
154extern int gettimeofday PARAMS ((struct timeval *, void *));
155#endif
156
6599da04
JM
157/* Get the amount of time the process has run, in microseconds. */
158
159extern long get_run_time PARAMS ((void));
160
42766f8d
DJ
161/* Generate a relocated path to some installation directory. Allocates
162 return value using malloc. */
163
164extern char *make_relative_prefix PARAMS ((const char *, const char *,
65ec6c0e 165 const char *)) ATTRIBUTE_MALLOC;
42766f8d 166
6599da04
JM
167/* Choose a temporary directory to use for scratch files. */
168
d9465687 169extern char *choose_temp_base PARAMS ((void)) ATTRIBUTE_MALLOC;
6599da04 170
5a657fc3
KG
171/* Return a temporary file name or NULL if unable to create one. */
172
d9465687 173extern char *make_temp_file PARAMS ((const char *)) ATTRIBUTE_MALLOC;
5a657fc3 174
c363985d
JB
175/* Remove a link to a file unless it is special. */
176
177extern int unlink_if_ordinary PARAMS((const char *));
178
6599da04
JM
179/* Allocate memory filled with spaces. Allocates using malloc. */
180
181extern const char *spaces PARAMS ((int count));
182
183/* Return the maximum error number for which strerror will return a
184 string. */
185
186extern int errno_max PARAMS ((void));
187
188/* Return the name of an errno value (e.g., strerrno (EINVAL) returns
189 "EINVAL"). */
190
191extern const char *strerrno PARAMS ((int));
192
193/* Given the name of an errno value, return the value. */
194
195extern int strtoerrno PARAMS ((const char *));
196
197/* ANSI's strerror(), but more robust. */
198
199extern char *xstrerror PARAMS ((int));
200
201/* Return the maximum signal number for which strsignal will return a
202 string. */
203
204extern int signo_max PARAMS ((void));
205
206/* Return a signal message string for a signal number
207 (e.g., strsignal (SIGHUP) returns something like "Hangup"). */
208/* This is commented out as it can conflict with one in system headers.
209 We still document its existence though. */
210
211/*extern const char *strsignal PARAMS ((int));*/
212
213/* Return the name of a signal number (e.g., strsigno (SIGHUP) returns
214 "SIGHUP"). */
215
216extern const char *strsigno PARAMS ((int));
217
218/* Given the name of a signal, return its number. */
219
220extern int strtosigno PARAMS ((const char *));
221
222/* Register a function to be run by xexit. Returns 0 on success. */
223
224extern int xatexit PARAMS ((void (*fn) (void)));
225
226/* Exit, calling all the functions registered with xatexit. */
227
d9465687 228extern void xexit PARAMS ((int status)) ATTRIBUTE_NORETURN;
6599da04
JM
229
230/* Set the program name used by xmalloc. */
231
232extern void xmalloc_set_program_name PARAMS ((const char *));
233
d1209685
ZW
234/* Report an allocation failure. */
235extern void xmalloc_failed PARAMS ((size_t)) ATTRIBUTE_NORETURN;
236
6599da04
JM
237/* Allocate memory without fail. If malloc fails, this will print a
238 message to stderr (using the name set by xmalloc_set_program_name,
239 if any) and then call xexit. */
240
d9465687 241extern PTR xmalloc PARAMS ((size_t)) ATTRIBUTE_MALLOC;
6599da04 242
d9465687
KG
243/* Reallocate memory without fail. This works like xmalloc. Note,
244 realloc type functions are not suitable for attribute malloc since
245 they may return the same address across multiple calls. */
6599da04
JM
246
247extern PTR xrealloc PARAMS ((PTR, size_t));
248
67d0f6ab
KG
249/* Allocate memory without fail and set it to zero. This works like
250 xmalloc. */
251
d9465687 252extern PTR xcalloc PARAMS ((size_t, size_t)) ATTRIBUTE_MALLOC;
67d0f6ab 253
6599da04
JM
254/* Copy a string into a memory buffer without fail. */
255
d9465687 256extern char *xstrdup PARAMS ((const char *)) ATTRIBUTE_MALLOC;
6599da04 257
17998b22
KG
258/* Copy at most N characters from string into a buffer without fail. */
259
260extern char *xstrndup PARAMS ((const char *, size_t)) ATTRIBUTE_MALLOC;
261
66599806
JG
262/* Copy an existing memory buffer to a new memory buffer without fail. */
263
d9465687 264extern PTR xmemdup PARAMS ((const PTR, size_t, size_t)) ATTRIBUTE_MALLOC;
66599806 265
9308be90 266/* Physical memory routines. Return values are in BYTES. */
a354191e
KG
267extern double physmem_total PARAMS ((void));
268extern double physmem_available PARAMS ((void));
269
b50a5a95
BI
270
271/* These macros provide a K&R/C89/C++-friendly way of allocating structures
272 with nice encapsulation. The XDELETE*() macros are technically
273 superfluous, but provided here for symmetry. Using them consistently
274 makes it easier to update client code to use different allocators such
275 as new/delete and new[]/delete[]. */
276
277/* Scalar allocators. */
278
279#define XNEW(T) ((T *) xmalloc (sizeof (T)))
280#define XCNEW(T) ((T *) xcalloc (1, sizeof (T)))
b1e8c0fd 281#define XDELETE(P) free ((void*) (P))
b50a5a95
BI
282
283/* Array allocators. */
284
285#define XNEWVEC(T, N) ((T *) xmalloc (sizeof (T) * (N)))
286#define XCNEWVEC(T, N) ((T *) xcalloc ((N), sizeof (T)))
b1e8c0fd
GDR
287#define XRESIZEVEC(T, P, N) ((T *) xrealloc ((void *) (P), sizeof (T) * (N)))
288#define XDELETEVEC(P) free ((void*) (P))
b50a5a95
BI
289
290/* Allocators for variable-sized structures and raw buffers. */
291
292#define XNEWVAR(T, S) ((T *) xmalloc ((S)))
293#define XCNEWVAR(T, S) ((T *) xcalloc (1, (S)))
294#define XRESIZEVAR(T, P, S) ((T *) xrealloc ((P), (S)))
295
296/* Type-safe obstack allocator. */
297
298#define XOBNEW(O, T) ((T *) obstack_alloc ((O), sizeof (T)))
299
300
6599da04
JM
301/* hex character manipulation routines */
302
303#define _hex_array_size 256
304#define _hex_bad 99
49a19cfd 305extern const unsigned char _hex_value[_hex_array_size];
e5969ba6 306extern void hex_init PARAMS ((void));
6599da04
JM
307#define hex_p(c) (hex_value (c) != _hex_bad)
308/* If you change this, note well: Some code relies on side effects in
309 the argument being performed exactly once. */
49a19cfd 310#define hex_value(c) ((unsigned int) _hex_value[(unsigned char) (c)])
6599da04
JM
311
312/* Definitions used by the pexecute routine. */
313
314#define PEXECUTE_FIRST 1
315#define PEXECUTE_LAST 2
316#define PEXECUTE_ONE (PEXECUTE_FIRST + PEXECUTE_LAST)
317#define PEXECUTE_SEARCH 4
318#define PEXECUTE_VERBOSE 8
319
320/* Execute a program. */
321
322extern int pexecute PARAMS ((const char *, char * const *, const char *,
323 const char *, char **, char **, int));
324
325/* Wait for pexecute to finish. */
326
327extern int pwait PARAMS ((int, int *, int));
328
e2bb0cb4 329#if !HAVE_DECL_ASPRINTF
146e60a0
KG
330/* Like sprintf but provides a pointer to malloc'd storage, which must
331 be freed by the caller. */
332
333extern int asprintf PARAMS ((char **, const char *, ...)) ATTRIBUTE_PRINTF_2;
e2bb0cb4 334#endif
146e60a0 335
e2bb0cb4 336#if !HAVE_DECL_VASPRINTF
146e60a0
KG
337/* Like vsprintf but provides a pointer to malloc'd storage, which
338 must be freed by the caller. */
339
340extern int vasprintf PARAMS ((char **, const char *, va_list))
341 ATTRIBUTE_PRINTF(2,0);
e2bb0cb4 342#endif
146e60a0 343
672a59e0
GM
344#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
345
b548dffb
ZW
346/* Drastically simplified alloca configurator. If we're using GCC,
347 we use __builtin_alloca; otherwise we use the C alloca. The C
348 alloca is always available. You can override GCC by defining
cc56c744
KG
349 USE_C_ALLOCA yourself. The canonical autoconf macro C_ALLOCA is
350 also set/unset as it is often used to indicate whether code needs
351 to call alloca(0). */
b05c4e59 352extern PTR C_alloca PARAMS ((size_t)) ATTRIBUTE_MALLOC;
b548dffb
ZW
353#undef alloca
354#if GCC_VERSION >= 2000 && !defined USE_C_ALLOCA
355# define alloca(x) __builtin_alloca(x)
cc56c744 356# undef C_ALLOCA
c1d49704
KG
357# define ASTRDUP(X) \
358 (__extension__ ({ const char *const libiberty_optr = (X); \
359 const unsigned long libiberty_len = strlen (libiberty_optr) + 1; \
5557e16d 360 char *const libiberty_nptr = (char *const) alloca (libiberty_len); \
c1d49704 361 (char *) memcpy (libiberty_nptr, libiberty_optr, libiberty_len); }))
b548dffb
ZW
362#else
363# define alloca(x) C_alloca(x)
364# undef USE_C_ALLOCA
365# define USE_C_ALLOCA 1
cc56c744
KG
366# undef C_ALLOCA
367# define C_ALLOCA 1
c1d49704
KG
368extern const char *libiberty_optr;
369extern char *libiberty_nptr;
370extern unsigned long libiberty_len;
371# define ASTRDUP(X) \
372 (libiberty_optr = (X), \
373 libiberty_len = strlen (libiberty_optr) + 1, \
5557e16d 374 libiberty_nptr = (char *) alloca (libiberty_len), \
c1d49704 375 (char *) memcpy (libiberty_nptr, libiberty_optr, libiberty_len))
b548dffb
ZW
376#endif
377
5a4917e5
JL
378#ifdef __cplusplus
379}
380#endif
381
382
6599da04 383#endif /* ! defined (LIBIBERTY_H) */