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