*** empty log message ***
[coreutils.git] / src / sys2.h
blob53ef13f57cf9a079a55a331acf9b362818361db0
1 /* WARNING -- this file is temporary. It is shared between the
2 sh-utils, fileutils, and textutils packages. Once I find a little
3 more time, I'll merge the remaining things in system.h and everything
4 in this file will go back there. */
6 #ifndef RETSIGTYPE
7 # define RETSIGTYPE void
8 #endif
10 #ifndef __GNUC__
11 # ifdef HAVE_ALLOCA_H
12 # include <alloca.h>
13 # else
14 # ifdef _AIX
15 # pragma alloca
16 # else
17 # ifdef _WIN32
18 # include <malloc.h>
19 # include <io.h>
20 # else
21 # ifndef alloca
22 char *alloca ();
23 # endif
24 # endif
25 # endif
26 # endif
27 #endif
29 #ifdef __DJGPP__
30 /* We need the declaration of setmode. */
31 # include <io.h>
32 /* We need the declaration of __djgpp_set_ctrl_c. */
33 # include <sys/exceptn.h>
34 #endif
36 #include <ctype.h>
38 /* Jim Meyering writes:
40 "... Some ctype macros are valid only for character codes that
41 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
42 using /bin/cc or gcc but without giving an ansi option). So, all
43 ctype uses should be through macros like ISPRINT... If
44 STDC_HEADERS is defined, then autoconf has verified that the ctype
45 macros don't need to be guarded with references to isascii. ...
46 Defining isascii to 1 should let any compiler worth its salt
47 eliminate the && through constant folding."
49 Bruno Haible adds:
51 "... Furthermore, isupper(c) etc. have an undefined result if c is
52 outside the range -1 <= c <= 255. One is tempted to write isupper(c)
53 with c being of type `char', but this is wrong if c is an 8-bit
54 character >= 128 which gets sign-extended to a negative value.
55 The macro ISUPPER protects against this as well." */
57 #if STDC_HEADERS || (!defined (isascii) && !HAVE_ISASCII)
58 # define IN_CTYPE_DOMAIN(c) 1
59 #else
60 # define IN_CTYPE_DOMAIN(c) isascii(c)
61 #endif
63 #ifdef isblank
64 # define ISBLANK(c) (IN_CTYPE_DOMAIN (c) && isblank (c))
65 #else
66 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
67 #endif
68 #ifdef isgraph
69 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isgraph (c))
70 #else
71 # define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isprint (c) && !isspace (c))
72 #endif
74 #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
75 #define ISALNUM(c) (IN_CTYPE_DOMAIN (c) && isalnum (c))
76 #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
77 #define ISCNTRL(c) (IN_CTYPE_DOMAIN (c) && iscntrl (c))
78 #define ISLOWER(c) (IN_CTYPE_DOMAIN (c) && islower (c))
79 #define ISPUNCT(c) (IN_CTYPE_DOMAIN (c) && ispunct (c))
80 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
81 #define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
82 #define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit (c))
83 #define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
85 #if STDC_HEADERS
86 # define TOLOWER(Ch) tolower (Ch)
87 # define TOUPPER(Ch) toupper (Ch)
88 #else
89 # define TOLOWER(Ch) (ISUPPER (Ch) ? tolower (Ch) : (Ch))
90 # define TOUPPER(Ch) (ISLOWER (Ch) ? toupper (Ch) : (Ch))
91 #endif
93 /* ISDIGIT differs from ISDIGIT_LOCALE, as follows:
94 - Its arg may be any int or unsigned int; it need not be an unsigned char.
95 - It's guaranteed to evaluate its argument exactly once.
96 - It's typically faster.
97 Posix 1003.2-1992 section 2.5.2.1 page 50 lines 1556-1558 says that
98 only '0' through '9' are digits. Prefer ISDIGIT to ISDIGIT_LOCALE unless
99 it's important to use the locale's definition of `digit' even when the
100 host does not conform to Posix. */
101 #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
103 #ifndef PARAMS
104 # if PROTOTYPES
105 # define PARAMS(Args) Args
106 # else
107 # define PARAMS(Args) ()
108 # endif
109 #endif
111 /* Take care of NLS matters. */
113 #if HAVE_LOCALE_H
114 # include <locale.h>
115 #endif
116 #if !HAVE_SETLOCALE
117 # define setlocale(Category, Locale) /* empty */
118 #endif
120 #if ENABLE_NLS
121 # include <libintl.h>
122 # define _(Text) gettext (Text)
123 #else
124 # undef bindtextdomain
125 # define bindtextdomain(Domain, Directory) /* empty */
126 # undef textdomain
127 # define textdomain(Domain) /* empty */
128 # define _(Text) Text
129 #endif
130 #define N_(Text) Text
132 #define STREQ(a, b) (strcmp ((a), (b)) == 0)
134 #ifndef HAVE_DECL_FREE
135 void free ();
136 #endif
138 #ifndef HAVE_DECL_MALLOC
139 char *malloc ();
140 #endif
142 #ifndef HAVE_DECL_MEMCHR
143 char *memchr ();
144 #endif
146 #ifndef HAVE_DECL_REALLOC
147 char *realloc ();
148 #endif
150 #ifndef HAVE_DECL_STPCPY
151 # ifndef stpcpy
152 char *stpcpy ();
153 # endif
154 #endif
156 #ifndef HAVE_DECL_STRSTR
157 char *strstr ();
158 #endif
160 #ifndef HAVE_DECL_GETENV
161 char *getenv ();
162 #endif
164 #ifndef HAVE_DECL_LSEEK
165 off_t lseek ();
166 #endif
168 #include "xalloc.h"
170 #if ! defined HAVE_MEMPCPY && ! defined mempcpy
171 /* Be CAREFUL that there are no side effects in N. */
172 # define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
173 #endif
175 /* These are wrappers for functions/macros from GNU libc.
176 The standard I/O functions are thread-safe. These *_unlocked ones
177 are more efficient but not thread-safe. That they're not thread-safe
178 is fine since all these applications are single threaded. */
180 #ifdef HAVE_CLEARERR_UNLOCKED
181 # undef clearerr
182 # define clearerr(S) clearerr_unlocked (S)
183 #endif
185 #ifdef HAVE_FEOF_UNLOCKED
186 # undef feof
187 # define feof(S) feof_unlocked (S)
188 #endif
190 #ifdef HAVE_FERROR_UNLOCKED
191 # undef ferror
192 # define ferror(S) ferror_unlocked (S)
193 #endif
195 #ifdef HAVE_FFLUSH_UNLOCKED
196 # undef fflush
197 # define fflush(S) fflush_unlocked (S)
198 #endif
200 #ifdef HAVE_FPUTC_UNLOCKED
201 # undef fputc
202 # define fputc(C, S) fputc_unlocked (C, S)
203 #endif
205 #ifdef HAVE_FREAD_UNLOCKED
206 # undef fread
207 # define fread(P, Z, N, S) fread_unlocked (P, Z, N, S)
208 #endif
210 #ifdef HAVE_FWRITE_UNLOCKED
211 # undef fwrite
212 # define fwrite(P, Z, N, S) fwrite_unlocked (P, Z, N, S)
213 #endif
215 #ifdef HAVE_GETC_UNLOCKED
216 # undef getc
217 # define getc(S) getc_unlocked (S)
218 #endif
220 #ifdef HAVE_GETCHAR_UNLOCKED
221 # undef getchar
222 # define getchar(S) getchar_unlocked (S)
223 #endif
225 #ifdef HAVE_PUTC_UNLOCKED
226 # undef putc
227 # define putc(C, S) putc_unlocked (C, S)
228 #endif
230 #ifdef HAVE_PUTCHAR_UNLOCKED
231 # undef putchar
232 # define putchar(C) putchar_unlocked (C)
233 #endif
235 #define SAME_INODE(Stat_buf_1, Stat_buf_2) \
236 ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
237 && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
239 #define DOT_OR_DOTDOT(Basename) \
240 (Basename[0] == '.' && (Basename[1] == '\0' \
241 || (Basename[1] == '.' && Basename[2] == '\0')))
243 #if SETVBUF_REVERSED
244 # define SETVBUF(Stream, Buffer, Type, Size) \
245 setvbuf (Stream, Type, Buffer, Size)
246 #else
247 # define SETVBUF(Stream, Buffer, Type, Size) \
248 setvbuf (Stream, Buffer, Type, Size)
249 #endif
251 char *base_name PARAMS ((char const *));
253 /* Factor out some of the common --help and --version processing code. */
255 #define GETOPT_HELP_CHAR 250
256 #define GETOPT_VERSION_CHAR 251
258 #define GETOPT_HELP_OPTION_DECL \
259 "help", no_argument, 0, GETOPT_HELP_CHAR
260 #define GETOPT_VERSION_OPTION_DECL \
261 "version", no_argument, 0, GETOPT_VERSION_CHAR
263 #define case_GETOPT_HELP_CHAR \
264 case GETOPT_HELP_CHAR: \
265 usage (EXIT_SUCCESS); \
266 break;
268 #include "closeout.h"
269 #include "version-etc.h"
271 #define case_GETOPT_VERSION_CHAR(Program_name, Authors) \
272 case GETOPT_VERSION_CHAR: \
273 version_etc (stdout, Program_name, GNU_PACKAGE, VERSION, Authors); \
274 close_stdout (); \
275 exit (EXIT_SUCCESS); \
276 break;
278 #ifndef MAX
279 # define MAX(a, b) ((a) > (b) ? (a) : (b))
280 #endif
282 #ifndef MIN
283 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
284 #endif
286 #ifndef CHAR_BIT
287 # define CHAR_BIT 8
288 #endif
290 /* The extra casts work around common compiler bugs. */
291 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
292 /* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
293 It is necessary at least when t == time_t. */
294 #define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
295 ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) : (t) 0))
296 #define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
298 #ifndef CHAR_MIN
299 # define CHAR_MIN TYPE_MINIMUM (char)
300 #endif
302 #ifndef CHAR_MAX
303 # define CHAR_MAX TYPE_MAXIMUM (char)
304 #endif
306 #ifndef SCHAR_MIN
307 # define SCHAR_MIN (-1 - SCHAR_MAX)
308 #endif
310 #ifndef SCHAR_MAX
311 # define SCHAR_MAX (CHAR_MAX == UCHAR_MAX ? CHAR_MAX / 2 : CHAR_MAX)
312 #endif
314 #ifndef UCHAR_MAX
315 # define UCHAR_MAX TYPE_MAXIMUM (unsigned char)
316 #endif
318 #ifndef SHRT_MIN
319 # define SHRT_MIN TYPE_MINIMUM (short int)
320 #endif
322 #ifndef SHRT_MAX
323 # define SHRT_MAX TYPE_MAXIMUM (short int)
324 #endif
326 #ifndef INT_MAX
327 # define INT_MAX TYPE_MAXIMUM (int)
328 #endif
330 #ifndef UINT_MAX
331 # define UINT_MAX TYPE_MAXIMUM (unsigned int)
332 #endif
334 #ifndef LONG_MAX
335 # define LONG_MAX TYPE_MAXIMUM (long)
336 #endif
338 #ifndef ULONG_MAX
339 # define ULONG_MAX TYPE_MAXIMUM (unsigned long)
340 #endif
342 #ifndef UID_T_MAX
343 # define UID_T_MAX TYPE_MAXIMUM (uid_t)
344 #endif
346 #ifndef GID_T_MAX
347 # define GID_T_MAX TYPE_MAXIMUM (gid_t)
348 #endif
350 #ifndef PID_T_MAX
351 # define PID_T_MAX TYPE_MAXIMUM (pid_t)
352 #endif