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