Import (slightly modified) ru.koi8-r.win.kbd:1.1 from FreeBSD (fjoe):
[dragonfly.git] / contrib / cvs-1.12.11 / lib / system.h
1 /* system-dependent definitions for CVS.
2    Copyright (C) 1989-1992 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.  */
13
14 /***
15  *** Begin the default set of autoconf includes.
16  ***/
17
18 /* Headers assumed for C89 freestanding compilers.  See HACKING for more.  */
19 #include <limits.h>
20 #include <stdarg.h>
21 #include <stddef.h>
22
23 /* C89 hosted headers assumed since they were included in UNIX version 7.
24  * See HACKING for more.
25  */
26 #include <assert.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <signal.h>
30 #include <stdio.h>
31
32 /* C89 hosted headers we _think_ GCC supplies even on freestanding systems.
33  * If we find any systems which do not have them, a replacement header should
34  * be discussed with the GNULIB folks.
35  *
36  * For more information, please see the `Portability' section of the `HACKING'
37  * file.
38  */
39 #include <stdlib.h>
40 #include <string.h>
41
42 /* We assume this because it has been around forever despite not being a part
43  * of any of the other standards we assume conformance to.  So far this hasn't
44  * been a problem.
45  *
46  * For more information, please see the `Portability' section of the `HACKING'
47  * file.
48  */
49 #include <sys/types.h>
50
51 /* A GNULIB replacement for this C99 header is supplied when it is missing.
52  * See the comments in stdbool_.h for its limitations.
53  */
54 #include <stdbool.h>
55
56 /* Ditto for these POSIX.2 headers.  */
57 #include <fnmatch.h>
58 #include <getopt.h>     /* Has GNU extensions,  */
59
60
61
62 #if HAVE_SYS_STAT_H
63 # include <sys/stat.h>
64 #endif /* HAVE_SYS_STAT_H */
65 #if !STDC_HEADERS && HAVE_MEMORY_H
66 # include <memory.h>
67 #endif /* !STDC_HEADERS && HAVE_MEMORY_H */
68 #if HAVE_INTTYPES_H
69 # include <inttypes.h>
70 #else /* ! HAVE_INTTYPES_H */
71 # if HAVE_STDINT_H
72 #  include <stdint.h>
73 # endif /* HAVE_STDINT_H */
74 #endif /* HAVE_INTTYPES_H */
75 #if HAVE_UNISTD_H
76 # include <unistd.h>
77 #endif /* HAVE_UNISTD_H */
78 /* End the default set of autoconf includes */
79
80 /* Assume these headers. */
81 #include <pwd.h>
82
83 /* More GNULIB includes */
84 /* This include enables the use of the *_unlocked IO functions from glibc. */
85 #include "unlocked-io.h"
86
87 /* For struct timespec.  */
88 #include "timespec.h"
89
90 /* This is a replacement stub for gettext provided by GNULIB when gettext is
91  * not available.
92  */
93 #include <gettext.h>
94 /* End GNULIB includes */
95
96 #ifdef STAT_MACROS_BROKEN
97 #undef S_ISBLK
98 #undef S_ISCHR
99 #undef S_ISDIR
100 #undef S_ISREG
101 #undef S_ISFIFO
102 #undef S_ISLNK
103 #undef S_ISSOCK
104 #undef S_ISMPB
105 #undef S_ISMPC
106 #undef S_ISNWK
107 #endif
108
109 /* Not all systems have S_IFMT, but we want to use it if we have it.
110    The S_IFMT code below looks right (it masks and compares).  The
111    non-S_IFMT code looks bogus (are there really systems on which
112    S_IFBLK, S_IFLNK, &c, each have their own bit?  I suspect it was
113    written for OS/2 using the IBM C/C++ Tools 2.01 compiler).
114
115    Of course POSIX systems will have S_IS*, so maybe the issue is
116    semi-moot.  */
117
118 #if !defined(S_ISBLK) && defined(S_IFBLK)
119 # if defined(S_IFMT)
120 # define        S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
121 # else
122 # define S_ISBLK(m) ((m) & S_IFBLK)
123 # endif
124 #endif
125
126 #if !defined(S_ISCHR) && defined(S_IFCHR)
127 # if defined(S_IFMT)
128 # define        S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
129 # else
130 # define S_ISCHR(m) ((m) & S_IFCHR)
131 # endif
132 #endif
133
134 #if !defined(S_ISDIR) && defined(S_IFDIR)
135 # if defined(S_IFMT)
136 # define        S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
137 # else
138 # define S_ISDIR(m) ((m) & S_IFDIR)
139 # endif
140 #endif
141
142 #if !defined(S_ISREG) && defined(S_IFREG)
143 # if defined(S_IFMT)
144 # define        S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
145 # else
146 # define S_ISREG(m) ((m) & S_IFREG)
147 # endif
148 #endif
149
150 #if !defined(S_ISFIFO) && defined(S_IFIFO)
151 # if defined(S_IFMT)
152 # define        S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
153 # else
154 # define S_ISFIFO(m) ((m) & S_IFIFO)
155 # endif
156 #endif
157
158 #if !defined(S_ISLNK) && defined(S_IFLNK)
159 # if defined(S_IFMT)
160 # define        S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
161 # else
162 # define S_ISLNK(m) ((m) & S_IFLNK)
163 # endif
164 #endif
165
166 #ifndef S_ISSOCK
167 # if defined( S_IFSOCK )
168 #   ifdef S_IFMT
169 #     define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
170 #   else
171 #     define S_ISSOCK(m) ((m) & S_IFSOCK)
172 #   endif /* S_IFMT */
173 # elif defined( S_ISNAM )
174     /* SCO OpenServer 5.0.6a */
175 #   define S_ISSOCK S_ISNAM
176 # endif /* !S_IFSOCK && S_ISNAM */
177 #endif /* !S_ISSOCK */
178
179 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
180 # if defined(S_IFMT)
181 # define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
182 # define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
183 # else
184 # define S_ISMPB(m) ((m) & S_IFMPB)
185 # define S_ISMPC(m) ((m) & S_IFMPC)
186 # endif
187 #endif
188
189 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
190 # if defined(S_IFMT)
191 # define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
192 # else
193 # define S_ISNWK(m) ((m) & S_IFNWK)
194 # endif
195 #endif
196
197 #ifdef NEED_DECOY_PERMISSIONS        /* OS/2, really */
198
199 #define S_IRUSR S_IREAD
200 #define S_IWUSR S_IWRITE
201 #define S_IXUSR S_IEXEC
202 #define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
203 #define S_IRGRP S_IREAD
204 #define S_IWGRP S_IWRITE
205 #define S_IXGRP S_IEXEC
206 #define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
207 #define S_IROTH S_IREAD
208 #define S_IWOTH S_IWRITE
209 #define S_IXOTH S_IEXEC
210 #define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
211
212 #else /* ! NEED_DECOY_PERMISSIONS */
213
214 #ifndef S_IRUSR
215 #define S_IRUSR 0400
216 #define S_IWUSR 0200
217 #define S_IXUSR 0100
218 /* Read, write, and execute by owner.  */
219 #define S_IRWXU (S_IRUSR|S_IWUSR|S_IXUSR)
220
221 #define S_IRGRP (S_IRUSR >> 3)  /* Read by group.  */
222 #define S_IWGRP (S_IWUSR >> 3)  /* Write by group.  */
223 #define S_IXGRP (S_IXUSR >> 3)  /* Execute by group.  */
224 /* Read, write, and execute by group.  */
225 #define S_IRWXG (S_IRWXU >> 3)
226
227 #define S_IROTH (S_IRGRP >> 3)  /* Read by others.  */
228 #define S_IWOTH (S_IWGRP >> 3)  /* Write by others.  */
229 #define S_IXOTH (S_IXGRP >> 3)  /* Execute by others.  */
230 /* Read, write, and execute by others.  */
231 #define S_IRWXO (S_IRWXG >> 3)
232 #endif /* !def S_IRUSR */
233 #endif /* NEED_DECOY_PERMISSIONS */
234
235 #ifndef DEVNULL
236 # define        DEVNULL         "/dev/null"
237 #endif
238
239 #ifdef HAVE_IO_H
240 #include <io.h>
241 #endif
242
243 #ifdef HAVE_DIRECT_H
244 #include <direct.h>
245 #endif
246
247 /* The NeXT (without _POSIX_SOURCE, which we don't want) has a utime.h
248    which doesn't define anything.  It would be cleaner to have configure
249    check for struct utimbuf, but for now I'm checking NeXT here (so I don't
250    have to debug the configure check across all the machines).  */
251 #if defined (HAVE_UTIME_H) && !defined (NeXT)
252 #  include <utime.h>
253 #else
254 #  if defined (HAVE_SYS_UTIME_H)
255 #    include <sys/utime.h>
256 #  else
257 #    ifndef ALTOS
258 struct utimbuf
259 {
260   long actime;
261   long modtime;
262 };
263 #    endif
264 int utime ();
265 #  endif
266 #endif
267
268 /* errno.h variations:
269  *
270  * Not all systems set the same error code on a non-existent-file
271  * error.  This tries to ask the question somewhat portably.
272  * On systems that don't have ENOTEXIST, this should behave just like
273  * x == ENOENT.  "x" is probably errno, of course.
274  */
275 #ifdef ENOTEXIST
276 #  ifdef EOS2ERR
277 #    define existence_error(x) \
278      (((x) == ENOTEXIST) || ((x) == ENOENT) || ((x) == EOS2ERR))
279 #  else
280 #    define existence_error(x) \
281      (((x) == ENOTEXIST) || ((x) == ENOENT))
282 #  endif
283 #else
284 #  ifdef EVMSERR
285 #     define existence_error(x) \
286 ((x) == ENOENT || (x) == EINVAL || (x) == EVMSERR)
287 #  else
288 #    define existence_error(x) ((x) == ENOENT)
289 #  endif
290 #endif
291
292 #ifdef HAVE_MALLOC
293 # define CVS_MALLOC malloc
294 # else /* !HAVE_MALLOC */
295 # define CVS_MALLOC rpl_malloc
296 #endif /* HAVE_MALLOC */
297 #ifdef HAVE_REALLOC
298 # define CVS_REALLOC realloc
299 #else /* !HAVE_REALLOC */
300 # define CVS_REALLOC rpl_realloc
301 #endif /* HAVE_REALLOC */
302
303 #ifndef HAVE_STDLIB_H
304 char *getenv ();
305 char *malloc ();
306 char *realloc ();
307 char *calloc ();
308 extern int errno;
309 #endif
310
311 /* check for POSIX signals */
312 #if defined(HAVE_SIGACTION) && defined(HAVE_SIGPROCMASK)
313 # define POSIX_SIGNALS
314 #endif
315
316 /* MINIX 1.6 doesn't properly support sigaction */
317 #if defined(_MINIX)
318 # undef POSIX_SIGNALS
319 #endif
320
321 /* If !POSIX, try for BSD.. Reason: 4.4BSD implements these as wrappers */
322 #if !defined(POSIX_SIGNALS)
323 # if defined(HAVE_SIGVEC) && defined(HAVE_SIGSETMASK) && defined(HAVE_SIGBLOCK)
324 #  define BSD_SIGNALS
325 # endif
326 #endif
327
328 /* Under OS/2, this must be included _after_ stdio.h; that's why we do
329    it here. */
330 #ifdef USE_OWN_TCPIP_H
331 # include "tcpip.h"
332 #endif
333
334 #ifdef HAVE_FCNTL_H
335 # include <fcntl.h>
336 #else
337 # include <sys/file.h>
338 #endif
339
340 #ifndef SEEK_SET
341 # define SEEK_SET 0
342 # define SEEK_CUR 1
343 # define SEEK_END 2
344 #endif
345
346 #ifndef F_OK
347 # define F_OK 0
348 # define X_OK 1
349 # define W_OK 2
350 # define R_OK 4
351 #endif
352
353 #if HAVE_DIRENT_H
354 # include <dirent.h>
355 # define NAMLEN(dirent) strlen((dirent)->d_name)
356 #else
357 # define dirent direct
358 # define NAMLEN(dirent) (dirent)->d_namlen
359 # if HAVE_SYS_NDIR_H
360 #  include <sys/ndir.h>
361 # endif
362 # if HAVE_SYS_DIR_H
363 #  include <sys/dir.h>
364 # endif
365 # if HAVE_NDIR_H
366 #  include <ndir.h>
367 # endif
368 #endif
369
370 /* Convert B 512-byte blocks to kilobytes if K is nonzero,
371    otherwise return it unchanged. */
372 #define convert_blocks(b, k) ((k) ? ((b) + 1) / 2 : (b))
373
374 #ifndef S_ISLNK
375 # define lstat stat
376 #endif
377
378 /*
379  * Some UNIX distributions don't include these in their stat.h Defined here
380  * because "config.h" is always included last.
381  */
382 #ifndef S_IWRITE
383 # define        S_IWRITE        0000200    /* write permission, owner */
384 #endif
385 #ifndef S_IWGRP
386 # define        S_IWGRP         0000020    /* write permission, grougroup */
387 #endif
388 #ifndef S_IWOTH
389 # define        S_IWOTH         0000002    /* write permission, other */
390 #endif
391
392 /* Under non-UNIX operating systems (MS-DOS, WinNT, MacOS), many filesystem
393    calls take  only one argument; permission is handled very differently on
394    those systems than in Unix.  So we leave such systems a hook on which they
395    can hang their own definitions.  */
396
397 #ifndef CVS_ACCESS
398 # define CVS_ACCESS access
399 #endif
400
401 #ifndef CVS_CHDIR
402 # define CVS_CHDIR chdir
403 #endif
404
405 #ifndef CVS_CREAT
406 # define CVS_CREAT creat
407 #endif
408
409 #ifndef CVS_FOPEN
410 # define CVS_FOPEN fopen
411 #endif
412
413 #ifndef CVS_FDOPEN
414 # define CVS_FDOPEN fdopen
415 #endif
416
417 #ifndef CVS_MKDIR
418 # define CVS_MKDIR mkdir
419 #endif
420
421 #ifndef CVS_OPEN
422 # define CVS_OPEN open
423 #endif
424
425 #ifndef CVS_READDIR
426 # define CVS_READDIR readdir
427 #endif
428
429 #ifndef CVS_CLOSEDIR
430 # define CVS_CLOSEDIR closedir
431 #endif
432
433 #ifndef CVS_OPENDIR
434 # define CVS_OPENDIR opendir
435 #endif
436
437 #ifndef CVS_RENAME
438 # define CVS_RENAME rename
439 #endif
440
441 #ifndef CVS_RMDIR
442 # define CVS_RMDIR rmdir
443 #endif
444
445 #ifndef CVS_STAT
446 /* Use the function from lib/stat.c when the system version is broken.
447  */
448 # ifdef HAVE_STAT_EMPTY_STRING_BUG
449 #   define CVS_STAT rpl_stat
450 # else /* !HAVE_STAT_EMPTY_STRING_BUG */
451 #   define CVS_STAT stat
452 # endif /* HAVE_STAT_EMPTY_STRING_BUG */
453 #endif
454
455 /* Open question: should CVS_STAT be lstat by default?  We need
456    to use lstat in order to handle symbolic links correctly with
457    the PreservePermissions option. -twp */
458 #ifndef CVS_LSTAT
459 /* Use the function from lib/lstat.c when the system version is broken.
460  */
461 # if defined( HAVE_STAT_EMPTY_STRING_BUG ) || !defined( LSTAT_FOLLOWS_SLASHED_SYMLINK )
462 #   define CVS_LSTAT rpl_lstat
463 # else /* !defined(HAVE_STAT_EMPTY_STRING_BUG )
464         *    && defined( LSTAT_FOLLOWS_SLASHED_SYMLINK )
465         */
466 #   define CVS_LSTAT lstat
467 # endif /* defined(HAVE_STAT_EMPTY_STRING_BUG )
468          * || !defined( LSTAT_FOLLOWS_SLASHED_SYMLINK )
469          */
470 #endif
471
472 #ifndef CVS_UNLINK
473 # define CVS_UNLINK unlink
474 #endif
475
476 /* Wildcard matcher.  Should be case-insensitive if the system is.  */
477 #ifndef CVS_FNMATCH
478 # define CVS_FNMATCH fnmatch
479 #endif
480
481 #ifndef HAVE_FSEEKO
482 off_t ftello (FILE *);
483 int fseeko (FILE *, off_t, int);
484 #endif /* HAVE_FSEEKO */
485
486 #ifdef WIN32
487 /*
488  * According to GNU conventions, we should avoid referencing any macro
489  * containing "WIN" as a reference to Microsoft Windows, as we would like to
490  * avoid any implication that we consider Microsoft Windows any sort of "win".
491  *
492  * FIXME: As of 2003-06-09, folks on the GNULIB project were discussing
493  * defining a configure macro to define WOE32 appropriately.  If they ever do
494  * write such a beast, we should use it, though in most cases it would be
495  * preferable to avoid referencing any OS or compiler anyhow, per Autoconf
496  * convention, and reference only tested features of the system.
497  */
498 # define WOE32 1
499 #endif /* WIN32 */
500
501
502
503 #ifdef FILENAMES_CASE_INSENSITIVE
504
505 # if defined (__CYGWIN32__) || defined (WOE32)
506     /* Under Windows, filenames are case-insensitive, and both / and \
507        are path component separators.  */
508 #   define FOLD_FN_CHAR(c) (WNT_filename_classes[(unsigned char) (c)])
509 extern unsigned char WNT_filename_classes[];
510 # else /* ! WOE32 */
511   /* As far as I know, just Macintosh OS X can make it here,
512    * but since the OS X fold just folds a-z into A-Z or visa-versa, I'm just
513    * allowing it to be used for any case insensitive system which we aren't
514    * yet making other specific folds or exceptions for (basically, anything
515    * case insensitive other than Windows, where \ and C:\ style absolute paths
516    * also need to be accounted for).
517    *
518    * Under Mac OS X, filenames are case-insensitive.
519    */
520 #   define FOLD_FN_CHAR(c) (OSX_filename_classes[(unsigned char) (c)])
521 extern unsigned char OSX_filename_classes[];
522 # endif /* __CYGWIN32__ || WOE32 */
523
524 /* The following need to be declared for all case insensitive filesystems.
525  * When not FOLD_FN_CHAR is not #defined, a default definition for these
526  * functions is provided later in this header file.  */
527
528 /* Like strcmp, but with the appropriate tweaks for file names.  */
529 extern int fncmp (const char *n1, const char *n2);
530
531 /* Fold characters in FILENAME to their canonical forms.  */
532 extern void fnfold (char *FILENAME);
533
534 #endif /* FILENAMES_CASE_INSENSITIVE */
535
536
537
538 /* Some file systems are case-insensitive.  If FOLD_FN_CHAR is
539    #defined, it maps the character C onto its "canonical" form.  In a
540    case-insensitive system, it would map all alphanumeric characters
541    to lower case.  Under Windows NT, / and \ are both path component
542    separators, so FOLD_FN_CHAR would map them both to /.  */
543 #ifndef FOLD_FN_CHAR
544 # define FOLD_FN_CHAR(c) (c)
545 # define fnfold(filename) (filename)
546 # define fncmp strcmp
547 #endif
548
549 /* Different file systems can have different naming patterns which designate
550  * a path as absolute.
551  */
552 #ifndef ISABSOLUTE
553 # define ISABSOLUTE(s) ISSLASH(s[FILE_SYSTEM_PREFIX_LEN(s)])
554 #endif
555
556
557 /* On some systems, we have to be careful about writing/reading files
558    in text or binary mode (so in text mode the system can handle CRLF
559    vs. LF, VMS text file conventions, &c).  We decide to just always
560    be careful.  That way we don't have to worry about whether text and
561    binary differ on this system.  We just have to worry about whether
562    the system has O_BINARY and "rb".  The latter is easy; all ANSI C
563    libraries have it, SunOS4 has it, and CVS has used it unguarded
564    some places for a while now without complaints (e.g. "rb" in
565    server.c (server_updated), since CVS 1.8).  The former is just an
566    #ifdef.  */
567
568 #define FOPEN_BINARY_READ ("rb")
569 #define FOPEN_BINARY_WRITE ("wb")
570 #define FOPEN_BINARY_READWRITE ("r+b")
571
572 #ifdef O_BINARY
573 #define OPEN_BINARY (O_BINARY)
574 #else
575 #define OPEN_BINARY (0)
576 #endif