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