Debian Codegraph @

options.c

raw symbols SCIP
1 /* $OpenBSD: options.c,v 1.102 2018/09/13 12:33:43 millert Exp $ */
2 /* $NetBSD: options.c,v 1.6 1996/03/26 23:54:18 mrg Exp $ */
3
4 /*-
5 * Copyright (c) 2005, 2006, 2007, 2009, 2011, 2012, 2014, 2016,
6 * 2019, 2020
7 * mirabilos <[email protected]>
8 * Copyright (c) 1992 Keith Muller.
9 * Copyright (c) 1992, 1993
10 * The Regents of the University of California. All rights reserved.
11 *
12 * This code is derived from software contributed to Berkeley by
13 * Keith Muller of the University of California, San Diego.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40 #include <sys/types.h>
41 #if HAVE_SYS_MTIO_H
42 #include <sys/mtio.h>
43 #endif
44 #include <sys/stat.h>
45 #include <err.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <limits.h>
49 #if HAVE_PATHS_H
50 #include <paths.h>
51 #endif
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #if HAVE_STRINGS_H
56 #include <strings.h>
57 #endif
58 #include <unistd.h>
59
60 #define EXTERN
61 #include "pax.h"
62 #include "ar.h"
63 #include "cpio.h"
64 #include "tar.h"
65 #include "extern.h"
66
67 __RCSID("$MirOS: src/bin/pax/options.c,v 1.76 2021/10/03 20:48:06 tg Exp $");
68
69 #ifndef _PATH_DEFTAPE
70 #define _PATH_DEFTAPE "/dev/rmt0"
71 #endif
72
73 #define NELEM(a) (sizeof(a) / sizeof((a)[0]))
74 #define SELEM(a) (sizeof((a)[0]))
75
76 /*
77 * argv[0] names. Used for tar and cpio emulation
78 */
79
80 #define NM_TAR "tar"
81 #define NM_CPIO "cpio"
82 #define NM_PAX "pax"
83
84 /*
85 * Constants used to specify the legal sets of flags in pax. For each major
86 * operation mode of pax, a set of illegal flags is defined. If any one of
87 * those illegal flags are found set, we scream and exit
88 */
89
90 /*
91 * flags (one for each option).
92 */
93 #define AF 0x00000001
94 #define BF 0x00000002
95 #define CF 0x00000004
96 #define DF 0x00000008
97 #define FF 0x00000010
98 #define IF 0x00000020
99 #define KF 0x00000040
100 #define LF 0x00000080
101 #define NF 0x00000100
102 #define OF 0x00000200
103 #define PF 0x00000400
104 #define RF 0x00000800
105 #define SF 0x00001000
106 #define TF 0x00002000
107 #define UF 0x00004000
108 #define VF 0x00008000
109 #define WF 0x00010000
110 #define XF 0x00020000
111 #define CBF 0x00040000 /* nonstandard extension */
112 #define CDF 0x00080000 /* nonstandard extension */
113 #define CEF 0x00100000 /* nonstandard extension */
114 #define CGF 0x00200000 /* nonstandard extension */
115 #define CHF 0x00400000 /* nonstandard extension */
116 #define CLF 0x00800000 /* nonstandard extension */
117 #define CPF 0x01000000 /* nonstandard extension */
118 #define CTF 0x02000000 /* nonstandard extension */
119 #define CUF 0x04000000 /* nonstandard extension */
120 #define CXF 0x08000000
121 #define CYF 0x10000000 /* nonstandard extension */
122 #define CZF 0x20000000 /* nonstandard extension */
123 #define C0F 0x40000000 /* nonstandard extension */
124
125 /*
126 * ascii string indexed by bit position above (alter the above and you must
127 * alter this string) used to tell the user what flags caused us to complain
128 */
129 #define FLGCH "abcdfiklnoprstuvwxBDEGHLPTUXYZ0"
130
131 /*
132 * legal pax operation bit patterns
133 */
134
135 #define ISLIST(x) (((x) & (RF|WF)) == 0)
136 #define ISEXTRACT(x) (((x) & (RF|WF)) == RF)
137 #define ISARCHIVE(x) (((x) & (AF|RF|WF)) == WF)
138 #define ISAPPND(x) (((x) & (AF|RF|WF)) == (AF|WF))
139 #define ISCOPY(x) (((x) & (RF|WF)) == (RF|WF))
140 #define ISWRITE(x) (((x) & (RF|WF)) == WF)
141
142 /*
143 * Illegal option flag subsets based on pax operation
144 */
145
146 #define BDEXTR (AF|BF|LF|TF|WF|XF|CBF|CHF|CLF|CPF|CXF)
147 #define BDARCH (CF|KF|LF|NF|PF|RF|CDF|CEF|CYF|CZF)
148 #define BDCOPY (AF|BF|FF|OF|XF|CBF|CEF)
149 #define BDLIST (AF|BF|IF|KF|LF|OF|PF|RF|TF|UF|WF|XF|CBF|CDF|CHF|CLF|CPF|CXF|CYF|CZF)
150
151 /*
152 * Routines which handle command line options
153 */
154
155 static char flgch[] = FLGCH; /* list of all possible flags */
156 static OPLIST *ophead = NULL; /* head for format specific options -x */
157 static OPLIST *optail = NULL; /* option tail */
158
159 #ifndef SMALL
160 static int no_op(void);
161 static int no_op_i(int);
162 #endif
163 static int mkpath(char *);
164 static void process_M(const char *, void (*)(void));
165 static void printflg(unsigned int);
166 static int c_frmt(const void *, const void *);
167 static off_t str_offt(char *);
168 static void pax_options(int, char **);
169 static void pax_usage(void) MKSH_A_NORETURN;
170 static void tar_set_action(int);
171 static void tar_options(int, char **);
172 static void tar_usage(void) MKSH_A_NORETURN;
173 static void cpio_set_action(int);
174 static void cpio_options(int, char **);
175 static void cpio_usage(void) MKSH_A_NORETURN;
176
177 #ifndef SMALL
178 static int compress_id(char *_blk, int _size);
179 static int gzip_id(char *_blk, int _size);
180 static int bzip2_id(char *_blk, int _size);
181 static int xz_id(char *_blk, int _size);
182 #endif
183
184 /* command to run as gzip */
185 static const char GZIP_CMD[] = "gzip";
186 #ifndef SMALL
187 /* command to run as compress */
188 static const char COMPRESS_CMD[] = "compress";
189 /* command to run as bzip2 */
190 static const char BZIP2_CMD[] = "bzip2";
191 /* command to run as lzma and xz */
192 static const char XZ_CMD[] = "xz";
193 /* command used for creating lzma archives */
194 static const char LZMA_WRCMD[] = "lzma";
195 /* command to run as lzop */
196 static const char LZOP_CMD[] = "lzop";
197 #endif
198 /* used as flag value */
199 #define COMPRESS_GUESS_CMD ((const void *)&compress_program)
200
201 /*
202 * Format specific routine table, MUST be in sorted order
203 * and MUST match enum fsub_order in pax.h
204 * (see pax.h for description of each function)
205 *
206 * name, blksz, hdsz, udev, hlk, blkagn, id, st_read,
207 * read, end_read, st_write, write, end_write, trail,
208 * rd_data, wr_data, options, inhead, is_uar
209 */
210
211 const FSUB fsub[] = {
212 #ifndef SMALL
213 /* FSUB_AR: UNIX ARCHIVER */
214 {"ar", 512, sizeof(HD_AR), 0, 0, 0, uar_id, no_op,
215 uar_rd, uar_endrd, uar_stwr, uar_wr, no_op, uar_trail,
216 rd_wrfile, uar_wr_data, bad_opt, 0, 1},
217
218 /* FSUB_BCPIO: OLD BINARY CPIO */
219 {"bcpio", 5120, sizeof(HD_BCPIO), 1, 0, 0, bcpio_id, cpio_strd,
220 bcpio_rd, bcpio_endrd, cpio_stwr, bcpio_wr, cpio_endwr, cpio_trail,
221 rd_wrfile, wr_rdfile, bad_opt, 1, 0},
222 #endif
223
224 /* FSUB_CPIO: OLD OCTAL CHARACTER CPIO */
225 {"cpio", 5120, sizeof(HD_CPIO), 1, 0, 0, cpio_id, cpio_strd,
226 cpio_rd, cpio_endrd, cpio_stwr, cpio_wr, cpio_endwr, cpio_trail,
227 rd_wrfile, wr_rdfile, bad_opt, 1, 0},
228
229 #ifndef SMALL
230 /* FSUB_DIST: OLD OCTAL CHARACTER CPIO, UID/GID CLEARED (ANONYMISED) */
231 {"dist", 512, sizeof(HD_CPIO), 1, 0, 0, cpio_id, cpio_strd,
232 cpio_rd, cpio_endrd, dist_stwr, cpio_wr, cpio_endwr, cpio_trail,
233 rd_wrfile, wr_rdfile, bad_opt, 1, 0},
234 #endif
235
236 /* FSUB_SV4CPIO: SVR4 HEX CPIO */
237 {"sv4cpio", 5120, sizeof(HD_VCPIO), 1, 0, 0, vcpio_id, cpio_strd,
238 vcpio_rd, vcpio_endrd, cpio_stwr, vcpio_wr, cpio_endwr, cpio_trail,
239 rd_wrfile, wr_rdfile, bad_opt, 1, 0},
240
241 /* FSUB_SV4CRC: SVR4 HEX CPIO WITH CRC */
242 {"sv4crc", 5120, sizeof(HD_VCPIO), 1, 0, 0, crc_id, crc_strd,
243 vcpio_rd, vcpio_endrd, crc_stwr, vcpio_wr, cpio_endwr, cpio_trail,
244 rd_wrfile, wr_rdfile, bad_opt, 1, 0},
245
246 #ifndef SMALL
247 /* FSUB_TAR: OLD TAR */
248 {"tar", 10240, BLKMULT, 0, 1, BLKMULT, tar_id, no_op,
249 tar_rd, tar_endrd, no_op_i, tar_wr, tar_endwr, tar_trail,
250 rd_wrfile, wr_rdfile, tar_opt, 0, 0},
251 #endif
252
253 /* FSUB_USTAR: POSIX USTAR */
254 {"ustar", 10240, BLKMULT, 0, 1, BLKMULT, ustar_id, ustar_strd,
255 ustar_rd, tar_endrd, ustar_stwr, ustar_wr, tar_endwr, tar_trail,
256 rd_wrfile, wr_rdfile, tar_opt, 0, 0},
257
258 #ifndef SMALL
259 /* FSUB_V4NORM: SVR4 HEX CPIO WITH CRC, UID/GID/MTIME CLEARED (NORMALISED) */
260 {"v4norm", 512, sizeof(HD_VCPIO), 1, 0, 0, crc_id, crc_strd,
261 vcpio_rd, vcpio_endrd, v4norm_stwr, vcpio_wr, cpio_endwr, cpio_trail,
262 rd_wrfile, wr_rdfile, bad_opt, 1, 0},
263
264 /* FSUB_V4ROOT: SVR4 HEX CPIO WITH CRC, UID/GID CLEARED (ANONYMISED) */
265 {"v4root", 512, sizeof(HD_VCPIO), 1, 0, 0, crc_id, crc_strd,
266 vcpio_rd, vcpio_endrd, v4root_stwr, vcpio_wr, cpio_endwr, cpio_trail,
267 rd_wrfile, wr_rdfile, bad_opt, 1, 0},
268
269 /* FSUBFAIL_Z: compress, to detect failure to use -Z */
270 {NULL, 0, 4, 0, 0, 0, compress_id, NULL,
271 NULL, NULL, NULL, NULL, NULL, NULL,
272 NULL, NULL, NULL, 0, 0},
273 /* FSUBFAIL_XZ: xz, to detect failure to decompress it */
274 {NULL, 0, 6, 0, 0, 0, xz_id, NULL,
275 NULL, NULL, NULL, NULL, NULL, NULL,
276 NULL, NULL, NULL, 0, 0},
277 /* FSUBFAIL_BZ2: bzip2, to detect failure to use -j */
278 {NULL, 0, 4, 0, 0, 0, bzip2_id, NULL,
279 NULL, NULL, NULL, NULL, NULL, NULL,
280 NULL, NULL, NULL, 0, 0},
281 /* FSUBFAIL_GZ: gzip, to detect failure to use -z */
282 {NULL, 0, 4, 0, 0, 0, gzip_id, NULL,
283 NULL, NULL, NULL, NULL, NULL, NULL,
284 NULL, NULL, NULL, 0, 0},
285 #endif
286 };
287
288 /*
289 * ford is the archive search order used by get_arc() to determine what kind
290 * of archive we are dealing with. This helps to properly id archive formats
291 * some formats may be subsets of others....
292 */
293 const unsigned char ford[] = {
294 FSUB_USTAR,
295 #ifndef SMALL
296 FSUB_TAR,
297 FSUBFAIL_GZ,
298 FSUBFAIL_BZ2,
299 FSUBFAIL_XZ,
300 FSUBFAIL_Z,
301 #endif
302 FSUB_SV4CRC,
303 FSUB_SV4CPIO,
304 FSUB_CPIO,
305 #ifndef SMALL
306 FSUB_BCPIO,
307 #endif
308 FSUB_MAX
309 };
310
311 /* normalise archives */
312 int anonarch = 0;
313
314 /* extract to standard output */
315 char to_stdout = 0;
316
317 /*
318 * Do we have -C anywhere and what is it?
319 */
320 char havechd = 0;
321 char *chdname = NULL;
322
323 /*
324 * options()
325 * figure out if we are pax, tar or cpio. Call the appropriate options
326 * parser
327 */
328
329 void
330 options(int argc, char **argv)
331 {
332 size_t n;
333
334 /*
335 * Are we acting like pax, tar or cpio (based on argv[0])
336 */
337 n = strlen(argv[0]);
338
339 if (n >= 3 && !strcmp(argv[0] + (n - 3), NM_TAR)) {
340 argv0 = NM_TAR;
341 op_mode = OP_TAR;
342 tar_options(argc, argv);
343 } else if (n >= 4 && !strcmp(argv[0] + (n - 4), NM_CPIO)) {
344 argv0 = NM_CPIO;
345 op_mode = OP_CPIO;
346 cpio_options(argc, argv);
347 } else {
348 /* assume pax as the default */
349 argv0 = NM_PAX;
350 op_mode = OP_PAX;
351 pax_options(argc, argv);
352 }
353 }
354
355 static int
356 gather_format(FSUB *fp, const char *name, int flag)
357 {
358 size_t i;
359
360 if ((frmt = (const FSUB *)bsearch((const void *)fp, (const void *)fsub,
361 NELEM(fsub), SELEM(fsub), c_frmt)) != NULL)
362 return (0);
363 paxwarn(1, "Unknown -%c format: %s", flag, optarg);
364 fprintf(stderr, "%s: Known -%c formats are:", name, flag);
365 for (i = 0; i < NELEM(fsub); ++i)
366 if (fsub[i].name != NULL)
367 fprintf(stderr, " %s", fsub[i].name);
368 fputs("\n\n", stderr);
369 return (1);
370 }
371
372 /*
373 * pax_options()
374 * look at the user specified flags. set globals as required and check if
375 * the user specified a legal set of flags. If not, complain and exit
376 */
377
378 static void
379 pax_options(int argc, char **argv)
380 {
381 int c;
382 unsigned int flg = 0;
383 unsigned int bflg = 0;
384 const char *errstr;
385 char *pt;
386 FSUB tmp;
387
388 /*
389 * process option flags
390 */
391 while ((c = getopt(argc, argv,
392 "0aB:b:cDdE:f:G:HiJjkLlM:nOo:Pp:rs:T:tU:uvwXx:YZz")) != -1) {
393 switch (c) {
394 case '0':
395 /*
396 * Use \0 as pathname terminator.
397 * (For use with the -print0 option of find(1).)
398 */
399 zeroflag = 1;
400 flg |= C0F;
401 break;
402 case 'a':
403 /*
404 * append
405 */
406 flg |= AF;
407 break;
408 case 'B':
409 /*
410 * non-standard option on number of bytes written on a
411 * single archive volume.
412 */
413 if ((wrlimit = str_offt(optarg)) <= 0) {
414 paxwarn(1, "Invalid write limit %s", optarg);
415 pax_usage();
416 }
417 if (wrlimit % BLKMULT) {
418 paxwarn(1, "Write limit is not a %d byte multiple",
419 BLKMULT);
420 pax_usage();
421 }
422 flg |= CBF;
423 break;
424 case 'b':
425 /*
426 * specify blocksize
427 */
428 flg |= BF;
429 if ((wrblksz = (int)str_offt(optarg)) <= 0) {
430 paxwarn(1, "Invalid block size %s", optarg);
431 pax_usage();
432 }
433 break;
434 case 'c':
435 /*
436 * inverse match on patterns
437 */
438 cflag = 1;
439 flg |= CF;
440 break;
441 case 'D':
442 /*
443 * On extraction check file inode change time before the
444 * modification of the file name. Non standard option.
445 */
446 Dflag = 1;
447 flg |= CDF;
448 break;
449 case 'd':
450 /*
451 * match only dir on extract, not the subtree at dir
452 */
453 dflag = 1;
454 flg |= DF;
455 break;
456 case 'E':
457 /*
458 * non-standard limit on read faults
459 * 0 indicates stop after first error, values
460 * indicate a limit
461 */
462 flg |= CEF;
463 maxflt = strtonum(optarg, 0, INT_MAX, &errstr);
464 if (errstr) {
465 paxwarn(1, "Error count value: %s", errstr);
466 pax_usage();
467 }
468 break;
469 case 'f':
470 /*
471 * filename where the archive is stored
472 */
473 arcname = optarg;
474 flg |= FF;
475 break;
476 case 'G':
477 /*
478 * non-standard option for selecting files within an
479 * archive by group (gid or name)
480 */
481 if (grp_add(optarg) < 0) {
482 pax_usage();
483 break;
484 }
485 flg |= CGF;
486 break;
487 case 'H':
488 /*
489 * follow command line symlinks only
490 */
491 Hflag = 1;
492 flg |= CHF;
493 break;
494 case 'i':
495 /*
496 * interactive file rename
497 */
498 iflag = 1;
499 flg |= IF;
500 break;
501 #ifndef SMALL
502 case 'J':
503 /*
504 * use xz (non-standard option)
505 */
506 compress_program = XZ_CMD;
507 break;
508 case 'j':
509 /*
510 * use bzip2 (non-standard option)
511 */
512 compress_program = BZIP2_CMD;
513 break;
514 #endif
515 case 'k':
516 /*
517 * do not clobber files that exist
518 */
519 kflag = 1;
520 flg |= KF;
521 break;
522 case 'L':
523 /*
524 * follow symlinks
525 */
526 Lflag = 1;
527 flg |= CLF;
528 break;
529 case 'l':
530 /*
531 * try to link src to dest with copy (-rw)
532 */
533 lflag = 1;
534 flg |= LF;
535 break;
536 case 'M':
537 /*
538 * MirOS extension: archive normaliser
539 */
540 process_M(optarg, pax_usage);
541 break;
542 case 'n':
543 /*
544 * select first match for a pattern only
545 */
546 nflag = 1;
547 flg |= NF;
548 break;
549 case 'O':
550 /*
551 * Force one volume. Non standard option.
552 */
553 force_one_volume = 1;
554 break;
555 case 'o':
556 /*
557 * pass format specific options
558 */
559 flg |= OF;
560 if (opt_add(optarg) < 0)
561 pax_usage();
562 break;
563 case 'P':
564 /*
565 * do NOT follow symlinks (default)
566 */
567 Lflag = 0;
568 flg |= CPF;
569 break;
570 case 'p':
571 /*
572 * specify file characteristic options
573 */
574 for (pt = optarg; *pt != '\0'; ++pt) {
575 switch (*pt) {
576 case 'a':
577 /*
578 * do not preserve access time
579 */
580 patime = 0;
581 break;
582 case 'e':
583 /*
584 * preserve user id, group id, file
585 * mode, access/modification times
586 */
587 pids = 1;
588 pmode = 1;
589 patime = 1;
590 pmtime = 1;
591 break;
592 case 'm':
593 /*
594 * do not preserve modification time
595 */
596 pmtime = 0;
597 break;
598 case 'o':
599 /*
600 * preserve uid/gid
601 */
602 pids = 1;
603 break;
604 case 'p':
605 /*
606 * preserve file mode bits
607 */
608 pmode = 1;
609 break;
610 default:
611 paxwarn(1, "Invalid -p string: %c", *pt);
612 pax_usage();
613 break;
614 }
615 }
616 flg |= PF;
617 break;
618 case 'r':
619 /*
620 * read the archive
621 */
622 flg |= RF;
623 break;
624 case 's':
625 /*
626 * file name substitution name pattern
627 */
628 if (rep_add(optarg) < 0) {
629 pax_usage();
630 break;
631 }
632 flg |= SF;
633 break;
634 case 'T':
635 /*
636 * non-standard option for selecting files within an
637 * archive by modification time range (lower,upper)
638 */
639 if (trng_add(optarg) < 0) {
640 pax_usage();
641 break;
642 }
643 flg |= CTF;
644 break;
645 case 't':
646 /*
647 * preserve access time on filesystem nodes we read
648 */
649 tflag = 1;
650 flg |= TF;
651 break;
652 case 'U':
653 /*
654 * non-standard option for selecting files within an
655 * archive by user (uid or name)
656 */
657 if (usr_add(optarg) < 0) {
658 pax_usage();
659 break;
660 }
661 flg |= CUF;
662 break;
663 case 'u':
664 /*
665 * ignore those older files
666 */
667 uflag = 1;
668 flg |= UF;
669 break;
670 case 'v':
671 /*
672 * verbose operation mode
673 */
674 ++vflag;
675 flg |= VF;
676 break;
677 case 'w':
678 /*
679 * write an archive
680 */
681 flg |= WF;
682 break;
683 case 'X':
684 /*
685 * do not pass over mount points in the filesystem
686 */
687 Xflag = 1;
688 flg |= CXF;
689 break;
690 case 'x':
691 /*
692 * specify an archive format on write
693 */
694 tmp.name = optarg;
695 if (gather_format(&tmp, "pax", 'x'))
696 pax_usage();
697 flg |= XF;
698 break;
699 case 'Y':
700 /*
701 * On extraction check file inode change time after the
702 * modification of the file name. Non standard option.
703 */
704 Yflag = 1;
705 flg |= CYF;
706 break;
707 case 'Z':
708 /*
709 * On extraction check modification time after the
710 * modification of the file name. Non standard option.
711 */
712 Zflag = 1;
713 flg |= CZF;
714 break;
715 case 'z':
716 /*
717 * use gzip (non-standard option)
718 */
719 compress_program = GZIP_CMD;
720 break;
721 default:
722 pax_usage();
723 break;
724 }
725 }
726
727 /*
728 * figure out the operation mode of pax read,write,extract,copy,append
729 * or list. check that we have not been given a bogus set of flags
730 * for the operation mode.
731 */
732 if (ISLIST(flg)) {
733 act = LIST;
734 listf = stdout;
735 bflg = flg & BDLIST;
736 } else if (ISEXTRACT(flg)) {
737 act = EXTRACT;
738 bflg = flg & BDEXTR;
739 } else if (ISARCHIVE(flg)) {
740 act = ARCHIVE;
741 bflg = flg & BDARCH;
742 } else if (ISAPPND(flg)) {
743 act = APPND;
744 bflg = flg & BDARCH;
745 } else if (ISCOPY(flg)) {
746 act = COPY;
747 bflg = flg & BDCOPY;
748 } else
749 pax_usage();
750 if (bflg) {
751 printflg(flg);
752 pax_usage();
753 }
754
755 /*
756 * if we are writing (ARCHIVE) we use the default format if the user
757 * did not specify a format. when we write during an APPEND, we will
758 * adopt the format of the existing archive if none was supplied.
759 */
760 if (!(flg & XF) && (act == ARCHIVE))
761 frmt = &(fsub[FSUB_USTAR]);
762
763 /*
764 * process the args as they are interpreted by the operation mode
765 */
766 switch (act) {
767 case LIST:
768 case EXTRACT:
769 for (; optind < argc; optind++)
770 if (pat_add(argv[optind], NULL) < 0)
771 pax_usage();
772 break;
773 case COPY:
774 if (optind >= argc) {
775 paxwarn(0, "Destination directory was not supplied");
776 pax_usage();
777 }
778 --argc;
779 dirptr = argv[argc];
780 /* FALLTHROUGH */
781 case ARCHIVE:
782 case APPND:
783 for (; optind < argc; optind++)
784 if (ftree_add(argv[optind], 0) < 0)
785 pax_usage();
786 /*
787 * no read errors allowed on updates/append operation!
788 */
789 maxflt = 0;
790 break;
791 }
792 }
793
794 /*
795 * tar_options()
796 * look at the user specified flags. set globals as required and check if
797 * the user specified a legal set of flags. If not, complain and exit
798 */
799
800 static void
801 tar_set_action(int op)
802 {
803 if (act != ERROR && act != op)
804 tar_usage();
805 act = op;
806 }
807
808 static void
809 tar_options(int argc, char **argv)
810 {
811 int c;
812 int Oflag = FSUB_USTAR;
813 int nincfiles = 0;
814 int incfiles_max = 0;
815 struct incfile {
816 char *file;
817 char *dir;
818 };
819 struct incfile *incfiles = NULL;
820 FSUB tmp;
821 char *bp, *cp;
822
823 /*
824 * Set default values.
825 */
826 rmleadslash = 1;
827
828 /*
829 * process option flags
830 */
831 while ((c = getoldopt(argc, argv,
832 "014578AaBb:C:cD:ef:HhI:JjLM:mNOoPpqRrSs:tuvwXxZz")) != -1) {
833 switch (c) {
834 case '0':
835 arcname = DEV_0;
836 break;
837 case '1':
838 arcname = DEV_1;
839 break;
840 case '4':
841 arcname = DEV_4;
842 break;
843 case '5':
844 arcname = DEV_5;
845 break;
846 case '7':
847 arcname = DEV_7;
848 break;
849 case '8':
850 arcname = DEV_8;
851 break;
852 #ifndef SMALL
853 case 'A':
854 Oflag = FSUB_AR;
855 mircpio_deprecated("-A flag", "-D ar");
856 break;
857 #endif
858 case 'a':
859 /*
860 * use compression dependent on arcname
861 * (non-standard option, gtar extension)
862 */
863 compress_program = COMPRESS_GUESS_CMD;
864 break;
865 case 'B':
866 /*
867 * Nothing to do here, this is pax default
868 */
869 break;
870 case 'b':
871 /*
872 * specify blocksize in 512-byte blocks
873 */
874 if ((wrblksz = (int)str_offt(optarg)) <= 0) {
875 paxwarn(1, "Invalid block size %s", optarg);
876 tar_usage();
877 }
878 /* XXX - check for integer overflow */
879 wrblksz *= 512;
880 break;
881 case 'C':
882 havechd++;
883 chdname = optarg;
884 break;
885 case 'c':
886 /*
887 * create an archive
888 */
889 tar_set_action(ARCHIVE);
890 break;
891 case 'D':
892 /*
893 * specify archive format and options
894 */
895 if (!(bp = strdup(optarg))) {
896 paxwarn(0, "out of memory");
897 exit(1);
898 }
899 if ((cp = strchr(bp, ',')))
900 *cp++ = '\0';
901 tmp.name = bp;
902 if (gather_format(&tmp, "tar", 'D'))
903 tar_usage();
904 if (cp && opt_add(cp) < 0)
905 tar_usage();
906 free(bp);
907 Oflag = -666; /* "frmt already set" */
908 break;
909 case 'e':
910 /*
911 * stop after first error
912 */
913 maxflt = 0;
914 break;
915 case 'f':
916 /*
917 * filename where the archive is stored
918 */
919 arcname = optarg;
920 break;
921 case 'H':
922 /*
923 * follow command line symlinks only
924 */
925 Hflag = 1;
926 break;
927 case 'h':
928 /*
929 * follow symlinks
930 */
931 Lflag = 1;
932 break;
933 case 'I':
934 if (++nincfiles > incfiles_max) {
935 size_t n = nincfiles + 3;
936 struct incfile *p;
937
938 p = reallocarray(incfiles, n,
939 sizeof(*incfiles));
940 if (p == NULL) {
941 paxwarn(0, "Unable to allocate space "
942 "for option list");
943 exit(1);
944 }
945 incfiles = p;
946 incfiles_max = n;
947 }
948 incfiles[nincfiles - 1].file = optarg;
949 incfiles[nincfiles - 1].dir = chdname;
950 break;
951 #ifndef SMALL
952 case 'J':
953 /*
954 * use xz (non-standard option)
955 */
956 compress_program = XZ_CMD;
957 break;
958 case 'j':
959 /*
960 * use bzip2 (non-standard option)
961 */
962 compress_program = BZIP2_CMD;
963 break;
964 #endif
965 case 'L':
966 /*
967 * follow symlinks
968 */
969 Lflag = 1;
970 break;
971 case 'M':
972 /*
973 * MirOS extension: archive normaliser
974 */
975 process_M(optarg, tar_usage);
976 break;
977 case 'm':
978 /*
979 * do not preserve modification time
980 */
981 pmtime = 0;
982 break;
983 #ifndef SMALL
984 case 'N':
985 /* numeric uid and gid only */
986 anonarch |= ANON_NUMID;
987 mircpio_deprecated("-N flag", "-M numid");
988 break;
989 #endif
990 case 'O':
991 #ifndef SMALL
992 Oflag = FSUB_TAR;
993 #else
994 Oflag = FSUB_MAX;
995 #endif
996 to_stdout = 2;
997 break;
998 #ifndef SMALL
999 case 'o':
1000 Oflag = FSUB_TAR;
1001 tar_nodir = 1;
1002 break;
1003 #endif
1004 case 'P':
1005 /*
1006 * do not remove leading '/' from pathnames
1007 */
1008 rmleadslash = 0;
1009 break;
1010 case 'p':
1011 /*
1012 * preserve uid/gid and file mode, regardless of umask
1013 */
1014 pmode = 1;
1015 pids = 1;
1016 break;
1017 case 'q':
1018 /*
1019 * select first match for a pattern only
1020 */
1021 nflag = 1;
1022 break;
1023 #ifndef SMALL
1024 case 'R':
1025 Oflag = FSUB_SV4CPIO;
1026 anonarch |= ANON_INODES | ANON_HARDLINKS;
1027 mircpio_deprecated("-R flag", "-D sv4cpio -M set");
1028 break;
1029 #endif
1030 case 'r':
1031 case 'u':
1032 /*
1033 * append to the archive
1034 */
1035 tar_set_action(APPND);
1036 break;
1037 #ifndef SMALL
1038 case 'S':
1039 Oflag = FSUB_SV4CRC;
1040 anonarch |= ANON_INODES | ANON_HARDLINKS;
1041 mircpio_deprecated("-S flag", "-D sv4crc -M set");
1042 break;
1043 #endif
1044 case 's':
1045 /*
1046 * file name substitution name pattern
1047 */
1048 if (rep_add(optarg) < 0) {
1049 tar_usage();
1050 break;
1051 }
1052 break;
1053 case 't':
1054 /*
1055 * list contents of the tape
1056 */
1057 tar_set_action(LIST);
1058 break;
1059 /* u: see r */
1060 case 'v':
1061 /*
1062 * verbose operation mode
1063 */
1064 vflag++;
1065 break;
1066 case 'w':
1067 /*
1068 * interactive file rename
1069 */
1070 iflag = 1;
1071 break;
1072 case 'X':
1073 /*
1074 * do not pass over mount points in the filesystem
1075 */
1076 Xflag = 1;
1077 break;
1078 case 'x':
1079 /*
1080 * extract an archive, preserving mode,
1081 * and mtime if possible.
1082 */
1083 tar_set_action(EXTRACT);
1084 pmtime = 1;
1085 break;
1086 #ifndef SMALL
1087 case 'Z':
1088 /*
1089 * use compress
1090 */
1091 compress_program = COMPRESS_CMD;
1092 break;
1093 #endif
1094 case 'z':
1095 /*
1096 * use gzip (non-standard option)
1097 */
1098 compress_program = GZIP_CMD;
1099 break;
1100 default:
1101 tar_usage();
1102 break;
1103 }
1104 }
1105 argc -= optind;
1106 argv += optind;
1107
1108 /* tar requires an action. */
1109 if (act == ERROR)
1110 tar_usage();
1111
1112 if ((arcname == NULL) || (*arcname == '\0')) {
1113 arcname = getenv("TAPE");
1114 if ((arcname == NULL) || (*arcname == '\0'))
1115 arcname = _PATH_DEFTAPE;
1116 }
1117 if ((arcname[0] == '-') && (arcname[1] == '\0'))
1118 arcname = NULL;
1119
1120 /*
1121 * Traditional tar behaviour: list-like output goes to stdout unless
1122 * writing the archive there. (pax uses stderr unless in list mode)
1123 */
1124 if (act == LIST || act == EXTRACT || arcname != NULL)
1125 listf = stdout;
1126
1127 /* Traditional tar behaviour (pax wants to read file list from stdin) */
1128 if ((act == ARCHIVE || act == APPND) && argc == 0 && nincfiles == 0)
1129 exit(0);
1130
1131 /*
1132 * process the args as they are interpreted by the operation mode
1133 */
1134 switch (act) {
1135 case EXTRACT:
1136 if (to_stdout == 2)
1137 to_stdout = 1;
1138 /* FALLTHROUGH */
1139 case LIST:
1140 default:
1141 {
1142 int sawpat = 0;
1143 char *file, *dir = NULL;
1144
1145 while (nincfiles || *argv != NULL) {
1146 /*
1147 * If we queued up any include files,
1148 * pull them in now. Otherwise, check
1149 * for -I and -C positional flags.
1150 * Anything else must be a file to
1151 * extract.
1152 */
1153 if (nincfiles) {
1154 file = incfiles->file;
1155 dir = incfiles->dir;
1156 incfiles++;
1157 nincfiles--;
1158 } else if (strcmp(*argv, "-I") == 0) {
1159 if (*++argv == NULL)
1160 break;
1161 file = *argv++;
1162 dir = chdname;
1163 } else
1164 file = NULL;
1165 if (file != NULL) {
1166 int fd;
1167 char *str;
1168
1169 if (strcmp(file, "-") == 0)
1170 fd = STDIN_FILENO;
1171 else if ((fd = binopen2(0, file, O_RDONLY)) == -1) {
1172 syswarn(1, errno,
1173 "Unable to open %s", file);
1174 tar_usage();
1175 }
1176 while ((str = fdgetline(fd)) != NULL) {
1177 if (pat_add(str, dir) < 0)
1178 tar_usage();
1179 sawpat = 1;
1180 }
1181 if (fd != STDIN_FILENO)
1182 close(fd);
1183 if (fdgetline_err) {
1184 paxwarn(1,
1185 "Unable to read from %s",
1186 strcmp(file, "-") ? file :
1187 "stdin");
1188 tar_usage();
1189 }
1190 } else if (strcmp(*argv, "-C") == 0) {
1191 if (*++argv == NULL)
1192 break;
1193 chdname = *argv++;
1194 havechd++;
1195 } else if (pat_add(*argv++, chdname) < 0)
1196 tar_usage();
1197 else
1198 sawpat = 1;
1199 }
1200 /*
1201 * if patterns were added, we are doing chdir()
1202 * on a file-by-file basis, else, just one
1203 * global chdir (if any) after opening input.
1204 */
1205 if (sawpat > 0)
1206 chdname = NULL;
1207 }
1208 break;
1209 case ARCHIVE:
1210 case APPND:
1211 if (Oflag == FSUB_MAX)
1212 tar_usage();
1213 else if (Oflag != -666)
1214 frmt = &(fsub[Oflag]);
1215
1216 if (chdname != NULL) {
1217 /* initial chdir() */
1218 if (ftree_add(chdname, 1) < 0)
1219 tar_usage();
1220 }
1221
1222 while (nincfiles || *argv != NULL) {
1223 char *file, *dir = NULL;
1224
1225 /*
1226 * If we queued up any include files, pull them in
1227 * now. Otherwise, check for -I and -C positional
1228 * flags. Anything else must be a file to include
1229 * in the archive.
1230 */
1231 if (nincfiles) {
1232 file = incfiles->file;
1233 dir = incfiles->dir;
1234 incfiles++;
1235 nincfiles--;
1236 } else if (strcmp(*argv, "-I") == 0) {
1237 if (*++argv == NULL)
1238 break;
1239 file = *argv++;
1240 dir = NULL;
1241 } else
1242 file = NULL;
1243 if (file != NULL) {
1244 int fd;
1245 char *str;
1246
1247 /* Set directory if needed */
1248 if (dir) {
1249 if (ftree_add(dir, 1) < 0)
1250 tar_usage();
1251 }
1252
1253 if (strcmp(file, "-") == 0)
1254 fd = STDIN_FILENO;
1255 else if ((fd = binopen2(0, file, O_RDONLY)) == -1) {
1256 syswarn(1, errno, "Unable to open %s",
1257 file);
1258 tar_usage();
1259 }
1260 while ((str = fdgetline(fd)) != NULL) {
1261 if (ftree_add(str, 0) < 0)
1262 tar_usage();
1263 }
1264 if (fd != STDIN_FILENO)
1265 close(fd);
1266 if (fdgetline_err) {
1267 paxwarn(1,
1268 "Unable to read from %s",
1269 strcmp(file, "-") ? file : "stdin");
1270 tar_usage();
1271 }
1272 } else if (strcmp(*argv, "-C") == 0) {
1273 if (*++argv == NULL)
1274 break;
1275 if (ftree_add(*argv++, 1) < 0)
1276 tar_usage();
1277 havechd++;
1278 } else if (ftree_add(*argv++, 0) < 0)
1279 tar_usage();
1280 }
1281 /*
1282 * no read errors allowed on updates/append operation!
1283 */
1284 maxflt = 0;
1285 break;
1286 }
1287 if (to_stdout != 1)
1288 to_stdout = 0;
1289 }
1290
1291 static int
1292 mkpath(char *path)
1293 {
1294 struct stat sb;
1295 char *slash;
1296 int done = 0;
1297
1298 slash = path;
1299
1300 while (!done) {
1301 slash += strspn(slash, "/");
1302 slash += strcspn(slash, "/");
1303
1304 done = (*slash == '\0');
1305 *slash = '\0';
1306
1307 if (stat(path, &sb)) {
1308 if (errno != ENOENT || mkdir(path, 0777)) {
1309 paxwarn(1, "%s", path);
1310 return (-1);
1311 }
1312 } else if (!S_ISDIR(sb.st_mode)) {
1313 syswarn(1, ENOTDIR, "%s", path);
1314 return (-1);
1315 }
1316
1317 if (!done)
1318 *slash = '/';
1319 }
1320
1321 return (0);
1322 }
1323
1324 /*
1325 * cpio_options()
1326 * look at the user specified flags. set globals as required and check if
1327 * the user specified a legal set of flags. If not, complain and exit
1328 */
1329
1330 static void
1331 cpio_set_action(int op)
1332 {
1333 if ((act == APPND && op == ARCHIVE) || (act == ARCHIVE && op == APPND))
1334 act = APPND;
1335 else if ((act == LIST && op == EXTRACT) || (act == EXTRACT && op == LIST))
1336 act = LIST;
1337 else if (act != ERROR && act != op)
1338 cpio_usage();
1339 else
1340 act = op;
1341 }
1342
1343 static void
1344 cpio_options(int argc, char **argv)
1345 {
1346 const char *errstr;
1347 int c, list_only = 0;
1348 char *str;
1349 int fd;
1350 const char *optstr;
1351 FSUB tmp;
1352
1353 kflag = 1;
1354 pids = 1;
1355 pmode = 1;
1356 pmtime = 0;
1357 arcname = NULL;
1358 dflag = 1;
1359 nodirs = 1;
1360 optstr = "iop";
1361 opterr = 0;
1362 while ((c = getopt(argc, argv, optstr)) != -1) {
1363 switch (c) {
1364 case '0':
1365 /*
1366 * Use \0 as pathname terminator.
1367 * (For use with the -print0 option of find(1).)
1368 */
1369 zeroflag = 1;
1370 break;
1371 #ifndef SMALL
1372 case '6':
1373 /*
1374 * process Version 6 cpio format
1375 */
1376 frmt = &(fsub[FSUB_BCPIO]);
1377 break;
1378 #endif
1379 case 'A':
1380 /*
1381 * append mode
1382 */
1383 cpio_set_action(APPND);
1384 break;
1385 case 'a':
1386 /*
1387 * preserve access time on files read
1388 */
1389 tflag = 1;
1390 break;
1391 case 'B':
1392 /*
1393 * Use 5120 byte block size
1394 */
1395 wrblksz = 5120;
1396 break;
1397 case 'b':
1398 /*
1399 * swap bytes and half-words when reading data
1400 */
1401 break;
1402 case 'C':
1403 /*
1404 * set block size in bytes
1405 */
1406 wrblksz = strtonum(optarg, 0, INT_MAX, &errstr);
1407 if (errstr) {
1408 paxwarn(1, "Invalid block size %s: %s",
1409 optarg, errstr);
1410 pax_usage();
1411 }
1412 break;
1413 case 'c':
1414 /*
1415 * ASCII cpio header
1416 */
1417 frmt = &(fsub[FSUB_CPIO]);
1418 break;
1419 case 'd':
1420 /*
1421 * create directories as needed
1422 */
1423 nodirs = 0;
1424 break;
1425 case 'E':
1426 /*
1427 * file with patterns to extract or list
1428 */
1429 if ((fd = binopen2(0, optarg, O_RDONLY)) == -1) {
1430 syswarn(1, errno, "Unable to open %s",
1431 optarg);
1432 cpio_usage();
1433 }
1434 while ((str = fdgetline(fd)) != NULL) {
1435 pat_add(str, NULL);
1436 }
1437 close(fd);
1438 if (fdgetline_err) {
1439 paxwarn(1,
1440 "Unable to read from %s", optarg);
1441 cpio_usage();
1442 }
1443 break;
1444 case 'F':
1445 case 'I':
1446 case 'O':
1447 /*
1448 * filename where the archive is stored
1449 */
1450 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
1451 /*
1452 * treat a - as stdin
1453 */
1454 arcname = NULL;
1455 break;
1456 }
1457 arcname = optarg;
1458 break;
1459 case 'f':
1460 /*
1461 * invert meaning of pattern list
1462 */
1463 cflag = 1;
1464 break;
1465 case 'H':
1466 /*
1467 * specify an archive format on write
1468 */
1469 if (!strcmp(optarg, "bin")) {
1470 tmp.name = "bcpio";
1471 } else if (!strcmp(optarg, "crc")) {
1472 tmp.name = "sv4crc";
1473 } else if (!strcmp(optarg, "newc")) {
1474 tmp.name = "sv4cpio";
1475 } else if (!strcmp(optarg, "odc")) {
1476 tmp.name = "cpio";
1477 } else {
1478 tmp.name = optarg;
1479 }
1480 if (gather_format(&tmp, "cpio", 'H'))
1481 cpio_usage();
1482 break;
1483 /* I: see F */
1484 case 'i':
1485 /*
1486 * restore an archive
1487 */
1488 cpio_set_action(EXTRACT);
1489 break;
1490 #ifndef SMALL
1491 case 'J':
1492 /*
1493 * use xz (non-standard option)
1494 */
1495 compress_program = XZ_CMD;
1496 break;
1497 case 'j':
1498 /*
1499 * use bzip2 (non-standard option)
1500 */
1501 compress_program = BZIP2_CMD;
1502 break;
1503 #endif
1504 case 'k':
1505 break;
1506 case 'L':
1507 /*
1508 * follow symbolic links
1509 */
1510 Lflag = 1;
1511 break;
1512 case 'l':
1513 /*
1514 * use links instead of copies when possible
1515 */
1516 lflag = 1;
1517 break;
1518 case 'M':
1519 /*
1520 * MirOS extension: archive normaliser
1521 */
1522 process_M(optarg, cpio_usage);
1523 break;
1524 case 'm':
1525 /*
1526 * preserve modification time
1527 */
1528 pmtime = 1;
1529 break;
1530 /* O: see F */
1531 case 'o':
1532 /*
1533 * create an archive
1534 */
1535 cpio_set_action(ARCHIVE);
1536 frmt = &(fsub[FSUB_SV4CRC]);
1537 break;
1538 case 'p':
1539 /*
1540 * copy-pass mode
1541 */
1542 cpio_set_action(COPY);
1543 break;
1544 case 'r':
1545 /*
1546 * interactively rename files
1547 */
1548 iflag = 1;
1549 break;
1550 case 'S':
1551 /*
1552 * swap halfwords after reading data
1553 */
1554 break;
1555 case 's':
1556 /*
1557 * swap bytes after reading data
1558 */
1559 break;
1560 case 't':
1561 /*
1562 * list contents of archive
1563 */
1564 list_only = 1;
1565 break;
1566 case 'u':
1567 /*
1568 * replace newer files
1569 */
1570 kflag = 0;
1571 break;
1572 case 'V':
1573 /*
1574 * print a dot for each file processed
1575 */
1576 Vflag++;
1577 break;
1578 case 'v':
1579 /*
1580 * verbose operation mode
1581 */
1582 ++vflag;
1583 break;
1584 #ifndef SMALL
1585 case 'Z':
1586 /*
1587 * use compress (non-standard option)
1588 */
1589 compress_program = COMPRESS_CMD;
1590 break;
1591 #endif
1592 case 'z':
1593 /*
1594 * use gzip (non-standard option)
1595 */
1596 compress_program = GZIP_CMD;
1597 break;
1598 case '?':
1599 default:
1600 if (opterr == 0) {
1601 paxwarn(1, "need -i or -o or -p option first");
1602 }
1603 cpio_usage();
1604 break;
1605 }
1606 if (opterr == 0) {
1607 optstr = "06AaBbC:cdE:F:fH:I:iJjkLlM:mO:oprSstuVvZz";
1608 opterr = 1;
1609 }
1610 }
1611 argc -= optind;
1612 argv += optind;
1613
1614 /*
1615 * process the args as they are interpreted by the operation mode
1616 */
1617 switch (act) {
1618 case EXTRACT:
1619 if (list_only) {
1620 act = LIST;
1621
1622 /*
1623 * cpio is like pax: list to stderr
1624 * unless in list mode
1625 */
1626 listf = stdout;
1627 }
1628 while (*argv != NULL)
1629 if (pat_add(*argv++, NULL) < 0)
1630 cpio_usage();
1631 break;
1632 case COPY:
1633 if (*argv == NULL) {
1634 paxwarn(0, "Destination directory was not supplied");
1635 cpio_usage();
1636 }
1637 dirptr = *argv;
1638 if (mkpath(dirptr) < 0)
1639 cpio_usage();
1640 --argc;
1641 ++argv;
1642 /* FALLTHROUGH */
1643 case ARCHIVE:
1644 case APPND:
1645 if (*argv != NULL)
1646 cpio_usage();
1647 /*
1648 * no read errors allowed on updates/append operation!
1649 */
1650 maxflt = 0;
1651 while ((str = fdgetline(STDIN_FILENO)) != NULL) {
1652 ftree_add(str, 0);
1653 }
1654 if (fdgetline_err) {
1655 paxwarn(1, "Unable to read from %s",
1656 "stdin");
1657 cpio_usage();
1658 }
1659 break;
1660 default:
1661 cpio_usage();
1662 break;
1663 }
1664 }
1665
1666 /*
1667 * printflg()
1668 * print out those invalid flag sets found to the user
1669 */
1670
1671 static void
1672 printflg(unsigned int flg)
1673 {
1674 int nxt;
1675 int pos = 0;
1676
1677 (void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
1678 while ((nxt = ffs(flg)) != 0) {
1679 flg >>= nxt;
1680 pos += nxt;
1681 (void)fprintf(stderr, " -%c", flgch[pos-1]);
1682 }
1683 (void)putc('\n', stderr);
1684 }
1685
1686 /*
1687 * c_frmt()
1688 * comparison routine used by bsearch to find the format specified
1689 * by the user
1690 */
1691
1692 static int
1693 c_frmt(const void *a, const void *b)
1694 {
1695 if (!((const FSUB *)b)->name)
1696 return (-1);
1697 return(strcmp(((const FSUB *)a)->name, ((const FSUB *)b)->name));
1698 }
1699
1700 /*
1701 * opt_next()
1702 * called by format specific options routines to get each format specific
1703 * flag and value specified with -o
1704 * Return:
1705 * pointer to next OPLIST entry or NULL (end of list).
1706 */
1707
1708 OPLIST *
1709 opt_next(void)
1710 {
1711 OPLIST *opt;
1712
1713 if ((opt = ophead) != NULL)
1714 ophead = ophead->fow;
1715 return(opt);
1716 }
1717
1718 /*
1719 * bad_opt()
1720 * generic routine used to complain about a format specific options
1721 * when the format does not support options.
1722 */
1723
1724 int
1725 bad_opt(void)
1726 {
1727 OPLIST *opt;
1728
1729 if (ophead == NULL)
1730 return(0);
1731 /*
1732 * print all we were given
1733 */
1734 paxwarn(1,"These format options are not supported");
1735 while ((opt = opt_next()) != NULL)
1736 (void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
1737 pax_usage();
1738 return(0);
1739 }
1740
1741 /*
1742 * opt_add()
1743 * breaks the value supplied to -o into a option name and value.
1744 * options are given to -o in the form -o name=value,name=value
1745 * or to -D in the form format,name=value[,name=value ...]
1746 * multiple -o may be specified.
1747 * Return:
1748 * 0 if format in name=value format, -1 if -o is passed junk
1749 */
1750
1751 int
1752 opt_add(const char *str)
1753 {
1754 OPLIST *opt;
1755 char *frpt;
1756 char *pt;
1757 char *endpt;
1758 char *dstr;
1759
1760 if ((str == NULL) || (*str == '\0')) {
1761 paxwarn(0, "Invalid option name");
1762 return(-1);
1763 }
1764 if ((dstr = strdup(str)) == NULL) {
1765 paxwarn(0, "Unable to allocate space for option list");
1766 return(-1);
1767 }
1768 frpt = endpt = dstr;
1769
1770 /*
1771 * break into name and values pieces and stuff each one into a
1772 * OPLIST structure. When we know the format, the format specific
1773 * option function will go through this list
1774 */
1775 while ((frpt != NULL) && (*frpt != '\0')) {
1776 if ((endpt = strchr(frpt, ',')) != NULL)
1777 *endpt = '\0';
1778 if ((pt = strchr(frpt, '=')) == NULL) {
1779 paxwarn(0, "Invalid options format");
1780 free(dstr);
1781 return(-1);
1782 }
1783 if ((opt = malloc(sizeof(OPLIST))) == NULL) {
1784 paxwarn(0, "Unable to allocate space for option list");
1785 free(dstr);
1786 return(-1);
1787 }
1788 /* parts of string going onto the OPLIST */
1789 dstr = NULL;
1790 *pt++ = '\0';
1791 opt->name = frpt;
1792 opt->value = pt;
1793 opt->fow = NULL;
1794 if (endpt != NULL)
1795 frpt = endpt + 1;
1796 else
1797 frpt = NULL;
1798 if (ophead == NULL) {
1799 optail = ophead = opt;
1800 continue;
1801 }
1802 optail->fow = opt;
1803 optail = opt;
1804 }
1805 free(dstr);
1806 return(0);
1807 }
1808
1809 /*
1810 * str_offt()
1811 * Convert an expression of the following forms to an off_t > 0.
1812 * 1) A positive decimal number.
1813 * 2) A positive decimal number followed by a b (mult by 512).
1814 * 3) A positive decimal number followed by a k (mult by 1024).
1815 * 4) A positive decimal number followed by a m (mult by 512).
1816 * 5) A positive decimal number followed by a w (mult by sizeof int)
1817 * 6) Two or more positive decimal numbers (with/without k,b or w).
1818 * separated by x (also * for backwards compatibility), specifying
1819 * the product of the indicated values.
1820 * Return:
1821 * 0 for an error, a positive value o.w.
1822 */
1823
1824 #if HAVE_OFFT_LONG
1825 #define OT_MAX LONG_MAX
1826 #define strtoot strtol
1827 #else
1828 #define OT_MAX LLONG_MAX
1829 #define strtoot strtoll
1830 #endif
1831
1832 static off_t
1833 str_offt(char *val)
1834 {
1835 char *expr;
1836 off_t num, t;
1837
1838 num = strtoot(val, &expr, 0);
1839 if ((num == OT_MAX) || (num <= 0) || (expr == val))
1840 return (0);
1841
1842 switch (*expr) {
1843 case 'b':
1844 t = num;
1845 num *= 512;
1846 if (t > num)
1847 return (0);
1848 ++expr;
1849 break;
1850 case 'k':
1851 t = num;
1852 num *= 1024;
1853 if (t > num)
1854 return (0);
1855 ++expr;
1856 break;
1857 case 'm':
1858 t = num;
1859 num *= 1048576;
1860 if (t > num)
1861 return (0);
1862 ++expr;
1863 break;
1864 case 'w':
1865 t = num;
1866 num *= sizeof(int);
1867 if (t > num)
1868 return (0);
1869 ++expr;
1870 break;
1871 }
1872
1873 switch (*expr) {
1874 case '\0':
1875 break;
1876 case '*':
1877 case 'x':
1878 t = num;
1879 num *= str_offt(expr + 1);
1880 if (t > num)
1881 return(0);
1882 break;
1883 default:
1884 return(0);
1885 }
1886 return (num);
1887 }
1888
1889 #ifndef SMALL
1890 /*
1891 * no_op()
1892 * for those option functions where the archive format has nothing to do.
1893 * Return:
1894 * 0
1895 */
1896
1897 static int
1898 no_op(void)
1899 {
1900 return (0);
1901 }
1902
1903 static int
1904 no_op_i(int is_app MKSH_A_UNUSED)
1905 {
1906 return (0);
1907 }
1908 #endif
1909
1910 /*
1911 * pax_usage()
1912 * print the usage summary to the user
1913 */
1914
1915 void
1916 pax_usage(void)
1917 {
1918 (void)fputs(
1919 #ifndef SMALL
1920 "paxmirabilis " MIRCPIO_VERSION "\n"
1921 #endif
1922 "usage: pax [-0cdJjnOvz] [-E limit] [-f archive] [-G group] [-s replstr]\n"
1923 " [-T range] [-U user] [pattern ...]\n"
1924 " pax -r [-0cDdiJjknOuvYZz] [-E limit] [-f archive] [-G group] [-M flag]\n"
1925 " [-o options] [-p string] [-s replstr] [-T range] [-U user]\n"
1926 " [pattern ...]\n"
1927 " pax -w [-0adHiJjLOPtuvXz] [-B bytes] [-b blocksize] [-f archive]\n"
1928 " [-G group] [-M flag] [-o options] [-s replstr] [-T range]\n"
1929 " [-U user] [-x format] [file ...]\n"
1930 " pax -rw [-0DdHikLlnOPtuvXYZ] [-G group] [-p string] [-s replstr]\n"
1931 " [-T range] [-U user] [file ...] directory\n",
1932 stderr);
1933 exit(1);
1934 }
1935
1936 /*
1937 * tar_usage()
1938 * print the usage summary to the user
1939 */
1940
1941 void
1942 tar_usage(void)
1943 {
1944 (void)fputs(
1945 #ifndef SMALL
1946 "paxmirabilis " MIRCPIO_VERSION "\n"
1947 #endif
1948 "usage: tar {crtux}[014578AabefHhJjLmNOoPpqRSsvwXZz]\n"
1949 " [blocking-factor | archive | replstr] [-C directory] [-I file]\n"
1950 " [file ...]\n"
1951 " tar {-crtux} [-014578AaeHhJjLmNOoPpqRSvwXZz] [-b blocking-factor]\n"
1952 " [-C directory] [-D format-options] [-f archive] [-I file]\n"
1953 " [-M flag] [-s replstr] [file ...]\n",
1954 stderr);
1955 exit(1);
1956 }
1957
1958 /*
1959 * cpio_usage()
1960 * print the usage summary to the user
1961 */
1962
1963 void
1964 cpio_usage(void)
1965 {
1966 (void)fputs(
1967 #ifndef SMALL
1968 "paxmirabilis " MIRCPIO_VERSION "\n"
1969 #endif
1970 "usage: cpio -o [-0AaBcJjLVvZz] [-C bytes] [-F archive] [-H format]\n"
1971 " [-M flag] [-O archive] <name-list [>archive]\n"
1972 " cpio -i [-06BbcdfJjmrSstuVvZz] [-C bytes] [-E file] [-F archive]\n"
1973 " [-H format] [-I archive] [-M flag] [pattern ...] [<archive]\n"
1974 " cpio -p [-0adLlmuVv] destination-directory <name-list\n",
1975 stderr);
1976 exit(1);
1977 }
1978
1979 void
1980 anonarch_init(void)
1981 {
1982 if (anonarch & ANON_VERBOSE) {
1983 anonarch &= ~ANON_VERBOSE;
1984 paxwarn(0, "debug: -M 0x%08X -x %s", anonarch, frmt->name);
1985 }
1986 }
1987
1988 static void
1989 process_M(const char *arg, void (*call_usage)(void))
1990 {
1991 int j, k = 0;
1992
1993 if ((arg[0] >= '0') && (arg[0] <= '9')) {
1994 #if HAVE_STRTONUM
1995 const char *s;
1996 int64_t i = strtonum(arg, 0,
1997 ANON_MAXVAL, &s);
1998 if (s)
1999 errx(1, "%s M value: %s", s,
2000 arg);
2001 #else
2002 char *ep;
2003 long long i = strtoll(arg, &ep, 0);
2004 if ((ep == arg) || (*ep != '\0') ||
2005 (i < 0) || (i > ANON_MAXVAL))
2006 errx(1, "impossible M value:"
2007 " %s", arg);
2008 #endif
2009 anonarch = i;
2010 return;
2011 }
2012
2013 if (!strncmp(arg, "no-", 3)) {
2014 j = 0;
2015 arg += 3;
2016 } else
2017 j = 1;
2018 if (!strncmp(arg, "uid", 3) ||
2019 !strncmp(arg, "gid", 3)) {
2020 k = ANON_UIDGID;
2021 } else if (!strncmp(arg, "ino", 3)) {
2022 k = ANON_INODES;
2023 } else if (!strncmp(arg, "mtim", 4)) {
2024 k = ANON_MTIME;
2025 } else if (!strncmp(arg, "link", 4)) {
2026 k = ANON_HARDLINKS;
2027 } else if (!strncmp(arg, "norm", 4)) {
2028 k = ANON_UIDGID | ANON_INODES | ANON_NUMID |
2029 ANON_MTIME | ANON_HARDLINKS;
2030 } else if (!strncmp(arg, "root", 4)) {
2031 k = ANON_UIDGID | ANON_INODES | ANON_NUMID;
2032 } else if (!strncmp(arg, "dist", 4)) {
2033 k = ANON_UIDGID | ANON_INODES | ANON_NUMID |
2034 ANON_HARDLINKS;
2035 } else if (!strncmp(arg, "set", 3)) {
2036 k = ANON_INODES | ANON_HARDLINKS;
2037 } else if (!strncmp(arg, "v", 1)) {
2038 k = ANON_VERBOSE;
2039 } else if (!strncmp(arg, "debug", 5)) {
2040 k = ANON_DEBUG;
2041 } else if (!strncmp(arg, "lncp", 4)) {
2042 k = ANON_LNCP;
2043 } else if (!strncmp(arg, "numid", 5)) {
2044 k = ANON_NUMID;
2045 } else if (!strncmp(arg, "gslash", 6)) {
2046 k = ANON_DIRSLASH;
2047 } else
2048 call_usage();
2049 if (j)
2050 anonarch |= k;
2051 else
2052 anonarch &= ~k;
2053 }
2054
2055 void
2056 guess_compress_program(int wr __attribute__((__unused__)))
2057 {
2058 const char *ccp;
2059
2060 if (compress_program != COMPRESS_GUESS_CMD)
2061 return;
2062
2063 if (arcname == NULL || (ccp = strrchr(arcname, '.')) == NULL) {
2064 compress_program = NULL;
2065 return;
2066 }
2067 ++ccp;
2068
2069 /* guess standard format gzip */
2070 if (!strcmp(ccp, "gz") ||
2071 !strcmp(ccp, "tgz") ||
2072 !strcmp(ccp, "cgz") ||
2073 !strcmp(ccp, "ngz") ||
2074 !strcmp(ccp, "taz")) {
2075 compress_program = GZIP_CMD;
2076 return;
2077 }
2078
2079 #ifndef SMALL
2080 /* guess extended format xz */
2081 if (!strcmp(ccp, "xz") ||
2082 !strcmp(ccp, "txz") ||
2083 !strcmp(ccp, "cxz") ||
2084 !strcmp(ccp, "nxz")) {
2085 compress_program = XZ_CMD;
2086 return;
2087 }
2088
2089 /* guess extended format bzip2 (not bzip) */
2090 if (!strcmp(ccp, "bz2") ||
2091 !strcmp(ccp, "tbz") ||
2092 !strcmp(ccp, "tz2") ||
2093 !strcmp(ccp, "tbz2") ||
2094 !strcmp(ccp, "cbz") ||
2095 !strcmp(ccp, "nbz")) {
2096 compress_program = BZIP2_CMD;
2097 return;
2098 }
2099
2100 /* guess standard format Unix compress */
2101 if (!strcmp(ccp, "Z") ||
2102 !strcmp(ccp, "mcz") ||
2103 !strcmp(ccp, "taZ")) {
2104 compress_program = COMPRESS_CMD;
2105 return;
2106 }
2107
2108 /* guess extended format lzma (using xz for decompression) */
2109 if (!strcmp(ccp, "lzma")) {
2110 compress_program = wr ? LZMA_WRCMD : XZ_CMD;
2111 return;
2112 }
2113
2114 /* guess extended format lzop */
2115 if (!strcmp(ccp, "lzo")) {
2116 compress_program = LZOP_CMD;
2117 return;
2118 }
2119 #endif
2120
2121 /* no sugar */
2122 compress_program = NULL;
2123 }
2124
2125 #ifndef SMALL
2126 static int
2127 compress_id(char *blk, int size)
2128 {
2129 if (size >= 2 && blk[0] == '\037' && blk[1] == '\235') {
2130 paxwarn(0, "input compressed with %s; use the -%c option"
2131 " to decompress it", "compress", 'Z');
2132 exit(1);
2133 }
2134 return (-1);
2135 }
2136
2137 static int
2138 gzip_id(char *blk, int size)
2139 {
2140 if (size >= 2 && blk[0] == '\037' && blk[1] == '\213') {
2141 paxwarn(0, "input compressed with %s; use the -%c option"
2142 " to decompress it", "gzip", 'z');
2143 exit(1);
2144 }
2145 return (-1);
2146 }
2147
2148 static int
2149 bzip2_id(char *blk, int size)
2150 {
2151 if (size >= 3 && blk[0] == 'B' && blk[1] == 'Z' && blk[2] == 'h') {
2152 paxwarn(0, "input compressed with %s; use the -%c option"
2153 " to decompress it", "bzip2", 'j');
2154 exit(1);
2155 }
2156 return (-1);
2157 }
2158
2159 static int
2160 xz_id(char *blk, int size)
2161 {
2162 if (size >= 6 && memcmp(blk, "\xFD\x37\x7A\x58\x5A", 6) == 0) {
2163 paxwarn(0, "input compressed with %s; use the -%c option"
2164 " to decompress it", "xz", 'J');
2165 exit(1);
2166 }
2167 return (-1);
2168 }
2169
2170 void
2171 mircpio_deprecated(const char *what, const char *with)
2172 {
2173 paxwarn(0, "the old MirCPIO %s is deprecated and will be removed soon; use %s instead",
2174 what, with);
2175 }
2176 #endif /* !SMALL */