PageRenderTime 40ms CodeModel.GetById 30ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/cvs/lib/system.h

https://bitbucket.org/freebsd/freebsd-head/
C++ Header | 570 lines | 345 code | 84 blank | 141 comment | 63 complexity | 356a0accd7d4a2ab40318d2210f5a358 MD5 | raw file
  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#include <sys/types.h>
 15#include <sys/stat.h>
 16
 17#ifdef STAT_MACROS_BROKEN
 18#undef S_ISBLK
 19#undef S_ISCHR
 20#undef S_ISDIR
 21#undef S_ISREG
 22#undef S_ISFIFO
 23#undef S_ISLNK
 24#undef S_ISSOCK
 25#undef S_ISMPB
 26#undef S_ISMPC
 27#undef S_ISNWK
 28#endif
 29
 30/* Not all systems have S_IFMT, but we want to use it if we have it.
 31   The S_IFMT code below looks right (it masks and compares).  The
 32   non-S_IFMT code looks bogus (are there really systems on which
 33   S_IFBLK, S_IFLNK, &c, each have their own bit?  I suspect it was
 34   written for OS/2 using the IBM C/C++ Tools 2.01 compiler).
 35
 36   Of course POSIX systems will have S_IS*, so maybe the issue is
 37   semi-moot.  */
 38
 39#if !defined(S_ISBLK) && defined(S_IFBLK)
 40# if defined(S_IFMT)
 41# define	S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
 42# else
 43# define S_ISBLK(m) ((m) & S_IFBLK)
 44# endif
 45#endif
 46
 47#if !defined(S_ISCHR) && defined(S_IFCHR)
 48# if defined(S_IFMT)
 49# define	S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
 50# else
 51# define S_ISCHR(m) ((m) & S_IFCHR)
 52# endif
 53#endif
 54
 55#if !defined(S_ISDIR) && defined(S_IFDIR)
 56# if defined(S_IFMT)
 57# define	S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
 58# else
 59# define S_ISDIR(m) ((m) & S_IFDIR)
 60# endif
 61#endif
 62
 63#if !defined(S_ISREG) && defined(S_IFREG)
 64# if defined(S_IFMT)
 65# define	S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
 66# else
 67# define S_ISREG(m) ((m) & S_IFREG)
 68# endif
 69#endif
 70
 71#if !defined(S_ISFIFO) && defined(S_IFIFO)
 72# if defined(S_IFMT)
 73# define	S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
 74# else
 75# define S_ISFIFO(m) ((m) & S_IFIFO)
 76# endif
 77#endif
 78
 79#if !defined(S_ISLNK) && defined(S_IFLNK)
 80# if defined(S_IFMT)
 81# define	S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
 82# else
 83# define S_ISLNK(m) ((m) & S_IFLNK)
 84# endif
 85#endif
 86
 87#ifndef S_ISSOCK
 88# if defined( S_IFSOCK )
 89#   ifdef S_IFMT
 90#     define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
 91#   else
 92#     define S_ISSOCK(m) ((m) & S_IFSOCK)
 93#   endif /* S_IFMT */
 94# elif defined( S_ISNAM )
 95    /* SCO OpenServer 5.0.6a */
 96#   define S_ISSOCK S_ISNAM
 97# endif /* !S_IFSOCK && S_ISNAM */
 98#endif /* !S_ISSOCK */
 99
100#if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
101# if defined(S_IFMT)
102# define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
103# define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
104# else
105# define S_ISMPB(m) ((m) & S_IFMPB)
106# define S_ISMPC(m) ((m) & S_IFMPC)
107# endif
108#endif
109
110#if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
111# if defined(S_IFMT)
112# define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
113# else
114# define S_ISNWK(m) ((m) & S_IFNWK)
115# endif
116#endif
117
118#ifdef NEED_DECOY_PERMISSIONS        /* OS/2, really */
119
120#define	S_IRUSR S_IREAD
121#define	S_IWUSR S_IWRITE
122#define	S_IXUSR S_IEXEC
123#define	S_IRWXU	(S_IRUSR | S_IWUSR | S_IXUSR)
124#define	S_IRGRP S_IREAD
125#define	S_IWGRP S_IWRITE
126#define	S_IXGRP S_IEXEC
127#define	S_IRWXG	(S_IRGRP | S_IWGRP | S_IXGRP)
128#define	S_IROTH S_IREAD
129#define	S_IWOTH S_IWRITE
130#define	S_IXOTH S_IEXEC
131#define	S_IRWXO	(S_IROTH | S_IWOTH | S_IXOTH)
132
133#else /* ! NEED_DECOY_PERMISSIONS */
134
135#ifndef S_IRUSR
136#define	S_IRUSR 0400
137#define	S_IWUSR 0200
138#define	S_IXUSR 0100
139/* Read, write, and execute by owner.  */
140#define	S_IRWXU	(S_IRUSR|S_IWUSR|S_IXUSR)
141
142#define	S_IRGRP	(S_IRUSR >> 3)	/* Read by group.  */
143#define	S_IWGRP	(S_IWUSR >> 3)	/* Write by group.  */
144#define	S_IXGRP	(S_IXUSR >> 3)	/* Execute by group.  */
145/* Read, write, and execute by group.  */
146#define	S_IRWXG	(S_IRWXU >> 3)
147
148#define	S_IROTH	(S_IRGRP >> 3)	/* Read by others.  */
149#define	S_IWOTH	(S_IWGRP >> 3)	/* Write by others.  */
150#define	S_IXOTH	(S_IXGRP >> 3)	/* Execute by others.  */
151/* Read, write, and execute by others.  */
152#define	S_IRWXO	(S_IRWXG >> 3)
153#endif /* !def S_IRUSR */
154#endif /* NEED_DECOY_PERMISSIONS */
155
156#if defined(POSIX) || defined(HAVE_UNISTD_H)
157#include <unistd.h>
158#include <limits.h>
159#else
160off_t lseek ();
161char *getcwd ();
162#endif
163
164#include "xtime.h"
165
166#ifdef HAVE_IO_H
167#include <io.h>
168#endif
169
170#ifdef HAVE_DIRECT_H
171#include <direct.h>
172#endif
173
174
175
176/*
177**  MAXPATHLEN and PATH_MAX
178**
179**     On most systems MAXPATHLEN is defined in sys/param.h to be 1024. Of
180**     those that this is not true, again most define PATH_MAX in limits.h
181**     or sys/limits.h which usually gets included by limits.h. On the few
182**     remaining systems that neither statement is true, _POSIX_PATH_MAX 
183**     is defined.
184**
185**     So:
186**         1. If PATH_MAX is defined just use it.
187**         2. If MAXPATHLEN is defined but not PATH_MAX, then define
188**            PATH_MAX in terms of MAXPATHLEN.
189**         3. If neither is defined, include limits.h and check for
190**            PATH_MAX again.
191**         3.1 If we now have PATHSIZE, define PATH_MAX in terms of that.
192**             and ignore the rest.  Since _POSIX_PATH_MAX (checked for
193**             next) is the *most* restrictive (smallest) value, if we
194**             trust _POSIX_PATH_MAX, several of our buffers are too small.
195**         4. If PATH_MAX is still not defined but _POSIX_PATH_MAX is,
196**            then define PATH_MAX in terms of _POSIX_PATH_MAX.
197**         5. And if even _POSIX_PATH_MAX doesn't exist just put in
198**            a reasonable value.
199**         *. All in all, this is an excellent argument for using pathconf()
200**            when at all possible.  Or better yet, dynamically allocate
201**            our buffers and use getcwd() not getwd().
202**
203**     This works on:
204**         Sun Sparc 10        SunOS 4.1.3  &  Solaris 1.2
205**         HP 9000/700         HP/UX 8.07   &  HP/UX 9.01
206**         Tektronix XD88/10   UTekV 3.2e
207**         IBM RS6000          AIX 3.2
208**         Dec Alpha           OSF 1 ????
209**         Intel 386           BSDI BSD/386
210**         Intel 386           SCO OpenServer Release 5
211**         Apollo              Domain 10.4
212**         NEC                 SVR4
213*/
214
215/* On MOST systems this will get you MAXPATHLEN.
216   Windows NT doesn't have this file, tho.  */
217#ifdef HAVE_SYS_PARAM_H
218#include <sys/param.h>
219#endif
220
221#ifndef PATH_MAX  
222#  ifdef MAXPATHLEN
223#    define PATH_MAX                 MAXPATHLEN
224#  else
225#    include <limits.h>
226#    ifndef PATH_MAX
227#      ifdef PATHSIZE
228#         define PATH_MAX               PATHSIZE
229#      else /* no PATHSIZE */
230#        ifdef _POSIX_PATH_MAX
231#          define PATH_MAX             _POSIX_PATH_MAX
232#        else
233#          define PATH_MAX             1024
234#        endif  /* no _POSIX_PATH_MAX */
235#      endif  /* no PATHSIZE */
236#    endif /* no PATH_MAX   */
237#  endif  /* MAXPATHLEN */
238#endif  /* PATH_MAX   */
239
240
241/* The NeXT (without _POSIX_SOURCE, which we don't want) has a utime.h
242   which doesn't define anything.  It would be cleaner to have configure
243   check for struct utimbuf, but for now I'm checking NeXT here (so I don't
244   have to debug the configure check across all the machines).  */
245#if defined (HAVE_UTIME_H) && !defined (NeXT)
246#  include <utime.h>
247#else
248#  if defined (HAVE_SYS_UTIME_H)
249#    include <sys/utime.h>
250#  else
251#    ifndef ALTOS
252struct utimbuf
253{
254  long actime;
255  long modtime;
256};
257#    endif
258int utime ();
259#  endif
260#endif
261
262#include <string.h>
263
264#ifndef ERRNO_H_MISSING
265#include <errno.h>
266#endif
267
268/* Not all systems set the same error code on a non-existent-file
269   error.  This tries to ask the question somewhat portably.
270   On systems that don't have ENOTEXIST, this should behave just like
271   x == ENOENT.  "x" is probably errno, of course. */
272
273#ifdef ENOTEXIST
274#  ifdef EOS2ERR
275#    define existence_error(x) \
276     (((x) == ENOTEXIST) || ((x) == ENOENT) || ((x) == EOS2ERR))
277#  else
278#    define existence_error(x) \
279     (((x) == ENOTEXIST) || ((x) == ENOENT))
280#  endif
281#else
282#  ifdef EVMSERR
283#     define existence_error(x) \
284((x) == ENOENT || (x) == EINVAL || (x) == EVMSERR)
285#  else
286#    define existence_error(x) ((x) == ENOENT)
287#  endif
288#endif
289
290
291#ifdef STDC_HEADERS
292# include <stdlib.h>
293#else
294char *getenv ();
295char *malloc ();
296char *realloc ();
297char *calloc ();
298extern int errno;
299#endif
300
301/* SunOS4 apparently does not define this in stdlib.h.  */
302#ifndef EXIT_FAILURE
303# define EXIT_FAILURE 1
304#endif
305
306/* check for POSIX signals */
307#if defined(HAVE_SIGACTION) && defined(HAVE_SIGPROCMASK)
308# define POSIX_SIGNALS
309#endif
310
311/* MINIX 1.6 doesn't properly support sigaction */
312#if defined(_MINIX)
313# undef POSIX_SIGNALS
314#endif
315
316/* If !POSIX, try for BSD.. Reason: 4.4BSD implements these as wrappers */
317#if !defined(POSIX_SIGNALS)
318# if defined(HAVE_SIGVEC) && defined(HAVE_SIGSETMASK) && defined(HAVE_SIGBLOCK)
319#  define BSD_SIGNALS
320# endif
321#endif
322
323/* Under OS/2, this must be included _after_ stdio.h; that's why we do
324   it here. */
325#ifdef USE_OWN_TCPIP_H
326# include "tcpip.h"
327#endif
328
329#ifdef HAVE_FCNTL_H
330# include <fcntl.h>
331#else
332# include <sys/file.h>
333#endif
334
335#ifndef SEEK_SET
336# define SEEK_SET 0
337# define SEEK_CUR 1
338# define SEEK_END 2
339#endif
340
341#ifndef F_OK
342# define F_OK 0
343# define X_OK 1
344# define W_OK 2
345# define R_OK 4
346#endif
347
348#if HAVE_DIRENT_H
349# include <dirent.h>
350# define NAMLEN(dirent) strlen((dirent)->d_name)
351#else
352# define dirent direct
353# define NAMLEN(dirent) (dirent)->d_namlen
354# if HAVE_SYS_NDIR_H
355#  include <sys/ndir.h>
356# endif
357# if HAVE_SYS_DIR_H
358#  include <sys/dir.h>
359# endif
360# if HAVE_NDIR_H
361#  include <ndir.h>
362# endif
363#endif
364
365/* Convert B 512-byte blocks to kilobytes if K is nonzero,
366   otherwise return it unchanged. */
367#define convert_blocks(b, k) ((k) ? ((b) + 1) / 2 : (b))
368
369#ifndef S_ISLNK
370# define lstat stat
371#endif
372
373/*
374 * Some UNIX distributions don't include these in their stat.h Defined here
375 * because "config.h" is always included last.
376 */
377#ifndef S_IWRITE
378# define	S_IWRITE	0000200    /* write permission, owner */
379#endif
380#ifndef S_IWGRP
381# define	S_IWGRP		0000020    /* write permission, grougroup */
382#endif
383#ifndef S_IWOTH
384# define	S_IWOTH		0000002    /* write permission, other */
385#endif
386
387/* Under non-UNIX operating systems (MS-DOS, WinNT, MacOS), many filesystem
388   calls take  only one argument; permission is handled very differently on
389   those systems than in Unix.  So we leave such systems a hook on which they
390   can hang their own definitions.  */
391
392#ifndef CVS_ACCESS
393# define CVS_ACCESS access
394#endif
395
396#ifndef CVS_CHDIR
397# define CVS_CHDIR chdir
398#endif
399
400#ifndef CVS_CREAT
401# define CVS_CREAT creat
402#endif
403
404#ifndef CVS_FOPEN
405# define CVS_FOPEN fopen
406#endif
407
408#ifndef CVS_FDOPEN
409# define CVS_FDOPEN fdopen
410#endif
411
412#ifndef CVS_MKDIR
413# define CVS_MKDIR mkdir
414#endif
415
416#ifndef CVS_OPEN
417# define CVS_OPEN open
418#endif
419
420#ifndef CVS_READDIR
421# define CVS_READDIR readdir
422#endif
423
424#ifndef CVS_CLOSEDIR
425# define CVS_CLOSEDIR closedir
426#endif
427
428#ifndef CVS_OPENDIR
429# define CVS_OPENDIR opendir
430#endif
431
432#ifndef CVS_RENAME
433# define CVS_RENAME rename
434#endif
435
436#ifndef CVS_RMDIR
437# define CVS_RMDIR rmdir
438#endif
439
440#ifndef CVS_STAT
441# define CVS_STAT stat
442#endif
443
444/* Open question: should CVS_STAT be lstat by default?  We need
445   to use lstat in order to handle symbolic links correctly with
446   the PreservePermissions option. -twp */
447#ifndef CVS_LSTAT
448# define CVS_LSTAT lstat
449#endif
450
451#ifndef CVS_UNLINK
452# define CVS_UNLINK unlink
453#endif
454
455/* Wildcard matcher.  Should be case-insensitive if the system is.  */
456#ifndef CVS_FNMATCH
457# define CVS_FNMATCH fnmatch
458#endif
459
460#ifdef WIN32
461/*
462 * According to GNU conventions, we should avoid referencing any macro
463 * containing "WIN" as a reference to Microsoft Windows, as we would like to
464 * avoid any implication that we consider Microsoft Windows any sort of "win".
465 *
466 * FIXME: As of 2003-06-09, folks on the GNULIB project were discussing
467 * defining a configure macro to define WOE32 appropriately.  If they ever do
468 * write such a beast, we should use it, though in most cases it would be
469 * preferable to avoid referencing any OS or compiler anyhow, per Autoconf
470 * convention, and reference only tested features of the system.
471 */
472# define WOE32 1
473#endif /* WIN32 */
474
475
476#ifdef WOE32
477  /* Under Windows NT, filenames are case-insensitive.  */
478# define FILENAMES_CASE_INSENSITIVE 1
479#endif /* WOE32 */
480
481
482
483#ifdef FILENAMES_CASE_INSENSITIVE
484
485# if defined (__CYGWIN32__) || defined (WOE32)
486    /* Under Windows, filenames are case-insensitive, and both / and \
487       are path component separators.  */
488#   define FOLD_FN_CHAR(c) (WNT_filename_classes[(unsigned char) (c)])
489extern unsigned char WNT_filename_classes[];
490    /* Is the character C a path name separator?  Under
491       Windows NT, you can use either / or \.  */
492#   define ISDIRSEP(c) (FOLD_FN_CHAR(c) == '/')
493#   define ISABSOLUTE(s) (ISDIRSEP(s[0]) || FOLD_FN_CHAR(s[0]) >= 'a' && FOLD_FN_CHAR(s[0]) <= 'z' && s[1] == ':' && ISDIRSEP(s[2]))
494# else /* !__CYGWIN32__ && !WOE32 */
495  /* As far as I know, only Macintosh OS X & VMS make it here, but any
496   * platform defining FILENAMES_CASE_INSENSITIVE which isn't WOE32 or
497   * piggy-backing the same could, in theory.  Since the OS X fold just folds
498   * A-Z into a-z, I'm just allowing it to be used for any case insensitive
499   * system which we aren't yet making other specific folds or exceptions for.
500   * WOE32 needs its own class since \ and C:\ style absolute paths also need
501   * to be accounted for.
502   */
503#  if defined(USE_VMS_FILENAMES)
504#   define FOLD_FN_CHAR(c) (VMS_filename_classes[(unsigned char) (c)])
505extern unsigned char VMS_filename_classes[];
506#  else
507#   define FOLD_FN_CHAR(c) (OSX_filename_classes[(unsigned char) (c)])
508extern unsigned char OSX_filename_classes[];
509#  endif
510# endif /* __CYGWIN32__ || WOE32 */
511
512/* The following need to be declared for all case insensitive filesystems.
513 * When not FOLD_FN_CHAR is not #defined, a default definition for these
514 * functions is provided later in this header file.  */
515
516/* Like strcmp, but with the appropriate tweaks for file names.  */
517extern int fncmp (const char *n1, const char *n2);
518
519/* Fold characters in FILENAME to their canonical forms.  */
520extern void fnfold (char *FILENAME);
521
522#endif /* FILENAMES_CASE_INSENSITIVE */
523
524
525
526/* Some file systems are case-insensitive.  If FOLD_FN_CHAR is
527   #defined, it maps the character C onto its "canonical" form.  In a
528   case-insensitive system, it would map all alphanumeric characters
529   to lower case.  Under Windows NT, / and \ are both path component
530   separators, so FOLD_FN_CHAR would map them both to /.  */
531#ifndef FOLD_FN_CHAR
532# define FOLD_FN_CHAR(c) (c)
533# define fnfold(filename) (filename)
534# define fncmp strcmp
535#endif
536
537/* Different file systems have different path component separators.
538   For the VMS port we might need to abstract further back than this.  */
539#ifndef ISDIRSEP
540# define ISDIRSEP(c) ((c) == '/')
541#endif
542
543/* Different file systems can have different naming patterns which designate
544 * a path as absolute
545 */
546#ifndef ISABSOLUTE
547# define ISABSOLUTE(s) ISDIRSEP(s[0])
548#endif
549
550
551/* On some systems, we have to be careful about writing/reading files
552   in text or binary mode (so in text mode the system can handle CRLF
553   vs. LF, VMS text file conventions, &c).  We decide to just always
554   be careful.  That way we don't have to worry about whether text and
555   binary differ on this system.  We just have to worry about whether
556   the system has O_BINARY and "rb".  The latter is easy; all ANSI C
557   libraries have it, SunOS4 has it, and CVS has used it unguarded
558   some places for a while now without complaints (e.g. "rb" in
559   server.c (server_updated), since CVS 1.8).  The former is just an
560   #ifdef.  */
561
562#define FOPEN_BINARY_READ ("rb")
563#define FOPEN_BINARY_WRITE ("wb")
564#define FOPEN_BINARY_READWRITE ("r+b")
565
566#ifdef O_BINARY
567#define OPEN_BINARY (O_BINARY)
568#else
569#define OPEN_BINARY (0)
570#endif