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