1e4b17023SJohn Marino /* Function declarations for libiberty.
2e4b17023SJohn Marino 
3e4b17023SJohn Marino    Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4e4b17023SJohn Marino    2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5e4b17023SJohn Marino 
6e4b17023SJohn Marino    Note - certain prototypes declared in this header file are for
7e4b17023SJohn Marino    functions whoes implementation copyright does not belong to the
8e4b17023SJohn Marino    FSF.  Those prototypes are present in this file for reference
9e4b17023SJohn Marino    purposes only and their presence in this file should not construed
10e4b17023SJohn Marino    as an indication of ownership by the FSF of the implementation of
11e4b17023SJohn Marino    those functions in any way or form whatsoever.
12e4b17023SJohn Marino 
13e4b17023SJohn Marino    This program is free software; you can redistribute it and/or modify
14e4b17023SJohn Marino    it under the terms of the GNU General Public License as published by
15e4b17023SJohn Marino    the Free Software Foundation; either version 2, or (at your option)
16e4b17023SJohn Marino    any later version.
17e4b17023SJohn Marino 
18e4b17023SJohn Marino    This program is distributed in the hope that it will be useful,
19e4b17023SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
20e4b17023SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21e4b17023SJohn Marino    GNU General Public License for more details.
22e4b17023SJohn Marino 
23e4b17023SJohn Marino    You should have received a copy of the GNU General Public License
24e4b17023SJohn Marino    along with this program; if not, write to the Free Software
25e4b17023SJohn Marino    Foundation, Inc., 51 Franklin Street - Fifth Floor,
26e4b17023SJohn Marino    Boston, MA 02110-1301, USA.
27e4b17023SJohn Marino 
28e4b17023SJohn Marino    Written by Cygnus Support, 1994.
29e4b17023SJohn Marino 
30e4b17023SJohn Marino    The libiberty library provides a number of functions which are
31e4b17023SJohn Marino    missing on some operating systems.  We do not declare those here,
32e4b17023SJohn Marino    to avoid conflicts with the system header files on operating
33e4b17023SJohn Marino    systems that do support those functions.  In this file we only
34e4b17023SJohn Marino    declare those functions which are specific to libiberty.  */
35e4b17023SJohn Marino 
36e4b17023SJohn Marino #ifndef LIBIBERTY_H
37e4b17023SJohn Marino #define LIBIBERTY_H
38e4b17023SJohn Marino 
39e4b17023SJohn Marino #ifdef __cplusplus
40e4b17023SJohn Marino extern "C" {
41e4b17023SJohn Marino #endif
42e4b17023SJohn Marino 
43e4b17023SJohn Marino #include "ansidecl.h"
44e4b17023SJohn Marino 
45e4b17023SJohn Marino /* Get a definition for size_t.  */
46e4b17023SJohn Marino #include <stddef.h>
47e4b17023SJohn Marino /* Get a definition for va_list.  */
48e4b17023SJohn Marino #include <stdarg.h>
49e4b17023SJohn Marino 
50e4b17023SJohn Marino #include <stdio.h>
51e4b17023SJohn Marino 
52e4b17023SJohn Marino /* If the OS supports it, ensure that the supplied stream is setup to
53e4b17023SJohn Marino    avoid any multi-threaded locking.  Otherwise leave the FILE pointer
54e4b17023SJohn Marino    unchanged.  If the stream is NULL do nothing.  */
55e4b17023SJohn Marino 
56e4b17023SJohn Marino extern void unlock_stream (FILE *);
57e4b17023SJohn Marino 
58e4b17023SJohn Marino /* If the OS supports it, ensure that the standard I/O streams, stdin,
59e4b17023SJohn Marino    stdout and stderr are setup to avoid any multi-threaded locking.
60e4b17023SJohn Marino    Otherwise do nothing.  */
61e4b17023SJohn Marino 
62e4b17023SJohn Marino extern void unlock_std_streams (void);
63e4b17023SJohn Marino 
64e4b17023SJohn Marino /* Open and return a FILE pointer.  If the OS supports it, ensure that
65e4b17023SJohn Marino    the stream is setup to avoid any multi-threaded locking.  Otherwise
66e4b17023SJohn Marino    return the FILE pointer unchanged.  */
67e4b17023SJohn Marino 
68e4b17023SJohn Marino extern FILE *fopen_unlocked (const char *, const char *);
69e4b17023SJohn Marino extern FILE *fdopen_unlocked (int, const char *);
70e4b17023SJohn Marino extern FILE *freopen_unlocked (const char *, const char *, FILE *);
71e4b17023SJohn Marino 
72e4b17023SJohn Marino /* Build an argument vector from a string.  Allocates memory using
73e4b17023SJohn Marino    malloc.  Use freeargv to free the vector.  */
74e4b17023SJohn Marino 
75e4b17023SJohn Marino extern char **buildargv (const char *) ATTRIBUTE_MALLOC;
76e4b17023SJohn Marino 
77e4b17023SJohn Marino /* Free a vector returned by buildargv.  */
78e4b17023SJohn Marino 
79e4b17023SJohn Marino extern void freeargv (char **);
80e4b17023SJohn Marino 
81e4b17023SJohn Marino /* Duplicate an argument vector. Allocates memory using malloc.  Use
82e4b17023SJohn Marino    freeargv to free the vector.  */
83e4b17023SJohn Marino 
84e4b17023SJohn Marino extern char **dupargv (char **) ATTRIBUTE_MALLOC;
85e4b17023SJohn Marino 
86e4b17023SJohn Marino /* Expand "@file" arguments in argv.  */
87e4b17023SJohn Marino 
88e4b17023SJohn Marino extern void expandargv PARAMS ((int *, char ***));
89e4b17023SJohn Marino 
90e4b17023SJohn Marino /* Write argv to an @-file, inserting necessary quoting.  */
91e4b17023SJohn Marino 
92e4b17023SJohn Marino extern int writeargv PARAMS ((char **, FILE *));
93e4b17023SJohn Marino 
94e4b17023SJohn Marino /* Return the number of elements in argv.  */
95e4b17023SJohn Marino 
96e4b17023SJohn Marino extern int countargv (char**);
97e4b17023SJohn Marino 
98e4b17023SJohn Marino /* Return the last component of a path name.  Note that we can't use a
99e4b17023SJohn Marino    prototype here because the parameter is declared inconsistently
100e4b17023SJohn Marino    across different systems, sometimes as "char *" and sometimes as
101e4b17023SJohn Marino    "const char *" */
102e4b17023SJohn Marino 
103e4b17023SJohn Marino /* HAVE_DECL_* is a three-state macro: undefined, 0 or 1.  If it is
104e4b17023SJohn Marino    undefined, we haven't run the autoconf check so provide the
105e4b17023SJohn Marino    declaration without arguments.  If it is 0, we checked and failed
106e4b17023SJohn Marino    to find the declaration so provide a fully prototyped one.  If it
107e4b17023SJohn Marino    is 1, we found it so don't provide any declaration at all.  */
108e4b17023SJohn Marino #if !HAVE_DECL_BASENAME
109*fdc4107cSJohn Marino #if defined (__GNU_LIBRARY__ ) \
110*fdc4107cSJohn Marino  || defined (__linux__) \
111*fdc4107cSJohn Marino  || defined (__DragonFly__) \
112*fdc4107cSJohn Marino  || defined (__FreeBSD__) \
113*fdc4107cSJohn Marino  || defined (__OpenBSD__) \
114*fdc4107cSJohn Marino  || defined (__NetBSD__) \
115*fdc4107cSJohn Marino  || defined (__CYGWIN__) \
116*fdc4107cSJohn Marino  || defined (__CYGWIN32__) \
117*fdc4107cSJohn Marino  || defined (__MINGW32__) \
118*fdc4107cSJohn Marino  || defined (HAVE_DECL_BASENAME)
119e4b17023SJohn Marino extern char *basename (const char *);
120e4b17023SJohn Marino #else
121e4b17023SJohn Marino /* Do not allow basename to be used if there is no prototype seen.  We
122e4b17023SJohn Marino    either need to use the above prototype or have one from
123e4b17023SJohn Marino    autoconf which would result in HAVE_DECL_BASENAME being set.  */
124e4b17023SJohn Marino #define basename basename_cannot_be_used_without_a_prototype
125e4b17023SJohn Marino #endif
126e4b17023SJohn Marino #endif
127e4b17023SJohn Marino 
128e4b17023SJohn Marino /* A well-defined basename () that is always compiled in.  */
129e4b17023SJohn Marino 
130e4b17023SJohn Marino extern const char *lbasename (const char *);
131e4b17023SJohn Marino 
132e4b17023SJohn Marino /* Same, but assumes DOS semantics (drive name, backslash is also a
133e4b17023SJohn Marino    dir separator) regardless of host.  */
134e4b17023SJohn Marino 
135e4b17023SJohn Marino extern const char *dos_lbasename (const char *);
136e4b17023SJohn Marino 
137e4b17023SJohn Marino /* Same, but assumes Unix semantics (absolute paths always start with
138e4b17023SJohn Marino    a slash, only forward slash is accepted as dir separator)
139e4b17023SJohn Marino    regardless of host.  */
140e4b17023SJohn Marino 
141e4b17023SJohn Marino extern const char *unix_lbasename (const char *);
142e4b17023SJohn Marino 
143e4b17023SJohn Marino /* A well-defined realpath () that is always compiled in.  */
144e4b17023SJohn Marino 
145e4b17023SJohn Marino extern char *lrealpath (const char *);
146e4b17023SJohn Marino 
147e4b17023SJohn Marino /* Concatenate an arbitrary number of strings.  You must pass NULL as
148e4b17023SJohn Marino    the last argument of this function, to terminate the list of
149e4b17023SJohn Marino    strings.  Allocates memory using xmalloc.  */
150e4b17023SJohn Marino 
151e4b17023SJohn Marino extern char *concat (const char *, ...) ATTRIBUTE_MALLOC ATTRIBUTE_SENTINEL;
152e4b17023SJohn Marino 
153e4b17023SJohn Marino /* Concatenate an arbitrary number of strings.  You must pass NULL as
154e4b17023SJohn Marino    the last argument of this function, to terminate the list of
155e4b17023SJohn Marino    strings.  Allocates memory using xmalloc.  The first argument is
156e4b17023SJohn Marino    not one of the strings to be concatenated, but if not NULL is a
157e4b17023SJohn Marino    pointer to be freed after the new string is created, similar to the
158e4b17023SJohn Marino    way xrealloc works.  */
159e4b17023SJohn Marino 
160e4b17023SJohn Marino extern char *reconcat (char *, const char *, ...) ATTRIBUTE_MALLOC ATTRIBUTE_SENTINEL;
161e4b17023SJohn Marino 
162e4b17023SJohn Marino /* Determine the length of concatenating an arbitrary number of
163e4b17023SJohn Marino    strings.  You must pass NULL as the last argument of this function,
164e4b17023SJohn Marino    to terminate the list of strings.  */
165e4b17023SJohn Marino 
166e4b17023SJohn Marino extern unsigned long concat_length (const char *, ...) ATTRIBUTE_SENTINEL;
167e4b17023SJohn Marino 
168e4b17023SJohn Marino /* Concatenate an arbitrary number of strings into a SUPPLIED area of
169e4b17023SJohn Marino    memory.  You must pass NULL as the last argument of this function,
170e4b17023SJohn Marino    to terminate the list of strings.  The supplied memory is assumed
171e4b17023SJohn Marino    to be large enough.  */
172e4b17023SJohn Marino 
173e4b17023SJohn Marino extern char *concat_copy (char *, const char *, ...) ATTRIBUTE_SENTINEL;
174e4b17023SJohn Marino 
175e4b17023SJohn Marino /* Concatenate an arbitrary number of strings into a GLOBAL area of
176e4b17023SJohn Marino    memory.  You must pass NULL as the last argument of this function,
177e4b17023SJohn Marino    to terminate the list of strings.  The supplied memory is assumed
178e4b17023SJohn Marino    to be large enough.  */
179e4b17023SJohn Marino 
180e4b17023SJohn Marino extern char *concat_copy2 (const char *, ...) ATTRIBUTE_SENTINEL;
181e4b17023SJohn Marino 
182e4b17023SJohn Marino /* This is the global area used by concat_copy2.  */
183e4b17023SJohn Marino 
184e4b17023SJohn Marino extern char *libiberty_concat_ptr;
185e4b17023SJohn Marino 
186e4b17023SJohn Marino /* Concatenate an arbitrary number of strings.  You must pass NULL as
187e4b17023SJohn Marino    the last argument of this function, to terminate the list of
188e4b17023SJohn Marino    strings.  Allocates memory using alloca.  The arguments are
189e4b17023SJohn Marino    evaluated twice!  */
190e4b17023SJohn Marino #define ACONCAT(ACONCAT_PARAMS) \
191e4b17023SJohn Marino   (libiberty_concat_ptr = (char *) alloca (concat_length ACONCAT_PARAMS + 1), \
192e4b17023SJohn Marino    concat_copy2 ACONCAT_PARAMS)
193e4b17023SJohn Marino 
194e4b17023SJohn Marino /* Check whether two file descriptors refer to the same file.  */
195e4b17023SJohn Marino 
196e4b17023SJohn Marino extern int fdmatch (int fd1, int fd2);
197e4b17023SJohn Marino 
198e4b17023SJohn Marino /* Return the position of the first bit set in the argument.  */
199e4b17023SJohn Marino /* Prototypes vary from system to system, so we only provide a
200e4b17023SJohn Marino    prototype on systems where we know that we need it.  */
201e4b17023SJohn Marino #if defined (HAVE_DECL_FFS) && !HAVE_DECL_FFS
202e4b17023SJohn Marino extern int ffs(int);
203e4b17023SJohn Marino #endif
204e4b17023SJohn Marino 
205e4b17023SJohn Marino /* Get the working directory.  The result is cached, so don't call
206e4b17023SJohn Marino    chdir() between calls to getpwd().  */
207e4b17023SJohn Marino 
208e4b17023SJohn Marino extern char * getpwd (void);
209e4b17023SJohn Marino 
210e4b17023SJohn Marino /* Get the current time.  */
211e4b17023SJohn Marino /* Prototypes vary from system to system, so we only provide a
212e4b17023SJohn Marino    prototype on systems where we know that we need it.  */
213e4b17023SJohn Marino #ifdef __MINGW32__
214e4b17023SJohn Marino /* Forward declaration to avoid #include <sys/time.h>.   */
215e4b17023SJohn Marino struct timeval;
216e4b17023SJohn Marino extern int gettimeofday (struct timeval *, void *);
217e4b17023SJohn Marino #endif
218e4b17023SJohn Marino 
219e4b17023SJohn Marino /* Get the amount of time the process has run, in microseconds.  */
220e4b17023SJohn Marino 
221e4b17023SJohn Marino extern long get_run_time (void);
222e4b17023SJohn Marino 
223e4b17023SJohn Marino /* Generate a relocated path to some installation directory.  Allocates
224e4b17023SJohn Marino    return value using malloc.  */
225e4b17023SJohn Marino 
226e4b17023SJohn Marino extern char *make_relative_prefix (const char *, const char *,
227e4b17023SJohn Marino                                    const char *) ATTRIBUTE_MALLOC;
228e4b17023SJohn Marino 
229e4b17023SJohn Marino /* Generate a relocated path to some installation directory without
230e4b17023SJohn Marino    attempting to follow any soft links.  Allocates
231e4b17023SJohn Marino    return value using malloc.  */
232e4b17023SJohn Marino 
233e4b17023SJohn Marino extern char *make_relative_prefix_ignore_links (const char *, const char *,
234e4b17023SJohn Marino 						const char *) ATTRIBUTE_MALLOC;
235e4b17023SJohn Marino 
236e4b17023SJohn Marino /* Choose a temporary directory to use for scratch files.  */
237e4b17023SJohn Marino 
238e4b17023SJohn Marino extern char *choose_temp_base (void) ATTRIBUTE_MALLOC;
239e4b17023SJohn Marino 
240e4b17023SJohn Marino /* Return a temporary file name or NULL if unable to create one.  */
241e4b17023SJohn Marino 
242e4b17023SJohn Marino extern char *make_temp_file (const char *) ATTRIBUTE_MALLOC;
243e4b17023SJohn Marino 
244e4b17023SJohn Marino /* Remove a link to a file unless it is special. */
245e4b17023SJohn Marino 
246e4b17023SJohn Marino extern int unlink_if_ordinary (const char *);
247e4b17023SJohn Marino 
248e4b17023SJohn Marino /* Allocate memory filled with spaces.  Allocates using malloc.  */
249e4b17023SJohn Marino 
250e4b17023SJohn Marino extern const char *spaces (int count);
251e4b17023SJohn Marino 
252e4b17023SJohn Marino /* Return the maximum error number for which strerror will return a
253e4b17023SJohn Marino    string.  */
254e4b17023SJohn Marino 
255e4b17023SJohn Marino extern int errno_max (void);
256e4b17023SJohn Marino 
257e4b17023SJohn Marino /* Return the name of an errno value (e.g., strerrno (EINVAL) returns
258e4b17023SJohn Marino    "EINVAL").  */
259e4b17023SJohn Marino 
260e4b17023SJohn Marino extern const char *strerrno (int);
261e4b17023SJohn Marino 
262e4b17023SJohn Marino /* Given the name of an errno value, return the value.  */
263e4b17023SJohn Marino 
264e4b17023SJohn Marino extern int strtoerrno (const char *);
265e4b17023SJohn Marino 
266e4b17023SJohn Marino /* ANSI's strerror(), but more robust.  */
267e4b17023SJohn Marino 
268e4b17023SJohn Marino extern char *xstrerror (int);
269e4b17023SJohn Marino 
270e4b17023SJohn Marino /* Return the maximum signal number for which strsignal will return a
271e4b17023SJohn Marino    string.  */
272e4b17023SJohn Marino 
273e4b17023SJohn Marino extern int signo_max (void);
274e4b17023SJohn Marino 
275e4b17023SJohn Marino /* Return a signal message string for a signal number
276e4b17023SJohn Marino    (e.g., strsignal (SIGHUP) returns something like "Hangup").  */
277e4b17023SJohn Marino /* This is commented out as it can conflict with one in system headers.
278e4b17023SJohn Marino    We still document its existence though.  */
279e4b17023SJohn Marino 
280e4b17023SJohn Marino /*extern const char *strsignal (int);*/
281e4b17023SJohn Marino 
282e4b17023SJohn Marino /* Return the name of a signal number (e.g., strsigno (SIGHUP) returns
283e4b17023SJohn Marino    "SIGHUP").  */
284e4b17023SJohn Marino 
285e4b17023SJohn Marino extern const char *strsigno (int);
286e4b17023SJohn Marino 
287e4b17023SJohn Marino /* Given the name of a signal, return its number.  */
288e4b17023SJohn Marino 
289e4b17023SJohn Marino extern int strtosigno (const char *);
290e4b17023SJohn Marino 
291e4b17023SJohn Marino /* Register a function to be run by xexit.  Returns 0 on success.  */
292e4b17023SJohn Marino 
293e4b17023SJohn Marino extern int xatexit (void (*fn) (void));
294e4b17023SJohn Marino 
295e4b17023SJohn Marino /* Exit, calling all the functions registered with xatexit.  */
296e4b17023SJohn Marino 
297e4b17023SJohn Marino extern void xexit (int status) ATTRIBUTE_NORETURN;
298e4b17023SJohn Marino 
299e4b17023SJohn Marino /* Set the program name used by xmalloc.  */
300e4b17023SJohn Marino 
301e4b17023SJohn Marino extern void xmalloc_set_program_name (const char *);
302e4b17023SJohn Marino 
303e4b17023SJohn Marino /* Report an allocation failure.  */
304e4b17023SJohn Marino extern void xmalloc_failed (size_t) ATTRIBUTE_NORETURN;
305e4b17023SJohn Marino 
306e4b17023SJohn Marino /* Allocate memory without fail.  If malloc fails, this will print a
307e4b17023SJohn Marino    message to stderr (using the name set by xmalloc_set_program_name,
308e4b17023SJohn Marino    if any) and then call xexit.  */
309e4b17023SJohn Marino 
310e4b17023SJohn Marino extern void *xmalloc (size_t) ATTRIBUTE_MALLOC;
311e4b17023SJohn Marino 
312e4b17023SJohn Marino /* Reallocate memory without fail.  This works like xmalloc.  Note,
313e4b17023SJohn Marino    realloc type functions are not suitable for attribute malloc since
314e4b17023SJohn Marino    they may return the same address across multiple calls. */
315e4b17023SJohn Marino 
316e4b17023SJohn Marino extern void *xrealloc (void *, size_t);
317e4b17023SJohn Marino 
318e4b17023SJohn Marino /* Allocate memory without fail and set it to zero.  This works like
319e4b17023SJohn Marino    xmalloc.  */
320e4b17023SJohn Marino 
321e4b17023SJohn Marino extern void *xcalloc (size_t, size_t) ATTRIBUTE_MALLOC;
322e4b17023SJohn Marino 
323e4b17023SJohn Marino /* Copy a string into a memory buffer without fail.  */
324e4b17023SJohn Marino 
325e4b17023SJohn Marino extern char *xstrdup (const char *) ATTRIBUTE_MALLOC;
326e4b17023SJohn Marino 
327e4b17023SJohn Marino /* Copy at most N characters from string into a buffer without fail.  */
328e4b17023SJohn Marino 
329e4b17023SJohn Marino extern char *xstrndup (const char *, size_t) ATTRIBUTE_MALLOC;
330e4b17023SJohn Marino 
331e4b17023SJohn Marino /* Copy an existing memory buffer to a new memory buffer without fail.  */
332e4b17023SJohn Marino 
333e4b17023SJohn Marino extern void *xmemdup (const void *, size_t, size_t) ATTRIBUTE_MALLOC;
334e4b17023SJohn Marino 
335e4b17023SJohn Marino /* Physical memory routines.  Return values are in BYTES.  */
336e4b17023SJohn Marino extern double physmem_total (void);
337e4b17023SJohn Marino extern double physmem_available (void);
338e4b17023SJohn Marino 
339e4b17023SJohn Marino /* Compute the 32-bit CRC of a block of memory.  */
340e4b17023SJohn Marino extern unsigned int xcrc32 (const unsigned char *, int, unsigned int);
341e4b17023SJohn Marino 
342e4b17023SJohn Marino /* These macros provide a K&R/C89/C++-friendly way of allocating structures
343e4b17023SJohn Marino    with nice encapsulation.  The XDELETE*() macros are technically
344e4b17023SJohn Marino    superfluous, but provided here for symmetry.  Using them consistently
345e4b17023SJohn Marino    makes it easier to update client code to use different allocators such
346e4b17023SJohn Marino    as new/delete and new[]/delete[].  */
347e4b17023SJohn Marino 
348e4b17023SJohn Marino /* Scalar allocators.  */
349e4b17023SJohn Marino 
350e4b17023SJohn Marino #define XALLOCA(T)		((T *) alloca (sizeof (T)))
351e4b17023SJohn Marino #define XNEW(T)			((T *) xmalloc (sizeof (T)))
352e4b17023SJohn Marino #define XCNEW(T)		((T *) xcalloc (1, sizeof (T)))
353e4b17023SJohn Marino #define XDUP(T, P)		((T *) xmemdup ((P), sizeof (T), sizeof (T)))
354e4b17023SJohn Marino #define XDELETE(P)		free ((void*) (P))
355e4b17023SJohn Marino 
356e4b17023SJohn Marino /* Array allocators.  */
357e4b17023SJohn Marino 
358e4b17023SJohn Marino #define XALLOCAVEC(T, N)	((T *) alloca (sizeof (T) * (N)))
359e4b17023SJohn Marino #define XNEWVEC(T, N)		((T *) xmalloc (sizeof (T) * (N)))
360e4b17023SJohn Marino #define XCNEWVEC(T, N)		((T *) xcalloc ((N), sizeof (T)))
361e4b17023SJohn Marino #define XDUPVEC(T, P, N)	((T *) xmemdup ((P), sizeof (T) * (N), sizeof (T) * (N)))
362e4b17023SJohn Marino #define XRESIZEVEC(T, P, N)	((T *) xrealloc ((void *) (P), sizeof (T) * (N)))
363e4b17023SJohn Marino #define XDELETEVEC(P)		free ((void*) (P))
364e4b17023SJohn Marino 
365e4b17023SJohn Marino /* Allocators for variable-sized structures and raw buffers.  */
366e4b17023SJohn Marino 
367e4b17023SJohn Marino #define XALLOCAVAR(T, S)	((T *) alloca ((S)))
368e4b17023SJohn Marino #define XNEWVAR(T, S)		((T *) xmalloc ((S)))
369e4b17023SJohn Marino #define XCNEWVAR(T, S)		((T *) xcalloc (1, (S)))
370e4b17023SJohn Marino #define XDUPVAR(T, P, S1, S2)	((T *) xmemdup ((P), (S1), (S2)))
371e4b17023SJohn Marino #define XRESIZEVAR(T, P, S)	((T *) xrealloc ((P), (S)))
372e4b17023SJohn Marino 
373e4b17023SJohn Marino /* Type-safe obstack allocator.  */
374e4b17023SJohn Marino 
375e4b17023SJohn Marino #define XOBNEW(O, T)		((T *) obstack_alloc ((O), sizeof (T)))
376e4b17023SJohn Marino #define XOBNEWVEC(O, T, N)	((T *) obstack_alloc ((O), sizeof (T) * (N)))
377e4b17023SJohn Marino #define XOBNEWVAR(O, T, S)	((T *) obstack_alloc ((O), (S)))
378e4b17023SJohn Marino #define XOBFINISH(O, T)         ((T) obstack_finish ((O)))
379e4b17023SJohn Marino 
380e4b17023SJohn Marino /* hex character manipulation routines */
381e4b17023SJohn Marino 
382e4b17023SJohn Marino #define _hex_array_size 256
383e4b17023SJohn Marino #define _hex_bad	99
384e4b17023SJohn Marino extern const unsigned char _hex_value[_hex_array_size];
385e4b17023SJohn Marino extern void hex_init (void);
386e4b17023SJohn Marino #define hex_p(c)	(hex_value (c) != _hex_bad)
387e4b17023SJohn Marino /* If you change this, note well: Some code relies on side effects in
388e4b17023SJohn Marino    the argument being performed exactly once.  */
389e4b17023SJohn Marino #define hex_value(c)	((unsigned int) _hex_value[(unsigned char) (c)])
390e4b17023SJohn Marino 
391e4b17023SJohn Marino /* Flags for pex_init.  These are bits to be or'ed together.  */
392e4b17023SJohn Marino 
393e4b17023SJohn Marino /* Record subprocess times, if possible.  */
394e4b17023SJohn Marino #define PEX_RECORD_TIMES	0x1
395e4b17023SJohn Marino 
396e4b17023SJohn Marino /* Use pipes for communication between processes, if possible.  */
397e4b17023SJohn Marino #define PEX_USE_PIPES		0x2
398e4b17023SJohn Marino 
399e4b17023SJohn Marino /* Save files used for communication between processes.  */
400e4b17023SJohn Marino #define PEX_SAVE_TEMPS		0x4
401e4b17023SJohn Marino 
402e4b17023SJohn Marino /* Prepare to execute one or more programs, with standard output of
403e4b17023SJohn Marino    each program fed to standard input of the next.
404e4b17023SJohn Marino    FLAGS	As above.
405e4b17023SJohn Marino    PNAME	The name of the program to report in error messages.
406e4b17023SJohn Marino    TEMPBASE	A base name to use for temporary files; may be NULL to
407e4b17023SJohn Marino    		use a random name.
408e4b17023SJohn Marino    Returns NULL on error.  */
409e4b17023SJohn Marino 
410e4b17023SJohn Marino extern struct pex_obj *pex_init (int flags, const char *pname,
411e4b17023SJohn Marino 				 const char *tempbase);
412e4b17023SJohn Marino 
413e4b17023SJohn Marino /* Flags for pex_run.  These are bits to be or'ed together.  */
414e4b17023SJohn Marino 
415e4b17023SJohn Marino /* Last program in pipeline.  Standard output of program goes to
416e4b17023SJohn Marino    OUTNAME, or, if OUTNAME is NULL, to standard output of caller.  Do
417e4b17023SJohn Marino    not set this if you want to call pex_read_output.  After this is
418e4b17023SJohn Marino    set, pex_run may no longer be called with the same struct
419e4b17023SJohn Marino    pex_obj.  */
420e4b17023SJohn Marino #define PEX_LAST		0x1
421e4b17023SJohn Marino 
422e4b17023SJohn Marino /* Search for program in executable search path.  */
423e4b17023SJohn Marino #define PEX_SEARCH		0x2
424e4b17023SJohn Marino 
425e4b17023SJohn Marino /* OUTNAME is a suffix.  */
426e4b17023SJohn Marino #define PEX_SUFFIX		0x4
427e4b17023SJohn Marino 
428e4b17023SJohn Marino /* Send program's standard error to standard output.  */
429e4b17023SJohn Marino #define PEX_STDERR_TO_STDOUT	0x8
430e4b17023SJohn Marino 
431e4b17023SJohn Marino /* Input file should be opened in binary mode.  This flag is ignored
432e4b17023SJohn Marino    on Unix.  */
433e4b17023SJohn Marino #define PEX_BINARY_INPUT	0x10
434e4b17023SJohn Marino 
435e4b17023SJohn Marino /* Output file should be opened in binary mode.  This flag is ignored
436e4b17023SJohn Marino    on Unix.  For proper behaviour PEX_BINARY_INPUT and
437e4b17023SJohn Marino    PEX_BINARY_OUTPUT have to match appropriately--i.e., a call using
438e4b17023SJohn Marino    PEX_BINARY_OUTPUT should be followed by a call using
439e4b17023SJohn Marino    PEX_BINARY_INPUT.  */
440e4b17023SJohn Marino #define PEX_BINARY_OUTPUT	0x20
441e4b17023SJohn Marino 
442e4b17023SJohn Marino /* Capture stderr to a pipe.  The output can be read by
443e4b17023SJohn Marino    calling pex_read_err and reading from the returned
444e4b17023SJohn Marino    FILE object.  This flag may be specified only for
445e4b17023SJohn Marino    the last program in a pipeline.
446e4b17023SJohn Marino 
447e4b17023SJohn Marino    This flag is supported only on Unix and Windows.  */
448e4b17023SJohn Marino #define PEX_STDERR_TO_PIPE	0x40
449e4b17023SJohn Marino 
450e4b17023SJohn Marino /* Capture stderr in binary mode.  This flag is ignored
451e4b17023SJohn Marino    on Unix.  */
452e4b17023SJohn Marino #define PEX_BINARY_ERROR	0x80
453e4b17023SJohn Marino 
454e4b17023SJohn Marino 
455e4b17023SJohn Marino /* Execute one program.  Returns NULL on success.  On error returns an
456e4b17023SJohn Marino    error string (typically just the name of a system call); the error
457e4b17023SJohn Marino    string is statically allocated.
458e4b17023SJohn Marino 
459e4b17023SJohn Marino    OBJ		Returned by pex_init.
460e4b17023SJohn Marino 
461e4b17023SJohn Marino    FLAGS	As above.
462e4b17023SJohn Marino 
463e4b17023SJohn Marino    EXECUTABLE	The program to execute.
464e4b17023SJohn Marino 
465e4b17023SJohn Marino    ARGV		NULL terminated array of arguments to pass to the program.
466e4b17023SJohn Marino 
467e4b17023SJohn Marino    OUTNAME	Sets the output file name as follows:
468e4b17023SJohn Marino 
469e4b17023SJohn Marino 		PEX_SUFFIX set (OUTNAME may not be NULL):
470e4b17023SJohn Marino 		  TEMPBASE parameter to pex_init not NULL:
471e4b17023SJohn Marino 		    Output file name is the concatenation of TEMPBASE
472e4b17023SJohn Marino 		    and OUTNAME.
473e4b17023SJohn Marino 		  TEMPBASE is NULL:
474e4b17023SJohn Marino 		    Output file name is a random file name ending in
475e4b17023SJohn Marino 		    OUTNAME.
476e4b17023SJohn Marino 		PEX_SUFFIX not set:
477e4b17023SJohn Marino 		  OUTNAME not NULL:
478e4b17023SJohn Marino 		    Output file name is OUTNAME.
479e4b17023SJohn Marino 		  OUTNAME NULL, TEMPBASE not NULL:
480e4b17023SJohn Marino 		    Output file name is randomly chosen using
481e4b17023SJohn Marino 		    TEMPBASE.
482e4b17023SJohn Marino 		  OUTNAME NULL, TEMPBASE NULL:
483e4b17023SJohn Marino 		    Output file name is randomly chosen.
484e4b17023SJohn Marino 
485e4b17023SJohn Marino 		If PEX_LAST is not set, the output file name is the
486e4b17023SJohn Marino    		name to use for a temporary file holding stdout, if
487e4b17023SJohn Marino    		any (there will not be a file if PEX_USE_PIPES is set
488e4b17023SJohn Marino    		and the system supports pipes).  If a file is used, it
489e4b17023SJohn Marino    		will be removed when no longer needed unless
490e4b17023SJohn Marino    		PEX_SAVE_TEMPS is set.
491e4b17023SJohn Marino 
492e4b17023SJohn Marino 		If PEX_LAST is set, and OUTNAME is not NULL, standard
493e4b17023SJohn Marino    		output is written to the output file name.  The file
494e4b17023SJohn Marino    		will not be removed.  If PEX_LAST and PEX_SUFFIX are
495e4b17023SJohn Marino    		both set, TEMPBASE may not be NULL.
496e4b17023SJohn Marino 
497e4b17023SJohn Marino    ERRNAME	If not NULL, this is the name of a file to which
498e4b17023SJohn Marino 		standard error is written.  If NULL, standard error of
499e4b17023SJohn Marino 		the program is standard error of the caller.
500e4b17023SJohn Marino 
501e4b17023SJohn Marino    ERR		On an error return, *ERR is set to an errno value, or
502e4b17023SJohn Marino    		to 0 if there is no relevant errno.
503e4b17023SJohn Marino */
504e4b17023SJohn Marino 
505e4b17023SJohn Marino extern const char *pex_run (struct pex_obj *obj, int flags,
506e4b17023SJohn Marino 			    const char *executable, char * const *argv,
507e4b17023SJohn Marino 			    const char *outname, const char *errname,
508e4b17023SJohn Marino 			    int *err);
509e4b17023SJohn Marino 
510e4b17023SJohn Marino /* As for pex_run (), but takes an extra parameter to enable the
511e4b17023SJohn Marino    environment for the child process to be specified.
512e4b17023SJohn Marino 
513e4b17023SJohn Marino    ENV		The environment for the child process, specified as
514e4b17023SJohn Marino 		an array of character pointers.  Each element of the
515e4b17023SJohn Marino 		array should point to a string of the form VAR=VALUE,
516e4b17023SJohn Marino                 with the exception of the last element which must be
517e4b17023SJohn Marino                 a null pointer.
518e4b17023SJohn Marino */
519e4b17023SJohn Marino 
520e4b17023SJohn Marino extern const char *pex_run_in_environment (struct pex_obj *obj, int flags,
521e4b17023SJohn Marino 			                   const char *executable,
522e4b17023SJohn Marino                                            char * const *argv,
523e4b17023SJohn Marino                                            char * const *env,
524e4b17023SJohn Marino               	          		   const char *outname,
525e4b17023SJohn Marino 					   const char *errname, int *err);
526e4b17023SJohn Marino 
527e4b17023SJohn Marino /* Return a stream for a temporary file to pass to the first program
528e4b17023SJohn Marino    in the pipeline as input.  The file name is chosen as for pex_run.
529e4b17023SJohn Marino    pex_run closes the file automatically; don't close it yourself.  */
530e4b17023SJohn Marino 
531e4b17023SJohn Marino extern FILE *pex_input_file (struct pex_obj *obj, int flags,
532e4b17023SJohn Marino                              const char *in_name);
533e4b17023SJohn Marino 
534e4b17023SJohn Marino /* Return a stream for a pipe connected to the standard input of the
535e4b17023SJohn Marino    first program in the pipeline.  You must have passed
536e4b17023SJohn Marino    `PEX_USE_PIPES' to `pex_init'.  Close the returned stream
537e4b17023SJohn Marino    yourself.  */
538e4b17023SJohn Marino 
539e4b17023SJohn Marino extern FILE *pex_input_pipe (struct pex_obj *obj, int binary);
540e4b17023SJohn Marino 
541e4b17023SJohn Marino /* Read the standard output of the last program to be executed.
542e4b17023SJohn Marino    pex_run can not be called after this.  BINARY should be non-zero if
543e4b17023SJohn Marino    the file should be opened in binary mode; this is ignored on Unix.
544e4b17023SJohn Marino    Returns NULL on error.  Don't call fclose on the returned FILE; it
545e4b17023SJohn Marino    will be closed by pex_free.  */
546e4b17023SJohn Marino 
547e4b17023SJohn Marino extern FILE *pex_read_output (struct pex_obj *, int binary);
548e4b17023SJohn Marino 
549e4b17023SJohn Marino /* Read the standard error of the last program to be executed.
550e4b17023SJohn Marino    pex_run can not be called after this.  BINARY should be non-zero if
551e4b17023SJohn Marino    the file should be opened in binary mode; this is ignored on Unix.
552e4b17023SJohn Marino    Returns NULL on error.  Don't call fclose on the returned FILE; it
553e4b17023SJohn Marino    will be closed by pex_free.  */
554e4b17023SJohn Marino 
555e4b17023SJohn Marino extern FILE *pex_read_err (struct pex_obj *, int binary);
556e4b17023SJohn Marino 
557e4b17023SJohn Marino /* Return exit status of all programs in VECTOR.  COUNT indicates the
558e4b17023SJohn Marino    size of VECTOR.  The status codes in the vector are in the order of
559e4b17023SJohn Marino    the calls to pex_run.  Returns 0 on error, 1 on success.  */
560e4b17023SJohn Marino 
561e4b17023SJohn Marino extern int pex_get_status (struct pex_obj *, int count, int *vector);
562e4b17023SJohn Marino 
563e4b17023SJohn Marino /* Return times of all programs in VECTOR.  COUNT indicates the size
564e4b17023SJohn Marino    of VECTOR.  struct pex_time is really just struct timeval, but that
565e4b17023SJohn Marino    is not portable to all systems.  Returns 0 on error, 1 on
566e4b17023SJohn Marino    success.  */
567e4b17023SJohn Marino 
568e4b17023SJohn Marino struct pex_time
569e4b17023SJohn Marino {
570e4b17023SJohn Marino   unsigned long user_seconds;
571e4b17023SJohn Marino   unsigned long user_microseconds;
572e4b17023SJohn Marino   unsigned long system_seconds;
573e4b17023SJohn Marino   unsigned long system_microseconds;
574e4b17023SJohn Marino };
575e4b17023SJohn Marino 
576e4b17023SJohn Marino extern int pex_get_times (struct pex_obj *, int count,
577e4b17023SJohn Marino 			  struct pex_time *vector);
578e4b17023SJohn Marino 
579e4b17023SJohn Marino /* Clean up a pex_obj.  If you have not called pex_get_times or
580e4b17023SJohn Marino    pex_get_status, this will try to kill the subprocesses.  */
581e4b17023SJohn Marino 
582e4b17023SJohn Marino extern void pex_free (struct pex_obj *);
583e4b17023SJohn Marino 
584e4b17023SJohn Marino /* Just execute one program.  Return value is as for pex_run.
585e4b17023SJohn Marino    FLAGS	Combination of PEX_SEARCH and PEX_STDERR_TO_STDOUT.
586e4b17023SJohn Marino    EXECUTABLE	As for pex_run.
587e4b17023SJohn Marino    ARGV		As for pex_run.
588e4b17023SJohn Marino    PNAME	As for pex_init.
589e4b17023SJohn Marino    OUTNAME	As for pex_run when PEX_LAST is set.
590e4b17023SJohn Marino    ERRNAME	As for pex_run.
591e4b17023SJohn Marino    STATUS	Set to exit status on success.
592e4b17023SJohn Marino    ERR		As for pex_run.
593e4b17023SJohn Marino */
594e4b17023SJohn Marino 
595e4b17023SJohn Marino extern const char *pex_one (int flags, const char *executable,
596e4b17023SJohn Marino 			    char * const *argv, const char *pname,
597e4b17023SJohn Marino 			    const char *outname, const char *errname,
598e4b17023SJohn Marino 			    int *status, int *err);
599e4b17023SJohn Marino 
600e4b17023SJohn Marino /* pexecute and pwait are the old pexecute interface, still here for
601e4b17023SJohn Marino    backward compatibility.  Don't use these for new code.  Instead,
602e4b17023SJohn Marino    use pex_init/pex_run/pex_get_status/pex_free, or pex_one.  */
603e4b17023SJohn Marino 
604e4b17023SJohn Marino /* Definitions used by the pexecute routine.  */
605e4b17023SJohn Marino 
606e4b17023SJohn Marino #define PEXECUTE_FIRST   1
607e4b17023SJohn Marino #define PEXECUTE_LAST    2
608e4b17023SJohn Marino #define PEXECUTE_ONE     (PEXECUTE_FIRST + PEXECUTE_LAST)
609e4b17023SJohn Marino #define PEXECUTE_SEARCH  4
610e4b17023SJohn Marino #define PEXECUTE_VERBOSE 8
611e4b17023SJohn Marino 
612e4b17023SJohn Marino /* Execute a program.  */
613e4b17023SJohn Marino 
614e4b17023SJohn Marino extern int pexecute (const char *, char * const *, const char *,
615e4b17023SJohn Marino                      const char *, char **, char **, int);
616e4b17023SJohn Marino 
617e4b17023SJohn Marino /* Wait for pexecute to finish.  */
618e4b17023SJohn Marino 
619e4b17023SJohn Marino extern int pwait (int, int *, int);
620e4b17023SJohn Marino 
621e4b17023SJohn Marino #if !HAVE_DECL_ASPRINTF
622e4b17023SJohn Marino /* Like sprintf but provides a pointer to malloc'd storage, which must
623e4b17023SJohn Marino    be freed by the caller.  */
624e4b17023SJohn Marino 
625e4b17023SJohn Marino extern int asprintf (char **, const char *, ...) ATTRIBUTE_PRINTF_2;
626e4b17023SJohn Marino #endif
627e4b17023SJohn Marino 
628e4b17023SJohn Marino #if !HAVE_DECL_VASPRINTF
629e4b17023SJohn Marino /* Like vsprintf but provides a pointer to malloc'd storage, which
630e4b17023SJohn Marino    must be freed by the caller.  */
631e4b17023SJohn Marino 
632e4b17023SJohn Marino extern int vasprintf (char **, const char *, va_list) ATTRIBUTE_PRINTF(2,0);
633e4b17023SJohn Marino #endif
634e4b17023SJohn Marino 
635e4b17023SJohn Marino #if defined(HAVE_DECL_SNPRINTF) && !HAVE_DECL_SNPRINTF
636e4b17023SJohn Marino /* Like sprintf but prints at most N characters.  */
637e4b17023SJohn Marino extern int snprintf (char *, size_t, const char *, ...) ATTRIBUTE_PRINTF_3;
638e4b17023SJohn Marino #endif
639e4b17023SJohn Marino 
640e4b17023SJohn Marino #if defined(HAVE_DECL_VSNPRINTF) && !HAVE_DECL_VSNPRINTF
641e4b17023SJohn Marino /* Like vsprintf but prints at most N characters.  */
642e4b17023SJohn Marino extern int vsnprintf (char *, size_t, const char *, va_list) ATTRIBUTE_PRINTF(3,0);
643e4b17023SJohn Marino #endif
644e4b17023SJohn Marino 
645e4b17023SJohn Marino #if defined(HAVE_DECL_STRVERSCMP) && !HAVE_DECL_STRVERSCMP
646e4b17023SJohn Marino /* Compare version strings.  */
647e4b17023SJohn Marino extern int strverscmp (const char *, const char *);
648e4b17023SJohn Marino #endif
649e4b17023SJohn Marino 
650e4b17023SJohn Marino /* Set the title of a process */
651e4b17023SJohn Marino extern void setproctitle (const char *name, ...);
652e4b17023SJohn Marino 
653e4b17023SJohn Marino /* Increase stack limit if possible.  */
654e4b17023SJohn Marino extern void stack_limit_increase (unsigned long);
655e4b17023SJohn Marino 
656e4b17023SJohn Marino #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
657e4b17023SJohn Marino 
658e4b17023SJohn Marino /* Drastically simplified alloca configurator.  If we're using GCC,
659e4b17023SJohn Marino    we use __builtin_alloca; otherwise we use the C alloca.  The C
660e4b17023SJohn Marino    alloca is always available.  You can override GCC by defining
661e4b17023SJohn Marino    USE_C_ALLOCA yourself.  The canonical autoconf macro C_ALLOCA is
662e4b17023SJohn Marino    also set/unset as it is often used to indicate whether code needs
663e4b17023SJohn Marino    to call alloca(0).  */
664e4b17023SJohn Marino extern void *C_alloca (size_t) ATTRIBUTE_MALLOC;
665e4b17023SJohn Marino #undef alloca
666e4b17023SJohn Marino #if GCC_VERSION >= 2000 && !defined USE_C_ALLOCA
667e4b17023SJohn Marino # define alloca(x) __builtin_alloca(x)
668e4b17023SJohn Marino # undef C_ALLOCA
669e4b17023SJohn Marino # define ASTRDUP(X) \
670e4b17023SJohn Marino   (__extension__ ({ const char *const libiberty_optr = (X); \
671e4b17023SJohn Marino    const unsigned long libiberty_len = strlen (libiberty_optr) + 1; \
672e4b17023SJohn Marino    char *const libiberty_nptr = (char *const) alloca (libiberty_len); \
673e4b17023SJohn Marino    (char *) memcpy (libiberty_nptr, libiberty_optr, libiberty_len); }))
674e4b17023SJohn Marino #else
675e4b17023SJohn Marino # define alloca(x) C_alloca(x)
676e4b17023SJohn Marino # undef USE_C_ALLOCA
677e4b17023SJohn Marino # define USE_C_ALLOCA 1
678e4b17023SJohn Marino # undef C_ALLOCA
679e4b17023SJohn Marino # define C_ALLOCA 1
680e4b17023SJohn Marino extern const char *libiberty_optr;
681e4b17023SJohn Marino extern char *libiberty_nptr;
682e4b17023SJohn Marino extern unsigned long libiberty_len;
683e4b17023SJohn Marino # define ASTRDUP(X) \
684e4b17023SJohn Marino   (libiberty_optr = (X), \
685e4b17023SJohn Marino    libiberty_len = strlen (libiberty_optr) + 1, \
686e4b17023SJohn Marino    libiberty_nptr = (char *) alloca (libiberty_len), \
687e4b17023SJohn Marino    (char *) memcpy (libiberty_nptr, libiberty_optr, libiberty_len))
688e4b17023SJohn Marino #endif
689e4b17023SJohn Marino 
690e4b17023SJohn Marino #ifdef __cplusplus
691e4b17023SJohn Marino }
692e4b17023SJohn Marino #endif
693e4b17023SJohn Marino 
694e4b17023SJohn Marino 
695e4b17023SJohn Marino #endif /* ! defined (LIBIBERTY_H) */
696