1 /* System dependent definitions for GNU tar.
2
3 Copyright 1994-2023 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18
19 #if HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <alloca.h>
24
25 #include <sys/types.h>
26 #include <ctype.h>
27
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
36 #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
37 #define ISODIGIT(c) ((unsigned) (c) - '0' <= 7)
38 #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
39 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
40
41 /* Declare string and memory handling routines. Take care that an ANSI
42 string.h and pre-ANSI memory.h might conflict, and that memory.h and
43 strings.h conflict on some systems. */
44
45 #if STDC_HEADERS || HAVE_STRING_H
46 # include <string.h>
47 # if !STDC_HEADERS && HAVE_MEMORY_H
48 # include <memory.h>
49 # endif
50 #else
51 # include <strings.h>
52 # ifndef strchr
53 # define strchr index
54 # endif
55 # ifndef strrchr
56 # define strrchr rindex
57 # endif
58 # ifndef memcpy
59 # define memcpy(d, s, n) bcopy ((char const *) (s), (char *) (d), n)
60 # endif
61 # ifndef memcmp
62 # define memcmp(a, b, n) bcmp ((char const *) (a), (char const *) (b), n)
63 # endif
64 #endif
65
66 /* Declare errno. */
67
68 #include <errno.h>
69 #ifndef errno
70 extern int errno;
71 #endif
72
73 /* Declare open parameters. */
74
75 #if HAVE_FCNTL_H
76 # include <fcntl.h>
77 #else
78 # include <sys/file.h>
79 #endif
80 /* Pick only one of the next three: */
81 #ifndef O_RDONLY
82 # define O_RDONLY 0 /* only allow read */
83 #endif
84 #ifndef O_WRONLY
85 # define O_WRONLY 1 /* only allow write */
86 #endif
87 #ifndef O_RDWR
88 # define O_RDWR 2 /* both are allowed */
89 #endif
90 #ifndef O_ACCMODE
91 # define O_ACCMODE (O_RDONLY | O_RDWR | O_WRONLY)
92 #endif
93 /* The rest can be OR-ed in to the above: */
94 #ifndef O_CREAT
95 # define O_CREAT 8 /* create file if needed */
96 #endif
97 #ifndef O_EXCL
98 # define O_EXCL 16 /* file cannot already exist */
99 #endif
100 #ifndef O_TRUNC
101 # define O_TRUNC 32 /* truncate file on open */
102 #endif
103
104 #ifndef O_BINARY
105 # define O_BINARY 0
106 #endif
107 #ifndef O_DIRECTORY
108 # define O_DIRECTORY 0
109 #endif
110 #ifndef O_NOATIME
111 # define O_NOATIME 0
112 #endif
113 #ifndef O_NONBLOCK
114 # define O_NONBLOCK 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 #include <unistd.h>
256
257 #ifndef SEEK_SET
258 # define SEEK_SET 0
259 #endif
260 #ifndef SEEK_CUR
261 # define SEEK_CUR 1
262 #endif
263 #ifndef SEEK_END
264 # define SEEK_END 2
265 #endif
266
267 #ifndef STDIN_FILENO
268 # define STDIN_FILENO 0
269 #endif
270 #ifndef STDOUT_FILENO
271 # define STDOUT_FILENO 1
272 #endif
273 #ifndef STDERR_FILENO
274 # define STDERR_FILENO 2
275 #endif
276
277 /* Declare make device, major and minor. Since major is a function on
278 SVR4, we have to resort to GOT_MAJOR instead of just testing if
279 major is #define'd. */
280
281 #if MAJOR_IN_MKDEV
282 # include <sys/mkdev.h>
283 # if !defined(makedev) && defined(mkdev)
284 # define makedev(a,b) mkdev((a),(b))
285 # endif
286 # define GOT_MAJOR
287 #endif
288
289 #if MAJOR_IN_SYSMACROS
290 # include <sys/sysmacros.h>
291 # define GOT_MAJOR
292 #endif
293
294 /* Some <sys/types.h> defines the macros. */
295 #ifdef major
296 # define GOT_MAJOR
297 #endif
298
299 #ifndef GOT_MAJOR
300 # if MSDOS
301 # define major(device) (device)
302 # define minor(device) (device)
303 # define makedev(major, minor) (((major) << 8) | (minor))
304 # define GOT_MAJOR
305 # endif
306 #endif
307
308 /* For HP-UX before HP-UX 8, major/minor are not in <sys/sysmacros.h>. */
309 #ifndef GOT_MAJOR
310 # if defined(hpux) || defined(__hpux__) || defined(__hpux)
311 # include <sys/mknod.h>
312 # define GOT_MAJOR
313 # endif
314 #endif
315
316 #ifndef GOT_MAJOR
317 # define major(device) (((device) >> 8) & 0xff)
318 # define minor(device) ((device) & 0xff)
319 # define makedev(major, minor) (((major) << 8) | (minor))
320 #endif
321
322 #undef GOT_MAJOR
323
324 /* Declare wait status. */
325
326 #if HAVE_SYS_WAIT_H
327 # include <sys/wait.h>
328 #endif
329 #ifndef WEXITSTATUS
330 # define WEXITSTATUS(s) (((s) >> 8) & 0xff)
331 #endif
332 #ifndef WIFSIGNALED
333 # define WIFSIGNALED(s) (((s) & 0xffff) - 1 < (unsigned) 0xff)
334 #endif
335 #ifndef WTERMSIG
336 # define WTERMSIG(s) ((s) & 0x7f)
337 #endif
338
339 /* FIXME: It is wrong to use BLOCKSIZE for buffers when the logical block
340 size is greater than 512 bytes; so ST_BLKSIZE code below, in preparation
341 for some cleanup in this area, later. */
342
343 /* Extract or fake data from a `struct stat'. ST_BLKSIZE gives the
344 optimal I/O blocksize for the file, in bytes. Some systems, like
345 Sequents, return st_blksize of 0 on pipes. */
346
347 #define DEFAULT_ST_BLKSIZE 512
348
349 #ifndef HAVE_STRUCT_STAT_ST_BLKSIZE
350 # define ST_BLKSIZE(statbuf) DEFAULT_ST_BLKSIZE
351 #else
352 # define ST_BLKSIZE(statbuf) \
353 ((statbuf).st_blksize > 0 ? (statbuf).st_blksize : DEFAULT_ST_BLKSIZE)
354 #endif
355
356 /* Extract or fake data from a `struct stat'. ST_NBLOCKS gives the
357 number of ST_NBLOCKSIZE-byte blocks in the file (including indirect blocks).
358 HP-UX counts st_blocks in 1024-byte units,
359 this loses when mixing HP-UX and BSD filesystems with NFS. AIX PS/2
360 counts st_blocks in 4K units. */
361
362 #if !HAVE_ST_BLOCKS
363 # if defined(_POSIX_SOURCE) || !defined(BSIZE)
364 # define ST_NBLOCKS(statbuf) ((statbuf).st_size / ST_NBLOCKSIZE + ((statbuf).st_size % ST_NBLOCKSIZE != 0))
365 # else
366 off_t st_blocks ();
367 # define ST_NBLOCKS(statbuf) (st_blocks ((statbuf).st_size))
368 # endif
369 #else
370 # define ST_NBLOCKS(statbuf) ((statbuf).st_blocks)
371 # if defined(hpux) || defined(__hpux__) || defined(__hpux)
372 # define ST_NBLOCKSIZE 1024
373 # else
374 # if defined(_AIX) && defined(_I386)
375 # define ST_NBLOCKSIZE (4 * 1024)
376 # endif
377 # endif
378 #endif
379
380 #ifndef ST_NBLOCKSIZE
381 # define ST_NBLOCKSIZE 512
382 #endif
383
384 /* Network Appliance file systems store small files directly in the
385 inode if st_size <= 64; in this case the number of blocks can be
386 zero. Perhaps other file systems have similar problems; so,
387 somewhat arbitrarily, do not consider a file to be sparse if
388 it has no blocks but st_size < ST_NBLOCKSIZE. */
389 #define ST_IS_SPARSE(st) \
390 (ST_NBLOCKS (st) \
391 < ((st).st_size / ST_NBLOCKSIZE \
392 + ((st).st_size % ST_NBLOCKSIZE != 0 \
393 && (st).st_size / ST_NBLOCKSIZE != 0)))
394
395 /* Declare standard functions. */
396
397 #if STDC_HEADERS
398 # include <stdlib.h>
399 #else
400 void *malloc ();
401 char *getenv ();
402 #endif
403
404 #include <stddef.h>
405
406 #include <stdio.h>
407 #if !defined _POSIX_VERSION && MSDOS
408 # include <io.h>
409 #endif
410
411 #if WITH_DMALLOC
412 # define DMALLOC_FUNC_CHECK
413 # include <dmalloc.h>
414 #endif
415
416 #include <limits.h>
417
418 #ifndef MB_LEN_MAX
419 # define MB_LEN_MAX 1
420 #endif
421
422 #include <inttypes.h>
423
424 #include <intprops.h>
425
426 /* Prototypes for external functions. */
427
428 #if HAVE_LOCALE_H
429 # include <locale.h>
430 #endif
431 #if !HAVE_SETLOCALE
432 # define setlocale(category, locale) /* empty */
433 #endif
434
435 #include <time.h>
436 #ifdef HAVE_SYS_TIME_H
437 # include <sys/time.h>
438 #endif
439
440 /* Library modules. */
441
442 #include <dirname.h>
443 #include <error.h>
444 #include <savedir.h>
445 #include <unlocked-io.h>
446 #include <xalloc.h>
447
448 #include <gettext.h>
449 #define _(msgid) gettext (msgid)
450 #define N_(msgid) msgid
451
452 #ifdef HAVE_PWD_H
453 # include <pwd.h>
454 #endif
455 #ifdef HAVE_GRP_H
456 # include <grp.h>
457 #endif
458
459 #if MSDOS
460 # include <process.h>
461 # define SET_BINARY_MODE(arc) setmode(arc, O_BINARY)
462 # define mkdir(file, mode) (mkdir) (file)
463 # define TTY_NAME "con"
464 #else
465 # define SET_BINARY_MODE(arc)
466 # define TTY_NAME "/dev/tty"
467 # include <paxlib.h>
468 #endif
469
470 #if XENIX
471 # include <sys/inode.h>
472 #endif