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