1169695Skan/* Get common system includes and various definitions and declarations based
2169695Skan   on autoconf macros.
3169695Skan   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
4169695Skan   Free Software Foundation, Inc.
5169695Skan
6169695SkanThis file is part of libcpp (aka cpplib).
7169695Skan
8169695SkanGCC is free software; you can redistribute it and/or modify it under
9169695Skanthe terms of the GNU General Public License as published by the Free
10169695SkanSoftware Foundation; either version 2, or (at your option) any later
11169695Skanversion.
12169695Skan
13169695SkanGCC is distributed in the hope that it will be useful, but WITHOUT ANY
14169695SkanWARRANTY; without even the implied warranty of MERCHANTABILITY or
15169695SkanFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16169695Skanfor more details.
17169695Skan
18169695SkanYou should have received a copy of the GNU General Public License
19169695Skanalong with GCC; see the file COPYING.  If not, write to the Free
20169695SkanSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21169695Skan02110-1301, USA.  */
22169695Skan
23169695Skan
24169695Skan#ifndef LIBCPP_SYSTEM_H
25169695Skan#define LIBCPP_SYSTEM_H
26169695Skan
27169695Skan/* We must include stdarg.h before stdio.h.  */
28169695Skan#include <stdarg.h>
29169695Skan
30169695Skan#ifdef HAVE_STDDEF_H
31169695Skan# include <stddef.h>
32169695Skan#endif
33169695Skan
34169695Skan#include <stdio.h>
35169695Skan
36169695Skan/* Define a generic NULL if one hasn't already been defined.  */
37169695Skan#ifndef NULL
38169695Skan#define NULL 0
39169695Skan#endif
40169695Skan
41169695Skan/* Use the unlocked open routines from libiberty.  */
42169695Skan#define fopen(PATH,MODE) fopen_unlocked(PATH,MODE)
43169695Skan#define fdopen(FILDES,MODE) fdopen_unlocked(FILDES,MODE)
44169695Skan#define freopen(PATH,MODE,STREAM) freopen_unlocked(PATH,MODE,STREAM)
45169695Skan
46169695Skan/* The compiler is not a multi-threaded application and therefore we
47169695Skan   do not have to use the locking functions.  In fact, using the locking
48169695Skan   functions can cause the compiler to be significantly slower under
49169695Skan   I/O bound conditions (such as -g -O0 on very large source files).
50169695Skan
51169695Skan   HAVE_DECL_PUTC_UNLOCKED actually indicates whether or not the stdio
52169695Skan   code is multi-thread safe by default.  If it is set to 0, then do
53169695Skan   not worry about using the _unlocked functions.
54169695Skan
55169695Skan   fputs_unlocked, fwrite_unlocked, and fprintf_unlocked are
56169695Skan   extensions and need to be prototyped by hand (since we do not
57169695Skan   define _GNU_SOURCE).  */
58169695Skan
59169695Skan#if defined HAVE_DECL_PUTC_UNLOCKED && HAVE_DECL_PUTC_UNLOCKED
60169695Skan
61169695Skan# ifdef HAVE_PUTC_UNLOCKED
62169695Skan#  undef putc
63169695Skan#  define putc(C, Stream) putc_unlocked (C, Stream)
64169695Skan# endif
65169695Skan# ifdef HAVE_PUTCHAR_UNLOCKED
66169695Skan#  undef putchar
67169695Skan#  define putchar(C) putchar_unlocked (C)
68169695Skan# endif
69169695Skan# ifdef HAVE_GETC_UNLOCKED
70169695Skan#  undef getc
71169695Skan#  define getc(Stream) getc_unlocked (Stream)
72169695Skan# endif
73169695Skan# ifdef HAVE_GETCHAR_UNLOCKED
74169695Skan#  undef getchar
75169695Skan#  define getchar() getchar_unlocked ()
76169695Skan# endif
77169695Skan# ifdef HAVE_FPUTC_UNLOCKED
78169695Skan#  undef fputc
79169695Skan#  define fputc(C, Stream) fputc_unlocked (C, Stream)
80169695Skan# endif
81169695Skan
82169695Skan# ifdef HAVE_CLEARERR_UNLOCKED
83169695Skan#  undef clearerr
84169695Skan#  define clearerr(Stream) clearerr_unlocked (Stream)
85169695Skan#  if defined (HAVE_DECL_CLEARERR_UNLOCKED) && !HAVE_DECL_CLEARERR_UNLOCKED
86169695Skanextern void clearerr_unlocked (FILE *);
87169695Skan#  endif
88169695Skan# endif
89169695Skan# ifdef HAVE_FEOF_UNLOCKED
90169695Skan#  undef feof
91169695Skan#  define feof(Stream) feof_unlocked (Stream)
92169695Skan#  if defined (HAVE_DECL_FEOF_UNLOCKED) && !HAVE_DECL_FEOF_UNLOCKED
93169695Skanextern int feof_unlocked (FILE *);
94169695Skan#  endif
95169695Skan# endif
96169695Skan# ifdef HAVE_FILENO_UNLOCKED
97169695Skan#  undef fileno
98169695Skan#  define fileno(Stream) fileno_unlocked (Stream)
99169695Skan#  if defined (HAVE_DECL_FILENO_UNLOCKED) && !HAVE_DECL_FILENO_UNLOCKED
100169695Skanextern int fileno_unlocked (FILE *);
101169695Skan#  endif
102169695Skan# endif
103169695Skan# ifdef HAVE_FFLUSH_UNLOCKED
104169695Skan#  undef fflush
105169695Skan#  define fflush(Stream) fflush_unlocked (Stream)
106169695Skan#  if defined (HAVE_DECL_FFLUSH_UNLOCKED) && !HAVE_DECL_FFLUSH_UNLOCKED
107169695Skanextern int fflush_unlocked (FILE *);
108169695Skan#  endif
109169695Skan# endif
110169695Skan# ifdef HAVE_FGETC_UNLOCKED
111169695Skan#  undef fgetc
112169695Skan#  define fgetc(Stream) fgetc_unlocked (Stream)
113169695Skan#  if defined (HAVE_DECL_FGETC_UNLOCKED) && !HAVE_DECL_FGETC_UNLOCKED
114169695Skanextern int fgetc_unlocked (FILE *);
115169695Skan#  endif
116169695Skan# endif
117169695Skan# ifdef HAVE_FGETS_UNLOCKED
118169695Skan#  undef fgets
119169695Skan#  define fgets(S, n, Stream) fgets_unlocked (S, n, Stream)
120169695Skan#  if defined (HAVE_DECL_FGETS_UNLOCKED) && !HAVE_DECL_FGETS_UNLOCKED
121169695Skanextern char *fgets_unlocked (char *, int, FILE *);
122169695Skan#  endif
123169695Skan# endif
124169695Skan# ifdef HAVE_FPUTS_UNLOCKED
125169695Skan#  undef fputs
126169695Skan#  define fputs(String, Stream) fputs_unlocked (String, Stream)
127169695Skan#  if defined (HAVE_DECL_FPUTS_UNLOCKED) && !HAVE_DECL_FPUTS_UNLOCKED
128169695Skanextern int fputs_unlocked (const char *, FILE *);
129169695Skan#  endif
130169695Skan# endif
131169695Skan# ifdef HAVE_FERROR_UNLOCKED
132169695Skan#  undef ferror
133169695Skan#  define ferror(Stream) ferror_unlocked (Stream)
134169695Skan#  if defined (HAVE_DECL_FERROR_UNLOCKED) && !HAVE_DECL_FERROR_UNLOCKED
135169695Skanextern int ferror_unlocked (FILE *);
136169695Skan#  endif
137169695Skan# endif
138169695Skan# ifdef HAVE_FREAD_UNLOCKED
139169695Skan#  undef fread
140169695Skan#  define fread(Ptr, Size, N, Stream) fread_unlocked (Ptr, Size, N, Stream)
141169695Skan#  if defined (HAVE_DECL_FREAD_UNLOCKED) && !HAVE_DECL_FREAD_UNLOCKED
142169695Skanextern size_t fread_unlocked (void *, size_t, size_t, FILE *);
143169695Skan#  endif
144169695Skan# endif
145169695Skan# ifdef HAVE_FWRITE_UNLOCKED
146169695Skan#  undef fwrite
147169695Skan#  define fwrite(Ptr, Size, N, Stream) fwrite_unlocked (Ptr, Size, N, Stream)
148169695Skan#  if defined (HAVE_DECL_FWRITE_UNLOCKED) && !HAVE_DECL_FWRITE_UNLOCKED
149169695Skanextern size_t fwrite_unlocked (const void *, size_t, size_t, FILE *);
150169695Skan#  endif
151169695Skan# endif
152169695Skan# ifdef HAVE_FPRINTF_UNLOCKED
153169695Skan#  undef fprintf
154169695Skan/* We can't use a function-like macro here because we don't know if
155169695Skan   we have varargs macros.  */
156169695Skan#  define fprintf fprintf_unlocked
157169695Skan#  if defined (HAVE_DECL_FPRINTF_UNLOCKED) && !HAVE_DECL_FPRINTF_UNLOCKED
158169695Skanextern int fprintf_unlocked (FILE *, const char *, ...);
159169695Skan#  endif
160169695Skan# endif
161169695Skan
162169695Skan#endif
163169695Skan
164169695Skan/* ??? Glibc's fwrite/fread_unlocked macros cause
165169695Skan   "warning: signed and unsigned type in conditional expression".  */
166169695Skan#undef fread_unlocked
167169695Skan#undef fwrite_unlocked
168169695Skan
169169695Skan#include <sys/types.h>
170169695Skan#include <errno.h>
171169695Skan
172169695Skan#if !defined (errno) && defined (HAVE_DECL_ERRNO) && !HAVE_DECL_ERRNO
173169695Skanextern int errno;
174169695Skan#endif
175169695Skan
176169695Skan/* Some of glibc's string inlines cause warnings.  Plus we'd rather
177169695Skan   rely on (and therefore test) GCC's string builtins.  */
178169695Skan#define __NO_STRING_INLINES
179169695Skan
180169695Skan#ifdef STRING_WITH_STRINGS
181169695Skan# include <string.h>
182169695Skan# include <strings.h>
183169695Skan#else
184169695Skan# ifdef HAVE_STRING_H
185169695Skan#  include <string.h>
186169695Skan# else
187169695Skan#  ifdef HAVE_STRINGS_H
188169695Skan#   include <strings.h>
189169695Skan#  endif
190169695Skan# endif
191169695Skan#endif
192169695Skan
193169695Skan#ifdef HAVE_STDLIB_H
194169695Skan# include <stdlib.h>
195169695Skan#endif
196169695Skan
197169695Skan#ifdef HAVE_UNISTD_H
198169695Skan# include <unistd.h>
199169695Skan#endif
200169695Skan
201169695Skan#if HAVE_LIMITS_H
202169695Skan# include <limits.h>
203169695Skan#endif
204169695Skan
205169695Skan/* Infrastructure for defining missing _MAX and _MIN macros.  Note that
206169695Skan   macros defined with these cannot be used in #if.  */
207169695Skan
208169695Skan/* The extra casts work around common compiler bugs.  */
209169695Skan#define INTTYPE_SIGNED(t) (! ((t) 0 < (t) -1))
210169695Skan/* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
211169695Skan   It is necessary at least when t == time_t.  */
212169695Skan#define INTTYPE_MINIMUM(t) ((t) (INTTYPE_SIGNED (t) \
213169695Skan                             ? ~ (t) 0 << (sizeof(t) * CHAR_BIT - 1) : (t) 0))
214169695Skan#define INTTYPE_MAXIMUM(t) ((t) (~ (t) 0 - INTTYPE_MINIMUM (t)))
215169695Skan
216169695Skan/* Use that infrastructure to provide a few constants.  */
217169695Skan#ifndef UCHAR_MAX
218169695Skan# define UCHAR_MAX INTTYPE_MAXIMUM (unsigned char)
219169695Skan#endif
220169695Skan
221169695Skan#ifdef TIME_WITH_SYS_TIME
222169695Skan# include <sys/time.h>
223169695Skan# include <time.h>
224169695Skan#else
225169695Skan# if HAVE_SYS_TIME_H
226169695Skan#  include <sys/time.h>
227169695Skan# else
228169695Skan#  ifdef HAVE_TIME_H
229169695Skan#   include <time.h>
230169695Skan#  endif
231169695Skan# endif
232169695Skan#endif
233169695Skan
234169695Skan#ifdef HAVE_FCNTL_H
235169695Skan# include <fcntl.h>
236169695Skan#else
237169695Skan# ifdef HAVE_SYS_FILE_H
238169695Skan#  include <sys/file.h>
239169695Skan# endif
240169695Skan#endif
241169695Skan
242169695Skan#ifdef HAVE_LOCALE_H
243169695Skan# include <locale.h>
244169695Skan#endif
245169695Skan
246169695Skan#ifdef HAVE_LANGINFO_CODESET
247169695Skan# include <langinfo.h>
248169695Skan#endif
249169695Skan
250169695Skan#ifndef HAVE_SETLOCALE
251169695Skan# define setlocale(category, locale) (locale)
252169695Skan#endif
253169695Skan
254169695Skan#ifdef ENABLE_NLS
255169695Skan#include <libintl.h>
256169695Skan#else
257169695Skan/* Stubs.  */
258169695Skan# undef dgettext
259169695Skan# define dgettext(package, msgid) (msgid)
260169695Skan#endif
261169695Skan
262169695Skan#ifndef _
263169695Skan# define _(msgid) dgettext (PACKAGE, msgid)
264169695Skan#endif
265169695Skan
266169695Skan#ifndef N_
267169695Skan# define N_(msgid) msgid
268169695Skan#endif
269169695Skan
270169695Skan/* Some systems define these in, e.g., param.h.  We undefine these names
271169695Skan   here to avoid the warnings.  We prefer to use our definitions since we
272169695Skan   know they are correct.  */
273169695Skan
274169695Skan#undef MIN
275169695Skan#undef MAX
276169695Skan#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
277169695Skan#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
278169695Skan
279169695Skan/* The HAVE_DECL_* macros are three-state, undefined, 0 or 1.  If they
280169695Skan   are defined to 0 then we must provide the relevant declaration
281169695Skan   here.  These checks will be in the undefined state while configure
282169695Skan   is running so be careful to test "defined (HAVE_DECL_*)".  */
283169695Skan
284169695Skan#if defined (HAVE_DECL_ABORT) && !HAVE_DECL_ABORT
285169695Skanextern void abort (void);
286169695Skan#endif
287169695Skan
288169695Skan#if HAVE_SYS_STAT_H
289169695Skan# include <sys/stat.h>
290169695Skan#endif
291169695Skan
292169695Skan/* Test if something is a normal file.  */
293169695Skan#ifndef S_ISREG
294169695Skan#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
295169695Skan#endif
296169695Skan
297169695Skan/* Test if something is a directory.  */
298169695Skan#ifndef S_ISDIR
299169695Skan#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
300169695Skan#endif
301169695Skan
302169695Skan/* Test if something is a character special file.  */
303169695Skan#ifndef S_ISCHR
304169695Skan#define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
305169695Skan#endif
306169695Skan
307169695Skan/* Test if something is a block special file.  */
308169695Skan#ifndef S_ISBLK
309169695Skan#define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
310169695Skan#endif
311169695Skan
312169695Skan/* Test if something is a socket.  */
313169695Skan#ifndef S_ISSOCK
314169695Skan# ifdef S_IFSOCK
315169695Skan#   define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
316169695Skan# else
317169695Skan#   define S_ISSOCK(m) 0
318169695Skan# endif
319169695Skan#endif
320169695Skan
321169695Skan/* Test if something is a FIFO.  */
322169695Skan#ifndef S_ISFIFO
323169695Skan# ifdef S_IFIFO
324169695Skan#  define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
325169695Skan# else
326169695Skan#  define S_ISFIFO(m) 0
327169695Skan# endif
328169695Skan#endif
329169695Skan
330169695Skan/* Approximate O_NOCTTY and O_BINARY.  */
331169695Skan#ifndef O_NOCTTY
332169695Skan#define O_NOCTTY 0
333169695Skan#endif
334169695Skan#ifndef O_BINARY
335169695Skan# define O_BINARY 0
336169695Skan#endif
337169695Skan
338169695Skan/* Filename handling macros.  */
339169695Skan#include "filenames.h"
340169695Skan
341169695Skan/* Get libiberty declarations.  */
342169695Skan#include "libiberty.h"
343169695Skan#include "safe-ctype.h"
344169695Skan
345169695Skan/* 1 if we have C99 designated initializers.
346169695Skan
347169695Skan   ??? C99 designated initializers are not supported by most C++
348169695Skan   compilers, including G++.  -- gdr, 2005-05-18  */
349169695Skan#if !defined(HAVE_DESIGNATED_INITIALIZERS)
350305693Sdim# if (!defined(__cplusplus) && (GCC_VERSION >= 2007)) \
351305693Sdim     ||(__STDC_VERSION__ >= 199901L)
352305693Sdim#  define HAVE_DESIGNATED_INITIALIZERS 1
353305693Sdim# else
354305693Sdim#  define HAVE_DESIGNATED_INITIALIZERS 0
355305693Sdim# endif
356169695Skan#endif
357169695Skan
358169695Skan/* Be conservative and only use enum bitfields with GCC.
359169695Skan   FIXME: provide a complete autoconf test for buggy enum bitfields.  */
360169695Skan
361169695Skan#if (GCC_VERSION > 2000)
362169695Skan#define ENUM_BITFIELD(TYPE) __extension__ enum TYPE
363169695Skan#else
364169695Skan#define ENUM_BITFIELD(TYPE) unsigned int
365169695Skan#endif
366169695Skan
367169695Skan#ifndef offsetof
368169695Skan#define offsetof(TYPE, MEMBER)	((size_t) &((TYPE *) 0)->MEMBER)
369169695Skan#endif
370169695Skan
371169695Skan/* __builtin_expect(A, B) evaluates to A, but notifies the compiler that
372169695Skan   the most likely value of A is B.  This feature was added at some point
373169695Skan   between 2.95 and 3.0.  Let's use 3.0 as the lower bound for now.  */
374169695Skan#if (GCC_VERSION < 3000)
375169695Skan#define __builtin_expect(a, b) (a)
376169695Skan#endif
377169695Skan
378169695Skan/* Provide a fake boolean type.  We make no attempt to use the
379169695Skan   C99 _Bool, as it may not be available in the bootstrap compiler,
380169695Skan   and even if it is, it is liable to be buggy.
381169695Skan   This must be after all inclusion of system headers, as some of
382169695Skan   them will mess us up.  */
383169695Skan#undef bool
384169695Skan#undef true
385169695Skan#undef false
386169695Skan#undef TRUE
387169695Skan#undef FALSE
388169695Skan
389169695Skan#ifndef __cplusplus
390169695Skan#define bool unsigned char
391169695Skan#endif
392169695Skan#define true 1
393169695Skan#define false 0
394169695Skan
395169695Skan/* Some compilers do not allow the use of unsigned char in bitfields.  */
396169695Skan#define BOOL_BITFIELD unsigned int
397169695Skan
398169695Skan/* Poison identifiers we do not want to use.  */
399169695Skan#if (GCC_VERSION >= 3000)
400169695Skan#undef calloc
401169695Skan#undef strdup
402169695Skan#undef malloc
403169695Skan#undef realloc
404169695Skan #pragma GCC poison calloc strdup
405169695Skan #pragma GCC poison malloc realloc
406169695Skan
407169695Skan/* Libiberty macros that are no longer used in GCC.  */
408169695Skan#undef ANSI_PROTOTYPES
409169695Skan#undef PTR_CONST
410169695Skan#undef LONG_DOUBLE
411169695Skan#undef VPARAMS
412169695Skan#undef VA_OPEN
413169695Skan#undef VA_FIXEDARG
414169695Skan#undef VA_CLOSE
415169695Skan#undef VA_START
416169695Skan #pragma GCC poison ANSI_PROTOTYPES PTR_CONST LONG_DOUBLE VPARAMS VA_OPEN \
417169695Skan  VA_FIXEDARG VA_CLOSE VA_START
418169695Skan
419169695Skan/* Note: not all uses of the `index' token (e.g. variable names and
420169695Skan   structure members) have been eliminated.  */
421169695Skan#undef bcopy
422169695Skan#undef bzero
423169695Skan#undef bcmp
424169695Skan#undef rindex
425169695Skan #pragma GCC poison bcopy bzero bcmp rindex
426169695Skan
427169695Skan#endif /* GCC >= 3.0 */
428169695Skan#endif /* ! LIBCPP_SYSTEM_H */
429