system.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /* System dependent definitions for GNU tar.
  2. Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2003
  3. Free Software Foundation, Inc.
  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. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #if HAVE_CONFIG_H
  17. # include <config.h>
  18. #endif
  19. #include <alloca.h>
  20. #ifndef __attribute__
  21. /* This feature is available in gcc versions 2.5 and later. */
  22. # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
  23. # define __attribute__(Spec) /* empty */
  24. # endif
  25. #endif
  26. #include <sys/types.h>
  27. #include <ctype.h>
  28. /* IN_CTYPE_DOMAIN (C) is nonzero if the unsigned char C can safely be given
  29. as an argument to <ctype.h> macros like `isspace'. */
  30. #if STDC_HEADERS
  31. # define IN_CTYPE_DOMAIN(c) 1
  32. #else
  33. # define IN_CTYPE_DOMAIN(c) ((unsigned) (c) <= 0177)
  34. #endif
  35. #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
  36. #define ISODIGIT(c) ((unsigned) (c) - '0' <= 7)
  37. #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
  38. #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
  39. /* Declare string and memory handling routines. Take care that an ANSI
  40. string.h and pre-ANSI memory.h might conflict, and that memory.h and
  41. strings.h conflict on some systems. */
  42. #if STDC_HEADERS || HAVE_STRING_H
  43. # include <string.h>
  44. # if !STDC_HEADERS && HAVE_MEMORY_H
  45. # include <memory.h>
  46. # endif
  47. #else
  48. # include <strings.h>
  49. # ifndef strchr
  50. # define strchr index
  51. # endif
  52. # ifndef strrchr
  53. # define strrchr rindex
  54. # endif
  55. # ifndef memcpy
  56. # define memcpy(d, s, n) bcopy ((char const *) (s), (char *) (d), n)
  57. # endif
  58. # ifndef memcmp
  59. # define memcmp(a, b, n) bcmp ((char const *) (a), (char const *) (b), n)
  60. # endif
  61. #endif
  62. /* Declare errno. */
  63. #include <errno.h>
  64. #ifndef errno
  65. extern int errno;
  66. #endif
  67. /* Declare open parameters. */
  68. #if HAVE_FCNTL_H
  69. # include <fcntl.h>
  70. #else
  71. # include <sys/file.h>
  72. #endif
  73. /* Pick only one of the next three: */
  74. #ifndef O_RDONLY
  75. # define O_RDONLY 0 /* only allow read */
  76. #endif
  77. #ifndef O_WRONLY
  78. # define O_WRONLY 1 /* only allow write */
  79. #endif
  80. #ifndef O_RDWR
  81. # define O_RDWR 2 /* both are allowed */
  82. #endif
  83. #ifndef O_ACCMODE
  84. # define O_ACCMODE (O_RDONLY | O_RDWR | O_WRONLY)
  85. #endif
  86. /* The rest can be OR-ed in to the above: */
  87. #ifndef O_CREAT
  88. # define O_CREAT 8 /* create file if needed */
  89. #endif
  90. #ifndef O_EXCL
  91. # define O_EXCL 16 /* file cannot already exist */
  92. #endif
  93. #ifndef O_TRUNC
  94. # define O_TRUNC 32 /* truncate file on open */
  95. #endif
  96. /* MS-DOG forever, with my love! */
  97. #ifndef O_BINARY
  98. # define O_BINARY 0
  99. #endif
  100. /* Declare file status routines and bits. */
  101. #include <sys/stat.h>
  102. #if !HAVE_LSTAT && !defined lstat
  103. # define lstat stat
  104. #endif
  105. #if STX_HIDDEN && !_LARGE_FILES /* AIX */
  106. # ifdef stat
  107. # undef stat
  108. # endif
  109. # define stat(path, buf) statx (path, buf, STATSIZE, STX_HIDDEN)
  110. # ifdef lstat
  111. # undef lstat
  112. # endif
  113. # define lstat(path, buf) statx (path, buf, STATSIZE, STX_HIDDEN | STX_LINK)
  114. #endif
  115. #if STAT_MACROS_BROKEN
  116. # undef S_ISBLK
  117. # undef S_ISCHR
  118. # undef S_ISCTG
  119. # undef S_ISDIR
  120. # undef S_ISFIFO
  121. # undef S_ISLNK
  122. # undef S_ISREG
  123. # undef S_ISSOCK
  124. #endif
  125. /* On MSDOS, there are missing things from <sys/stat.h>. */
  126. #if MSDOS
  127. # define S_ISUID 0
  128. # define S_ISGID 0
  129. # define S_ISVTX 0
  130. #endif
  131. #ifndef S_ISDIR
  132. # define S_ISDIR(Mode) (((Mode) & S_IFMT) == S_IFDIR)
  133. #endif
  134. #ifndef S_ISREG
  135. # define S_ISREG(Mode) (((Mode) & S_IFMT) == S_IFREG)
  136. #endif
  137. #ifndef S_ISBLK
  138. # ifdef S_IFBLK
  139. # define S_ISBLK(Mode) (((Mode) & S_IFMT) == S_IFBLK)
  140. # else
  141. # define S_ISBLK(Mode) 0
  142. # endif
  143. #endif
  144. #ifndef S_ISCHR
  145. # ifdef S_IFCHR
  146. # define S_ISCHR(Mode) (((Mode) & S_IFMT) == S_IFCHR)
  147. # else
  148. # define S_ISCHR(Mode) 0
  149. # endif
  150. #endif
  151. #ifndef S_ISCTG
  152. # ifdef S_IFCTG
  153. # define S_ISCTG(Mode) (((Mode) & S_IFMT) == S_IFCTG)
  154. # else
  155. # define S_ISCTG(Mode) 0
  156. # endif
  157. #endif
  158. #ifndef S_ISDOOR
  159. # define S_ISDOOR(Mode) 0
  160. #endif
  161. #ifndef S_ISFIFO
  162. # ifdef S_IFIFO
  163. # define S_ISFIFO(Mode) (((Mode) & S_IFMT) == S_IFIFO)
  164. # else
  165. # define S_ISFIFO(Mode) 0
  166. # endif
  167. #endif
  168. #ifndef S_ISLNK
  169. # ifdef S_IFLNK
  170. # define S_ISLNK(Mode) (((Mode) & S_IFMT) == S_IFLNK)
  171. # else
  172. # define S_ISLNK(Mode) 0
  173. # endif
  174. #endif
  175. #ifndef S_ISSOCK
  176. # ifdef S_IFSOCK
  177. # define S_ISSOCK(Mode) (((Mode) & S_IFMT) == S_IFSOCK)
  178. # else
  179. # define S_ISSOCK(Mode) 0
  180. # endif
  181. #endif
  182. #if !HAVE_MKFIFO && !defined mkfifo && defined S_IFIFO
  183. # define mkfifo(Path, Mode) (mknod (Path, (Mode) | S_IFIFO, 0))
  184. #endif
  185. #ifndef S_ISUID
  186. # define S_ISUID 0004000
  187. #endif
  188. #ifndef S_ISGID
  189. # define S_ISGID 0002000
  190. #endif
  191. #ifndef S_ISVTX
  192. # define S_ISVTX 0001000
  193. #endif
  194. #ifndef S_IRUSR
  195. # define S_IRUSR 0000400
  196. #endif
  197. #ifndef S_IWUSR
  198. # define S_IWUSR 0000200
  199. #endif
  200. #ifndef S_IXUSR
  201. # define S_IXUSR 0000100
  202. #endif
  203. #ifndef S_IRGRP
  204. # define S_IRGRP 0000040
  205. #endif
  206. #ifndef S_IWGRP
  207. # define S_IWGRP 0000020
  208. #endif
  209. #ifndef S_IXGRP
  210. # define S_IXGRP 0000010
  211. #endif
  212. #ifndef S_IROTH
  213. # define S_IROTH 0000004
  214. #endif
  215. #ifndef S_IWOTH
  216. # define S_IWOTH 0000002
  217. #endif
  218. #ifndef S_IXOTH
  219. # define S_IXOTH 0000001
  220. #endif
  221. #define MODE_WXUSR (S_IWUSR | S_IXUSR)
  222. #define MODE_R (S_IRUSR | S_IRGRP | S_IROTH)
  223. #define MODE_RW (S_IWUSR | S_IWGRP | S_IWOTH | MODE_R)
  224. #define MODE_RWX (S_IXUSR | S_IXGRP | S_IXOTH | MODE_RW)
  225. #define MODE_ALL (S_ISUID | S_ISGID | S_ISVTX | MODE_RWX)
  226. /* Include <unistd.h> before any preprocessor test of _POSIX_VERSION. */
  227. #if HAVE_UNISTD_H
  228. # include <unistd.h>
  229. #endif
  230. #ifndef SEEK_SET
  231. # define SEEK_SET 0
  232. #endif
  233. #ifndef SEEK_CUR
  234. # define SEEK_CUR 1
  235. #endif
  236. #ifndef SEEK_END
  237. # define SEEK_END 2
  238. #endif
  239. #ifndef STDIN_FILENO
  240. # define STDIN_FILENO 0
  241. #endif
  242. #ifndef STDOUT_FILENO
  243. # define STDOUT_FILENO 1
  244. #endif
  245. #ifndef STDERR_FILENO
  246. # define STDERR_FILENO 2
  247. #endif
  248. /* Declare make device, major and minor. Since major is a function on
  249. SVR4, we have to resort to GOT_MAJOR instead of just testing if
  250. major is #define'd. */
  251. #if MAJOR_IN_MKDEV
  252. # include <sys/mkdev.h>
  253. # define GOT_MAJOR
  254. #endif
  255. #if MAJOR_IN_SYSMACROS
  256. # include <sys/sysmacros.h>
  257. # define GOT_MAJOR
  258. #endif
  259. /* Some <sys/types.h> defines the macros. */
  260. #ifdef major
  261. # define GOT_MAJOR
  262. #endif
  263. #ifndef GOT_MAJOR
  264. # if MSDOS
  265. # define major(Device) (Device)
  266. # define minor(Device) (Device)
  267. # define makedev(Major, Minor) (((Major) << 8) | (Minor))
  268. # define GOT_MAJOR
  269. # endif
  270. #endif
  271. /* For HP-UX before HP-UX 8, major/minor are not in <sys/sysmacros.h>. */
  272. #ifndef GOT_MAJOR
  273. # if defined(hpux) || defined(__hpux__) || defined(__hpux)
  274. # include <sys/mknod.h>
  275. # define GOT_MAJOR
  276. # endif
  277. #endif
  278. #ifndef GOT_MAJOR
  279. # define major(Device) (((Device) >> 8) & 0xff)
  280. # define minor(Device) ((Device) & 0xff)
  281. # define makedev(Major, Minor) (((Major) << 8) | (Minor))
  282. #endif
  283. #undef GOT_MAJOR
  284. /* Declare wait status. */
  285. #if HAVE_SYS_WAIT_H
  286. # include <sys/wait.h>
  287. #endif
  288. #ifndef WEXITSTATUS
  289. # define WEXITSTATUS(s) (((s) >> 8) & 0xff)
  290. #endif
  291. #ifndef WIFSIGNALED
  292. # define WIFSIGNALED(s) (((s) & 0xffff) - 1 < (unsigned) 0xff)
  293. #endif
  294. #ifndef WTERMSIG
  295. # define WTERMSIG(s) ((s) & 0x7f)
  296. #endif
  297. /* FIXME: It is wrong to use BLOCKSIZE for buffers when the logical block
  298. size is greater than 512 bytes; so ST_BLKSIZE code below, in preparation
  299. for some cleanup in this area, later. */
  300. /* Get or fake the disk device blocksize. Usually defined by sys/param.h
  301. (if at all). */
  302. #if !defined(DEV_BSIZE) && defined(BSIZE)
  303. # define DEV_BSIZE BSIZE
  304. #endif
  305. #if !defined(DEV_BSIZE) && defined(BBSIZE) /* SGI */
  306. # define DEV_BSIZE BBSIZE
  307. #endif
  308. #ifndef DEV_BSIZE
  309. # define DEV_BSIZE 4096
  310. #endif
  311. /* Extract or fake data from a `struct stat'. ST_BLKSIZE gives the
  312. optimal I/O blocksize for the file, in bytes. Some systems, like
  313. Sequents, return st_blksize of 0 on pipes. */
  314. #if !HAVE_ST_BLKSIZE
  315. # define ST_BLKSIZE(Statbuf) DEV_BSIZE
  316. #else
  317. # define ST_BLKSIZE(Statbuf) \
  318. ((Statbuf).st_blksize > 0 ? (Statbuf).st_blksize : DEV_BSIZE)
  319. #endif
  320. /* Extract or fake data from a `struct stat'. ST_NBLOCKS gives the
  321. number of ST_NBLOCKSIZE-byte blocks in the file (including indirect blocks).
  322. HP-UX counts st_blocks in 1024-byte units,
  323. this loses when mixing HP-UX and BSD filesystems with NFS. AIX PS/2
  324. counts st_blocks in 4K units. */
  325. #if !HAVE_ST_BLOCKS
  326. # if defined(_POSIX_SOURCE) || !defined(BSIZE)
  327. # define ST_NBLOCKS(Statbuf) ((Statbuf).st_size / ST_NBLOCKSIZE + ((Statbuf).st_size % ST_NBLOCKSIZE != 0))
  328. # else
  329. off_t st_blocks ();
  330. # define ST_NBLOCKS(Statbuf) (st_blocks ((Statbuf).st_size))
  331. # endif
  332. #else
  333. # define ST_NBLOCKS(Statbuf) ((Statbuf).st_blocks)
  334. # if defined(hpux) || defined(__hpux__) || defined(__hpux)
  335. # define ST_NBLOCKSIZE 1024
  336. # else
  337. # if defined(_AIX) && defined(_I386)
  338. # define ST_NBLOCKSIZE (4 * 1024)
  339. # endif
  340. # endif
  341. #endif
  342. #ifndef ST_NBLOCKSIZE
  343. #define ST_NBLOCKSIZE 512
  344. #endif
  345. /* This is a real challenge to properly get MTIO* symbols :-(. ISC uses
  346. <sys/gentape.h>. SCO and BSDi uses <sys/tape.h>; BSDi also requires
  347. <sys/tprintf.h> and <sys/device.h> for defining tp_dev and tpr_t. It
  348. seems that the rest use <sys/mtio.h>, which itself requires other files,
  349. depending on systems. Pyramid defines _IOW in <sgtty.h>, for example. */
  350. #if HAVE_SYS_GENTAPE_H
  351. # include <sys/gentape.h>
  352. #else
  353. # if HAVE_SYS_TAPE_H
  354. # if HAVE_SYS_DEVICE_H
  355. # include <sys/device.h>
  356. # endif
  357. # if HAVE_SYS_BUF_H
  358. # include <sys/buf.h>
  359. # endif
  360. # if HAVE_SYS_TPRINTF_H
  361. # include <sys/tprintf.h>
  362. # endif
  363. # include <sys/tape.h>
  364. # else
  365. # if HAVE_SYS_MTIO_H
  366. # include <sys/ioctl.h>
  367. # if HAVE_SGTTY_H
  368. # include <sgtty.h>
  369. # endif
  370. # if HAVE_SYS_IO_TRIOCTL_H
  371. # include <sys/io/trioctl.h>
  372. # endif
  373. # include <sys/mtio.h>
  374. # endif
  375. # endif
  376. #endif
  377. /* Declare standard functions. */
  378. #if STDC_HEADERS
  379. # include <stdlib.h>
  380. #else
  381. void *malloc ();
  382. char *getenv ();
  383. #endif
  384. #include <stdbool.h>
  385. #include <stddef.h>
  386. #include <stdio.h>
  387. #if !defined _POSIX_VERSION && MSDOS
  388. # include <io.h>
  389. #endif
  390. #if WITH_DMALLOC
  391. # undef HAVE_DECL_VALLOC
  392. # define DMALLOC_FUNC_CHECK
  393. # include <dmalloc.h>
  394. #endif
  395. #include <limits.h>
  396. #ifndef MB_LEN_MAX
  397. # define MB_LEN_MAX 1
  398. #endif
  399. #if HAVE_INTTYPES_H
  400. # include <inttypes.h>
  401. #endif
  402. /* These macros work even on ones'-complement hosts (!).
  403. The extra casts work around common compiler bugs. */
  404. #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
  405. #define TYPE_MINIMUM(t) (TYPE_SIGNED (t) \
  406. ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) \
  407. : (t) 0)
  408. #define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
  409. /* Bound on length of the string representing an integer value of type t.
  410. Subtract one for the sign bit if t is signed;
  411. 302 / 1000 is log10 (2) rounded up;
  412. add one for integer division truncation;
  413. add one more for a minus sign if t is signed. */
  414. #define INT_STRLEN_BOUND(t) \
  415. ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 1000 \
  416. + 1 + TYPE_SIGNED (t))
  417. #define UINTMAX_STRSIZE_BOUND (INT_STRLEN_BOUND (uintmax_t) + 1)
  418. /* Prototypes for external functions. */
  419. #if HAVE_LOCALE_H
  420. # include <locale.h>
  421. #endif
  422. #if !HAVE_SETLOCALE
  423. # define setlocale(Category, Locale) /* empty */
  424. #endif
  425. #include <time.h>
  426. #ifndef time
  427. time_t time ();
  428. #endif
  429. /* Library modules. */
  430. #include <dirname.h>
  431. #include <error.h>
  432. #include <savedir.h>
  433. #include <unlocked-io.h>
  434. #include <xalloc.h>
  435. #include <gettext.h>
  436. #define _(msgid) gettext (msgid)
  437. #define N_(msgid) msgid
  438. #if ! defined valloc && ! HAVE_DECL_VALLOC
  439. # define valloc(Size) malloc (Size)
  440. #endif
  441. #if MSDOS
  442. # include <process.h>
  443. # define SET_BINARY_MODE(arc) setmode(arc, O_BINARY)
  444. # define ERRNO_IS_EACCESS errno == EACCESS
  445. #else
  446. # include <pwd.h>
  447. # include <grp.h>
  448. # define SET_BINARY_MODE(arc)
  449. # define ERRNO_IS_EACCESS 0
  450. #endif
  451. #if XENIX
  452. # include <sys/inode.h>
  453. #endif