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 $ */
5 * Copyright (c) 1992 Keith Muller.
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * Keith Muller of the University of California, San Diego.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
43 static char sccsid[] = "@(#)options.c 8.2 (Berkeley) 4/18/94";
45 static char rcsid[] = "$OpenBSD: options.c,v 1.34 1998/09/20 02:22:22 millert Exp $";
49 #include <sys/types.h>
53 #include <sys/param.h>
67 #include "fgetln.h" /* added, David */
70 * Routines which handle command line options
73 static char flgch[] = FLGCH; /* list of all possible flags */
74 static OPLIST *ophead = NULL; /* head for format specific options -x */
75 static OPLIST *optail = NULL; /* option tail */
77 static int no_op __P((void));
78 static void printflg __P((unsigned int));
79 static int c_frmt __P((const void *, const void *));
80 static off_t str_offt __P((char *));
81 static void pax_options __P((register int, register char **));
82 static void pax_usage __P((void));
83 static void tar_options __P((register int, register char **));
84 static void tar_usage __P((void));
85 static void cpio_options __P((register int, register char **));
86 static void cpio_usage __P((void));
88 #define GZIP_CMD "gzip" /* command to run as gzip */
89 #define COMPRESS_CMD "compress" /* command to run as compress */
90 #define BZIP2_CMD "bzip2" /* command to run as bzip2 */
93 * Format specific routine table - MUST BE IN SORTED ORDER BY NAME
94 * (see pax.h for description of each function)
96 * name, blksz, hdsz, udev, hlk, blkagn, inhead, id, st_read,
97 * read, end_read, st_write, write, end_write, trail,
98 * rd_data, wr_data, options
102 /* 0: OLD BINARY CPIO */
103 {"bcpio", 5120, sizeof(HD_BCPIO), 1, 0, 0, 1, bcpio_id, cpio_strd,
104 bcpio_rd, bcpio_endrd, cpio_stwr, bcpio_wr, cpio_endwr, cpio_trail,
105 rd_wrfile, wr_rdfile, bad_opt},
107 /* 1: OLD OCTAL CHARACTER CPIO */
108 {"cpio", 5120, sizeof(HD_CPIO), 1, 0, 0, 1, cpio_id, cpio_strd,
109 cpio_rd, cpio_endrd, cpio_stwr, cpio_wr, cpio_endwr, cpio_trail,
110 rd_wrfile, wr_rdfile, bad_opt},
112 /* 2: SVR4 HEX CPIO */
113 {"sv4cpio", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, vcpio_id, cpio_strd,
114 vcpio_rd, vcpio_endrd, cpio_stwr, vcpio_wr, cpio_endwr, cpio_trail,
115 rd_wrfile, wr_rdfile, bad_opt},
117 /* 3: SVR4 HEX CPIO WITH CRC */
118 {"sv4crc", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, crc_id, crc_strd,
119 vcpio_rd, vcpio_endrd, crc_stwr, vcpio_wr, cpio_endwr, cpio_trail,
120 rd_wrfile, wr_rdfile, bad_opt},
123 {"tar", 10240, BLKMULT, 0, 1, BLKMULT, 0, tar_id, no_op,
124 tar_rd, tar_endrd, no_op, tar_wr, tar_endwr, tar_trail,
125 rd_wrfile, wr_rdfile, tar_opt},
128 {"ustar", 10240, BLKMULT, 0, 1, BLKMULT, 0, ustar_id, ustar_strd,
129 ustar_rd, tar_endrd, ustar_stwr, ustar_wr, tar_endwr, tar_trail,
130 rd_wrfile, wr_rdfile, bad_opt},
132 #define F_OCPIO 0 /* format when called as cpio -6 */
133 #define F_ACPIO 1 /* format when called as cpio -c */
134 #define F_CPIO 3 /* format when called as cpio */
135 #define F_OTAR 4 /* format when called as tar -o */
136 #define F_TAR 5 /* format when called as tar */
137 #define DEFLT 5 /* default write format from list above */
140 * ford is the archive search order used by get_arc() to determine what kind
141 * of archive we are dealing with. This helps to properly id archive formats
142 * some formats may be subsets of others....
144 int ford[] = {5, 4, 3, 2, 1, 0, -1 };
148 * figure out if we are pax, tar or cpio. Call the appropriate options
154 options(register int argc, register char **argv)
159 register char **argv;
164 * Are we acting like pax, tar or cpio (based on argv[0])
166 if ((argv0 = strrchr(argv[0], '/')) != NULL)
171 if (strcmp(NM_TAR, argv0) == 0)
172 return(tar_options(argc, argv));
173 else if (strcmp(NM_CPIO, argv0) == 0)
174 return(cpio_options(argc, argv));
176 * assume pax as the default
179 return(pax_options(argc, argv));
184 * look at the user specified flags. set globals as required and check if
185 * the user specified a legal set of flags. If not, complain and exit
190 pax_options(register int argc, register char **argv)
193 pax_options(argc, argv)
195 register char **argv;
200 unsigned int flg = 0;
201 unsigned int bflg = 0;
208 * process option flags
210 while ((c=getopt(argc,argv,"ab:cdf:ijklno:p:rs:tuvwx:zB:DE:G:HLPT:U:XYZ"))
224 if ((wrblksz = (int)str_offt(optarg)) <= 0) {
225 paxwarn(1, "Invalid block size %s", optarg);
231 * inverse match on patterns
238 * match only dir on extract, not the subtree at dir
245 * filename where the archive is stored
252 * interactive file rename
259 * use bzip2. Non standard option.
262 gzip_program = BZIP2_CMD;
266 * do not clobber files that exist
273 * try to link src to dest with copy (-rw)
280 * select first match for a pattern only
287 * pass format specific options
290 if (opt_add(optarg) < 0)
295 * specify file characteristic options
297 for (pt = optarg; *pt != '\0'; ++pt) {
301 * do not preserve access time
307 * preserve user id, group id, file
308 * mode, access/modification times
317 * do not preserve modification time
329 * preserver file mode bits
334 paxwarn(1, "Invalid -p string: %c", *pt);
349 * file name substitution name pattern
351 if (rep_add(optarg) < 0) {
359 * preserve access time on filesystem nodes we read
366 * ignore those older files
373 * verbose operation mode
386 * specify an archive format on write
389 if ((frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
390 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt)) != NULL) {
394 paxwarn(1, "Unknown -x format: %s", optarg);
395 (void)fputs("pax: Known -x formats are:", stderr);
396 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
397 (void)fprintf(stderr, " %s", fsub[i].name);
398 (void)fputs("\n\n", stderr);
403 * use gzip. Non standard option.
406 gzip_program = GZIP_CMD;
410 * non-standard option on number of bytes written on a
411 * single archive volume.
413 if ((wrlimit = str_offt(optarg)) <= 0) {
414 paxwarn(1, "Invalid write limit %s", optarg);
417 if (wrlimit % BLKMULT) {
418 paxwarn(1, "Write limit is not a %d byte multiple",
426 * On extraction check file inode change time before the
427 * modification of the file name. Non standard option.
434 * non-standard limit on read faults
435 * 0 indicates stop after first error, values
436 * indicate a limit, "NONE" try forever
439 if (strcmp(NONE, optarg) == 0)
441 else if ((maxflt = atoi(optarg)) < 0) {
442 paxwarn(1, "Error count value must be positive");
448 * non-standard option for selecting files within an
449 * archive by group (gid or name)
451 if (grp_add(optarg) < 0) {
459 * follow command line symlinks only
473 * do NOT follow symlinks (default)
480 * non-standard option for selecting files within an
481 * archive by modification time range (lower,upper)
483 if (trng_add(optarg) < 0) {
491 * non-standard option for selecting files within an
492 * archive by user (uid or name)
494 if (usr_add(optarg) < 0) {
502 * do not pass over mount points in the file system
509 * On extraction check file inode change time after the
510 * modification of the file name. Non standard option.
517 * On extraction check modification time after the
518 * modification of the file name. Non standard option.
530 * figure out the operation mode of pax read,write,extract,copy,append
531 * or list. check that we have not been given a bogus set of flags
532 * for the operation mode.
538 } else if (ISEXTRACT(flg)) {
541 } else if (ISARCHIVE(flg)) {
544 } else if (ISAPPND(flg)) {
547 } else if (ISCOPY(flg)) {
558 * if we are writing (ARCHIVE) we use the default format if the user
559 * did not specify a format. when we write during an APPEND, we will
560 * adopt the format of the existing archive if none was supplied.
562 if (!(flg & XF) && (act == ARCHIVE))
563 frmt = &(fsub[DEFLT]);
566 * process the args as they are interpreted by the operation mode
571 for (; optind < argc; optind++)
572 if (pat_add(argv[optind], NULL) < 0)
576 if (optind >= argc) {
577 paxwarn(0, "Destination directory was not supplied");
585 for (; optind < argc; optind++)
586 if (ftree_add(argv[optind], 0) < 0)
589 * no read errors allowed on updates/append operation!
599 * look at the user specified flags. set globals as required and check if
600 * the user specified a legal set of flags. If not, complain and exit
605 tar_options(register int argc, register char **argv)
608 tar_options(argc, argv)
610 register char **argv;
618 * Set default values.
623 * process option flags
625 while ((c = getoldopt(argc, argv,
626 "b:cef:hjmopruts:vwxzBC:HLOPXZ014578"))
631 * specify blocksize in 512-byte blocks
633 if ((wrblksz = (int)str_offt(optarg)) <= 0) {
634 paxwarn(1, "Invalid block size %s", optarg);
637 wrblksz *= 512; /* XXX - check for int oflow */
647 * stop after first error
653 * filename where the archive is stored
655 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
674 * use bzip2. Non standard option.
677 gzip_program = BZIP2_CMD;
681 * do not preserve modification time
686 if (opt_add("write_opt=nodir") < 0)
693 * preserve uid/gid and file mode, regardless of umask
701 * append to the archive
707 * file name substitution name pattern
709 if (rep_add(optarg) < 0) {
716 * list contents of the tape
722 * verbose operation mode
728 * interactive file rename
734 * extract an archive, preserving mode,
735 * and mtime if possible.
742 * use gzip. Non standard option.
745 gzip_program = GZIP_CMD;
749 * Nothing to do here, this is pax default
757 * follow command line symlinks only
769 * do not remove leading '/' from pathnames
775 * do not pass over mount points in the file system
784 gzip_program = COMPRESS_CMD;
812 /* Traditional tar behaviour (pax uses stderr unless in list mode) */
813 if (fstdin == 1 && act == ARCHIVE)
818 /* Traditional tar behaviour (pax wants to read filelist from stdin) */
819 if ((act == ARCHIVE || act == APPND) && argc == 0)
823 * if we are writing (ARCHIVE) specify tar, otherwise run like pax
824 * (unless -o specified)
826 if (act == ARCHIVE || act == APPND)
827 frmt = &(fsub[Oflag ? F_OTAR : F_TAR]);
829 paxwarn(1, "The -O/-o options are only valid when writing an archive");
830 tar_usage(); /* only valid when writing */
834 * process the args as they are interpreted by the operation mode
843 while (*argv != NULL) {
844 if (strcmp(*argv, "-C") == 0) {
851 if (pat_add(*argv++, chdname) < 0)
856 * if patterns were added, we are doing chdir()
857 * on a file-by-file basis, else, just one
858 * global chdir (if any) after opening input.
866 if (chdname != NULL) { /* initial chdir() */
867 if (ftree_add(chdname, 1) < 0)
871 while (*argv != NULL) {
872 if (!strcmp(*argv, "-C")) {
875 if (ftree_add(*argv++, 1) < 0)
878 if (ftree_add(*argv++, 0) < 0)
883 * no read errors allowed on updates/append operation!
888 if (!fstdin && ((arcname == NULL) || (*arcname == '\0'))) {
889 arcname = getenv("TAPE");
890 if ((arcname == NULL) || (*arcname == '\0'))
891 arcname = _PATH_DEFTAPE;
900 register char *slash;
906 slash += strspn(slash, "/");
907 slash += strcspn(slash, "/");
909 done = (*slash == '\0');
912 if (stat(path, &sb)) {
913 if (errno != ENOENT || mkdir(path, 0777)) {
914 paxwarn(1, "%s", path);
917 } else if (!S_ISDIR(sb.st_mode)) {
918 syswarn(1, ENOTDIR, "%s", path);
930 * look at the user specified flags. set globals as required and check if
931 * the user specified a legal set of flags. If not, complain and exit
936 cpio_options(register int argc, register char **argv)
939 cpio_options(argc, argv)
941 register char **argv;
958 while ((c=getopt(argc,argv,"abcdfijklmoprstuvzABC:E:F:H:I:LO:SZ6")) != EOF)
962 * preserve access time on files read
968 * swap bytes and half-words when reading data
975 frmt = &(fsub[F_ACPIO]);
979 * create directories as needed
985 * invert meaning of pattern list
997 * use bzip2. Non standard option.
1000 gzip_program = BZIP2_CMD;
1006 * use links instead of copies when possible
1012 * preserve modification time
1021 frmt = &(fsub[F_CPIO]);
1031 * interactively rename files
1037 * swap bytes after reading data
1042 * list contents of archive
1049 * replace newer files
1055 * verbose operation mode
1061 * use gzip. Non standard option.
1064 gzip_program = GZIP_CMD;
1074 * Use 5120 byte block size
1080 * set block size in bytes
1082 wrblksz = atoi(optarg);
1086 * file with patterns to extract or list
1088 if ((fp = fopen(optarg, "r")) == NULL) {
1089 paxwarn(1, "Unable to open file '%s' for read", optarg);
1092 while ((str = fgetln(fp, &len)) != NULL) {
1093 str[len - 1] = '\0';
1102 * filename where the archive is stored
1104 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
1106 * treat a - as stdin
1115 * specify an archive format on write
1118 if ((frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
1119 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt)) != NULL)
1121 paxwarn(1, "Unknown -H format: %s", optarg);
1122 (void)fputs("cpio: Known -H formats are:", stderr);
1123 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
1124 (void)fprintf(stderr, " %s", fsub[i].name);
1125 (void)fputs("\n\n", stderr);
1130 * follow symbolic links
1136 * swap halfwords after reading data
1141 * use compress. Non standard option.
1144 gzip_program = COMPRESS_CMD;
1148 * process Version 6 cpio format
1150 frmt = &(fsub[F_OCPIO]);
1161 * process the args as they are interpreted by the operation mode
1166 while (*argv != NULL)
1167 if (pat_add(*argv++, NULL) < 0)
1171 if (*argv == NULL) {
1172 paxwarn(0, "Destination directory was not supplied");
1176 if (mkpath(dirptr) < 0)
1186 * no read errors allowed on updates/append operation!
1189 while ((str = fgetln(stdin, &len)) != NULL) {
1190 str[len - 1] = '\0';
1191 ftree_add(strdup(str), NULL);
1202 * print out those invalid flag sets found to the user
1207 printflg(unsigned int flg)
1217 (void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
1218 while ((nxt = ffs(flg)) != 0) {
1221 (void)fprintf(stderr, " -%c", flgch[pos-1]);
1223 (void)putc('\n', stderr);
1228 * comparison routine used by bsearch to find the format specified
1234 c_frmt(const void *a, const void *b)
1242 return(strcmp(((FSUB *)a)->name, ((FSUB *)b)->name));
1247 * called by format specific options routines to get each format specific
1248 * flag and value specified with -o
1250 * pointer to next OPLIST entry or NULL (end of list).
1263 if ((opt = ophead) != NULL)
1264 ophead = ophead->fow;
1270 * generic routine used to complain about a format specific options
1271 * when the format does not support options.
1282 register OPLIST *opt;
1287 * print all we were given
1289 paxwarn(1,"These format options are not supported");
1290 while ((opt = opt_next()) != NULL)
1291 (void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
1298 * breaks the value supplied to -o into a option name and value. options
1299 * are given to -o in the form -o name-value,name=value
1300 * mulltiple -o may be specified.
1302 * 0 if format in name=value format, -1 if -o is passed junk
1307 opt_add(register char *str)
1314 register OPLIST *opt;
1315 register char *frpt;
1317 register char *endpt;
1319 if ((str == NULL) || (*str == '\0')) {
1320 paxwarn(0, "Invalid option name");
1323 if ((str = strdup(str)) == NULL) {
1324 paxwarn(0, "Unable to allocate space for option list");
1330 * break into name and values pieces and stuff each one into a
1331 * OPLIST structure. When we know the format, the format specific
1332 * option function will go through this list
1334 while ((frpt != NULL) && (*frpt != '\0')) {
1335 if ((endpt = strchr(frpt, ',')) != NULL)
1337 if ((pt = strchr(frpt, '=')) == NULL) {
1338 paxwarn(0, "Invalid options format");
1342 if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
1343 paxwarn(0, "Unable to allocate space for option list");
1355 if (ophead == NULL) {
1356 optail = ophead = opt;
1367 * Convert an expression of the following forms to an off_t > 0.
1368 * 1) A positive decimal number.
1369 * 2) A positive decimal number followed by a b (mult by 512).
1370 * 3) A positive decimal number followed by a k (mult by 1024).
1371 * 4) A positive decimal number followed by a m (mult by 512).
1372 * 5) A positive decimal number followed by a w (mult by sizeof int)
1373 * 6) Two or more positive decimal numbers (with/without k,b or w).
1374 * seperated by x (also * for backwards compatibility), specifying
1375 * the product of the indicated values.
1377 * 0 for an error, a positive value o.w.
1393 num = strtol(val, &expr, 0);
1394 if ((num == LONG_MAX) || (num <= 0) || (expr == val))
1396 num = strtoq(val, &expr, 0);
1397 if ((num == QUAD_MAX) || (num <= 0) || (expr == val))
1438 num *= str_offt(expr + 1);
1450 * for those option functions where the archive format has nothing to do.
1468 * print the usage summary to the user
1479 (void)fputs("usage: pax [-cdjnvz] [-E limit] [-f archive] ", stderr);
1480 (void)fputs("[-s replstr] ... [-U user] ...", stderr);
1481 (void)fputs("\n [-G group] ... ", stderr);
1482 (void)fputs("[-T [from_date][,to_date]] ... ", stderr);
1483 (void)fputs("[pattern ...]\n", stderr);
1484 (void)fputs(" pax -r [-cdijknuvzDYZ] [-E limit] ", stderr);
1485 (void)fputs("[-f archive] [-o options] ... \n", stderr);
1486 (void)fputs(" [-p string] ... [-s replstr] ... ", stderr);
1487 (void)fputs("[-U user] ... [-G group] ...\n ", stderr);
1488 (void)fputs("[-T [from_date][,to_date]] ... ", stderr);
1489 (void)fputs(" [pattern ...]\n", stderr);
1490 (void)fputs(" pax -w [-dijtuvzHLPX] [-b blocksize] ", stderr);
1491 (void)fputs("[ [-a] [-f archive] ] [-x format] \n", stderr);
1492 (void)fputs(" [-B bytes] [-s replstr] ... ", stderr);
1493 (void)fputs("[-o options] ... [-U user] ...", stderr);
1494 (void)fputs("\n [-G group] ... ", stderr);
1495 (void)fputs("[-T [from_date][,to_date][/[c][m]]] ... ", stderr);
1496 (void)fputs("[file ...]\n", stderr);
1497 (void)fputs(" pax -r -w [-diklntuvDHLPXYZ] ", stderr);
1498 (void)fputs("[-p string] ... [-s replstr] ...", stderr);
1499 (void)fputs("\n [-U user] ... [-G group] ... ", stderr);
1500 (void)fputs("[-T [from_date][,to_date][/[c][m]]] ... ", stderr);
1501 (void)fputs("\n [file ...] directory\n", stderr);
1507 * print the usage summary to the user
1518 (void)fputs("usage: tar -{txru}[cevfbjmopswzBHLPXZ014578] [tapefile] ",
1520 (void)fputs("[blocksize] [replstr] [-C directory] file1 file2...\n",
1527 * print the usage summary to the user
1538 (void)fputs("usage: cpio -o [-aABcjLvVzZ] [-C bytes] [-H format] [-O archive]\n", stderr);
1539 (void)fputs(" [-F archive] < name-list [> archive]\n", stderr);
1540 (void)fputs(" cpio -i [-bBcdfjmnrsStuvVzZ6] [-C bytes] [-E file] [-H format]\n", stderr);
1541 (void)fputs(" [-I archive] [-F archive] [pattern...] [< archive]\n", stderr);
1542 (void)fputs(" cpio -p [-adlLmuvV] destination-directory < name-list\n", stderr);