source: trunk/minix/commands/pax/options.c@ 21

Last change on this file since 21 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 32.4 KB
Line 
1/*-
2 * Copyright (c) 1992 Keith Muller.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Keith Muller of the University of California, San Diego.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#define _POSIX_SOURCE 1
35#define _MINIX 1
36
37#if 0
38#ifndef lint
39static char sccsid[] = "@(#)options.c 8.2 (Berkeley) 4/18/94";
40#endif /* not lint */
41#endif
42
43
44#include <sys/types.h>
45#include <sys/stat.h>
46#include <sys/mtio.h>
47#include <stdio.h>
48#include <string.h>
49#include <errno.h>
50#include <unistd.h>
51#include <stdlib.h>
52#include <limits.h>
53#include <minix/paths.h>
54
55#include <sys/types.h>
56#include <string.h>
57#include <strings.h>
58#include <unistd.h>
59
60
61#include "pax.h"
62#include "options.h"
63#include "cpio.h"
64#include "tar.h"
65#include "extern.h"
66
67/*
68 * Routines which handle command line options
69 */
70
71static char flgch[] = FLGCH; /* list of all possible flags */
72static OPLIST *ophead = NULL; /* head for format specific options -x */
73static OPLIST *optail = NULL; /* option tail */
74
75static int no_op(void);
76static void printflg(unsigned int);
77static int c_frmt(const void *, const void *);
78static off_t str_offt(char *);
79static char *getline(FILE *fp);
80static void pax_options(int, char **);
81static void pax_usage(void);
82static void tar_options(int, char **);
83static void tar_usage(void);
84static void cpio_options(int, char **);
85static void cpio_usage(void);
86
87/* errors from getline */
88#define GETLINE_FILE_CORRUPT 1
89#define GETLINE_OUT_OF_MEM 2
90static int getline_error;
91
92
93#define GZIP_CMD "gzip" /* command to run as gzip */
94#define COMPRESS_CMD "compress" /* command to run as compress */
95#define BZIP2_CMD "bzip2" /* command to run as gzip */
96
97/*
98 * Format specific routine table - MUST BE IN SORTED ORDER BY NAME
99 * (see pax.h for description of each function)
100 *
101 * name, blksz, hdsz, udev, hlk, blkagn, inhead, id, st_read,
102 * read, end_read, st_write, write, end_write, trail,
103 * rd_data, wr_data, options
104 */
105
106FSUB fsub[] = {
107/* 0: OLD BINARY CPIO */
108 {"bcpio", 5120, sizeof(HD_BCPIO), 1, 0, 0, 1, bcpio_id, cpio_strd,
109 bcpio_rd, bcpio_endrd, cpio_stwr, bcpio_wr, cpio_endwr, cpio_trail,
110 NULL, rd_wrfile, wr_rdfile, bad_opt},
111
112/* 1: OLD OCTAL CHARACTER CPIO */
113 {"cpio", 5120, sizeof(HD_CPIO), 1, 0, 0, 1, cpio_id, cpio_strd,
114 cpio_rd, cpio_endrd, cpio_stwr, cpio_wr, cpio_endwr, cpio_trail,
115 NULL, rd_wrfile, wr_rdfile, bad_opt},
116
117/* 2: SVR4 HEX CPIO */
118 {"sv4cpio", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, vcpio_id, cpio_strd,
119 vcpio_rd, vcpio_endrd, cpio_stwr, vcpio_wr, cpio_endwr, cpio_trail,
120 NULL, rd_wrfile, wr_rdfile, bad_opt},
121
122/* 3: SVR4 HEX CPIO WITH CRC */
123 {"sv4crc", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, crc_id, crc_strd,
124 vcpio_rd, vcpio_endrd, crc_stwr, vcpio_wr, cpio_endwr, cpio_trail,
125 NULL, rd_wrfile, wr_rdfile, bad_opt},
126
127/* 4: OLD TAR */
128 {"tar", 10240, BLKMULT, 0, 1, BLKMULT, 0, tar_id, no_op,
129 tar_rd, tar_endrd, no_op, tar_wr, tar_endwr, NULL, tar_trail,
130 rd_wrfile, wr_rdfile, tar_opt},
131
132/* 5: POSIX USTAR */
133 {"ustar", 10240, BLKMULT, 0, 1, BLKMULT, 0, ustar_id, ustar_strd,
134 ustar_rd, tar_endrd, ustar_stwr, ustar_wr, tar_endwr, NULL, tar_trail,
135 rd_wrfile, wr_rdfile, bad_opt},
136};
137#define F_OCPIO 0 /* format when called as cpio -6 */
138#define F_ACPIO 1 /* format when called as cpio -c */
139#define F_CPIO 3 /* format when called as cpio */
140#define F_OTAR 4 /* format when called as tar -o */
141#define F_TAR 5 /* format when called as tar */
142#define DEFLT 5 /* default write format from list above */
143
144/*
145 * ford is the archive search order used by get_arc() to determine what kind
146 * of archive we are dealing with. This helps to properly id archive formats
147 * some formats may be subsets of others....
148 */
149int ford[] = {5, 4, 3, 2, 1, 0, -1 };
150
151/*
152 * options()
153 * figure out if we are pax, tar or cpio. Call the appropriate options
154 * parser
155 */
156
157void
158options(int argc, char **argv)
159{
160
161 /*
162 * Are we acting like pax, tar or cpio (based on argv[0])
163 */
164 if ((argv0 = strrchr(argv[0], '/')) != NULL)
165 argv0++;
166 else
167 argv0 = argv[0];
168
169 if (strcmp(NM_TAR, argv0) == 0) {
170 tar_options(argc, argv);
171 return;
172 }
173 else if (strcmp(NM_CPIO, argv0) == 0) {
174 cpio_options(argc, argv);
175 return;
176 }
177 /*
178 * assume pax as the default
179 */
180 argv0 = NM_PAX;
181 pax_options(argc, argv);
182 return;
183}
184
185/*
186 * pax_options()
187 * look at the user specified flags. set globals as required and check if
188 * the user specified a legal set of flags. If not, complain and exit
189 */
190
191static void
192pax_options(int argc, char **argv)
193{
194 int c;
195 size_t i;
196 unsigned int flg = 0;
197 unsigned int bflg = 0;
198 char *pt;
199 FSUB tmp;
200
201 /*
202 * process option flags
203 */
204 while ((c=getopt(argc,argv,"ab:cdf:iklno:p:rs:tuvwx:zB:DE:G:HLPT:U:XYZ"))
205 != -1) {
206 switch (c) {
207 case 'a':
208 /*
209 * append
210 */
211 flg |= AF;
212 break;
213 case 'b':
214 /*
215 * specify blocksize
216 */
217 flg |= BF;
218 if ((wrblksz = (int)str_offt(optarg)) <= 0) {
219 paxwarn(1, "Invalid block size %s", optarg);
220 pax_usage();
221 }
222 break;
223 case 'c':
224 /*
225 * inverse match on patterns
226 */
227 cflag = 1;
228 flg |= CF;
229 break;
230 case 'd':
231 /*
232 * match only dir on extract, not the subtree at dir
233 */
234 dflag = 1;
235 flg |= DF;
236 break;
237 case 'f':
238 /*
239 * filename where the archive is stored
240 */
241 arcname = optarg;
242 flg |= FF;
243 break;
244 case 'i':
245 /*
246 * interactive file rename
247 */
248 iflag = 1;
249 flg |= IF;
250 break;
251 case 'k':
252 /*
253 * do not clobber files that exist
254 */
255 kflag = 1;
256 flg |= KF;
257 break;
258 case 'l':
259 /*
260 * try to link src to dest with copy (-rw)
261 */
262 lflag = 1;
263 flg |= LF;
264 break;
265 case 'n':
266 /*
267 * select first match for a pattern only
268 */
269 nflag = 1;
270 flg |= NF;
271 break;
272 case 'o':
273 /*
274 * pass format specific options
275 */
276 flg |= OF;
277 if (opt_add(optarg) < 0)
278 pax_usage();
279 break;
280 case 'p':
281 /*
282 * specify file characteristic options
283 */
284 for (pt = optarg; *pt != '\0'; ++pt) {
285 switch(*pt) {
286 case 'a':
287 /*
288 * do not preserve access time
289 */
290 patime = 0;
291 break;
292 case 'e':
293 /*
294 * preserve user id, group id, file
295 * mode, access/modification times
296 */
297 pids = 1;
298 pmode = 1;
299 patime = 1;
300 pmtime = 1;
301 break;
302 case 'm':
303 /*
304 * do not preserve modification time
305 */
306 pmtime = 0;
307 break;
308 case 'o':
309 /*
310 * preserve uid/gid
311 */
312 pids = 1;
313 break;
314 case 'p':
315 /*
316 * preserver file mode bits
317 */
318 pmode = 1;
319 break;
320 default:
321 paxwarn(1, "Invalid -p string: %c", *pt);
322 pax_usage();
323 break;
324 }
325 }
326 flg |= PF;
327 break;
328 case 'r':
329 /*
330 * read the archive
331 */
332 flg |= RF;
333 break;
334 case 's':
335 /*
336 * file name substitution name pattern
337 */
338 if (rep_add(optarg) < 0) {
339 pax_usage();
340 break;
341 }
342 flg |= SF;
343 break;
344 case 't':
345 /*
346 * preserve access time on file system nodes we read
347 */
348 tflag = 1;
349 flg |= TF;
350 break;
351 case 'u':
352 /*
353 * ignore those older files
354 */
355 uflag = 1;
356 flg |= UF;
357 break;
358 case 'v':
359 /*
360 * verbose operation mode
361 */
362 vflag = 1;
363 flg |= VF;
364 break;
365 case 'w':
366 /*
367 * write an archive
368 */
369 flg |= WF;
370 break;
371 case 'x':
372 /*
373 * specify an archive format on write
374 */
375 tmp.name = optarg;
376 if ((frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
377 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt)) != NULL) {
378 flg |= XF;
379 break;
380 }
381 paxwarn(1, "Unknown -x format: %s", optarg);
382 (void)fputs("pax: Known -x formats are:", stderr);
383 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
384 (void)fprintf(stderr, " %s", fsub[i].name);
385 (void)fputs("\n\n", stderr);
386 pax_usage();
387 break;
388 case 'z':
389 /*
390 * use gzip. Non standard option.
391 */
392 gzip_program = GZIP_CMD;
393 break;
394 case 'B':
395 /*
396 * non-standard option on number of bytes written on a
397 * single archive volume.
398 */
399 if ((wrlimit = str_offt(optarg)) <= 0) {
400 paxwarn(1, "Invalid write limit %s", optarg);
401 pax_usage();
402 }
403 if (wrlimit % BLKMULT) {
404 paxwarn(1, "Write limit is not a %d byte multiple",
405 BLKMULT);
406 pax_usage();
407 }
408 flg |= CBF;
409 break;
410 case 'D':
411 /*
412 * On extraction check file inode change time before the
413 * modification of the file name. Non standard option.
414 */
415 Dflag = 1;
416 flg |= CDF;
417 break;
418 case 'E':
419 /*
420 * non-standard limit on read faults
421 * 0 indicates stop after first error, values
422 * indicate a limit, "NONE" try forever
423 */
424 flg |= CEF;
425 if (strcmp(NONE, optarg) == 0)
426 maxflt = -1;
427 else if ((maxflt = atoi(optarg)) < 0) {
428 paxwarn(1, "Error count value must be positive");
429 pax_usage();
430 }
431 break;
432 case 'G':
433 /*
434 * non-standard option for selecting files within an
435 * archive by group (gid or name)
436 */
437 if (grp_add(optarg) < 0) {
438 pax_usage();
439 break;
440 }
441 flg |= CGF;
442 break;
443 case 'H':
444 /*
445 * follow command line symlinks only
446 */
447 Hflag = 1;
448 flg |= CHF;
449 break;
450 case 'L':
451 /*
452 * follow symlinks
453 */
454 Lflag = 1;
455 flg |= CLF;
456 break;
457 case 'P':
458 /*
459 * do NOT follow symlinks (default)
460 */
461 Lflag = 0;
462 flg |= CPF;
463 break;
464 case 'T':
465 /*
466 * non-standard option for selecting files within an
467 * archive by modification time range (lower,upper)
468 */
469 if (trng_add(optarg) < 0) {
470 pax_usage();
471 break;
472 }
473 flg |= CTF;
474 break;
475 case 'U':
476 /*
477 * non-standard option for selecting files within an
478 * archive by user (uid or name)
479 */
480 if (usr_add(optarg) < 0) {
481 pax_usage();
482 break;
483 }
484 flg |= CUF;
485 break;
486 case 'X':
487 /*
488 * do not pass over mount points in the file system
489 */
490 Xflag = 1;
491 flg |= CXF;
492 break;
493 case 'Y':
494 /*
495 * On extraction check file inode change time after the
496 * modification of the file name. Non standard option.
497 */
498 Yflag = 1;
499 flg |= CYF;
500 break;
501 case 'Z':
502 /*
503 * On extraction check modification time after the
504 * modification of the file name. Non standard option.
505 */
506 Zflag = 1;
507 flg |= CZF;
508 break;
509 default:
510 pax_usage();
511 break;
512 }
513 }
514
515 /*
516 * figure out the operation mode of pax read,write,extract,copy,append
517 * or list. check that we have not been given a bogus set of flags
518 * for the operation mode.
519 */
520 if (ISLIST(flg)) {
521 act = LIST;
522 listf = stdout;
523 bflg = flg & BDLIST;
524 } else if (ISEXTRACT(flg)) {
525 act = EXTRACT;
526 bflg = flg & BDEXTR;
527 } else if (ISARCHIVE(flg)) {
528 act = ARCHIVE;
529 bflg = flg & BDARCH;
530 } else if (ISAPPND(flg)) {
531 act = APPND;
532 bflg = flg & BDARCH;
533 } else if (ISCOPY(flg)) {
534 act = COPY;
535 bflg = flg & BDCOPY;
536 } else
537 pax_usage();
538 if (bflg) {
539 printflg(flg);
540 pax_usage();
541 }
542
543 /*
544 * if we are writing (ARCHIVE) we use the default format if the user
545 * did not specify a format. when we write during an APPEND, we will
546 * adopt the format of the existing archive if none was supplied.
547 */
548 if (!(flg & XF) && (act == ARCHIVE))
549 frmt = &(fsub[DEFLT]);
550
551 /*
552 * process the args as they are interpreted by the operation mode
553 */
554 switch (act) {
555 case LIST:
556 case EXTRACT:
557 for (; optind < argc; optind++)
558 if (pat_add(argv[optind], NULL) < 0)
559 pax_usage();
560 break;
561 case COPY:
562 if (optind >= argc) {
563 paxwarn(0, "Destination directory was not supplied");
564 pax_usage();
565 }
566 --argc;
567 dirptr = argv[argc];
568 /* FALLTHROUGH */
569 case ARCHIVE:
570 case APPND:
571 for (; optind < argc; optind++)
572 if (ftree_add(argv[optind], 0) < 0)
573 pax_usage();
574 /*
575 * no read errors allowed on updates/append operation!
576 */
577 maxflt = 0;
578 break;
579 }
580}
581
582
583/*
584 * tar_options()
585 * look at the user specified flags. set globals as required and check if
586 * the user specified a legal set of flags. If not, complain and exit
587 */
588
589static void
590tar_options(int argc, char **argv)
591{
592 int c;
593 int fstdin = 0;
594 int Oflag = 0;
595 int nincfiles = 0;
596 int incfiles_max = 0;
597 struct incfile {
598 char *file;
599 char *dir;
600 };
601 struct incfile *incfiles = NULL;
602
603 /*
604 * Set default values.
605 */
606 rmleadslash = 1;
607
608 /*
609 * process option flags
610 */
611 while ((c = getoldopt(argc, argv,
612 "b:cef:hjmopqruts:vwxyzBC:HI:LOPXZ014578")) != -1) {
613 switch(c) {
614 case 'b':
615 /*
616 * specify blocksize in 512-byte blocks
617 */
618 if ((wrblksz = (int)str_offt(optarg)) <= 0) {
619 paxwarn(1, "Invalid block size %s", optarg);
620 tar_usage();
621 }
622 wrblksz *= 512; /* XXX - check for int oflow */
623 break;
624 case 'c':
625 /*
626 * create an archive
627 */
628 act = ARCHIVE;
629 break;
630 case 'e':
631 /*
632 * stop after first error
633 */
634 maxflt = 0;
635 break;
636 case 'f':
637 /*
638 * filename where the archive is stored
639 */
640 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
641 /*
642 * treat a - as stdin
643 */
644 fstdin = 1;
645 arcname = NULL;
646 break;
647 }
648 fstdin = 0;
649 arcname = optarg;
650 break;
651 case 'h':
652 /*
653 * follow symlinks
654 */
655 Lflag = 1;
656 break;
657 case 'j':
658 case 'y':
659 /*
660 * use bzip2. Non standard option.
661 */
662 gzip_program = BZIP2_CMD;
663 break;
664 case 'm':
665 /*
666 * do not preserve modification time
667 */
668 pmtime = 0;
669 break;
670 case 'o':
671 if (opt_add("write_opt=nodir") < 0)
672 tar_usage();
673 case 'O':
674 Oflag = 1;
675 break;
676 case 'p':
677 /*
678 * preserve uid/gid and file mode, regardless of umask
679 */
680 pmode = 1;
681 pids = 1;
682 break;
683 case 'q':
684 /*
685 * select first match for a pattern only
686 */
687 nflag = 1;
688 break;
689 case 'r':
690 case 'u':
691 /*
692 * append to the archive
693 */
694 act = APPND;
695 break;
696 case 's':
697 /*
698 * file name substitution name pattern
699 */
700 if (rep_add(optarg) < 0) {
701 tar_usage();
702 break;
703 }
704 break;
705 case 't':
706 /*
707 * list contents of the tape
708 */
709 act = LIST;
710 break;
711 case 'v':
712 /*
713 * verbose operation mode
714 */
715 vflag++;
716 break;
717 case 'w':
718 /*
719 * interactive file rename
720 */
721 iflag = 1;
722 break;
723 case 'x':
724 /*
725 * extract an archive, preserving mode,
726 * and mtime if possible.
727 */
728 act = EXTRACT;
729 pmtime = 1;
730 break;
731 case 'z':
732 /*
733 * use gzip. Non standard option.
734 */
735 gzip_program = GZIP_CMD;
736 break;
737 case 'B':
738 /*
739 * Nothing to do here, this is pax default
740 */
741 break;
742 case 'C':
743 chdname = optarg;
744 break;
745 case 'H':
746 /*
747 * follow command line symlinks only
748 */
749 Hflag = 1;
750 break;
751 case 'I':
752 if (++nincfiles > incfiles_max) {
753 incfiles_max = nincfiles + 3;
754 incfiles = realloc(incfiles,
755 sizeof(*incfiles) * incfiles_max);
756 if (incfiles == NULL) {
757 paxwarn(0, "Unable to allocate space "
758 "for option list");
759 exit(1);
760 }
761 }
762 incfiles[nincfiles - 1].file = optarg;
763 incfiles[nincfiles - 1].dir = chdname;
764 break;
765 case 'L':
766 /*
767 * follow symlinks
768 */
769 Lflag = 1;
770 break;
771 case 'P':
772 /*
773 * do not remove leading '/' from pathnames
774 */
775 rmleadslash = 0;
776 break;
777 case 'X':
778 /*
779 * do not pass over mount points in the file system
780 */
781 Xflag = 1;
782 break;
783 case 'Z':
784 /*
785 * use compress.
786 */
787 gzip_program = COMPRESS_CMD;
788 break;
789 case '0':
790 arcname = DEV_0;
791 break;
792 case '1':
793 arcname = DEV_1;
794 break;
795 case '4':
796 arcname = DEV_4;
797 break;
798 case '5':
799 arcname = DEV_5;
800 break;
801 case '7':
802 arcname = DEV_7;
803 break;
804 case '8':
805 arcname = DEV_8;
806 break;
807 default:
808 tar_usage();
809 break;
810 }
811 }
812 argc -= optind;
813 argv += optind;
814
815 /* Traditional tar behaviour (pax uses stderr unless in list mode) */
816 if (fstdin == 1 && act == ARCHIVE)
817 listf = stderr;
818 else
819 listf = stdout;
820
821 /* Traditional tar behaviour (pax wants to read file list from stdin) */
822 if ((act == ARCHIVE || act == APPND) && argc == 0 && nincfiles == 0)
823 exit(0);
824
825 /*
826 * if we are writing (ARCHIVE) specify tar, otherwise run like pax
827 * (unless -o specified)
828 */
829 if (act == ARCHIVE || act == APPND)
830 frmt = &(fsub[Oflag ? F_OTAR : F_TAR]);
831 else if (Oflag) {
832 paxwarn(1, "The -O/-o options are only valid when writing an archive");
833 tar_usage(); /* only valid when writing */
834 }
835
836 /*
837 * process the args as they are interpreted by the operation mode
838 */
839 switch (act) {
840 case LIST:
841 case EXTRACT:
842 default:
843 {
844 int sawpat = 0;
845 char *file, *dir = NULL;
846
847 while (nincfiles || *argv != NULL) {
848 /*
849 * If we queued up any include files,
850 * pull them in now. Otherwise, check
851 * for -I and -C positional flags.
852 * Anything else must be a file to
853 * extract.
854 */
855 if (nincfiles) {
856 file = incfiles->file;
857 dir = incfiles->dir;
858 incfiles++;
859 nincfiles--;
860 } else if (strcmp(*argv, "-I") == 0) {
861 if (*++argv == NULL)
862 break;
863 file = *argv++;
864 dir = chdname;
865 } else
866 file = NULL;
867 if (file != NULL) {
868 FILE *fp;
869 char *str;
870
871 if (strcmp(file, "-") == 0)
872 fp = stdin;
873 else if ((fp = fopen(file, "r")) == NULL) {
874 paxwarn(1, "Unable to open file '%s' for read", file);
875 tar_usage();
876 }
877 while ((str = getline(fp)) != NULL) {
878 if (pat_add(str, dir) < 0)
879 tar_usage();
880 sawpat = 1;
881 }
882 if (strcmp(file, "-") != 0)
883 fclose(fp);
884 if (getline_error) {
885 paxwarn(1, "Problem with file '%s'", file);
886 tar_usage();
887 }
888 } else if (strcmp(*argv, "-C") == 0) {
889 if (*++argv == NULL)
890 break;
891 chdname = *argv++;
892 } else if (pat_add(*argv++, chdname) < 0)
893 tar_usage();
894 else
895 sawpat = 1;
896 }
897 /*
898 * if patterns were added, we are doing chdir()
899 * on a file-by-file basis, else, just one
900 * global chdir (if any) after opening input.
901 */
902 if (sawpat > 0)
903 chdname = NULL;
904 }
905 break;
906 case ARCHIVE:
907 case APPND:
908 if (chdname != NULL) { /* initial chdir() */
909 if (ftree_add(chdname, 1) < 0)
910 tar_usage();
911 }
912
913 while (nincfiles || *argv != NULL) {
914 char *file, *dir = NULL;
915
916 /*
917 * If we queued up any include files, pull them in
918 * now. Otherwise, check for -I and -C positional
919 * flags. Anything else must be a file to include
920 * in the archive.
921 */
922 if (nincfiles) {
923 file = incfiles->file;
924 dir = incfiles->dir;
925 incfiles++;
926 nincfiles--;
927 } else if (strcmp(*argv, "-I") == 0) {
928 if (*++argv == NULL)
929 break;
930 file = *argv++;
931 dir = NULL;
932 } else
933 file = NULL;
934 if (file != NULL) {
935 FILE *fp;
936 char *str;
937
938 /* Set directory if needed */
939 if (dir) {
940 if (ftree_add(dir, 1) < 0)
941 tar_usage();
942 }
943
944 if (strcmp(file, "-") == 0)
945 fp = stdin;
946 else if ((fp = fopen(file, "r")) == NULL) {
947 paxwarn(1, "Unable to open file '%s' for read", file);
948 tar_usage();
949 }
950 while ((str = getline(fp)) != NULL) {
951 if (ftree_add(str, 0) < 0)
952 tar_usage();
953 }
954 if (strcmp(file, "-") != 0)
955 fclose(fp);
956 if (getline_error) {
957 paxwarn(1, "Problem with file '%s'",
958 file);
959 tar_usage();
960 }
961 } else if (strcmp(*argv, "-C") == 0) {
962 if (*++argv == NULL)
963 break;
964 if (ftree_add(*argv++, 1) < 0)
965 tar_usage();
966 } else if (ftree_add(*argv++, 0) < 0)
967 tar_usage();
968 }
969 /*
970 * no read errors allowed on updates/append operation!
971 */
972 maxflt = 0;
973 break;
974 }
975 if (!fstdin && ((arcname == NULL) || (*arcname == '\0'))) {
976 arcname = getenv("TAPE");
977 if ((arcname == NULL) || (*arcname == '\0'))
978 arcname = _PATH_DEFTAPE;
979 }
980}
981
982static int
983mkpath(char *path)
984{
985 struct stat sb;
986 char *slash;
987 int done = 0;
988
989 slash = path;
990
991 while (!done) {
992 slash += strspn(slash, "/");
993 slash += strcspn(slash, "/");
994
995 done = (*slash == '\0');
996 *slash = '\0';
997
998 if (stat(path, &sb)) {
999 if (errno != ENOENT || mkdir(path, 0777)) {
1000 paxwarn(1, "%s", path);
1001 return (-1);
1002 }
1003 } else if (!S_ISDIR(sb.st_mode)) {
1004 syswarn(1, ENOTDIR, "%s", path);
1005 return (-1);
1006 }
1007
1008 if (!done)
1009 *slash = '/';
1010 }
1011
1012 return (0);
1013}
1014/*
1015 * cpio_options()
1016 * look at the user specified flags. set globals as required and check if
1017 * the user specified a legal set of flags. If not, complain and exit
1018 */
1019
1020static void
1021cpio_options(int argc, char **argv)
1022{
1023 int c;
1024 size_t i;
1025 char *str;
1026 FSUB tmp;
1027 FILE *fp;
1028
1029 kflag = 1;
1030 pids = 1;
1031 pmode = 1;
1032 pmtime = 0;
1033 arcname = NULL;
1034 dflag = 1;
1035 act = -1;
1036 nodirs = 1;
1037 while ((c=getopt(argc,argv,"abcdfiklmoprstuvzABC:E:F:H:I:LO:SZ6")) != -1)
1038 switch (c) {
1039 case 'a':
1040 /*
1041 * preserve access time on files read
1042 */
1043 tflag = 1;
1044 break;
1045 case 'b':
1046 /*
1047 * swap bytes and half-words when reading data
1048 */
1049 break;
1050 case 'c':
1051 /*
1052 * ASCII cpio header
1053 */
1054 frmt = &(fsub[F_ACPIO]);
1055 break;
1056 case 'd':
1057 /*
1058 * create directories as needed
1059 */
1060 nodirs = 0;
1061 break;
1062 case 'f':
1063 /*
1064 * invert meaning of pattern list
1065 */
1066 cflag = 1;
1067 break;
1068 case 'i':
1069 /*
1070 * restore an archive
1071 */
1072 act = EXTRACT;
1073 break;
1074 case 'k':
1075 break;
1076 case 'l':
1077 /*
1078 * use links instead of copies when possible
1079 */
1080 lflag = 1;
1081 break;
1082 case 'm':
1083 /*
1084 * preserve modification time
1085 */
1086 pmtime = 1;
1087 break;
1088 case 'o':
1089 /*
1090 * create an archive
1091 */
1092 act = ARCHIVE;
1093 frmt = &(fsub[F_CPIO]);
1094 break;
1095 case 'p':
1096 /*
1097 * copy-pass mode
1098 */
1099 act = COPY;
1100 break;
1101 case 'r':
1102 /*
1103 * interactively rename files
1104 */
1105 iflag = 1;
1106 break;
1107 case 's':
1108 /*
1109 * swap bytes after reading data
1110 */
1111 break;
1112 case 't':
1113 /*
1114 * list contents of archive
1115 */
1116 act = LIST;
1117 listf = stdout;
1118 break;
1119 case 'u':
1120 /*
1121 * replace newer files
1122 */
1123 kflag = 0;
1124 break;
1125 case 'v':
1126 /*
1127 * verbose operation mode
1128 */
1129 vflag = 1;
1130 break;
1131 case 'z':
1132 /*
1133 * use gzip. Non standard option.
1134 */
1135 gzip_program = GZIP_CMD;
1136 break;
1137 case 'A':
1138 /*
1139 * append mode
1140 */
1141 act = APPND;
1142 break;
1143 case 'B':
1144 /*
1145 * Use 5120 byte block size
1146 */
1147 wrblksz = 5120;
1148 break;
1149 case 'C':
1150 /*
1151 * set block size in bytes
1152 */
1153 wrblksz = atoi(optarg);
1154 break;
1155 case 'E':
1156 /*
1157 * file with patterns to extract or list
1158 */
1159 if ((fp = fopen(optarg, "r")) == NULL) {
1160 paxwarn(1, "Unable to open file '%s' for read", optarg);
1161 cpio_usage();
1162 }
1163 while ((str = getline(fp)) != NULL) {
1164 pat_add(str, NULL);
1165 }
1166 fclose(fp);
1167 if (getline_error) {
1168 paxwarn(1, "Problem with file '%s'", optarg);
1169 cpio_usage();
1170 }
1171 break;
1172 case 'F':
1173 case 'I':
1174 case 'O':
1175 /*
1176 * filename where the archive is stored
1177 */
1178 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
1179 /*
1180 * treat a - as stdin
1181 */
1182 arcname = NULL;
1183 break;
1184 }
1185 arcname = optarg;
1186 break;
1187 case 'H':
1188 /*
1189 * specify an archive format on write
1190 */
1191 tmp.name = optarg;
1192 if ((frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
1193 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt)) != NULL)
1194 break;
1195 paxwarn(1, "Unknown -H format: %s", optarg);
1196 (void)fputs("cpio: Known -H formats are:", stderr);
1197 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
1198 (void)fprintf(stderr, " %s", fsub[i].name);
1199 (void)fputs("\n\n", stderr);
1200 cpio_usage();
1201 break;
1202 case 'L':
1203 /*
1204 * follow symbolic links
1205 */
1206 Lflag = 1;
1207 break;
1208 case 'S':
1209 /*
1210 * swap halfwords after reading data
1211 */
1212 break;
1213 case 'Z':
1214 /*
1215 * use compress. Non standard option.
1216 */
1217 gzip_program = COMPRESS_CMD;
1218 break;
1219 case '6':
1220 /*
1221 * process Version 6 cpio format
1222 */
1223 frmt = &(fsub[F_OCPIO]);
1224 break;
1225 case '?':
1226 default:
1227 cpio_usage();
1228 break;
1229 }
1230 argc -= optind;
1231 argv += optind;
1232
1233 /*
1234 * process the args as they are interpreted by the operation mode
1235 */
1236 switch (act) {
1237 case LIST:
1238 case EXTRACT:
1239 while (*argv != NULL)
1240 if (pat_add(*argv++, NULL) < 0)
1241 cpio_usage();
1242 break;
1243 case COPY:
1244 if (*argv == NULL) {
1245 paxwarn(0, "Destination directory was not supplied");
1246 cpio_usage();
1247 }
1248 dirptr = *argv;
1249 if (mkpath(dirptr) < 0)
1250 cpio_usage();
1251 --argc;
1252 ++argv;
1253 /* FALLTHROUGH */
1254 case ARCHIVE:
1255 case APPND:
1256 if (*argv != NULL)
1257 cpio_usage();
1258 /*
1259 * no read errors allowed on updates/append operation!
1260 */
1261 maxflt = 0;
1262 while ((str = getline(stdin)) != NULL) {
1263 ftree_add(str, 0);
1264 }
1265 if (getline_error) {
1266 paxwarn(1, "Problem while reading stdin");
1267 cpio_usage();
1268 }
1269 break;
1270 default:
1271 cpio_usage();
1272 break;
1273 }
1274}
1275
1276/*
1277 * printflg()
1278 * print out those invalid flag sets found to the user
1279 */
1280
1281static void
1282printflg(unsigned int flg)
1283{
1284 int nxt;
1285 int pos = 0;
1286
1287 (void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
1288 while ((nxt = ffs(flg)) != 0) {
1289 flg = flg >> nxt;
1290 pos += nxt;
1291 (void)fprintf(stderr, " -%c", flgch[pos-1]);
1292 }
1293 (void)putc('\n', stderr);
1294}
1295
1296/*
1297 * c_frmt()
1298 * comparison routine used by bsearch to find the format specified
1299 * by the user
1300 */
1301
1302static int
1303c_frmt(const void *a, const void *b)
1304{
1305 return(strcmp(((const FSUB *)a)->name, ((const FSUB *)b)->name));
1306}
1307
1308/*
1309 * opt_next()
1310 * called by format specific options routines to get each format specific
1311 * flag and value specified with -o
1312 * Return:
1313 * pointer to next OPLIST entry or NULL (end of list).
1314 */
1315
1316OPLIST *
1317opt_next(void)
1318{
1319 OPLIST *opt;
1320
1321 if ((opt = ophead) != NULL)
1322 ophead = ophead->fow;
1323 return(opt);
1324}
1325
1326/*
1327 * bad_opt()
1328 * generic routine used to complain about a format specific options
1329 * when the format does not support options.
1330 */
1331
1332int
1333bad_opt(void)
1334{
1335 OPLIST *opt;
1336
1337 if (ophead == NULL)
1338 return(0);
1339 /*
1340 * print all we were given
1341 */
1342 paxwarn(1,"These format options are not supported");
1343 while ((opt = opt_next()) != NULL)
1344 (void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
1345 pax_usage();
1346 return(0);
1347}
1348
1349/*
1350 * opt_add()
1351 * breaks the value supplied to -o into an option name and value. Options
1352 * are given to -o in the form -o name-value,name=value
1353 * multiple -o may be specified.
1354 * Return:
1355 * 0 if format in name=value format, -1 if -o is passed junk.
1356 */
1357
1358int
1359opt_add(const char *str)
1360{
1361 OPLIST *opt;
1362 char *frpt;
1363 char *pt;
1364 char *endpt;
1365 char *lstr;
1366
1367 if ((str == NULL) || (*str == '\0')) {
1368 paxwarn(0, "Invalid option name");
1369 return(-1);
1370 }
1371 if ((lstr = strdup(str)) == NULL) {
1372 paxwarn(0, "Unable to allocate space for option list");
1373 return(-1);
1374 }
1375 frpt = endpt = lstr;
1376
1377 /*
1378 * break into name and values pieces and stuff each one into a
1379 * OPLIST structure. When we know the format, the format specific
1380 * option function will go through this list
1381 */
1382 while ((frpt != NULL) && (*frpt != '\0')) {
1383 if ((endpt = strchr(frpt, ',')) != NULL)
1384 *endpt = '\0';
1385 if ((pt = strchr(frpt, '=')) == NULL) {
1386 paxwarn(0, "Invalid options format");
1387 free(lstr);
1388 return(-1);
1389 }
1390 if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
1391 paxwarn(0, "Unable to allocate space for option list");
1392 free(lstr);
1393 return(-1);
1394 }
1395 *pt++ = '\0';
1396 opt->name = frpt;
1397 opt->value = pt;
1398 opt->fow = NULL;
1399 if (endpt != NULL)
1400 frpt = endpt + 1;
1401 else
1402 frpt = NULL;
1403 if (ophead == NULL) {
1404 optail = ophead = opt;
1405 continue;
1406 }
1407 optail->fow = opt;
1408 optail = opt;
1409 }
1410 return(0);
1411}
1412
1413/*
1414 * str_offt()
1415 * Convert an expression of the following forms to an off_t > 0.
1416 * 1) A positive decimal number.
1417 * 2) A positive decimal number followed by a b (mult by 512).
1418 * 3) A positive decimal number followed by a k (mult by 1024).
1419 * 4) A positive decimal number followed by a m (mult by 512).
1420 * 5) A positive decimal number followed by a w (mult by sizeof int)
1421 * 6) Two or more positive decimal numbers (with/without k,b or w).
1422 * separated by x (also * for backwards compatibility), specifying
1423 * the product of the indicated values.
1424 * Return:
1425 * 0 for an error, a positive value o.w.
1426 */
1427
1428static off_t
1429str_offt(char *val)
1430{
1431 char *expr;
1432 off_t num, t;
1433
1434# ifdef NET2_STAT
1435 num = strtol(val, &expr, 0);
1436 if ((num == LONG_MAX) || (num <= 0) || (expr == val))
1437# else
1438 num = strtoq(val, &expr, 0);
1439 if ((num == QUAD_MAX) || (num <= 0) || (expr == val))
1440# endif
1441 return(0);
1442
1443 switch(*expr) {
1444 case 'b':
1445 t = num;
1446 num *= 512;
1447 if (t > num)
1448 return(0);
1449 ++expr;
1450 break;
1451 case 'k':
1452 t = num;
1453 num *= 1024;
1454 if (t > num)
1455 return(0);
1456 ++expr;
1457 break;
1458 case 'm':
1459 t = num;
1460 num *= 1048576;
1461 if (t > num)
1462 return(0);
1463 ++expr;
1464 break;
1465 case 'w':
1466 t = num;
1467 num *= sizeof(int);
1468 if (t > num)
1469 return(0);
1470 ++expr;
1471 break;
1472 }
1473
1474 switch(*expr) {
1475 case '\0':
1476 break;
1477 case '*':
1478 case 'x':
1479 t = num;
1480 num *= str_offt(expr + 1);
1481 if (t > num)
1482 return(0);
1483 break;
1484 default:
1485 return(0);
1486 }
1487 return(num);
1488}
1489
1490char *fgetln(FILE *f, size_t *);
1491
1492static char *
1493getline(FILE *f)
1494{
1495 char *name, *temp;
1496 size_t len;
1497
1498 name = fgetln(f, &len);
1499 if (!name) {
1500 getline_error = ferror(f) ? GETLINE_FILE_CORRUPT : 0;
1501 return(0);
1502 }
1503 if (name[len-1] != '\n')
1504 len++;
1505 temp = malloc(len);
1506 if (!temp) {
1507 getline_error = GETLINE_OUT_OF_MEM;
1508 return(0);
1509 }
1510 memcpy(temp, name, len-1);
1511 temp[len-1] = 0;
1512 return(temp);
1513}
1514
1515/*
1516 * no_op()
1517 * for those option functions where the archive format has nothing to do.
1518 * Return:
1519 * 0
1520 */
1521
1522static int
1523no_op(void)
1524{
1525 return(0);
1526}
1527
1528/*
1529 * pax_usage()
1530 * print the usage summary to the user
1531 */
1532
1533static void
1534pax_usage(void)
1535{
1536 (void)fputs("usage: pax [-cdnvz] [-E limit] [-f archive] ", stderr);
1537 (void)fputs("[-s replstr] ... [-U user] ...", stderr);
1538 (void)fputs("\n [-G group] ... ", stderr);
1539 (void)fputs("[-T [from_date][,to_date]] ... ", stderr);
1540 (void)fputs("[pattern ...]\n", stderr);
1541 (void)fputs(" pax -r [-cdiknuvzDYZ] [-E limit] ", stderr);
1542 (void)fputs("[-f archive] [-o options] ... \n", stderr);
1543 (void)fputs(" [-p string] ... [-s replstr] ... ", stderr);
1544 (void)fputs("[-U user] ... [-G group] ...\n ", stderr);
1545 (void)fputs("[-T [from_date][,to_date]] ... ", stderr);
1546 (void)fputs(" [pattern ...]\n", stderr);
1547 (void)fputs(" pax -w [-dituvzHLPX] [-b blocksize] ", stderr);
1548 (void)fputs("[ [-a] [-f archive] ] [-x format] \n", stderr);
1549 (void)fputs(" [-B bytes] [-s replstr] ... ", stderr);
1550 (void)fputs("[-o options] ... [-U user] ...", stderr);
1551 (void)fputs("\n [-G group] ... ", stderr);
1552 (void)fputs("[-T [from_date][,to_date][/[c][m]]] ... ", stderr);
1553 (void)fputs("[file ...]\n", stderr);
1554 (void)fputs(" pax -r -w [-diklntuvDHLPXYZ] ", stderr);
1555 (void)fputs("[-p string] ... [-s replstr] ...", stderr);
1556 (void)fputs("\n [-U user] ... [-G group] ... ", stderr);
1557 (void)fputs("[-T [from_date][,to_date][/[c][m]]] ... ", stderr);
1558 (void)fputs("\n [file ...] directory\n", stderr);
1559 exit(1);
1560}
1561
1562/*
1563 * tar_usage()
1564 * print the usage summary to the user
1565 */
1566
1567static void
1568tar_usage(void)
1569{
1570 (void)fputs("usage: tar [-]{crtux}[-befhjmopqsvwyzHLOPXZ014578] [blocksize] ",
1571 stderr);
1572 (void)fputs("[archive] [replstr] [-C directory] [-I file] [file ...]\n",
1573 stderr);
1574 exit(1);
1575}
1576
1577/*
1578 * cpio_usage()
1579 * print the usage summary to the user
1580 */
1581
1582static void
1583cpio_usage(void)
1584{
1585 (void)fputs("usage: cpio -o [-aABcLvVzZ] [-C bytes] [-H format] [-O archive]\n", stderr);
1586 (void)fputs(" [-F archive] < name-list [> archive]\n", stderr);
1587 (void)fputs(" cpio -i [-bBcdfmnrsStuvVzZ6] [-C bytes] [-E file] [-H format]\n", stderr);
1588 (void)fputs(" [-I archive] [-F archive] [pattern...] [< archive]\n", stderr);
1589 (void)fputs(" cpio -p [-adlLmuvV] destination-directory < name-list\n", stderr);
1590 exit(1);
1591}
Note: See TracBrowser for help on using the repository browser.