Fix some memory leaks, found with valgrind.
[make.git] / make.h
blob60d141293a0007bcce0ed519fa7ef1bd89ece715
1 /* Miscellaneous global declarations and portability cruft for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
4 Foundation, Inc.
5 This file is part of GNU Make.
7 GNU Make is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 3 of the License, or (at your option) any later
10 version.
12 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along with
17 this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* We use <config.h> instead of "config.h" so that a compilation
20 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
21 (which it would do because make.h was found in $srcdir). */
22 #include <config.h>
23 #undef HAVE_CONFIG_H
24 #define HAVE_CONFIG_H 1
26 /* AIX requires this to be the first thing in the file. */
27 #ifndef __GNUC__
28 # if HAVE_ALLOCA_H
29 # include <alloca.h>
30 # else
31 # ifdef _AIX
32 #pragma alloca
33 # else
34 # ifndef alloca /* predefined by HP cc +Olibcalls */
35 char *alloca ();
36 # endif
37 # endif
38 # endif
39 #endif
42 /* Specify we want GNU source code. This must be defined before any
43 system headers are included. */
45 #define _GNU_SOURCE 1
48 #ifdef CRAY
49 /* This must happen before #include <signal.h> so
50 that the declaration therein is changed. */
51 # define signal bsdsignal
52 #endif
54 /* If we're compiling for the dmalloc debugger, turn off string inlining. */
55 #if defined(HAVE_DMALLOC_H) && defined(__GNUC__)
56 # define __NO_STRING_INLINES
57 #endif
59 #include <sys/types.h>
60 #include <sys/stat.h>
61 #include <signal.h>
62 #include <stdio.h>
63 #include <ctype.h>
64 #ifdef HAVE_SYS_TIMEB_H
65 /* SCO 3.2 "devsys 4.2" has a prototype for `ftime' in <time.h> that bombs
66 unless <sys/timeb.h> has been included first. Does every system have a
67 <sys/timeb.h>? If any does not, configure should check for it. */
68 # include <sys/timeb.h>
69 #endif
71 #if TIME_WITH_SYS_TIME
72 # include <sys/time.h>
73 # include <time.h>
74 #else
75 # if HAVE_SYS_TIME_H
76 # include <sys/time.h>
77 # else
78 # include <time.h>
79 # endif
80 #endif
82 #include <errno.h>
84 #ifndef errno
85 extern int errno;
86 #endif
88 #ifndef isblank
89 # define isblank(c) ((c) == ' ' || (c) == '\t')
90 #endif
92 #ifdef HAVE_UNISTD_H
93 # include <unistd.h>
94 /* Ultrix's unistd.h always defines _POSIX_VERSION, but you only get
95 POSIX.1 behavior with `cc -YPOSIX', which predefines POSIX itself! */
96 # if defined (_POSIX_VERSION) && !defined (ultrix) && !defined (VMS)
97 # define POSIX 1
98 # endif
99 #endif
101 /* Some systems define _POSIX_VERSION but are not really POSIX.1. */
102 #if (defined (butterfly) || defined (__arm) || (defined (__mips) && defined (_SYSTYPE_SVR3)) || (defined (sequent) && defined (i386)))
103 # undef POSIX
104 #endif
106 #if !defined (POSIX) && defined (_AIX) && defined (_POSIX_SOURCE)
107 # define POSIX 1
108 #endif
110 #ifndef RETSIGTYPE
111 # define RETSIGTYPE void
112 #endif
114 #ifndef sigmask
115 # define sigmask(sig) (1 << ((sig) - 1))
116 #endif
118 #ifndef HAVE_SA_RESTART
119 # define SA_RESTART 0
120 #endif
122 #ifdef HAVE_LIMITS_H
123 # include <limits.h>
124 #endif
125 #ifdef HAVE_SYS_PARAM_H
126 # include <sys/param.h>
127 #endif
129 #ifndef PATH_MAX
130 # ifndef POSIX
131 # define PATH_MAX MAXPATHLEN
132 # endif
133 #endif
134 #ifndef MAXPATHLEN
135 # define MAXPATHLEN 1024
136 #endif
138 #ifdef PATH_MAX
139 # define GET_PATH_MAX PATH_MAX
140 # define PATH_VAR(var) char var[PATH_MAX]
141 #else
142 # define NEED_GET_PATH_MAX 1
143 # define GET_PATH_MAX (get_path_max ())
144 # define PATH_VAR(var) char *var = alloca (GET_PATH_MAX)
145 unsigned int get_path_max (void);
146 #endif
148 #ifndef CHAR_BIT
149 # define CHAR_BIT 8
150 #endif
152 /* Nonzero if the integer type T is signed. */
153 #define INTEGER_TYPE_SIGNED(t) ((t) -1 < 0)
155 /* The minimum and maximum values for the integer type T.
156 Use ~ (t) 0, not -1, for portability to 1's complement hosts. */
157 #define INTEGER_TYPE_MINIMUM(t) \
158 (! INTEGER_TYPE_SIGNED (t) ? (t) 0 : ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))
159 #define INTEGER_TYPE_MAXIMUM(t) (~ (t) 0 - INTEGER_TYPE_MINIMUM (t))
161 #ifndef CHAR_MAX
162 # define CHAR_MAX INTEGER_TYPE_MAXIMUM (char)
163 #endif
165 #ifdef STAT_MACROS_BROKEN
166 # ifdef S_ISREG
167 # undef S_ISREG
168 # endif
169 # ifdef S_ISDIR
170 # undef S_ISDIR
171 # endif
172 #endif /* STAT_MACROS_BROKEN. */
174 #ifndef S_ISREG
175 # define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
176 #endif
177 #ifndef S_ISDIR
178 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
179 #endif
181 #ifdef VMS
182 # include <types.h>
183 # include <unixlib.h>
184 # include <unixio.h>
185 # include <perror.h>
186 /* Needed to use alloca on VMS. */
187 # include <builtins.h>
188 #endif
190 #ifndef __attribute__
191 /* This feature is available in gcc versions 2.5 and later. */
192 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
193 # define __attribute__(x)
194 # endif
195 /* The __-protected variants of `format' and `printf' attributes
196 are accepted by gcc versions 2.6.4 (effectively 2.7) and later. */
197 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
198 # define __format__ format
199 # define __printf__ printf
200 # endif
201 #endif
202 #define UNUSED __attribute__ ((unused))
204 #if defined (STDC_HEADERS) || defined (__GNU_LIBRARY__)
205 # include <stdlib.h>
206 # include <string.h>
207 # define ANSI_STRING 1
208 #else /* No standard headers. */
209 # ifdef HAVE_STRING_H
210 # include <string.h>
211 # define ANSI_STRING 1
212 # else
213 # include <strings.h>
214 # endif
215 # ifdef HAVE_MEMORY_H
216 # include <memory.h>
217 # endif
218 # ifdef HAVE_STDLIB_H
219 # include <stdlib.h>
220 # else
221 void *malloc (int);
222 void *realloc (void *, int);
223 void free (void *);
225 void abort (void) __attribute__ ((noreturn));
226 void exit (int) __attribute__ ((noreturn));
227 # endif /* HAVE_STDLIB_H. */
229 #endif /* Standard headers. */
231 /* These should be in stdlib.h. Make sure we have them. */
232 #ifndef EXIT_SUCCESS
233 # define EXIT_SUCCESS 0
234 #endif
235 #ifndef EXIT_FAILURE
236 # define EXIT_FAILURE 1
237 #endif
239 #ifndef ANSI_STRING
241 /* SCO Xenix has a buggy macro definition in <string.h>. */
242 #undef strerror
243 #if !defined(__DECC)
244 char *strerror (int errnum);
245 #endif
247 #endif /* !ANSI_STRING. */
248 #undef ANSI_STRING
250 #if HAVE_INTTYPES_H
251 # include <inttypes.h>
252 #endif
253 #define FILE_TIMESTAMP uintmax_t
255 #if !defined(HAVE_STRSIGNAL)
256 char *strsignal (int signum);
257 #endif
259 /* ISDIGIT offers the following features:
260 - Its arg may be any int or unsigned int; it need not be an unsigned char.
261 - It's guaranteed to evaluate its argument exactly once.
262 NOTE! Make relies on this behavior, don't change it!
263 - It's typically faster.
264 POSIX 1003.2-1992 section 2.5.2.1 page 50 lines 1556-1558 says that
265 only '0' through '9' are digits. Prefer ISDIGIT to isdigit() unless
266 it's important to use the locale's definition of `digit' even when the
267 host does not conform to POSIX. */
268 #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
270 #ifndef iAPX286
271 # define streq(a, b) \
272 ((a) == (b) || \
273 (*(a) == *(b) && (*(a) == '\0' || !strcmp ((a) + 1, (b) + 1))))
274 # ifdef HAVE_CASE_INSENSITIVE_FS
275 # define strieq(a, b) \
276 ((a) == (b) \
277 || (tolower((unsigned char)*(a)) == tolower((unsigned char)*(b)) \
278 && (*(a) == '\0' || !strcasecmp ((a) + 1, (b) + 1))))
279 # else
280 # define strieq(a, b) streq(a, b)
281 # endif
282 #else
283 /* Buggy compiler can't handle this. */
284 # define streq(a, b) (strcmp ((a), (b)) == 0)
285 # define strieq(a, b) (strcmp ((a), (b)) == 0)
286 #endif
287 #define strneq(a, b, l) (strncmp ((a), (b), (l)) == 0)
289 #if defined(__GNUC__) || defined(ENUM_BITFIELDS)
290 # define ENUM_BITFIELD(bits) :bits
291 #else
292 # define ENUM_BITFIELD(bits)
293 #endif
295 /* Handle gettext and locales. */
297 #if HAVE_LOCALE_H
298 # include <locale.h>
299 #else
300 # define setlocale(category, locale)
301 #endif
303 #include <gettext.h>
305 #define _(msgid) gettext (msgid)
306 #define N_(msgid) gettext_noop (msgid)
307 #define S_(msg1,msg2,num) ngettext (msg1,msg2,num)
309 /* Handle other OSs. */
310 #ifndef PATH_SEPARATOR_CHAR
311 # if defined(HAVE_DOS_PATHS)
312 # define PATH_SEPARATOR_CHAR ';'
313 # elif defined(VMS)
314 # define PATH_SEPARATOR_CHAR ','
315 # else
316 # define PATH_SEPARATOR_CHAR ':'
317 # endif
318 #endif
320 /* This is needed for getcwd() and chdir(), on some W32 systems. */
321 #if defined(HAVE_DIRECT_H)
322 # include <direct.h>
323 #endif
325 #ifdef WINDOWS32
326 # include <fcntl.h>
327 # include <malloc.h>
328 # define pipe(_p) _pipe((_p), 512, O_BINARY)
329 # define kill(_pid,_sig) w32_kill((_pid),(_sig))
331 void sync_Path_environment (void);
332 int w32_kill (int pid, int sig);
333 char *end_of_token_w32 (const char *s, char stopchar);
334 int find_and_set_default_shell (const char *token);
336 /* indicates whether or not we have Bourne shell */
337 extern int no_default_sh_exe;
339 /* is default_shell unixy? */
340 extern int unixy_shell;
341 #endif /* WINDOWS32 */
343 #if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT)
344 # define SET_STACK_SIZE
345 #endif
346 #ifdef SET_STACK_SIZE
347 # include <sys/resource.h>
348 struct rlimit stack_limit;
349 #endif
351 struct floc
353 const char *filenm;
354 unsigned long lineno;
356 #define NILF ((struct floc *)0)
358 #define STRING_SIZE_TUPLE(_s) (_s), (sizeof (_s)-1)
361 /* We have to have stdarg.h or varargs.h AND v*printf or doprnt to use
362 variadic versions of these functions. */
364 #if HAVE_STDARG_H || HAVE_VARARGS_H
365 # if HAVE_VPRINTF || HAVE_DOPRNT
366 # define USE_VARIADIC 1
367 # endif
368 #endif
370 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
371 const char *concat (unsigned int, ...);
372 void message (int prefix, const char *fmt, ...)
373 __attribute__ ((__format__ (__printf__, 2, 3)));
374 void error (const struct floc *flocp, const char *fmt, ...)
375 __attribute__ ((__format__ (__printf__, 2, 3)));
376 void fatal (const struct floc *flocp, const char *fmt, ...)
377 __attribute__ ((noreturn, __format__ (__printf__, 2, 3)));
378 #else
379 const char *concat ();
380 void message ();
381 void error ();
382 void fatal ();
383 #endif
385 void die (int) __attribute__ ((noreturn));
386 void log_working_directory (int);
387 void pfatal_with_name (const char *) __attribute__ ((noreturn));
388 void perror_with_name (const char *, const char *);
389 void *xmalloc (unsigned int);
390 void *xcalloc (unsigned int);
391 void *xrealloc (void *, unsigned int);
392 char *xstrdup (const char *);
393 char *xstrndup (const char *, unsigned int);
394 char *find_next_token (const char **, unsigned int *);
395 char *next_token (const char *);
396 char *end_of_token (const char *);
397 void collapse_continuations (char *);
398 char *lindex (const char *, const char *, int);
399 int alpha_compare (const void *, const void *);
400 void print_spaces (unsigned int);
401 char *find_percent (char *);
402 const char *find_percent_cached (const char **);
403 FILE *open_tmpfile (char **, const char *);
405 #ifndef NO_ARCHIVES
406 int ar_name (const char *);
407 void ar_parse_name (const char *, char **, char **);
408 int ar_touch (const char *);
409 time_t ar_member_date (const char *);
411 typedef long int (*ar_member_func_t) (int desc, const char *mem, int truncated,
412 long int hdrpos, long int datapos,
413 long int size, long int date, int uid,
414 int gid, int mode, const void *arg);
416 long int ar_scan (const char *archive, ar_member_func_t function, const void *arg);
417 int ar_name_equal (const char *name, const char *mem, int truncated);
418 #ifndef VMS
419 int ar_member_touch (const char *arname, const char *memname);
420 #endif
421 #endif
423 int dir_file_exists_p (const char *, const char *);
424 int file_exists_p (const char *);
425 int file_impossible_p (const char *);
426 void file_impossible (const char *);
427 const char *dir_name (const char *);
428 void hash_init_directories (void);
430 void define_default_variables (void);
431 void set_default_suffixes (void);
432 void install_default_suffix_rules (void);
433 void install_default_implicit_rules (void);
435 void build_vpath_lists (void);
436 void construct_vpath_list (char *pattern, char *dirpath);
437 const char *vpath_search (const char *file, FILE_TIMESTAMP *mtime_ptr);
438 int gpath_search (const char *file, unsigned int len);
440 void construct_include_path (const char **arg_dirs);
442 void user_access (void);
443 void make_access (void);
444 void child_access (void);
446 void close_stdout (void);
448 char *strip_whitespace (const char **begpp, const char **endpp);
450 /* String caching */
451 void strcache_init (void);
452 void strcache_print_stats (const char *prefix);
453 int strcache_iscached (const char *str);
454 const char *strcache_add (const char *str);
455 const char *strcache_add_len (const char *str, int len);
456 int strcache_setbufsize (int size);
458 #ifdef HAVE_VFORK_H
459 # include <vfork.h>
460 #endif
462 /* We omit these declarations on non-POSIX systems which define _POSIX_VERSION,
463 because such systems often declare them in header files anyway. */
465 #if !defined (__GNU_LIBRARY__) && !defined (POSIX) && !defined (_POSIX_VERSION) && !defined(WINDOWS32)
467 long int atol ();
468 # ifndef VMS
469 long int lseek ();
470 # endif
472 #endif /* Not GNU C library or POSIX. */
474 #ifdef HAVE_GETCWD
475 # if !defined(VMS) && !defined(__DECC)
476 char *getcwd ();
477 # endif
478 #else
479 char *getwd ();
480 # define getcwd(buf, len) getwd (buf)
481 #endif
483 #if !HAVE_STRCASECMP
484 # if HAVE_STRICMP
485 # define strcasecmp stricmp
486 # elif HAVE_STRCMPI
487 # define strcasecmp strcmpi
488 # else
489 /* Create our own, in misc.c */
490 int strcasecmp (const char *s1, const char *s2);
491 # endif
492 #endif
494 extern const struct floc *reading_file;
495 extern const struct floc **expanding_var;
497 extern char **environ;
499 extern int just_print_flag, silent_flag, ignore_errors_flag, keep_going_flag;
500 extern int print_data_base_flag, question_flag, touch_flag, always_make_flag;
501 extern int env_overrides, no_builtin_rules_flag, no_builtin_variables_flag;
502 extern int print_version_flag, print_directory_flag, check_symlink_flag;
503 extern int warn_undefined_variables_flag, posix_pedantic, not_parallel;
504 extern int second_expansion, clock_skew_detected, rebuilding_makefiles;
506 /* can we run commands via 'sh -c xxx' or must we use batch files? */
507 extern int batch_mode_shell;
509 /* Resetting the command script introduction prefix character. */
510 #define RECIPEPREFIX_NAME ".RECIPEPREFIX"
511 #define RECIPEPREFIX_DEFAULT '\t'
512 extern char cmd_prefix;
514 extern unsigned int job_slots;
515 extern int job_fds[2];
516 extern int job_rfd;
517 #ifndef NO_FLOAT
518 extern double max_load_average;
519 #else
520 extern int max_load_average;
521 #endif
523 extern char *program;
524 extern char *starting_directory;
525 extern unsigned int makelevel;
526 extern char *version_string, *remote_description, *make_host;
528 extern unsigned int commands_started;
530 extern int handling_fatal_signal;
533 #ifndef MIN
534 #define MIN(_a,_b) ((_a)<(_b)?(_a):(_b))
535 #endif
536 #ifndef MAX
537 #define MAX(_a,_b) ((_a)>(_b)?(_a):(_b))
538 #endif
540 #ifdef VMS
541 # define MAKE_SUCCESS 1
542 # define MAKE_TROUBLE 2
543 # define MAKE_FAILURE 3
544 #else
545 # define MAKE_SUCCESS 0
546 # define MAKE_TROUBLE 1
547 # define MAKE_FAILURE 2
548 #endif
550 /* Set up heap debugging library dmalloc. */
552 #ifdef HAVE_DMALLOC_H
553 #include <dmalloc.h>
554 #endif
556 #ifndef initialize_main
557 # ifdef __EMX__
558 # define initialize_main(pargc, pargv) \
559 { _wildcard(pargc, pargv); _response(pargc, pargv); }
560 # else
561 # define initialize_main(pargc, pargv)
562 # endif
563 #endif
565 #ifdef __EMX__
566 # if !defined chdir
567 # define chdir _chdir2
568 # endif
569 # if !defined getcwd
570 # define getcwd _getcwd2
571 # endif
573 /* NO_CHDIR2 causes make not to use _chdir2() and _getcwd2() instead of
574 chdir() and getcwd(). This avoids some error messages for the
575 make testsuite but restricts the drive letter support. */
576 # ifdef NO_CHDIR2
577 # warning NO_CHDIR2: usage of drive letters restricted
578 # undef chdir
579 # undef getcwd
580 # endif
581 #endif
583 #ifndef initialize_main
584 # define initialize_main(pargc, pargv)
585 #endif
588 /* Some systems (like Solaris, PTX, etc.) do not support the SA_RESTART flag
589 properly according to POSIX. So, we try to wrap common system calls with
590 checks for EINTR. Note that there are still plenty of system calls that
591 can fail with EINTR but this, reportedly, gets the vast majority of
592 failure cases. If you still experience failures you'll need to either get
593 a system where SA_RESTART works, or you need to avoid -j. */
595 #define EINTRLOOP(_v,_c) while (((_v)=_c)==-1 && errno==EINTR)
597 /* While system calls that return integers are pretty consistent about
598 returning -1 on failure and setting errno in that case, functions that
599 return pointers are not always so well behaved. Sometimes they return
600 NULL for expected behavior: one good example is readdir() which returns
601 NULL at the end of the directory--and _doesn't_ reset errno. So, we have
602 to do it ourselves here. */
604 #define ENULLLOOP(_v,_c) do { errno = 0; (_v) = _c; } \
605 while((_v)==0 && errno==EINTR)