1 /* $OpenBSD: options.c,v 1.70 2008/06/11 00:49:08 pvalchev 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. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static const char sccsid[] = "@(#)options.c 8.2 (Berkeley) 4/18/94";
41 static const char rcsid[] = "$OpenBSD: options.c,v 1.70 2008/06/11 00:49:08 pvalchev Exp $";
45 #include <sys/types.h>
49 #include <sys/param.h>
64 * Routines which handle command line options
67 static char flgch[] = FLGCH; /* list of all possible flags */
68 static OPLIST *ophead = NULL; /* head for format specific options -x */
69 static OPLIST *optail = NULL; /* option tail */
71 static int no_op(void);
72 static void printflg(unsigned int);
73 static int c_frmt(const void *, const void *);
74 static off_t str_offt(char *);
75 static char *getline(FILE *fp);
76 static void pax_options(int, char **);
77 static void pax_usage(void);
78 static void tar_options(int, char **);
79 static void tar_usage(void);
80 static void cpio_options(int, char **);
81 static void cpio_usage(void);
83 /* errors from getline */
84 #define GETLINE_FILE_CORRUPT 1
85 #define GETLINE_OUT_OF_MEM 2
86 static int getline_error;
89 #define GZIP_CMD "gzip" /* command to run as gzip */
90 #define COMPRESS_CMD "compress" /* command to run as compress */
91 #define BZIP2_CMD "bzip2" /* command to run as bzip2 */
94 * Format specific routine table - MUST BE IN SORTED ORDER BY NAME
95 * (see pax.h for description of each function)
97 * name, blksz, hdsz, udev, hlk, blkagn, inhead, id, st_read,
98 * read, end_read, st_write, write, end_write, trail,
99 * rd_data, wr_data, options
103 /* 0: OLD BINARY CPIO */
104 {"bcpio", 5120, sizeof(HD_BCPIO), 1, 0, 0, 1, bcpio_id, cpio_strd,
105 bcpio_rd, bcpio_endrd, cpio_stwr, bcpio_wr, cpio_endwr, cpio_trail,
106 rd_wrfile, wr_rdfile, bad_opt},
108 /* 1: OLD OCTAL CHARACTER CPIO */
109 {"cpio", 5120, sizeof(HD_CPIO), 1, 0, 0, 1, cpio_id, cpio_strd,
110 cpio_rd, cpio_endrd, cpio_stwr, cpio_wr, cpio_endwr, cpio_trail,
111 rd_wrfile, wr_rdfile, bad_opt},
113 /* 2: SVR4 HEX CPIO */
114 {"sv4cpio", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, vcpio_id, cpio_strd,
115 vcpio_rd, vcpio_endrd, cpio_stwr, vcpio_wr, cpio_endwr, cpio_trail,
116 rd_wrfile, wr_rdfile, bad_opt},
118 /* 3: SVR4 HEX CPIO WITH CRC */
119 {"sv4crc", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, crc_id, crc_strd,
120 vcpio_rd, vcpio_endrd, crc_stwr, vcpio_wr, cpio_endwr, cpio_trail,
121 rd_wrfile, wr_rdfile, bad_opt},
124 {"tar", 10240, BLKMULT, 0, 1, BLKMULT, 0, tar_id, no_op,
125 tar_rd, tar_endrd, no_op, tar_wr, tar_endwr, tar_trail,
126 rd_wrfile, wr_rdfile, tar_opt},
129 {"ustar", 10240, BLKMULT, 0, 1, BLKMULT, 0, ustar_id, ustar_strd,
130 ustar_rd, tar_endrd, ustar_stwr, ustar_wr, tar_endwr, tar_trail,
131 rd_wrfile, wr_rdfile, bad_opt},
133 #define F_OCPIO 0 /* format when called as cpio -6 */
134 #define F_ACPIO 1 /* format when called as cpio -c */
135 #define F_CPIO 3 /* format when called as cpio */
136 #define F_OTAR 4 /* format when called as tar -o */
137 #define F_TAR 5 /* format when called as tar */
138 #define DEFLT 5 /* default write format from list above */
141 * ford is the archive search order used by get_arc() to determine what kind
142 * of archive we are dealing with. This helps to properly id archive formats
143 * some formats may be subsets of others....
145 int ford[] = {5, 4, 3, 2, 1, 0, -1 };
148 * Do we have -C anywhere?
154 * figure out if we are pax, tar or cpio. Call the appropriate options
159 options(int argc, char **argv)
163 * Are we acting like pax, tar or cpio (based on argv[0])
165 if ((argv0 = strrchr(argv[0], '/')) != NULL)
170 if (strcmp(NM_TAR, argv0) == 0) {
171 tar_options(argc, argv);
173 } else if (strcmp(NM_CPIO, argv0) == 0) {
174 cpio_options(argc, argv);
178 * assume pax as the default
181 pax_options(argc, argv);
186 * look at the user specified flags. set globals as required and check if
187 * the user specified a legal set of flags. If not, complain and exit
191 pax_options(int argc, char **argv)
195 unsigned int flg = 0;
196 unsigned int bflg = 0;
201 * process option flags
203 while ((c=getopt(argc,argv,"ab:cdf:ijklno:p:rs:tuvwx:zB:DE:G:HLOPT:U:XYZ0"))
217 if ((wrblksz = (int)str_offt(optarg)) <= 0) {
218 paxwarn(1, "Invalid block size %s", optarg);
224 * inverse match on patterns
231 * match only dir on extract, not the subtree at dir
238 * filename where the archive is stored
245 * interactive file rename
252 * use bzip2. Non standard option.
254 gzip_program = BZIP2_CMD;
258 * do not clobber files that exist
265 * try to link src to dest with copy (-rw)
272 * select first match for a pattern only
279 * pass format specific options
282 if (opt_add(optarg) < 0)
287 * specify file characteristic options
289 for (pt = optarg; *pt != '\0'; ++pt) {
293 * do not preserve access time
299 * preserve user id, group id, file
300 * mode, access/modification times
309 * do not preserve modification time
321 * preserve file mode bits
326 paxwarn(1, "Invalid -p string: %c", *pt);
341 * file name substitution name pattern
343 if (rep_add(optarg) < 0) {
351 * preserve access time on filesystem nodes we read
358 * ignore those older files
365 * verbose operation mode
378 * specify an archive format on write
381 if ((frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
382 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt)) != NULL) {
386 paxwarn(1, "Unknown -x format: %s", optarg);
387 (void)fputs("pax: Known -x formats are:", stderr);
388 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
389 (void)fprintf(stderr, " %s", fsub[i].name);
390 (void)fputs("\n\n", stderr);
395 * use gzip. Non standard option.
397 gzip_program = GZIP_CMD;
401 * non-standard option on number of bytes written on a
402 * single archive volume.
404 if ((wrlimit = str_offt(optarg)) <= 0) {
405 paxwarn(1, "Invalid write limit %s", optarg);
408 if (wrlimit % BLKMULT) {
409 paxwarn(1, "Write limit is not a %d byte multiple",
417 * On extraction check file inode change time before the
418 * modification of the file name. Non standard option.
425 * non-standard limit on read faults
426 * 0 indicates stop after first error, values
427 * indicate a limit, "NONE" try forever
430 if (strcmp(NONE, optarg) == 0)
432 else if ((maxflt = atoi(optarg)) < 0) {
433 paxwarn(1, "Error count value must be positive");
439 * non-standard option for selecting files within an
440 * archive by group (gid or name)
442 if (grp_add(optarg) < 0) {
450 * follow command line symlinks only
464 * Force one volume. Non standard option.
466 force_one_volume = 1;
470 * do NOT follow symlinks (default)
477 * non-standard option for selecting files within an
478 * archive by modification time range (lower,upper)
480 if (trng_add(optarg) < 0) {
488 * non-standard option for selecting files within an
489 * archive by user (uid or name)
491 if (usr_add(optarg) < 0) {
499 * do not pass over mount points in the file system
506 * On extraction check file inode change time after the
507 * modification of the file name. Non standard option.
514 * On extraction check modification time after the
515 * modification of the file name. Non standard option.
522 * Use \0 as pathname terminator.
523 * (For use with the -print0 option of find(1).)
535 * figure out the operation mode of pax read,write,extract,copy,append
536 * or list. check that we have not been given a bogus set of flags
537 * for the operation mode.
543 } else if (ISEXTRACT(flg)) {
546 } else if (ISARCHIVE(flg)) {
549 } else if (ISAPPND(flg)) {
552 } else if (ISCOPY(flg)) {
563 * if we are writing (ARCHIVE) we use the default format if the user
564 * did not specify a format. when we write during an APPEND, we will
565 * adopt the format of the existing archive if none was supplied.
567 if (!(flg & XF) && (act == ARCHIVE))
568 frmt = &(fsub[DEFLT]);
571 * process the args as they are interpreted by the operation mode
576 for (; optind < argc; optind++)
577 if (pat_add(argv[optind], NULL) < 0)
581 if (optind >= argc) {
582 paxwarn(0, "Destination directory was not supplied");
590 for (; optind < argc; optind++)
591 if (ftree_add(argv[optind], 0) < 0)
594 * no read errors allowed on updates/append operation!
604 * look at the user specified flags. set globals as required and check if
605 * the user specified a legal set of flags. If not, complain and exit
609 tar_options(int argc, char **argv)
615 int incfiles_max = 0;
620 struct incfile *incfiles = NULL;
623 * Set default values.
628 * process option flags
630 while ((c = getoldopt(argc, argv,
631 "b:cef:hjmopqruts:vwxzBC:HI:LOPXZ014578")) != -1) {
635 * specify blocksize in 512-byte blocks
637 if ((wrblksz = (int)str_offt(optarg)) <= 0) {
638 paxwarn(1, "Invalid block size %s", optarg);
641 wrblksz *= 512; /* XXX - check for int oflow */
651 * stop after first error
657 * filename where the archive is stored
659 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
678 * use bzip2. Non standard option.
680 gzip_program = BZIP2_CMD;
684 * do not preserve modification time
696 * preserve uid/gid and file mode, regardless of umask
703 * select first match for a pattern only
710 * append to the archive
716 * file name substitution name pattern
718 if (rep_add(optarg) < 0) {
725 * list contents of the tape
731 * verbose operation mode
737 * interactive file rename
743 * extract an archive, preserving mode,
744 * and mtime if possible.
751 * use gzip. Non standard option.
753 gzip_program = GZIP_CMD;
757 * Nothing to do here, this is pax default
766 * follow command line symlinks only
771 if (++nincfiles > incfiles_max) {
772 incfiles_max = nincfiles + 3;
773 incfiles = realloc(incfiles,
774 sizeof(*incfiles) * incfiles_max);
775 if (incfiles == NULL) {
776 paxwarn(0, "Unable to allocate space "
781 incfiles[nincfiles - 1].file = optarg;
782 incfiles[nincfiles - 1].dir = chdname;
792 * do not remove leading '/' from pathnames
798 * do not pass over mount points in the file system
806 gzip_program = COMPRESS_CMD;
834 /* Traditional tar behaviour (pax uses stderr unless in list mode) */
835 if (fstdin == 1 && act == ARCHIVE)
840 /* Traditional tar behaviour (pax wants to read file list from stdin) */
841 if ((act == ARCHIVE || act == APPND) && argc == 0 && nincfiles == 0)
845 * process the args as they are interpreted by the operation mode
855 while (nincfiles || *argv != NULL) {
857 * If we queued up any include files,
858 * pull them in now. Otherwise, check
859 * for -I and -C positional flags.
860 * Anything else must be a file to
864 file = incfiles->file;
868 } else if (strcmp(*argv, "-I") == 0) {
879 if (strcmp(file, "-") == 0)
881 else if ((fp = fopen(file, "r")) == NULL) {
882 paxwarn(1, "Unable to open file '%s' for read", file);
885 while ((str = getline(fp)) != NULL) {
886 if (pat_add(str, dir) < 0)
890 if (strcmp(file, "-") != 0)
893 paxwarn(1, "Problem with file '%s'", file);
896 } else if (strcmp(*argv, "-C") == 0) {
901 } else if (pat_add(*argv++, chdname) < 0)
907 * if patterns were added, we are doing chdir()
908 * on a file-by-file basis, else, just one
909 * global chdir (if any) after opening input.
917 frmt = &(fsub[Oflag ? F_OTAR : F_TAR]);
919 if (Oflag == 2 && opt_add("write_opt=nodir") < 0)
922 if (chdname != NULL) { /* initial chdir() */
923 if (ftree_add(chdname, 1) < 0)
927 while (nincfiles || *argv != NULL) {
931 * If we queued up any include files, pull them in
932 * now. Otherwise, check for -I and -C positional
933 * flags. Anything else must be a file to include
937 file = incfiles->file;
941 } else if (strcmp(*argv, "-I") == 0) {
952 /* Set directory if needed */
954 if (ftree_add(dir, 1) < 0)
958 if (strcmp(file, "-") == 0)
960 else if ((fp = fopen(file, "r")) == NULL) {
961 paxwarn(1, "Unable to open file '%s' for read", file);
964 while ((str = getline(fp)) != NULL) {
965 if (ftree_add(str, 0) < 0)
968 if (strcmp(file, "-") != 0)
971 paxwarn(1, "Problem with file '%s'",
975 } else if (strcmp(*argv, "-C") == 0) {
978 if (ftree_add(*argv++, 1) < 0)
981 } else if (ftree_add(*argv++, 0) < 0)
985 * no read errors allowed on updates/append operation!
990 if (!fstdin && ((arcname == NULL) || (*arcname == '\0'))) {
991 arcname = getenv("TAPE");
992 if ((arcname == NULL) || (*arcname == '\0'))
993 arcname = _PATH_DEFTAPE;
1010 slash += strspn(slash, "/");
1011 slash += strcspn(slash, "/");
1013 done = (*slash == '\0');
1016 if (stat(path, &sb)) {
1017 if (errno != ENOENT || mkdir(path, 0777)) {
1018 paxwarn(1, "%s", path);
1021 } else if (!S_ISDIR(sb.st_mode)) {
1022 syswarn(1, ENOTDIR, "%s", path);
1034 * look at the user specified flags. set globals as required and check if
1035 * the user specified a legal set of flags. If not, complain and exit
1039 cpio_options(int argc, char **argv)
1054 while ((c=getopt(argc,argv,"abcdfijklmoprstuvzABC:E:F:H:I:LO:SZ6")) != -1)
1058 * preserve access time on files read
1064 * swap bytes and half-words when reading data
1071 frmt = &(fsub[F_ACPIO]);
1075 * create directories as needed
1081 * invert meaning of pattern list
1087 * restore an archive
1093 * use bzip2. Non standard option.
1095 gzip_program = BZIP2_CMD;
1101 * use links instead of copies when possible
1107 * preserve modification time
1116 frmt = &(fsub[F_CPIO]);
1126 * interactively rename files
1132 * swap bytes after reading data
1137 * list contents of archive
1144 * replace newer files
1150 * verbose operation mode
1156 * use gzip. Non standard option.
1158 gzip_program = GZIP_CMD;
1168 * Use 5120 byte block size
1174 * set block size in bytes
1176 wrblksz = atoi(optarg);
1180 * file with patterns to extract or list
1182 if ((fp = fopen(optarg, "r")) == NULL) {
1183 paxwarn(1, "Unable to open file '%s' for read", optarg);
1186 while ((str = getline(fp)) != NULL) {
1190 if (getline_error) {
1191 paxwarn(1, "Problem with file '%s'", optarg);
1199 * filename where the archive is stored
1201 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
1203 * treat a - as stdin
1212 * specify an archive format on write
1215 if ((frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
1216 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt)) != NULL)
1218 paxwarn(1, "Unknown -H format: %s", optarg);
1219 (void)fputs("cpio: Known -H formats are:", stderr);
1220 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
1221 (void)fprintf(stderr, " %s", fsub[i].name);
1222 (void)fputs("\n\n", stderr);
1227 * follow symbolic links
1233 * swap halfwords after reading data
1238 * use compress. Non standard option.
1240 gzip_program = COMPRESS_CMD;
1244 * process Version 6 cpio format
1246 frmt = &(fsub[F_OCPIO]);
1257 * process the args as they are interpreted by the operation mode
1262 while (*argv != NULL)
1263 if (pat_add(*argv++, NULL) < 0)
1267 if (*argv == NULL) {
1268 paxwarn(0, "Destination directory was not supplied");
1272 if (mkpath(dirptr) < 0)
1282 * no read errors allowed on updates/append operation!
1285 while ((str = getline(stdin)) != NULL) {
1288 if (getline_error) {
1289 paxwarn(1, "Problem while reading stdin");
1301 * print out those invalid flag sets found to the user
1305 printflg(unsigned int flg)
1310 (void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
1311 while ((nxt = ffs(flg)) != 0) {
1314 (void)fprintf(stderr, " -%c", flgch[pos-1]);
1316 (void)putc('\n', stderr);
1321 * comparison routine used by bsearch to find the format specified
1326 c_frmt(const void *a, const void *b)
1328 return(strcmp(((FSUB *)a)->name, ((FSUB *)b)->name));
1333 * called by format specific options routines to get each format specific
1334 * flag and value specified with -o
1336 * pointer to next OPLIST entry or NULL (end of list).
1344 if ((opt = ophead) != NULL)
1345 ophead = ophead->fow;
1351 * generic routine used to complain about a format specific options
1352 * when the format does not support options.
1363 * print all we were given
1365 paxwarn(1,"These format options are not supported");
1366 while ((opt = opt_next()) != NULL)
1367 (void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
1374 * breaks the value supplied to -o into a option name and value. options
1375 * are given to -o in the form -o name-value,name=value
1376 * multiple -o may be specified.
1378 * 0 if format in name=value format, -1 if -o is passed junk
1382 opt_add(const char *str)
1390 if ((str == NULL) || (*str == '\0')) {
1391 paxwarn(0, "Invalid option name");
1394 if ((dstr = strdup(str)) == NULL) {
1395 paxwarn(0, "Unable to allocate space for option list");
1398 frpt = endpt = dstr;
1401 * break into name and values pieces and stuff each one into a
1402 * OPLIST structure. When we know the format, the format specific
1403 * option function will go through this list
1405 while ((frpt != NULL) && (*frpt != '\0')) {
1406 if ((endpt = strchr(frpt, ',')) != NULL)
1408 if ((pt = strchr(frpt, '=')) == NULL) {
1409 paxwarn(0, "Invalid options format");
1413 if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
1414 paxwarn(0, "Unable to allocate space for option list");
1426 if (ophead == NULL) {
1427 optail = ophead = opt;
1438 * Convert an expression of the following forms to an off_t > 0.
1439 * 1) A positive decimal number.
1440 * 2) A positive decimal number followed by a b (mult by 512).
1441 * 3) A positive decimal number followed by a k (mult by 1024).
1442 * 4) A positive decimal number followed by a m (mult by 512).
1443 * 5) A positive decimal number followed by a w (mult by sizeof int)
1444 * 6) Two or more positive decimal numbers (with/without k,b or w).
1445 * separated by x (also * for backwards compatibility), specifying
1446 * the product of the indicated values.
1448 * 0 for an error, a positive value o.w.
1458 num = strtol(val, &expr, 0);
1459 if ((num == LONG_MAX) || (num <= 0) || (expr == val))
1461 num = strtoq(val, &expr, 0);
1462 if ((num == QUAD_MAX) || (num <= 0) || (expr == val))
1503 num *= str_offt(expr + 1);
1519 name = fgetln(f, &len);
1521 getline_error = ferror(f) ? GETLINE_FILE_CORRUPT : 0;
1524 if (name[len-1] != '\n')
1528 getline_error = GETLINE_OUT_OF_MEM;
1531 memcpy(temp, name, len-1);
1538 * for those option functions where the archive format has nothing to do.
1551 * print the usage summary to the user
1558 "usage: pax [-0cdjnOvz] [-E limit] [-f archive] [-G group] [-s replstr]\n"
1559 " [-T range] [-U user] [pattern ...]\n"
1560 " pax -r [-0cDdijknOuvYZz] [-E limit] [-f archive] [-G group] [-o options]\n"
1561 " [-p string] [-s replstr] [-T range] [-U user] [pattern ...]\n"
1562 " pax -w [-0adHijLOPtuvXz] [-B bytes] [-b blocksize] [-f archive]\n"
1563 " [-G group] [-o options] [-s replstr] [-T range] [-U user]\n"
1564 " [-x format] [file ...]\n"
1565 " pax -rw [-0DdHikLlnOPtuvXYZ] [-G group] [-p string] [-s replstr]\n"
1566 " [-T range] [-U user] [file ...] directory\n",
1573 * print the usage summary to the user
1580 "usage: tar {crtux}[014578befHhjLmOoPpqsvwXZz]\n"
1581 " [blocking-factor | archive | replstr] [-C directory] [-I file]\n"
1583 " tar {-crtux} [-014578eHhjLmOoPpqvwXZz] [-b blocking-factor]\n"
1584 " [-C directory] [-f archive] [-I file] [-s replstr] [file ...]\n",
1591 * print the usage summary to the user
1598 "usage: cpio -o [-AaBcjLvZz] [-C bytes] [-F archive] [-H format]\n"
1599 " [-O archive] < name-list [> archive]\n"
1600 " cpio -i [-6BbcdfjmrSstuvZz] [-C bytes] [-E file] [-F archive] [-H format]\n"
1601 " [-I archive] [pattern ...] [< archive]\n"
1602 " cpio -p [-adLlmuv] destination-directory < name-list\n",