1/* Get common system includes and various definitions and declarations based
2   on autoconf macros.
3   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
4   Free Software Foundation, Inc.
5
6This file is part of libcpp (aka cpplib).
7
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 2, or (at your option) any later
11version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING.  If not, write to the Free
20Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
2102110-1301, USA.  */
22
23
24#ifndef LIBCPP_SYSTEM_H
25#define LIBCPP_SYSTEM_H
26
27/* We must include stdarg.h before stdio.h.  */
28#include <stdarg.h>
29
30#ifdef HAVE_STDDEF_H
31# include <stddef.h>
32#endif
33
34#include <stdio.h>
35
36/* Define a generic NULL if one hasn't already been defined.  */
37#ifndef NULL
38#define NULL 0
39#endif
40
41/* Use the unlocked open routines from libiberty.  */
42#define fopen(PATH,MODE) fopen_unlocked(PATH,MODE)
43#define fdopen(FILDES,MODE) fdopen_unlocked(FILDES,MODE)
44#define freopen(PATH,MODE,STREAM) freopen_unlocked(PATH,MODE,STREAM)
45
46/* The compiler is not a multi-threaded application and therefore we
47   do not have to use the locking functions.  In fact, using the locking
48   functions can cause the compiler to be significantly slower under
49   I/O bound conditions (such as -g -O0 on very large source files).
50
51   HAVE_DECL_PUTC_UNLOCKED actually indicates whether or not the stdio
52   code is multi-thread safe by default.  If it is set to 0, then do
53   not worry about using the _unlocked functions.
54
55   fputs_unlocked, fwrite_unlocked, and fprintf_unlocked are
56   extensions and need to be prototyped by hand (since we do not
57   define _GNU_SOURCE).  */
58
59#if defined HAVE_DECL_PUTC_UNLOCKED && HAVE_DECL_PUTC_UNLOCKED
60
61# ifdef HAVE_PUTC_UNLOCKED
62#  undef putc
63#  define putc(C, Stream) putc_unlocked (C, Stream)
64# endif
65# ifdef HAVE_PUTCHAR_UNLOCKED
66#  undef putchar
67#  define putchar(C) putchar_unlocked (C)
68# endif
69# ifdef HAVE_GETC_UNLOCKED
70#  undef getc
71#  define getc(Stream) getc_unlocked (Stream)
72# endif
73# ifdef HAVE_GETCHAR_UNLOCKED
74#  undef getchar
75#  define getchar() getchar_unlocked ()
76# endif
77# ifdef HAVE_FPUTC_UNLOCKED
78#  undef fputc
79#  define fputc(C, Stream) fputc_unlocked (C, Stream)
80# endif
81
82# ifdef HAVE_CLEARERR_UNLOCKED
83#  undef clearerr
84#  define clearerr(Stream) clearerr_unlocked (Stream)
85#  if defined (HAVE_DECL_CLEARERR_UNLOCKED) && !HAVE_DECL_CLEARERR_UNLOCKED
86extern void clearerr_unlocked (FILE *);
87#  endif
88# endif
89# ifdef HAVE_FEOF_UNLOCKED
90#  undef feof
91#  define feof(Stream) feof_unlocked (Stream)
92#  if defined (HAVE_DECL_FEOF_UNLOCKED) && !HAVE_DECL_FEOF_UNLOCKED
93extern int feof_unlocked (FILE *);
94#  endif
95# endif
96# ifdef HAVE_FILENO_UNLOCKED
97#  undef fileno
98#  define fileno(Stream) fileno_unlocked (Stream)
99#  if defined (HAVE_DECL_FILENO_UNLOCKED) && !HAVE_DECL_FILENO_UNLOCKED
100extern int fileno_unlocked (FILE *);
101#  endif
102# endif
103# ifdef HAVE_FFLUSH_UNLOCKED
104#  undef fflush
105#  define fflush(Stream) fflush_unlocked (Stream)
106#  if defined (HAVE_DECL_FFLUSH_UNLOCKED) && !HAVE_DECL_FFLUSH_UNLOCKED
107extern int fflush_unlocked (FILE *);
108#  endif
109# endif
110# ifdef HAVE_FGETC_UNLOCKED
111#  undef fgetc
112#  define fgetc(Stream) fgetc_unlocked (Stream)
113#  if defined (HAVE_DECL_FGETC_UNLOCKED) && !HAVE_DECL_FGETC_UNLOCKED
114extern int fgetc_unlocked (FILE *);
115#  endif
116# endif
117# ifdef HAVE_FGETS_UNLOCKED
118#  undef fgets
119#  define fgets(S, n, Stream) fgets_unlocked (S, n, Stream)
120#  if defined (HAVE_DECL_FGETS_UNLOCKED) && !HAVE_DECL_FGETS_UNLOCKED
121extern char *fgets_unlocked (char *, int, FILE *);
122#  endif
123# endif
124# ifdef HAVE_FPUTS_UNLOCKED
125#  undef fputs
126#  define fputs(String, Stream) fputs_unlocked (String, Stream)
127#  if defined (HAVE_DECL_FPUTS_UNLOCKED) && !HAVE_DECL_FPUTS_UNLOCKED
128extern int fputs_unlocked (const char *, FILE *);
129#  endif
130# endif
131# ifdef HAVE_FERROR_UNLOCKED
132#  undef ferror
133#  define ferror(Stream) ferror_unlocked (Stream)
134#  if defined (HAVE_DECL_FERROR_UNLOCKED) && !HAVE_DECL_FERROR_UNLOCKED
135extern int ferror_unlocked (FILE *);
136#  endif
137# endif
138# ifdef HAVE_FREAD_UNLOCKED
139#  undef fread
140#  define fread(Ptr, Size, N, Stream) fread_unlocked (Ptr, Size, N, Stream)
141#  if defined (HAVE_DECL_FREAD_UNLOCKED) && !HAVE_DECL_FREAD_UNLOCKED
142extern size_t fread_unlocked (void *, size_t, size_t, FILE *);
143#  endif
144# endif
145# ifdef HAVE_FWRITE_UNLOCKED
146#  undef fwrite
147#  define fwrite(Ptr, Size, N, Stream) fwrite_unlocked (Ptr, Size, N, Stream)
148#  if defined (HAVE_DECL_FWRITE_UNLOCKED) && !HAVE_DECL_FWRITE_UNLOCKED
149extern size_t fwrite_unlocked (const void *, size_t, size_t, FILE *);
150#  endif
151# endif
152# ifdef HAVE_FPRINTF_UNLOCKED
153#  undef fprintf
154/* We can't use a function-like macro here because we don't know if
155   we have varargs macros.  */
156#  define fprintf fprintf_unlocked
157#  if defined (HAVE_DECL_FPRINTF_UNLOCKED) && !HAVE_DECL_FPRINTF_UNLOCKED
158extern int fprintf_unlocked (FILE *, const char *, ...);
159#  endif
160# endif
161
162#endif
163
164/* ??? Glibc's fwrite/fread_unlocked macros cause
165   "warning: signed and unsigned type in conditional expression".  */
166#undef fread_unlocked
167#undef fwrite_unlocked
168
169#include <sys/types.h>
170#include <errno.h>
171
172#if !defined (errno) && defined (HAVE_DECL_ERRNO) && !HAVE_DECL_ERRNO
173extern int errno;
174#endif
175
176/* Some of glibc's string inlines cause warnings.  Plus we'd rather
177   rely on (and therefore test) GCC's string builtins.  */
178#define __NO_STRING_INLINES
179
180#ifdef STRING_WITH_STRINGS
181# include <string.h>
182# include <strings.h>
183#else
184# ifdef HAVE_STRING_H
185#  include <string.h>
186# else
187#  ifdef HAVE_STRINGS_H
188#   include <strings.h>
189#  endif
190# endif
191#endif
192
193#ifdef HAVE_STDLIB_H
194# include <stdlib.h>
195#endif
196
197#ifdef HAVE_UNISTD_H
198# include <unistd.h>
199#endif
200
201#if HAVE_LIMITS_H
202# include <limits.h>
203#endif
204
205/* Infrastructure for defining missing _MAX and _MIN macros.  Note that
206   macros defined with these cannot be used in #if.  */
207
208/* The extra casts work around common compiler bugs.  */
209#define INTTYPE_SIGNED(t) (! ((t) 0 < (t) -1))
210/* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
211   It is necessary at least when t == time_t.  */
212#define INTTYPE_MINIMUM(t) ((t) (INTTYPE_SIGNED (t) \
213                             ? ~ (t) 0 << (sizeof(t) * CHAR_BIT - 1) : (t) 0))
214#define INTTYPE_MAXIMUM(t) ((t) (~ (t) 0 - INTTYPE_MINIMUM (t)))
215
216/* Use that infrastructure to provide a few constants.  */
217#ifndef UCHAR_MAX
218# define UCHAR_MAX INTTYPE_MAXIMUM (unsigned char)
219#endif
220
221#ifdef TIME_WITH_SYS_TIME
222# include <sys/time.h>
223# include <time.h>
224#else
225# if HAVE_SYS_TIME_H
226#  include <sys/time.h>
227# else
228#  ifdef HAVE_TIME_H
229#   include <time.h>
230#  endif
231# endif
232#endif
233
234#ifdef HAVE_FCNTL_H
235# include <fcntl.h>
236#else
237# ifdef HAVE_SYS_FILE_H
238#  include <sys/file.h>
239# endif
240#endif
241
242#ifdef HAVE_LOCALE_H
243# include <locale.h>
244#endif
245
246#ifdef HAVE_LANGINFO_CODESET
247# include <langinfo.h>
248#endif
249
250#ifndef HAVE_SETLOCALE
251# define setlocale(category, locale) (locale)
252#endif
253
254#ifdef ENABLE_NLS
255#include <libintl.h>
256#else
257/* Stubs.  */
258# undef dgettext
259# define dgettext(package, msgid) (msgid)
260#endif
261
262#ifndef _
263# define _(msgid) dgettext (PACKAGE, msgid)
264#endif
265
266#ifndef N_
267# define N_(msgid) msgid
268#endif
269
270/* Some systems define these in, e.g., param.h.  We undefine these names
271   here to avoid the warnings.  We prefer to use our definitions since we
272   know they are correct.  */
273
274#undef MIN
275#undef MAX
276#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
277#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
278
279/* The HAVE_DECL_* macros are three-state, undefined, 0 or 1.  If they
280   are defined to 0 then we must provide the relevant declaration
281   here.  These checks will be in the undefined state while configure
282   is running so be careful to test "defined (HAVE_DECL_*)".  */
283
284#if defined (HAVE_DECL_ABORT) && !HAVE_DECL_ABORT
285extern void abort (void);
286#endif
287
288#if HAVE_SYS_STAT_H
289# include <sys/stat.h>
290#endif
291
292/* Test if something is a normal file.  */
293#ifndef S_ISREG
294#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
295#endif
296
297/* Test if something is a directory.  */
298#ifndef S_ISDIR
299#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
300#endif
301
302/* Test if something is a character special file.  */
303#ifndef S_ISCHR
304#define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
305#endif
306
307/* Test if something is a block special file.  */
308#ifndef S_ISBLK
309#define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
310#endif
311
312/* Test if something is a socket.  */
313#ifndef S_ISSOCK
314# ifdef S_IFSOCK
315#   define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
316# else
317#   define S_ISSOCK(m) 0
318# endif
319#endif
320
321/* Test if something is a FIFO.  */
322#ifndef S_ISFIFO
323# ifdef S_IFIFO
324#  define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
325# else
326#  define S_ISFIFO(m) 0
327# endif
328#endif
329
330/* Approximate O_NOCTTY and O_BINARY.  */
331#ifndef O_NOCTTY
332#define O_NOCTTY 0
333#endif
334#ifndef O_BINARY
335# define O_BINARY 0
336#endif
337
338/* Filename handling macros.  */
339#include "filenames.h"
340
341/* Get libiberty declarations.  */
342#include "libiberty.h"
343#include "safe-ctype.h"
344
345/* 1 if we have C99 designated initializers.
346
347   ??? C99 designated initializers are not supported by most C++
348   compilers, including G++.  -- gdr, 2005-05-18  */
349#if !defined(HAVE_DESIGNATED_INITIALIZERS)
350# if (!defined(__cplusplus) && (GCC_VERSION >= 2007)) \
351     ||(__STDC_VERSION__ >= 199901L)
352#  define HAVE_DESIGNATED_INITIALIZERS 1
353# else
354#  define HAVE_DESIGNATED_INITIALIZERS 0
355# endif
356#endif
357
358/* Be conservative and only use enum bitfields with GCC.
359   FIXME: provide a complete autoconf test for buggy enum bitfields.  */
360
361#if (GCC_VERSION > 2000)
362#define ENUM_BITFIELD(TYPE) __extension__ enum TYPE
363#else
364#define ENUM_BITFIELD(TYPE) unsigned int
365#endif
366
367#ifndef offsetof
368#define offsetof(TYPE, MEMBER)	((size_t) &((TYPE *) 0)->MEMBER)
369#endif
370
371/* __builtin_expect(A, B) evaluates to A, but notifies the compiler that
372   the most likely value of A is B.  This feature was added at some point
373   between 2.95 and 3.0.  Let's use 3.0 as the lower bound for now.  */
374#if (GCC_VERSION < 3000)
375#define __builtin_expect(a, b) (a)
376#endif
377
378/* Provide a fake boolean type.  We make no attempt to use the
379   C99 _Bool, as it may not be available in the bootstrap compiler,
380   and even if it is, it is liable to be buggy.
381   This must be after all inclusion of system headers, as some of
382   them will mess us up.  */
383#undef bool
384#undef true
385#undef false
386#undef TRUE
387#undef FALSE
388
389#ifndef __cplusplus
390#define bool unsigned char
391#endif
392#define true 1
393#define false 0
394
395/* Some compilers do not allow the use of unsigned char in bitfields.  */
396#define BOOL_BITFIELD unsigned int
397
398/* Poison identifiers we do not want to use.  */
399#if (GCC_VERSION >= 3000)
400#undef calloc
401#undef strdup
402#undef malloc
403#undef realloc
404 #pragma GCC poison calloc strdup
405 #pragma GCC poison malloc realloc
406
407/* Libiberty macros that are no longer used in GCC.  */
408#undef ANSI_PROTOTYPES
409#undef PTR_CONST
410#undef LONG_DOUBLE
411#undef VPARAMS
412#undef VA_OPEN
413#undef VA_FIXEDARG
414#undef VA_CLOSE
415#undef VA_START
416 #pragma GCC poison ANSI_PROTOTYPES PTR_CONST LONG_DOUBLE VPARAMS VA_OPEN \
417  VA_FIXEDARG VA_CLOSE VA_START
418
419/* Note: not all uses of the `index' token (e.g. variable names and
420   structure members) have been eliminated.  */
421#undef bcopy
422#undef bzero
423#undef bcmp
424#undef rindex
425 #pragma GCC poison bcopy bzero bcmp rindex
426
427#endif /* GCC >= 3.0 */
428#endif /* ! LIBCPP_SYSTEM_H */
429