]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - contrib/cpio/lib/system.h
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / contrib / cpio / lib / system.h
1 /* System dependent definitions for GNU tar.
2
3    Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2003,
4    2004 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(file_name, buf) statx (file_name, buf, STATSIZE, STX_HIDDEN)
130 # ifdef lstat
131 #  undef lstat
132 # endif
133 # define lstat(file_name, buf) statx (file_name, 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(file_name, mode) (mknod (file_name, (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_PARAM_H
397 #   include <sys/param.h>
398 #  endif
399 #  if HAVE_SYS_BUF_H
400 #   include <sys/buf.h>
401 #  endif
402 #  if HAVE_SYS_TPRINTF_H
403 #   include <sys/tprintf.h>
404 #  endif
405 #  include <sys/tape.h>
406 # else
407 #  if HAVE_SYS_MTIO_H
408 #   include <sys/ioctl.h>
409 #   if HAVE_SGTTY_H
410 #    include <sgtty.h>
411 #   endif
412 #   if HAVE_SYS_IO_TRIOCTL_H
413 #    include <sys/io/trioctl.h>
414 #   endif
415 #   include <sys/mtio.h>
416 #  endif
417 # endif
418 #endif
419
420 /* Declare standard functions.  */
421
422 #if STDC_HEADERS
423 # include <stdlib.h>
424 #else
425 void *malloc ();
426 char *getenv ();
427 #endif
428
429 #include <stdbool.h>
430 #include <stddef.h>
431
432 #include <stdio.h>
433 #if !defined _POSIX_VERSION && MSDOS
434 # include <io.h>
435 #endif
436
437 #if WITH_DMALLOC
438 # undef HAVE_DECL_VALLOC
439 # define DMALLOC_FUNC_CHECK
440 # include <dmalloc.h>
441 #endif
442
443 #include <limits.h>
444
445 #ifndef MB_LEN_MAX
446 # define MB_LEN_MAX 1
447 #endif
448
449 #if HAVE_INTTYPES_H
450 # include <inttypes.h>
451 #endif
452
453 /* These macros work even on ones'-complement hosts (!).
454    The extra casts work around common compiler bugs.  */
455 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
456 #define TYPE_MINIMUM(t) (TYPE_SIGNED (t) \
457                          ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) \
458                          : (t) 0)
459 #define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
460
461 /* Bound on length of the string representing an integer value of type t.
462    Subtract one for the sign bit if t is signed;
463    302 / 1000 is log10 (2) rounded up;
464    add one for integer division truncation;
465    add one more for a minus sign if t is signed.  */
466 #define INT_STRLEN_BOUND(t) \
467   ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 1000 \
468    + 1 + TYPE_SIGNED (t))
469
470 #define UINTMAX_STRSIZE_BOUND (INT_STRLEN_BOUND (uintmax_t) + 1)
471 \f
472 /* Prototypes for external functions.  */
473
474 #if HAVE_LOCALE_H
475 # include <locale.h>
476 #endif
477 #if !HAVE_SETLOCALE
478 # define setlocale(category, locale) /* empty */
479 #endif
480
481 #include <time.h>
482 #if defined(HAVE_SYS_TIME_H) && defined(TIME_WITH_SYS_TIME)
483 # include <sys/time.h>
484 #endif
485 #if ! HAVE_DECL_TIME
486 time_t time ();
487 #endif
488
489 #ifdef HAVE_UTIME_H
490 # include <utime.h>
491 #endif
492
493 /* Library modules.  */
494
495 #include <dirname.h>
496 #include <error.h>
497 #include <savedir.h>
498 #include <unlocked-io.h>
499 #include <xalloc.h>
500
501 #include <gettext.h>
502 #define _(msgid) gettext (msgid)
503 #define N_(msgid) msgid
504
505 #if MSDOS
506 # include <process.h>
507 # define SET_BINARY_MODE(arc) setmode(arc, O_BINARY)
508 # define ERRNO_IS_EACCES errno == EACCES
509 # define mkdir(file, mode) (mkdir) (file)
510 # define TTY_NAME "con"
511 # define sys_reset_uid_gid()
512 #else
513 # include <pwd.h>
514 # include <grp.h>
515 # define SET_BINARY_MODE(arc)
516 # define ERRNO_IS_EACCES 0
517 # define TTY_NAME "/dev/tty"
518 # define sys_reset_uid_gid() \
519  do { setuid (getuid ()); setgid (getgid ()); } while (0)
520 #endif
521
522 #if XENIX
523 # include <sys/inode.h>
524 #endif