1 /* Function declarations for libiberty.
3 Copyright 2001, 2002, 2005, 2007 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., 51 Franklin Street - Fifth Floor,
25 Boston, MA 02110-1301, 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. */
51 /* If the OS supports it, ensure that the supplied stream is setup to
52 avoid any multi-threaded locking. Otherwise leave the FILE pointer
53 unchanged. If the stream is NULL do nothing. */
55 extern void unlock_stream (FILE *);
57 /* If the OS supports it, ensure that the standard I/O streams, stdin,
58 stdout and stderr are setup to avoid any multi-threaded locking.
59 Otherwise do nothing. */
61 extern void unlock_std_streams (void);
63 /* Open and return a FILE pointer. If the OS supports it, ensure that
64 the stream is setup to avoid any multi-threaded locking. Otherwise
65 return the FILE pointer unchanged. */
67 extern FILE *fopen_unlocked (const char *, const char *);
68 extern FILE *fdopen_unlocked (int, const char *);
69 extern FILE *freopen_unlocked (const char *, const char *, FILE *);
71 /* Build an argument vector from a string. Allocates memory using
72 malloc. Use freeargv to free the vector. */
74 extern char **buildargv (const char *) ATTRIBUTE_MALLOC;
76 /* Free a vector returned by buildargv. */
78 extern void freeargv (char **);
80 /* Duplicate an argument vector. Allocates memory using malloc. Use
81 freeargv to free the vector. */
83 extern char **dupargv (char **) ATTRIBUTE_MALLOC;
85 /* Expand "@file" arguments in argv. */
87 extern void expandargv PARAMS ((int *, char ***));
89 /* Write argv to an @-file, inserting necessary quoting. */
91 extern int writeargv PARAMS ((char **, FILE *));
93 /* Return the last component of a path name. Note that we can't use a
94 prototype here because the parameter is declared inconsistently
95 across different systems, sometimes as "char *" and sometimes as
98 /* HAVE_DECL_* is a three-state macro: undefined, 0 or 1. If it is
99 undefined, we haven't run the autoconf check so provide the
100 declaration without arguments. If it is 0, we checked and failed
101 to find the declaration so provide a fully prototyped one. If it
102 is 1, we found it so don't provide any declaration at all. */
103 #if !HAVE_DECL_BASENAME
104 #if defined (__GNU_LIBRARY__ ) \
105 || defined (__linux__) \
106 || defined (__FreeBSD__) \
107 || defined (__OpenBSD__) \
108 || defined (__NetBSD__) \
109 || defined (__DragonFly__) \
110 || defined (__CYGWIN__) \
111 || defined (__CYGWIN32__) \
112 || defined (__MINGW32__) \
113 || defined (HAVE_DECL_BASENAME)
114 extern char *basename (const char *);
116 /* Do not allow basename to be used if there is no prototype seen. We
117 either need to use the above prototype or have one from
118 autoconf which would result in HAVE_DECL_BASENAME being set. */
119 #define basename basename_cannot_be_used_without_a_prototype
123 /* A well-defined basename () that is always compiled in. */
125 extern const char *lbasename (const char *);
127 /* A well-defined realpath () that is always compiled in. */
129 extern char *lrealpath (const char *);
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 xmalloc. */
135 extern char *concat (const char *, ...) ATTRIBUTE_MALLOC ATTRIBUTE_SENTINEL;
137 /* Concatenate an arbitrary number of strings. You must pass NULL as
138 the last argument of this function, to terminate the list of
139 strings. Allocates memory using xmalloc. The first argument is
140 not one of the strings to be concatenated, but if not NULL is a
141 pointer to be freed after the new string is created, similar to the
142 way xrealloc works. */
144 extern char *reconcat (char *, const char *, ...) ATTRIBUTE_MALLOC ATTRIBUTE_SENTINEL;
146 /* Determine the length of concatenating an arbitrary number of
147 strings. You must pass NULL as the last argument of this function,
148 to terminate the list of strings. */
150 extern unsigned long concat_length (const char *, ...) ATTRIBUTE_SENTINEL;
152 /* Concatenate an arbitrary number of strings into a SUPPLIED area of
153 memory. You must pass NULL as the last argument of this function,
154 to terminate the list of strings. The supplied memory is assumed
155 to be large enough. */
157 extern char *concat_copy (char *, const char *, ...) ATTRIBUTE_SENTINEL;
159 /* Concatenate an arbitrary number of strings into a GLOBAL area of
160 memory. You must pass NULL as the last argument of this function,
161 to terminate the list of strings. The supplied memory is assumed
162 to be large enough. */
164 extern char *concat_copy2 (const char *, ...) ATTRIBUTE_SENTINEL;
166 /* This is the global area used by concat_copy2. */
168 extern char *libiberty_concat_ptr;
170 /* Concatenate an arbitrary number of strings. You must pass NULL as
171 the last argument of this function, to terminate the list of
172 strings. Allocates memory using alloca. The arguments are
174 #define ACONCAT(ACONCAT_PARAMS) \
175 (libiberty_concat_ptr = (char *) alloca (concat_length ACONCAT_PARAMS + 1), \
176 concat_copy2 ACONCAT_PARAMS)
178 /* Check whether two file descriptors refer to the same file. */
180 extern int fdmatch (int fd1, int fd2);
182 /* Return the position of the first bit set in the argument. */
183 /* Prototypes vary from system to system, so we only provide a
184 prototype on systems where we know that we need it. */
185 #if defined (HAVE_DECL_FFS) && !HAVE_DECL_FFS
189 /* Get the working directory. The result is cached, so don't call
190 chdir() between calls to getpwd(). */
192 extern char * getpwd (void);
194 /* Get the current time. */
195 /* Prototypes vary from system to system, so we only provide a
196 prototype on systems where we know that we need it. */
198 /* Forward declaration to avoid #include <sys/time.h>. */
200 extern int gettimeofday (struct timeval *, void *);
203 /* Get the amount of time the process has run, in microseconds. */
205 extern long get_run_time (void);
207 /* Generate a relocated path to some installation directory. Allocates
208 return value using malloc. */
210 extern char *make_relative_prefix (const char *, const char *,
211 const char *) ATTRIBUTE_MALLOC;
213 /* Generate a relocated path to some installation directory without
214 attempting to follow any soft links. Allocates
215 return value using malloc. */
217 extern char *make_relative_prefix_ignore_links (const char *, const char *,
218 const char *) ATTRIBUTE_MALLOC;
220 /* Choose a temporary directory to use for scratch files. */
222 extern char *choose_temp_base (void) ATTRIBUTE_MALLOC;
224 /* Return a temporary file name or NULL if unable to create one. */
226 extern char *make_temp_file (const char *) ATTRIBUTE_MALLOC;
228 /* Remove a link to a file unless it is special. */
230 extern int unlink_if_ordinary (const char *);
232 /* Allocate memory filled with spaces. Allocates using malloc. */
234 extern const char *spaces (int count);
236 /* Return the maximum error number for which strerror will return a
239 extern int errno_max (void);
241 /* Return the name of an errno value (e.g., strerrno (EINVAL) returns
244 extern const char *strerrno (int);
246 /* Given the name of an errno value, return the value. */
248 extern int strtoerrno (const char *);
250 /* ANSI's strerror(), but more robust. */
252 extern char *xstrerror (int);
254 /* Return the maximum signal number for which strsignal will return a
257 extern int signo_max (void);
259 /* Return a signal message string for a signal number
260 (e.g., strsignal (SIGHUP) returns something like "Hangup"). */
261 /* This is commented out as it can conflict with one in system headers.
262 We still document its existence though. */
264 /*extern const char *strsignal (int);*/
266 /* Return the name of a signal number (e.g., strsigno (SIGHUP) returns
269 extern const char *strsigno (int);
271 /* Given the name of a signal, return its number. */
273 extern int strtosigno (const char *);
275 /* Register a function to be run by xexit. Returns 0 on success. */
277 extern int xatexit (void (*fn) (void));
279 /* Exit, calling all the functions registered with xatexit. */
281 extern void xexit (int status) ATTRIBUTE_NORETURN;
283 /* Set the program name used by xmalloc. */
285 extern void xmalloc_set_program_name (const char *);
287 /* Report an allocation failure. */
288 extern void xmalloc_failed (size_t) ATTRIBUTE_NORETURN;
290 /* Allocate memory without fail. If malloc fails, this will print a
291 message to stderr (using the name set by xmalloc_set_program_name,
292 if any) and then call xexit. */
294 extern void *xmalloc (size_t) ATTRIBUTE_MALLOC;
296 /* Reallocate memory without fail. This works like xmalloc. Note,
297 realloc type functions are not suitable for attribute malloc since
298 they may return the same address across multiple calls. */
300 extern void *xrealloc (void *, size_t);
302 /* Allocate memory without fail and set it to zero. This works like
305 extern void *xcalloc (size_t, size_t) ATTRIBUTE_MALLOC;
307 /* Copy a string into a memory buffer without fail. */
309 extern char *xstrdup (const char *) ATTRIBUTE_MALLOC;
311 /* Copy at most N characters from string into a buffer without fail. */
313 extern char *xstrndup (const char *, size_t) ATTRIBUTE_MALLOC;
315 /* Copy an existing memory buffer to a new memory buffer without fail. */
317 extern void *xmemdup (const void *, size_t, size_t) ATTRIBUTE_MALLOC;
319 /* Physical memory routines. Return values are in BYTES. */
320 extern double physmem_total (void);
321 extern double physmem_available (void);
324 /* These macros provide a K&R/C89/C++-friendly way of allocating structures
325 with nice encapsulation. The XDELETE*() macros are technically
326 superfluous, but provided here for symmetry. Using them consistently
327 makes it easier to update client code to use different allocators such
328 as new/delete and new[]/delete[]. */
330 /* Scalar allocators. */
332 #define XALLOCA(T) ((T *) alloca (sizeof (T)))
333 #define XNEW(T) ((T *) xmalloc (sizeof (T)))
334 #define XCNEW(T) ((T *) xcalloc (1, sizeof (T)))
335 #define XDUP(T, P) ((T *) xmemdup ((P), sizeof (T), sizeof (T)))
336 #define XDELETE(P) free ((void*) (P))
338 /* Array allocators. */
340 #define XALLOCAVEC(T, N) ((T *) alloca (sizeof (T) * (N)))
341 #define XNEWVEC(T, N) ((T *) xmalloc (sizeof (T) * (N)))
342 #define XCNEWVEC(T, N) ((T *) xcalloc ((N), sizeof (T)))
343 #define XDUPVEC(T, P, N) ((T *) xmemdup ((P), sizeof (T) * (N), sizeof (T) * (N)))
344 #define XRESIZEVEC(T, P, N) ((T *) xrealloc ((void *) (P), sizeof (T) * (N)))
345 #define XDELETEVEC(P) free ((void*) (P))
347 /* Allocators for variable-sized structures and raw buffers. */
349 #define XALLOCAVAR(T, S) ((T *) alloca ((S)))
350 #define XNEWVAR(T, S) ((T *) xmalloc ((S)))
351 #define XCNEWVAR(T, S) ((T *) xcalloc (1, (S)))
352 #define XDUPVAR(T, P, S1, S2) ((T *) xmemdup ((P), (S1), (S2)))
353 #define XRESIZEVAR(T, P, S) ((T *) xrealloc ((P), (S)))
355 /* Type-safe obstack allocator. */
357 #define XOBNEW(O, T) ((T *) obstack_alloc ((O), sizeof (T)))
358 #define XOBNEWVEC(O, T, N) ((T *) obstack_alloc ((O), sizeof (T) * (N)))
359 #define XOBNEWVAR(O, T, S) ((T *) obstack_alloc ((O), (S)))
360 #define XOBFINISH(O, T) ((T) obstack_finish ((O)))
362 /* hex character manipulation routines */
364 #define _hex_array_size 256
366 extern const unsigned char _hex_value[_hex_array_size];
367 extern void hex_init (void);
368 #define hex_p(c) (hex_value (c) != _hex_bad)
369 /* If you change this, note well: Some code relies on side effects in
370 the argument being performed exactly once. */
371 #define hex_value(c) ((unsigned int) _hex_value[(unsigned char) (c)])
373 /* Flags for pex_init. These are bits to be or'ed together. */
375 /* Record subprocess times, if possible. */
376 #define PEX_RECORD_TIMES 0x1
378 /* Use pipes for communication between processes, if possible. */
379 #define PEX_USE_PIPES 0x2
381 /* Save files used for communication between processes. */
382 #define PEX_SAVE_TEMPS 0x4
384 /* Prepare to execute one or more programs, with standard output of
385 each program fed to standard input of the next.
387 PNAME The name of the program to report in error messages.
388 TEMPBASE A base name to use for temporary files; may be NULL to
390 Returns NULL on error. */
392 extern struct pex_obj *pex_init (int flags, const char *pname,
393 const char *tempbase);
395 /* Flags for pex_run. These are bits to be or'ed together. */
397 /* Last program in pipeline. Standard output of program goes to
398 OUTNAME, or, if OUTNAME is NULL, to standard output of caller. Do
399 not set this if you want to call pex_read_output. After this is
400 set, pex_run may no longer be called with the same struct
404 /* Search for program in executable search path. */
405 #define PEX_SEARCH 0x2
407 /* OUTNAME is a suffix. */
408 #define PEX_SUFFIX 0x4
410 /* Send program's standard error to standard output. */
411 #define PEX_STDERR_TO_STDOUT 0x8
413 /* Input file should be opened in binary mode. This flag is ignored
415 #define PEX_BINARY_INPUT 0x10
417 /* Output file should be opened in binary mode. This flag is ignored
418 on Unix. For proper behaviour PEX_BINARY_INPUT and
419 PEX_BINARY_OUTPUT have to match appropriately--i.e., a call using
420 PEX_BINARY_OUTPUT should be followed by a call using
422 #define PEX_BINARY_OUTPUT 0x20
424 /* Capture stderr to a pipe. The output can be read by
425 calling pex_read_err and reading from the returned
426 FILE object. This flag may be specified only for
427 the last program in a pipeline.
429 This flag is supported only on Unix and Windows. */
430 #define PEX_STDERR_TO_PIPE 0x40
432 /* Capture stderr in binary mode. This flag is ignored
434 #define PEX_BINARY_ERROR 0x80
437 /* Execute one program. Returns NULL on success. On error returns an
438 error string (typically just the name of a system call); the error
439 string is statically allocated.
441 OBJ Returned by pex_init.
445 EXECUTABLE The program to execute.
447 ARGV NULL terminated array of arguments to pass to the program.
449 OUTNAME Sets the output file name as follows:
451 PEX_SUFFIX set (OUTNAME may not be NULL):
452 TEMPBASE parameter to pex_init not NULL:
453 Output file name is the concatenation of TEMPBASE
456 Output file name is a random file name ending in
460 Output file name is OUTNAME.
461 OUTNAME NULL, TEMPBASE not NULL:
462 Output file name is randomly chosen using
464 OUTNAME NULL, TEMPBASE NULL:
465 Output file name is randomly chosen.
467 If PEX_LAST is not set, the output file name is the
468 name to use for a temporary file holding stdout, if
469 any (there will not be a file if PEX_USE_PIPES is set
470 and the system supports pipes). If a file is used, it
471 will be removed when no longer needed unless
472 PEX_SAVE_TEMPS is set.
474 If PEX_LAST is set, and OUTNAME is not NULL, standard
475 output is written to the output file name. The file
476 will not be removed. If PEX_LAST and PEX_SUFFIX are
477 both set, TEMPBASE may not be NULL.
479 ERRNAME If not NULL, this is the name of a file to which
480 standard error is written. If NULL, standard error of
481 the program is standard error of the caller.
483 ERR On an error return, *ERR is set to an errno value, or
484 to 0 if there is no relevant errno.
487 extern const char *pex_run (struct pex_obj *obj, int flags,
488 const char *executable, char * const *argv,
489 const char *outname, const char *errname,
492 /* As for pex_run (), but takes an extra parameter to enable the
493 environment for the child process to be specified.
495 ENV The environment for the child process, specified as
496 an array of character pointers. Each element of the
497 array should point to a string of the form VAR=VALUE,
498 with the exception of the last element which must be
502 extern const char *pex_run_in_environment (struct pex_obj *obj, int flags,
503 const char *executable,
507 const char *errname, int *err);
509 /* Return a stream for a temporary file to pass to the first program
510 in the pipeline as input. The file name is chosen as for pex_run.
511 pex_run closes the file automatically; don't close it yourself. */
513 extern FILE *pex_input_file (struct pex_obj *obj, int flags,
514 const char *in_name);
516 /* Return a stream for a pipe connected to the standard input of the
517 first program in the pipeline. You must have passed
518 `PEX_USE_PIPES' to `pex_init'. Close the returned stream
521 extern FILE *pex_input_pipe (struct pex_obj *obj, int binary);
523 /* Read the standard output of the last program to be executed.
524 pex_run can not be called after this. BINARY should be non-zero if
525 the file should be opened in binary mode; this is ignored on Unix.
526 Returns NULL on error. Don't call fclose on the returned FILE; it
527 will be closed by pex_free. */
529 extern FILE *pex_read_output (struct pex_obj *, int binary);
531 /* Read the standard error of the last program to be executed.
532 pex_run can not be called after this. BINARY should be non-zero if
533 the file should be opened in binary mode; this is ignored on Unix.
534 Returns NULL on error. Don't call fclose on the returned FILE; it
535 will be closed by pex_free. */
537 extern FILE *pex_read_err (struct pex_obj *, int binary);
539 /* Return exit status of all programs in VECTOR. COUNT indicates the
540 size of VECTOR. The status codes in the vector are in the order of
541 the calls to pex_run. Returns 0 on error, 1 on success. */
543 extern int pex_get_status (struct pex_obj *, int count, int *vector);
545 /* Return times of all programs in VECTOR. COUNT indicates the size
546 of VECTOR. struct pex_time is really just struct timeval, but that
547 is not portable to all systems. Returns 0 on error, 1 on
552 unsigned long user_seconds;
553 unsigned long user_microseconds;
554 unsigned long system_seconds;
555 unsigned long system_microseconds;
558 extern int pex_get_times (struct pex_obj *, int count,
559 struct pex_time *vector);
561 /* Clean up a pex_obj. If you have not called pex_get_times or
562 pex_get_status, this will try to kill the subprocesses. */
564 extern void pex_free (struct pex_obj *);
566 /* Just execute one program. Return value is as for pex_run.
567 FLAGS Combination of PEX_SEARCH and PEX_STDERR_TO_STDOUT.
568 EXECUTABLE As for pex_run.
570 PNAME As for pex_init.
571 OUTNAME As for pex_run when PEX_LAST is set.
572 ERRNAME As for pex_run.
573 STATUS Set to exit status on success.
577 extern const char *pex_one (int flags, const char *executable,
578 char * const *argv, const char *pname,
579 const char *outname, const char *errname,
580 int *status, int *err);
582 /* pexecute and pwait are the old pexecute interface, still here for
583 backward compatibility. Don't use these for new code. Instead,
584 use pex_init/pex_run/pex_get_status/pex_free, or pex_one. */
586 /* Definitions used by the pexecute routine. */
588 #define PEXECUTE_FIRST 1
589 #define PEXECUTE_LAST 2
590 #define PEXECUTE_ONE (PEXECUTE_FIRST + PEXECUTE_LAST)
591 #define PEXECUTE_SEARCH 4
592 #define PEXECUTE_VERBOSE 8
594 /* Execute a program. */
596 extern int pexecute (const char *, char * const *, const char *,
597 const char *, char **, char **, int);
599 /* Wait for pexecute to finish. */
601 extern int pwait (int, int *, int);
603 #if !HAVE_DECL_ASPRINTF
604 /* Like sprintf but provides a pointer to malloc'd storage, which must
605 be freed by the caller. */
607 extern int asprintf (char **, const char *, ...) ATTRIBUTE_PRINTF_2;
610 #if !HAVE_DECL_VASPRINTF
611 /* Like vsprintf but provides a pointer to malloc'd storage, which
612 must be freed by the caller. */
614 extern int vasprintf (char **, const char *, va_list) ATTRIBUTE_PRINTF(2,0);
617 #if defined(HAVE_DECL_SNPRINTF) && !HAVE_DECL_SNPRINTF
618 /* Like sprintf but prints at most N characters. */
619 extern int snprintf (char *, size_t, const char *, ...) ATTRIBUTE_PRINTF_3;
622 #if defined(HAVE_DECL_VSNPRINTF) && !HAVE_DECL_VSNPRINTF
623 /* Like vsprintf but prints at most N characters. */
624 extern int vsnprintf (char *, size_t, const char *, va_list) ATTRIBUTE_PRINTF(3,0);
627 #if defined(HAVE_DECL_STRVERSCMP) && !HAVE_DECL_STRVERSCMP
628 /* Compare version strings. */
629 extern int strverscmp (const char *, const char *);
632 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
634 /* Drastically simplified alloca configurator. If we're using GCC,
635 we use __builtin_alloca; otherwise we use the C alloca. The C
636 alloca is always available. You can override GCC by defining
637 USE_C_ALLOCA yourself. The canonical autoconf macro C_ALLOCA is
638 also set/unset as it is often used to indicate whether code needs
639 to call alloca(0). */
640 extern void *C_alloca (size_t) ATTRIBUTE_MALLOC;
642 #if GCC_VERSION >= 2000 && !defined USE_C_ALLOCA
643 # define alloca(x) __builtin_alloca(x)
645 # define ASTRDUP(X) \
646 (__extension__ ({ const char *const libiberty_optr = (X); \
647 const unsigned long libiberty_len = strlen (libiberty_optr) + 1; \
648 char *const libiberty_nptr = (char *const) alloca (libiberty_len); \
649 (char *) memcpy (libiberty_nptr, libiberty_optr, libiberty_len); }))
651 # define alloca(x) C_alloca(x)
653 # define USE_C_ALLOCA 1
656 extern const char *libiberty_optr;
657 extern char *libiberty_nptr;
658 extern unsigned long libiberty_len;
659 # define ASTRDUP(X) \
660 (libiberty_optr = (X), \
661 libiberty_len = strlen (libiberty_optr) + 1, \
662 libiberty_nptr = (char *) alloca (libiberty_len), \
663 (char *) memcpy (libiberty_nptr, libiberty_optr, libiberty_len))
671 #endif /* ! defined (LIBIBERTY_H) */