add ext4,vfat and tar.bz2
[u-tools.git] / u-tools / apps / tar / lib / system.h
blob2deb585b41baf7b16b27d79cfe8f7a8b5912995e
1 /* System dependent definitions for GNU tar.
3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2003,
4 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
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 3, or (at your option)
9 any later version.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <alloca.h>
26 #ifndef __attribute__
27 /* This feature is available in gcc versions 2.5 and later. */
28 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
29 # define __attribute__(spec) /* empty */
30 # endif
31 #endif
33 #include <sys/types.h>
34 #include <ctype.h>
36 /* IN_CTYPE_DOMAIN (C) is nonzero if the unsigned char C can safely be given
37 as an argument to <ctype.h> macros like `isspace'. */
38 #if STDC_HEADERS
39 # define IN_CTYPE_DOMAIN(c) 1
40 #else
41 # define IN_CTYPE_DOMAIN(c) ((unsigned) (c) <= 0177)
42 #endif
44 #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
45 #define ISODIGIT(c) ((unsigned) (c) - '0' <= 7)
46 #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
47 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
49 /* Declare string and memory handling routines. Take care that an ANSI
50 string.h and pre-ANSI memory.h might conflict, and that memory.h and
51 strings.h conflict on some systems. */
53 #if STDC_HEADERS || HAVE_STRING_H
54 # include <string.h>
55 # if !STDC_HEADERS && HAVE_MEMORY_H
56 # include <memory.h>
57 # endif
58 #else
59 # include <strings.h>
60 # ifndef strchr
61 # define strchr index
62 # endif
63 # ifndef strrchr
64 # define strrchr rindex
65 # endif
66 # ifndef memcpy
67 # define memcpy(d, s, n) bcopy ((char const *) (s), (char *) (d), n)
68 # endif
69 # ifndef memcmp
70 # define memcmp(a, b, n) bcmp ((char const *) (a), (char const *) (b), n)
71 # endif
72 #endif
74 /* Declare errno. */
76 #include <errno.h>
77 #ifndef errno
78 extern int errno;
79 #endif
81 /* Declare open parameters. */
83 #if HAVE_FCNTL_H
84 # include <fcntl.h>
85 #else
86 # include <sys/file.h>
87 #endif
88 /* Pick only one of the next three: */
89 #ifndef O_RDONLY
90 # define O_RDONLY 0 /* only allow read */
91 #endif
92 #ifndef O_WRONLY
93 # define O_WRONLY 1 /* only allow write */
94 #endif
95 #ifndef O_RDWR
96 # define O_RDWR 2 /* both are allowed */
97 #endif
98 #ifndef O_ACCMODE
99 # define O_ACCMODE (O_RDONLY | O_RDWR | O_WRONLY)
100 #endif
101 /* The rest can be OR-ed in to the above: */
102 #ifndef O_CREAT
103 # define O_CREAT 8 /* create file if needed */
104 #endif
105 #ifndef O_EXCL
106 # define O_EXCL 16 /* file cannot already exist */
107 #endif
108 #ifndef O_TRUNC
109 # define O_TRUNC 32 /* truncate file on open */
110 #endif
112 #ifndef O_BINARY
113 # define O_BINARY 0
114 #endif
115 #ifndef O_DIRECTORY
116 # define O_DIRECTORY 0
117 #endif
118 #ifndef O_NOATIME
119 # define O_NOATIME 0
120 #endif
121 #ifndef O_NONBLOCK
122 # define O_NONBLOCK 0
123 #endif
125 /* Declare file status routines and bits. */
127 #include <sys/stat.h>
129 #if !HAVE_LSTAT && !defined lstat
130 # define lstat stat
131 #endif
133 #if STX_HIDDEN && !_LARGE_FILES /* AIX */
134 # ifdef stat
135 # undef stat
136 # endif
137 # define stat(file_name, buf) statx (file_name, buf, STATSIZE, STX_HIDDEN)
138 # ifdef lstat
139 # undef lstat
140 # endif
141 # define lstat(file_name, buf) statx (file_name, buf, STATSIZE, STX_HIDDEN | STX_LINK)
142 #endif
144 #if STAT_MACROS_BROKEN
145 # undef S_ISBLK
146 # undef S_ISCHR
147 # undef S_ISCTG
148 # undef S_ISDIR
149 # undef S_ISFIFO
150 # undef S_ISLNK
151 # undef S_ISREG
152 # undef S_ISSOCK
153 #endif
155 /* On MSDOS, there are missing things from <sys/stat.h>. */
156 #if MSDOS
157 # define S_ISUID 0
158 # define S_ISGID 0
159 # define S_ISVTX 0
160 #endif
162 #ifndef S_ISDIR
163 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
164 #endif
165 #ifndef S_ISREG
166 # define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
167 #endif
169 #ifndef S_ISBLK
170 # ifdef S_IFBLK
171 # define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
172 # else
173 # define S_ISBLK(mode) 0
174 # endif
175 #endif
176 #ifndef S_ISCHR
177 # ifdef S_IFCHR
178 # define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
179 # else
180 # define S_ISCHR(mode) 0
181 # endif
182 #endif
183 #ifndef S_ISCTG
184 # ifdef S_IFCTG
185 # define S_ISCTG(mode) (((mode) & S_IFMT) == S_IFCTG)
186 # else
187 # define S_ISCTG(mode) 0
188 # endif
189 #endif
190 #ifndef S_ISDOOR
191 # define S_ISDOOR(mode) 0
192 #endif
193 #ifndef S_ISFIFO
194 # ifdef S_IFIFO
195 # define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
196 # else
197 # define S_ISFIFO(mode) 0
198 # endif
199 #endif
200 #ifndef S_ISLNK
201 # ifdef S_IFLNK
202 # define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
203 # else
204 # define S_ISLNK(mode) 0
205 # endif
206 #endif
207 #ifndef S_ISSOCK
208 # ifdef S_IFSOCK
209 # define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
210 # else
211 # define S_ISSOCK(mode) 0
212 # endif
213 #endif
215 #if !HAVE_MKFIFO && !defined mkfifo && defined S_IFIFO
216 # define mkfifo(file_name, mode) (mknod (file_name, (mode) | S_IFIFO, 0))
217 #endif
219 #ifndef S_ISUID
220 # define S_ISUID 0004000
221 #endif
222 #ifndef S_ISGID
223 # define S_ISGID 0002000
224 #endif
225 #ifndef S_ISVTX
226 # define S_ISVTX 0001000
227 #endif
228 #ifndef S_IRUSR
229 # define S_IRUSR 0000400
230 #endif
231 #ifndef S_IWUSR
232 # define S_IWUSR 0000200
233 #endif
234 #ifndef S_IXUSR
235 # define S_IXUSR 0000100
236 #endif
237 #ifndef S_IRGRP
238 # define S_IRGRP 0000040
239 #endif
240 #ifndef S_IWGRP
241 # define S_IWGRP 0000020
242 #endif
243 #ifndef S_IXGRP
244 # define S_IXGRP 0000010
245 #endif
246 #ifndef S_IROTH
247 # define S_IROTH 0000004
248 #endif
249 #ifndef S_IWOTH
250 # define S_IWOTH 0000002
251 #endif
252 #ifndef S_IXOTH
253 # define S_IXOTH 0000001
254 #endif
256 #define MODE_WXUSR (S_IWUSR | S_IXUSR)
257 #define MODE_R (S_IRUSR | S_IRGRP | S_IROTH)
258 #define MODE_RW (S_IWUSR | S_IWGRP | S_IWOTH | MODE_R)
259 #define MODE_RWX (S_IXUSR | S_IXGRP | S_IXOTH | MODE_RW)
260 #define MODE_ALL (S_ISUID | S_ISGID | S_ISVTX | MODE_RWX)
262 /* Include <unistd.h> before any preprocessor test of _POSIX_VERSION. */
263 #include <unistd.h>
265 #ifndef SEEK_SET
266 # define SEEK_SET 0
267 #endif
268 #ifndef SEEK_CUR
269 # define SEEK_CUR 1
270 #endif
271 #ifndef SEEK_END
272 # define SEEK_END 2
273 #endif
275 #ifndef STDIN_FILENO
276 # define STDIN_FILENO 0
277 #endif
278 #ifndef STDOUT_FILENO
279 # define STDOUT_FILENO 1
280 #endif
281 #ifndef STDERR_FILENO
282 # define STDERR_FILENO 2
283 #endif
285 /* Declare make device, major and minor. Since major is a function on
286 SVR4, we have to resort to GOT_MAJOR instead of just testing if
287 major is #define'd. */
289 #if MAJOR_IN_MKDEV
290 # include <sys/mkdev.h>
291 # if !defined(makedev) && defined(mkdev)
292 # define makedev(a,b) mkdev((a),(b))
293 # endif
294 # define GOT_MAJOR
295 #endif
297 #if MAJOR_IN_SYSMACROS
298 # include <sys/sysmacros.h>
299 # define GOT_MAJOR
300 #endif
302 /* Some <sys/types.h> defines the macros. */
303 #ifdef major
304 # define GOT_MAJOR
305 #endif
307 #ifndef GOT_MAJOR
308 # if MSDOS
309 # define major(device) (device)
310 # define minor(device) (device)
311 # define makedev(major, minor) (((major) << 8) | (minor))
312 # define GOT_MAJOR
313 # endif
314 #endif
316 /* For HP-UX before HP-UX 8, major/minor are not in <sys/sysmacros.h>. */
317 #ifndef GOT_MAJOR
318 # if defined(hpux) || defined(__hpux__) || defined(__hpux)
319 # include <sys/mknod.h>
320 # define GOT_MAJOR
321 # endif
322 #endif
324 #ifndef GOT_MAJOR
325 # define major(device) (((device) >> 8) & 0xff)
326 # define minor(device) ((device) & 0xff)
327 # define makedev(major, minor) (((major) << 8) | (minor))
328 #endif
330 #undef GOT_MAJOR
332 /* Declare wait status. */
334 #if HAVE_SYS_WAIT_H
335 # include <sys/wait.h>
336 #endif
337 #ifndef WEXITSTATUS
338 # define WEXITSTATUS(s) (((s) >> 8) & 0xff)
339 #endif
340 #ifndef WIFSIGNALED
341 # define WIFSIGNALED(s) (((s) & 0xffff) - 1 < (unsigned) 0xff)
342 #endif
343 #ifndef WTERMSIG
344 # define WTERMSIG(s) ((s) & 0x7f)
345 #endif
347 /* FIXME: It is wrong to use BLOCKSIZE for buffers when the logical block
348 size is greater than 512 bytes; so ST_BLKSIZE code below, in preparation
349 for some cleanup in this area, later. */
351 /* Extract or fake data from a `struct stat'. ST_BLKSIZE gives the
352 optimal I/O blocksize for the file, in bytes. Some systems, like
353 Sequents, return st_blksize of 0 on pipes. */
355 #define DEFAULT_ST_BLKSIZE 512
357 #if !HAVE_ST_BLKSIZE
358 # define ST_BLKSIZE(statbuf) DEFAULT_ST_BLKSIZE
359 #else
360 # define ST_BLKSIZE(statbuf) \
361 ((statbuf).st_blksize > 0 ? (statbuf).st_blksize : DEFAULT_ST_BLKSIZE)
362 #endif
364 /* Extract or fake data from a `struct stat'. ST_NBLOCKS gives the
365 number of ST_NBLOCKSIZE-byte blocks in the file (including indirect blocks).
366 HP-UX counts st_blocks in 1024-byte units,
367 this loses when mixing HP-UX and BSD filesystems with NFS. AIX PS/2
368 counts st_blocks in 4K units. */
370 #if !HAVE_ST_BLOCKS
371 # if defined(_POSIX_SOURCE) || !defined(BSIZE)
372 # define ST_NBLOCKS(statbuf) ((statbuf).st_size / ST_NBLOCKSIZE + ((statbuf).st_size % ST_NBLOCKSIZE != 0))
373 # else
374 off_t st_blocks ();
375 # define ST_NBLOCKS(statbuf) (st_blocks ((statbuf).st_size))
376 # endif
377 #else
378 # define ST_NBLOCKS(statbuf) ((statbuf).st_blocks)
379 # if defined(hpux) || defined(__hpux__) || defined(__hpux)
380 # define ST_NBLOCKSIZE 1024
381 # else
382 # if defined(_AIX) && defined(_I386)
383 # define ST_NBLOCKSIZE (4 * 1024)
384 # endif
385 # endif
386 #endif
388 #ifndef ST_NBLOCKSIZE
389 # define ST_NBLOCKSIZE 512
390 #endif
392 #define ST_IS_SPARSE(st) \
393 (ST_NBLOCKS (st) \
394 < ((st).st_size / ST_NBLOCKSIZE + ((st).st_size % ST_NBLOCKSIZE != 0)))
396 /* Declare standard functions. */
398 #if STDC_HEADERS
399 # include <stdlib.h>
400 #else
401 void *malloc ();
402 char *getenv ();
403 #endif
405 #include <stdbool.h>
406 #include <stddef.h>
408 #include <stdio.h>
409 #if !defined _POSIX_VERSION && MSDOS
410 # include <io.h>
411 #endif
413 #if WITH_DMALLOC
414 # define DMALLOC_FUNC_CHECK
415 # include <dmalloc.h>
416 #endif
418 #include <limits.h>
420 #ifndef MB_LEN_MAX
421 # define MB_LEN_MAX 1
422 #endif
424 #include <inttypes.h>
426 #include <intprops.h>
428 #define UINTMAX_STRSIZE_BOUND INT_BUFSIZE_BOUND (uintmax_t)
430 /* Prototypes for external functions. */
432 #if HAVE_LOCALE_H
433 # include <locale.h>
434 #endif
435 #if !HAVE_SETLOCALE
436 # define setlocale(category, locale) /* empty */
437 #endif
439 #include <time.h>
440 #ifdef TIME_WITH_SYS_TIME
441 # include <sys/time.h>
442 #endif
444 /* Library modules. */
446 #include <dirname.h>
447 #include <error.h>
448 #include <savedir.h>
449 #include <unlocked-io.h>
450 #include <xalloc.h>
452 #include <gettext.h>
453 #define _(msgid) gettext (msgid)
454 #define N_(msgid) msgid
456 #ifdef HAVE_PWD_H
457 # include <pwd.h>
458 #endif
459 #ifdef HAVE_GRP_H
460 # include <grp.h>
461 #endif
463 #if MSDOS
464 # include <process.h>
465 # define SET_BINARY_MODE(arc) setmode(arc, O_BINARY)
466 # define ERRNO_IS_EACCES errno == EACCES
467 # define mkdir(file, mode) (mkdir) (file)
468 # define TTY_NAME "con"
469 # define sys_reset_uid_gid()
470 #else
471 # define SET_BINARY_MODE(arc)
472 # define ERRNO_IS_EACCES 0
473 # define TTY_NAME "/dev/tty"
474 # define sys_reset_uid_gid() \
475 do { setuid (getuid ()); setgid (getgid ()); } while (0)
476 #endif
478 #if XENIX
479 # include <sys/inode.h>
480 #endif