#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
#include <sys/cdefs.h>
#if !defined(lint)
#if 0
static char sccsid[] = "@(#)options.c 8.2 (Berkeley) 4/18/94";
#else
__RCSID("$NetBSD: options.c,v 1.122 2025/05/04 20:06:23 rillig Exp $");
#endif
#endif
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <ctype.h>
#include <errno.h>
#if HAVE_NBTOOL_CONFIG_H
#include "compat_getopt.h"
#else
#include <getopt.h>
#endif
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <inttypes.h>
#include <paths.h>
#include <util.h>
#include "pax.h"
#include "options.h"
#include "cpio.h"
#include "tar.h"
#include "extern.h"
#ifndef SMALL
#include "mtree.h"
#endif
char *chdname;
#if !HAVE_NBTOOL_CONFIG_H
int do_chroot;
#endif
struct stat tst;
static int nopids;
static char flgch[] = FLGCH;
static OPLIST *ophead = NULL;
static OPLIST *optail = NULL;
static int opt_add(const char *);
static int no_op(void);
static void printflg(unsigned int);
static int c_frmt(const void *, const void *);
static off_t str_offt(char *);
static char *get_line(FILE *fp);
#ifndef SMALL
static int set_tstamp(const char *, struct stat *);
#endif
static void pax_options(int, char **);
__dead static void pax_usage(void);
static void tar_options(int, char **);
__dead static void tar_usage(void);
#ifndef NO_CPIO
static void cpio_options(int, char **);
__dead static void cpio_usage(void);
#endif
#define GETLINE_FILE_CORRUPT 1
#define GETLINE_OUT_OF_MEM 2
static int get_line_error;
#define BZIP2_CMD "bzip2"
#define GZIP_CMD "gzip"
#define XZ_CMD "xz"
#define COMPRESS_CMD "compress"
#define OPT_USE_COMPRESS_PROGRAM 0
#define OPT_CHECKPOINT 1
#define OPT_UNLINK 2
#define OPT_HELP 3
#define OPT_ATIME_PRESERVE 4
#define OPT_IGNORE_FAILED_READ 5
#define OPT_REMOVE_FILES 6
#define OPT_NULL 7
#define OPT_TOTALS 8
#define OPT_VERSION 9
#define OPT_EXCLUDE 10
#define OPT_BLOCK_COMPRESS 11
#define OPT_NORECURSE 12
#define OPT_FORCE_LOCAL 13
#define OPT_INSECURE 14
#define OPT_STRICT 15
#define OPT_SPARSE 16
#define OPT_XZ 17
#define OPT_GNU 18
#define OPT_TIMESTAMP 19
#if !HAVE_NBTOOL_CONFIG_H
#define OPT_CHROOT 20
#endif
FSUB fsub[] = {
#ifndef NO_CPIO
{ "bcpio", 5120, sizeof(HD_BCPIO), 1, 0, 0, 1, bcpio_id, cpio_strd,
bcpio_rd, bcpio_endrd, cpio_stwr, bcpio_wr, cpio_endwr, NULL,
cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
{ "cpio", 5120, sizeof(HD_CPIO), 1, 0, 0, 1, cpio_id, cpio_strd,
cpio_rd, cpio_endrd, cpio_stwr, cpio_wr, cpio_endwr, NULL,
cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
{ "sv4cpio", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, vcpio_id, cpio_strd,
vcpio_rd, vcpio_endrd, cpio_stwr, vcpio_wr, cpio_endwr, NULL,
cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
{ "sv4crc", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, crc_id, crc_strd,
vcpio_rd, vcpio_endrd, crc_stwr, vcpio_wr, cpio_endwr, NULL,
cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
#endif
{ "tar", 10240, BLKMULT, 0, 1, BLKMULT, 0, tar_id, no_op,
tar_rd, tar_endrd, no_op, tar_wr, tar_endwr, tar_trail,
NULL, rd_wrfile, wr_rdfile, tar_opt },
{ "ustar", 10240, BLKMULT, 0, 1, BLKMULT, 0, ustar_id, ustar_strd,
ustar_rd, tar_endrd, ustar_stwr, ustar_wr, tar_endwr, tar_trail,
NULL, rd_wrfile, wr_rdfile, bad_opt }
};
#ifndef NO_CPIO
#define F_BCPIO 0
#define F_CPIO 1
#define F_SV4CPIO 2
#define F_SV4CRC 3
#define F_TAR 4
#define F_USTAR 5
#else
#define F_TAR 0
#define F_USTAR 1
#endif
#define DEFLT F_USTAR
int ford[] = {F_USTAR, F_TAR,
#ifndef NO_CPIO
F_SV4CRC, F_SV4CPIO, F_CPIO, F_BCPIO,
#endif
-1};
int sep = '\n';
int havechd = 0;
void
options(int argc, char **argv)
{
if ((argv0 = strrchr(argv[0], '/')) != NULL)
argv0++;
else
argv0 = argv[0];
if (strstr(argv0, NM_TAR)) {
argv0 = NM_TAR;
tar_options(argc, argv);
#ifndef NO_CPIO
} else if (strstr(argv0, NM_CPIO)) {
argv0 = NM_CPIO;
cpio_options(argc, argv);
#endif
} else {
argv0 = NM_PAX;
pax_options(argc, argv);
}
}
struct option pax_longopts[] = {
{ "insecure", no_argument, 0,
OPT_INSECURE },
{ "force-local", no_argument, 0,
OPT_FORCE_LOCAL },
{ "use-compress-program", required_argument, 0,
OPT_USE_COMPRESS_PROGRAM },
{ "xz", no_argument, 0,
OPT_XZ },
{ "gnu", no_argument, 0,
OPT_GNU },
{ "timestamp", required_argument, 0,
OPT_TIMESTAMP },
{ 0, 0, 0,
0 },
};
static void
pax_options(int argc, char **argv)
{
int c;
size_t i;
u_int64_t flg = 0;
u_int64_t bflg = 0;
char *pt;
FSUB tmp;
while ((c = getopt_long(argc, argv,
"0ab:cdf:ijklno:p:rs:tuvwx:zAB:DE:G:HLMN:OPT:U:VXYZ",
pax_longopts, NULL)) != -1) {
switch (c) {
case '0':
sep = '\0';
break;
case 'a':
flg |= AF;
break;
case 'b':
flg |= BF;
if ((wrblksz = (int)str_offt(optarg)) <= 0) {
tty_warn(1, "Invalid block size %s", optarg);
pax_usage();
}
break;
case 'c':
cflag = 1;
flg |= CF;
break;
case 'd':
dflag = 1;
flg |= DF;
break;
case 'f':
arcname = optarg;
flg |= FF;
break;
case 'i':
iflag = 1;
flg |= IF;
break;
case 'j':
gzip_program = BZIP2_CMD;
break;
case 'k':
kflag = 1;
flg |= KF;
break;
case 'l':
lflag = 1;
flg |= LF;
break;
case 'n':
nflag = 1;
flg |= NF;
break;
case 'o':
flg |= OF;
if (opt_add(optarg) < 0)
pax_usage();
break;
case 'p':
for (pt = optarg; *pt != '\0'; ++pt) {
switch(*pt) {
case 'a':
patime = 0;
break;
case 'e':
pids = 1;
pmode = 1;
patime = 1;
pmtime = 1;
pfflags = 1;
break;
#if 0
case 'f':
pfflags = 0;
break;
#endif
case 'm':
pmtime = 0;
break;
case 'o':
pids = 1;
break;
case 'p':
pmode = 1;
break;
default:
tty_warn(1, "Invalid -p string: %c",
*pt);
pax_usage();
}
}
flg |= PF;
break;
case 'r':
flg |= RF;
break;
case 's':
if (rep_add(optarg) < 0)
pax_usage();
flg |= SF;
break;
case 't':
tflag = 1;
flg |= TF;
break;
case 'u':
uflag = 1;
flg |= UF;
break;
case 'v':
vflag = 1;
flg |= VF;
break;
case 'w':
flg |= WF;
break;
case 'x':
tmp.name = optarg;
frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt);
if (frmt != NULL) {
flg |= XF;
break;
}
tty_warn(1, "Unknown -x format: %s", optarg);
(void)fputs("pax: Known -x formats are:", stderr);
for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
(void)fprintf(stderr, " %s", fsub[i].name);
(void)fputs("\n\n", stderr);
pax_usage();
case 'z':
gzip_program = GZIP_CMD;
break;
case 'A':
Aflag = 1;
flg |= CAF;
break;
case 'B':
if ((wrlimit = str_offt(optarg)) <= 0) {
tty_warn(1, "Invalid write limit %s", optarg);
pax_usage();
}
if (wrlimit % BLKMULT) {
tty_warn(1,
"Write limit is not a %d byte multiple",
BLKMULT);
pax_usage();
}
flg |= CBF;
break;
case 'D':
Dflag = 1;
flg |= CDF;
break;
case 'E':
flg |= CEF;
if (strcmp(none, optarg) == 0)
maxflt = -1;
else if ((maxflt = atoi(optarg)) < 0) {
tty_warn(1,
"Error count value must be positive");
pax_usage();
}
break;
case 'G':
if (grp_add(optarg) < 0)
pax_usage();
flg |= CGF;
break;
case 'H':
Hflag = 1;
flg |= CHF;
break;
case 'L':
Lflag = 1;
flg |= CLF;
break;
#ifdef SMALL
case 'M':
case 'N':
tty_warn(1, "Support for -%c is not compiled in", c);
exit(1);
#else
case 'M':
Mflag = 1;
flg |= CMF;
break;
case 'N':
if (!setup_getid(optarg)) {
tty_warn(1,
"Unable to use user and group databases in `%s'",
optarg);
pax_usage();
}
break;
#endif
case 'O':
force_one_volume = 1;
break;
case 'P':
Lflag = 0;
flg |= CPF;
break;
case 'T':
if (trng_add(optarg) < 0)
pax_usage();
flg |= CTF;
break;
case 'U':
if (usr_add(optarg) < 0)
pax_usage();
flg |= CUF;
break;
case 'V':
Vflag = 1;
flg |= VSF;
break;
case 'X':
Xflag = 1;
flg |= CXF;
break;
case 'Y':
Yflag = 1;
flg |= CYF;
break;
case 'Z':
Zflag = 1;
flg |= CZF;
break;
case OPT_INSECURE:
secure = 0;
break;
case OPT_FORCE_LOCAL:
forcelocal = 1;
break;
case OPT_USE_COMPRESS_PROGRAM:
gzip_program = optarg;
break;
case OPT_XZ:
gzip_program = XZ_CMD;
break;
case OPT_GNU:
is_gnutar = 1;
break;
#ifndef SMALL
case OPT_TIMESTAMP:
if (set_tstamp(optarg, &tst) == -1) {
tty_warn(1, "Invalid timestamp `%s'", optarg);
tar_usage();
}
break;
#endif
case '?':
default:
pax_usage();
}
}
if (ISLIST(flg)) {
act = LIST;
listf = stdout;
bflg = flg & BDLIST;
} else if (ISEXTRACT(flg)) {
act = EXTRACT;
bflg = flg & BDEXTR;
} else if (ISARCHIVE(flg)) {
act = ARCHIVE;
bflg = flg & BDARCH;
} else if (ISAPPND(flg)) {
act = APPND;
bflg = flg & BDARCH;
} else if (ISCOPY(flg)) {
act = COPY;
bflg = flg & BDCOPY;
} else
pax_usage();
if (bflg) {
printflg(flg);
pax_usage();
}
if (!(flg & XF) && (act == ARCHIVE))
frmt = &(fsub[DEFLT]);
switch (act) {
case LIST:
case EXTRACT:
for (; optind < argc; optind++)
if (pat_add(argv[optind], NULL, 0) < 0)
pax_usage();
break;
case COPY:
if (optind >= argc) {
tty_warn(0, "Destination directory was not supplied");
pax_usage();
}
--argc;
dirptr = argv[argc];
if (mkpath(dirptr) < 0)
exit(1);
case ARCHIVE:
case APPND:
for (; optind < argc; optind++)
if (ftree_add(argv[optind], 0) < 0)
pax_usage();
maxflt = 0;
break;
}
}
struct option tar_longopts[] = {
{ "block-size", required_argument, 0, 'b' },
{ "bunzip2", no_argument, 0, 'j' },
{ "bzip2", no_argument, 0, 'j' },
{ "create", no_argument, 0, 'c' },
{ "file", required_argument, 0, 'f' },
{ "dereference", no_argument, 0, 'h' },
{ "keep-old-files", no_argument, 0, 'k' },
{ "one-file-system", no_argument, 0, 'l' },
{ "modification-time", no_argument, 0, 'm' },
{ "old-archive", no_argument, 0, 'o' },
{ "portability", no_argument, 0, 'o' },
{ "same-permissions", no_argument, 0, 'p' },
{ "preserve-permissions", no_argument, 0, 'p' },
{ "preserve", no_argument, 0, 'p' },
{ "fast-read", no_argument, 0, 'q' },
{ "append", no_argument, 0, 'r' },
{ "update", no_argument, 0, 'u' },
{ "list", no_argument, 0, 't' },
{ "verbose", no_argument, 0, 'v' },
{ "interactive", no_argument, 0, 'w' },
{ "confirmation", no_argument, 0, 'w' },
{ "extract", no_argument, 0, 'x' },
{ "get", no_argument, 0, 'x' },
{ "gzip", no_argument, 0, 'z' },
{ "gunzip", no_argument, 0, 'z' },
{ "read-full-blocks", no_argument, 0, 'B' },
{ "directory", required_argument, 0, 'C' },
{ "xz", no_argument, 0, 'J' },
{ "to-stdout", no_argument, 0, 'O' },
{ "absolute-paths", no_argument, 0, 'P' },
{ "sparse", no_argument, 0, 'S' },
{ "files-from", required_argument, 0, 'T' },
{ "summary", no_argument, 0, 'V' },
{ "stats", no_argument, 0, 'V' },
{ "exclude-from", required_argument, 0, 'X' },
{ "compress", no_argument, 0, 'Z' },
{ "uncompress", no_argument, 0, 'Z' },
{ "strict", no_argument, 0,
OPT_STRICT },
{ "atime-preserve", no_argument, 0,
OPT_ATIME_PRESERVE },
{ "unlink", no_argument, 0,
OPT_UNLINK },
{ "use-compress-program", required_argument, 0,
OPT_USE_COMPRESS_PROGRAM },
{ "force-local", no_argument, 0,
OPT_FORCE_LOCAL },
{ "insecure", no_argument, 0,
OPT_INSECURE },
{ "exclude", required_argument, 0,
OPT_EXCLUDE },
{ "no-recursion", no_argument, 0,
OPT_NORECURSE },
#if !HAVE_NBTOOL_CONFIG_H
{ "chroot", no_argument, 0,
OPT_CHROOT },
#endif
{ "timestamp", required_argument, 0,
OPT_TIMESTAMP },
#if 0
{ "catenate", no_argument, 0, 'A' },
{ "concatenate", no_argument, 0, 'A' },
{ "diff", no_argument, 0, 'd' },
{ "compare", no_argument, 0, 'd' },
{ "checkpoint", no_argument, 0,
OPT_CHECKPOINT },
{ "help", no_argument, 0,
OPT_HELP },
{ "info-script", required_argument, 0, 'F' },
{ "new-volume-script", required_argument, 0, 'F' },
{ "incremental", no_argument, 0, 'G' },
{ "listed-incremental", required_argument, 0, 'g' },
{ "ignore-zeros", no_argument, 0, 'i' },
{ "ignore-failed-read", no_argument, 0,
OPT_IGNORE_FAILED_READ },
{ "starting-file", no_argument, 0, 'K' },
{ "tape-length", required_argument, 0, 'L' },
{ "multi-volume", no_argument, 0, 'M' },
{ "after-date", required_argument, 0, 'N' },
{ "newer", required_argument, 0, 'N' },
{ "record-number", no_argument, 0, 'R' },
{ "remove-files", no_argument, 0,
OPT_REMOVE_FILES },
{ "same-order", no_argument, 0, 's' },
{ "preserve-order", no_argument, 0, 's' },
{ "null", no_argument, 0,
OPT_NULL },
{ "totals", no_argument, 0,
OPT_TOTALS },
{ "volume-name", required_argument, 0, 'V' },
{ "label", required_argument, 0, 'V' },
{ "version", no_argument, 0,
OPT_VERSION },
{ "verify", no_argument, 0, 'W' },
{ "block-compress", no_argument, 0,
OPT_BLOCK_COMPRESS },
#endif
{ 0, 0, 0, 0 },
};
static void
tar_set_action(int op)
{
if (act != ERROR && act != op)
tar_usage();
act = op;
}
static void
tar_options(int argc, char **argv)
{
int c;
int fstdin = 0;
int Oflag = 0;
int nincfiles = 0;
int incfiles_max = 0;
struct incfile {
char *file;
char *dir;
};
struct incfile *incfiles = NULL;
rmleadslash = 1;
is_gnutar = 1;
while ((c = getoldopt(argc, argv,
"+b:cef:hjklmopqrs:tuvwxzBC:HI:JOPST:X:Z014578",
tar_longopts, NULL))
!= -1) {
switch(c) {
case 'b':
if ((wrblksz = (int)str_offt(optarg)) <= 0) {
tty_warn(1, "Invalid block size %s", optarg);
tar_usage();
}
wrblksz *= 512;
break;
case 'c':
tar_set_action(ARCHIVE);
break;
case 'e':
maxflt = 0;
break;
case 'f':
if ((optarg[0] == '-') && (optarg[1]== '\0')) {
fstdin = 1;
arcname = NULL;
break;
}
fstdin = 0;
arcname = optarg;
break;
case 'h':
Lflag = 1;
break;
case 'j':
gzip_program = BZIP2_CMD;
break;
case 'k':
kflag = 1;
break;
case 'l':
Xflag = 1;
break;
case 'm':
pmtime = 0;
break;
case 'o':
if (act == ARCHIVE) {
Oflag = 1;
if (opt_add("write_opt=nodir") < 0)
tar_usage();
} else {
pids = 0;
nopids = 1;
}
break;
case 'p':
if (!nopids)
pids = 1;
pmode = 1;
patime = 1;
pmtime = 1;
break;
case 'q':
nflag = 1;
break;
case 'r':
case 'u':
tar_set_action(APPND);
break;
case 's':
if (rep_add(optarg) < 0)
tar_usage();
break;
case 't':
tar_set_action(LIST);
break;
case 'v':
vflag = 1;
break;
case 'w':
iflag = 1;
break;
case 'x':
tar_set_action(EXTRACT);
pmtime = 1;
break;
case 'z':
gzip_program = GZIP_CMD;
break;
case 'B':
break;
case 'C':
havechd++;
chdname = optarg;
break;
case 'H':
Hflag = 1;
break;
case 'I':
case 'T':
if (++nincfiles > incfiles_max) {
incfiles_max = nincfiles + 3;
incfiles = realloc(incfiles,
sizeof(*incfiles) * incfiles_max);
if (incfiles == NULL) {
tty_warn(0, "Unable to allocate space "
"for option list");
exit(1);
}
}
incfiles[nincfiles - 1].file = optarg;
incfiles[nincfiles - 1].dir = chdname;
break;
case 'J':
gzip_program = XZ_CMD;
break;
case 'O':
Oflag = 1;
break;
case 'P':
rmleadslash = 0;
Aflag = 1;
break;
case 'S':
break;
case 'V':
Vflag = 1;
break;
case 'X':
if (tar_gnutar_X_compat(optarg) != 0)
tar_usage();
break;
case 'Z':
gzip_program = COMPRESS_CMD;
break;
case '0':
arcname = DEV_0;
break;
case '1':
arcname = DEV_1;
break;
case '4':
arcname = DEV_4;
break;
case '5':
arcname = DEV_5;
break;
case '7':
arcname = DEV_7;
break;
case '8':
arcname = DEV_8;
break;
case OPT_ATIME_PRESERVE:
patime = 1;
break;
case OPT_UNLINK:
break;
case OPT_USE_COMPRESS_PROGRAM:
gzip_program = optarg;
break;
case OPT_FORCE_LOCAL:
forcelocal = 1;
break;
case OPT_INSECURE:
secure = 0;
break;
case OPT_STRICT:
is_gnutar = 0;
break;
case OPT_EXCLUDE:
if (tar_gnutar_minus_minus_exclude(optarg) != 0)
tar_usage();
break;
case OPT_NORECURSE:
dflag = 1;
break;
#if !HAVE_NBTOOL_CONFIG_H
case OPT_CHROOT:
do_chroot = 1;
break;
#endif
#ifndef SMALL
case OPT_TIMESTAMP:
if (set_tstamp(optarg, &tst) == -1) {
tty_warn(1, "Invalid timestamp `%s'", optarg);
tar_usage();
}
break;
#endif
default:
tar_usage();
}
}
argc -= optind;
argv += optind;
if (act == ERROR)
tar_usage();
if (fstdin == 1 && act == ARCHIVE)
listf = stderr;
else
listf = stdout;
if ((act == ARCHIVE || act == APPND) && argc == 0 && nincfiles == 0)
exit(0);
if (act == ARCHIVE || act == APPND)
frmt = &(fsub[Oflag ? F_TAR : F_USTAR]);
else if (Oflag) {
if (act == EXTRACT)
to_stdout = 1;
else {
tty_warn(1, "The -O/-o options are only valid when "
"writing or extracting an archive");
tar_usage();
}
}
switch (act) {
case LIST:
case EXTRACT:
default:
{
int sawpat = 0;
int dirisnext = 0;
char *file, *dir = NULL;
int mustfreedir = 0;
while (nincfiles || *argv != NULL) {
if (nincfiles) {
file = incfiles->file;
dir = incfiles->dir;
mustfreedir = 0;
incfiles++;
nincfiles--;
} else if (strcmp(*argv, "-I") == 0) {
if (*++argv == NULL)
break;
file = *argv++;
dir = chdname;
mustfreedir = 0;
} else {
file = NULL;
dir = NULL;
mustfreedir = 0;
}
if (file != NULL) {
FILE *fp;
char *str;
if (strcmp(file, "-") == 0)
fp = stdin;
else if ((fp = fopen(file, "r")) == NULL) {
tty_warn(1, "Unable to open file '%s' for read", file);
tar_usage();
}
while ((str = get_line(fp)) != NULL) {
if (dirisnext) {
if (dir && mustfreedir)
free(dir);
dir = str;
mustfreedir = 1;
dirisnext = 0;
continue;
}
if (strcmp(str, "-C") == 0) {
havechd++;
dirisnext = 1;
free(str);
continue;
}
if (strncmp(str, "-C ", 3) == 0) {
havechd++;
if (dir && mustfreedir)
free(dir);
dir = strdup(str + 3);
mustfreedir = 1;
free(str);
continue;
}
if (pat_add(str, dir, NOGLOB_MTCH) < 0)
tar_usage();
sawpat = 1;
}
if (dirisnext)
tar_usage();
if (dir && mustfreedir)
free(dir);
if (strcmp(file, "-") != 0)
fclose(fp);
if (get_line_error) {
tty_warn(1, "Problem with file '%s'", file);
tar_usage();
}
} else if (strcmp(*argv, "-C") == 0) {
if (*++argv == NULL)
break;
chdname = *argv++;
havechd++;
} else if (pat_add(*argv++, chdname, 0) < 0)
tar_usage();
else
sawpat = 1;
}
if (sawpat > 0)
chdname = NULL;
}
break;
case ARCHIVE:
case APPND:
if (chdname != NULL) {
if (ftree_add(chdname, 1) < 0)
tar_usage();
}
while (nincfiles || *argv != NULL) {
char *file, *dir;
if (nincfiles) {
file = incfiles->file;
dir = incfiles->dir;
incfiles++;
nincfiles--;
} else if (strcmp(*argv, "-I") == 0) {
if (*++argv == NULL)
break;
file = *argv++;
dir = NULL;
} else {
file = NULL;
dir = NULL;
}
if (file != NULL) {
FILE *fp;
char *str;
int dirisnext = 0;
if (dir) {
if (ftree_add(dir, 1) < 0)
tar_usage();
}
if (strcmp(file, "-") == 0)
fp = stdin;
else if ((fp = fopen(file, "r")) == NULL) {
tty_warn(1, "Unable to open file '%s' for read", file);
tar_usage();
}
while ((str = get_line(fp)) != NULL) {
if (dirisnext) {
if (ftree_add(str, 1) < 0)
tar_usage();
dirisnext = 0;
continue;
}
if (strcmp(str, "-C") == 0) {
dirisnext = 1;
continue;
}
if (strncmp(str, "-C ", 3) == 0) {
if (ftree_add(str + 3, 1) < 0)
tar_usage();
continue;
}
if (ftree_add(str, 0) < 0)
tar_usage();
}
if (dirisnext)
tar_usage();
if (strcmp(file, "-") != 0)
fclose(fp);
if (get_line_error) {
tty_warn(1, "Problem with file '%s'",
file);
tar_usage();
}
} else if (strcmp(*argv, "-C") == 0) {
if (*++argv == NULL)
break;
if (ftree_add(*argv++, 1) < 0)
tar_usage();
} else if (ftree_add(*argv++, 0) < 0)
tar_usage();
}
maxflt = 0;
break;
}
if (!fstdin && ((arcname == NULL) || (*arcname == '\0'))) {
arcname = getenv("TAPE");
if ((arcname == NULL) || (*arcname == '\0'))
arcname = _PATH_DEFTAPE;
}
}
int
mkpath(char *path)
{
char *slash;
int done = 0;
slash = path;
while (!done) {
slash += strspn(slash, "/");
slash += strcspn(slash, "/");
done = (*slash == '\0');
*slash = '\0';
if (domkdir(path, 0777) == -1)
goto out;
if (!done)
*slash = '/';
}
return 0;
out:
syswarn(1, errno, "Cannot create directory `%s'", path);
return -1;
}
#ifndef NO_CPIO
struct option cpio_longopts[] = {
{ "reset-access-time", no_argument, 0, 'a' },
{ "make-directories", no_argument, 0, 'd' },
{ "nonmatching", no_argument, 0, 'f' },
{ "extract", no_argument, 0, 'i' },
{ "link", no_argument, 0, 'l' },
{ "preserve-modification-time", no_argument, 0, 'm' },
{ "create", no_argument, 0, 'o' },
{ "pass-through", no_argument, 0, 'p' },
{ "rename", no_argument, 0, 'r' },
{ "list", no_argument, 0, 't' },
{ "unconditional", no_argument, 0, 'u' },
{ "verbose", no_argument, 0, 'v' },
{ "append", no_argument, 0, 'A' },
{ "pattern-file", required_argument, 0, 'E' },
{ "file", required_argument, 0, 'F' },
{ "force-local", no_argument, 0,
OPT_FORCE_LOCAL },
{ "format", required_argument, 0, 'H' },
{ "dereference", no_argument, 0, 'L' },
{ "swap-halfwords", no_argument, 0, 'S' },
{ "summary", no_argument, 0, 'V' },
{ "stats", no_argument, 0, 'V' },
{ "insecure", no_argument, 0,
OPT_INSECURE },
{ "sparse", no_argument, 0,
OPT_SPARSE },
{ "xz", no_argument, 0,
OPT_XZ },
#ifdef notyet
{ "null", no_argument, 0, '0' },
{ "swap", no_argument, 0, 'b' },
{ "numeric-uid-gid", no_argument, 0, 'n' },
{ "swap-bytes", no_argument, 0, 's' },
{ "message", required_argument, 0, 'M' },
{ "owner", required_argument, 0 'R' },
{ "dot", no_argument, 0, 'V' },
{ "block-size", required_argument, 0,
OPT_BLOCK_SIZE },
{ "no-absolute-pathnames", no_argument, 0,
OPT_NO_ABSOLUTE_PATHNAMES },
{ "no-preserve-owner", no_argument, 0,
OPT_NO_PRESERVE_OWNER },
{ "only-verify-crc", no_argument, 0,
OPT_ONLY_VERIFY_CRC },
{ "rsh-command", required_argument, 0,
OPT_RSH_COMMAND },
{ "version", no_argument, 0,
OPT_VERSION },
#endif
{ 0, 0, 0, 0 },
};
static void
cpio_set_action(int op)
{
if ((act == APPND && op == ARCHIVE) || (act == ARCHIVE && op == APPND))
act = APPND;
else if (act == EXTRACT && op == LIST)
act = op;
else if (act != ERROR && act != op)
cpio_usage();
else
act = op;
}
static void
cpio_options(int argc, char **argv)
{
FSUB tmp;
u_int64_t flg = 0;
u_int64_t bflg = 0;
int c;
size_t i;
FILE *fp;
char *str;
uflag = 1;
kflag = 1;
pids = 1;
pmode = 1;
pmtime = 0;
arcname = NULL;
dflag = 1;
nodirs = 1;
while ((c = getoldopt(argc, argv,
"+abcdfiklmoprstuvzABC:E:F:H:I:LM:O:R:SVZ6",
cpio_longopts, NULL)) != -1) {
switch(c) {
case 'a':
tflag = 1;
flg |= TF;
break;
#ifdef notyet
case 'b':
break;
#endif
case 'c':
frmt = &fsub[F_SV4CPIO];
break;
case 'd':
nodirs = 0;
break;
case 'f':
cflag = 1;
flg |= CF;
break;
case 'i':
cpio_set_action(EXTRACT);
flg |= RF;
break;
#ifdef notyet
case 'k':
break;
#endif
case 'l':
lflag = 1;
flg |= LF;
break;
case 'm':
flg |= PF;
pmtime = 1;
break;
case 'o':
cpio_set_action(ARCHIVE);
frmt = &(fsub[F_SV4CRC]);
flg |= WF;
break;
case 'p':
cpio_set_action(COPY);
flg |= RF | WF;
break;
case 'r':
iflag = 1;
flg |= IF;
break;
#ifdef notyet
case 's':
break;
#endif
case 't':
cpio_set_action(LIST);
listf = stdout;
flg &= ~RF;
break;
case 'u':
uflag = 0;
kflag = 0;
flg |= UF;
break;
case 'v':
vflag = 1;
flg |= VF;
break;
case 'z':
gzip_program = GZIP_CMD;
break;
case 'A':
cpio_set_action(APPND);
flg |= AF;
break;
case 'B':
blksz = 5120;
break;
case 'C':
if ((blksz = (int)str_offt(optarg)) <= 0) {
tty_warn(1, "Invalid block size %s", optarg);
cpio_usage();
}
break;
case 'E':
if ((fp = fopen(optarg, "r")) == NULL) {
tty_warn(1, "Unable to open file '%s' for read",
optarg);
cpio_usage();
}
while ((str = get_line(fp)) != NULL) {
pat_add(str, NULL, 0);
}
fclose(fp);
if (get_line_error) {
tty_warn(1, "Problem with file '%s'", optarg);
cpio_usage();
}
break;
case 'H':
tmp.name = optarg;
frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt);
if (frmt != NULL) {
flg |= XF;
break;
}
tty_warn(1, "Unknown -H format: %s", optarg);
(void)fputs("cpio: Known -H formats are:", stderr);
for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
(void)fprintf(stderr, " %s", fsub[i].name);
(void)fputs("\n\n", stderr);
cpio_usage();
case 'F':
case 'I':
case 'O':
if ((optarg[0] == '-') && (optarg[1]== '\0')) {
arcname = NULL;
break;
}
arcname = optarg;
break;
case 'L':
Lflag = 1;
flg |= CLF;
break;
#ifdef notyet
case 'M':
arg = optarg;
break;
case 'R':
arg = optarg;
break;
#endif
case 'S':
cpio_swp_head = 1;
break;
#ifdef notyet
case 'V':
break;
#endif
case 'V':
Vflag = 1;
flg |= VF;
break;
case 'Z':
gzip_program = COMPRESS_CMD;
break;
case '6':
frmt = &(fsub[F_BCPIO]);
break;
case OPT_FORCE_LOCAL:
forcelocal = 1;
break;
case OPT_INSECURE:
secure = 0;
break;
case OPT_SPARSE:
break;
case OPT_XZ:
gzip_program = XZ_CMD;
break;
default:
cpio_usage();
}
}
if (ISLIST(flg)) {
act = LIST;
bflg = flg & BDLIST;
} else if (ISEXTRACT(flg)) {
act = EXTRACT;
bflg = flg & BDEXTR;
} else if (ISARCHIVE(flg)) {
act = ARCHIVE;
bflg = flg & BDARCH;
} else if (ISAPPND(flg)) {
act = APPND;
bflg = flg & BDARCH;
} else if (ISCOPY(flg)) {
act = COPY;
bflg = flg & BDCOPY;
} else
cpio_usage();
if (bflg) {
cpio_usage();
}
if (!(flg & XF) && (act == ARCHIVE))
frmt = &(fsub[F_BCPIO]);
switch (act) {
case LIST:
case EXTRACT:
for (; optind < argc; optind++)
if (pat_add(argv[optind], NULL, 0) < 0)
cpio_usage();
break;
case COPY:
if (optind >= argc) {
tty_warn(0, "Destination directory was not supplied");
cpio_usage();
}
--argc;
dirptr = argv[argc];
case ARCHIVE:
case APPND:
if (argc != optind) {
for (; optind < argc; optind++)
if (ftree_add(argv[optind], 0) < 0)
cpio_usage();
break;
}
maxflt = 0;
while ((str = get_line(stdin)) != NULL) {
ftree_add(str, 0);
}
if (get_line_error) {
tty_warn(1, "Problem while reading stdin");
cpio_usage();
}
break;
default:
cpio_usage();
}
}
#endif
static void
printflg(unsigned int flg)
{
int nxt;
(void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
while ((nxt = ffs(flg)) != 0) {
flg &= ~(1 << (nxt - 1));
(void)fprintf(stderr, " -%c", flgch[nxt - 1]);
}
(void)putc('\n', stderr);
}
static int
c_frmt(const void *a, const void *b)
{
return strcmp(((const FSUB *)a)->name, ((const FSUB *)b)->name);
}
OPLIST *
opt_next(void)
{
OPLIST *opt;
if ((opt = ophead) != NULL)
ophead = ophead->fow;
return opt;
}
int
bad_opt(void)
{
OPLIST *opt;
if (ophead == NULL)
return 0;
tty_warn(1," These format options are not supported for %s",
frmt->name);
while ((opt = opt_next()) != NULL)
(void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
if (strcmp(NM_TAR, argv0) == 0)
tar_usage();
#ifndef NO_CPIO
else if (strcmp(NM_CPIO, argv0) == 0)
cpio_usage();
#endif
else
pax_usage();
}
int
opt_add(const char *str)
{
OPLIST *opt;
char *frpt;
char *pt;
char *endpt;
char *dstr;
if ((str == NULL) || (*str == '\0')) {
tty_warn(0, "Invalid option name");
return -1;
}
if ((dstr = strdup(str)) == NULL) {
tty_warn(0, "Unable to allocate space for option list");
return -1;
}
frpt = endpt = dstr;
while ((frpt != NULL) && (*frpt != '\0')) {
if ((endpt = strchr(frpt, ',')) != NULL)
*endpt = '\0';
if ((pt = strchr(frpt, '=')) == NULL) {
tty_warn(0, "Invalid options format");
free(dstr);
return -1;
}
if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
tty_warn(0, "Unable to allocate space for option list");
free(dstr);
return -1;
}
*pt++ = '\0';
opt->name = frpt;
opt->value = pt;
opt->fow = NULL;
if (endpt != NULL)
frpt = endpt + 1;
else
frpt = NULL;
if (ophead == NULL) {
optail = ophead = opt;
continue;
}
optail->fow = opt;
optail = opt;
}
return 0;
}
static off_t
str_offt(char *val)
{
char *expr;
off_t num, t;
num = STRTOOFFT(val, &expr, 0);
if ((num == OFFT_MAX) || (num <= 0) || (expr == val))
return 0;
switch(*expr) {
case 'b':
t = num;
num *= 512;
if (t > num)
return 0;
++expr;
break;
case 'k':
t = num;
num *= 1024;
if (t > num)
return 0;
++expr;
break;
case 'm':
t = num;
num *= 1048576;
if (t > num)
return 0;
++expr;
break;
case 'w':
t = num;
num *= sizeof(int);
if (t > num)
return 0;
++expr;
break;
}
switch(*expr) {
case '\0':
break;
case '*':
case 'x':
t = num;
num *= str_offt(expr + 1);
if (t > num)
return 0;
break;
default:
return 0;
}
return num;
}
static char *
get_line(FILE *f)
{
char *name, *temp;
size_t len;
name = fgetln(f, &len);
if (!name) {
get_line_error = ferror(f) ? GETLINE_FILE_CORRUPT : 0;
return 0;
}
if (name[len-1] != '\n')
len++;
temp = malloc(len);
if (!temp) {
get_line_error = GETLINE_OUT_OF_MEM;
return 0;
}
memcpy(temp, name, len-1);
temp[len-1] = 0;
return temp;
}
#ifndef SMALL
static int
set_tstamp(const char *b, struct stat *st)
{
time_t when;
char *eb;
long long l;
if (stat(b, st) != -1)
return 0;
#ifndef HAVE_NBTOOL_CONFIG_H
errno = 0;
if ((when = parsedate(b, NULL, NULL)) == -1 && errno != 0)
#endif
{
errno = 0;
l = strtoll(b, &eb, 0);
if (b == eb || *eb || errno)
return -1;
when = (time_t)l;
}
st->st_ino = 1;
#if HAVE_STRUCT_STAT_BIRTHTIME
st->st_birthtime =
#endif
st->st_mtime = st->st_ctime = st->st_atime = when;
return 0;
}
#endif
static int
no_op(void)
{
return 0;
}
static void
pax_usage(void)
{
fprintf(stderr,
"usage: pax [-0cdjnvzVO] [-E limit] [-f archive] [-N dbdir] [-s replstr] ...\n"
" [-U user] ... [-G group] ... [-T [from_date][,to_date]] ...\n"
" [pattern ...]\n");
fprintf(stderr,
" pax -r [-cdijknuvzADOVYZ] [-E limit] [-f archive] [-N dbdir]\n"
" [-o options] ... [-p string] ... [-s replstr] ... [-U user] ...\n"
" [-G group] ... [-T [from_date][,to_date]] ... [pattern ...]\n");
fprintf(stderr,
" pax -w [-dijtuvzAHLMOPVX] [-b blocksize] [[-a] [-f archive]] [-x format]\n"
" [-B bytes] [-N dbdir] [-o options] ... [-s replstr] ...\n"
" [-U user] ... [-G group] ...\n"
" [-T [from_date][,to_date][/[c][m]]] ... [file ...]\n");
fprintf(stderr,
" pax -r -w [-dijklntuvzADHLMOPVXYZ] [-N dbdir] [-p string] ...\n"
" [-s replstr] ... [-U user] ... [-G group] ...\n"
" [-T [from_date][,to_date][/[c][m]]] ... [file ...] directory\n");
exit(1);
}
static void
tar_usage(void)
{
(void)fputs("usage: tar [-]{crtux}[-befhjklmopqvwzHJOPSXZ014578] "
"[archive] [blocksize]\n"
" [-C directory] [-T file] [-s replstr] "
"[file ...]\n", stderr);
exit(1);
}
#ifndef NO_CPIO
static void
cpio_usage(void)
{
(void)fputs("usage: cpio -o [-aABcLvzZ] [-C bytes] [-F archive] "
"[-H format] [-O archive]\n"
" < name-list [> archive]\n"
" cpio -i [-bBcdfmrsStuvzZ6] [-C bytes] [-E file] "
"[-F archive] [-H format] \n"
" [-I archive] "
"[pattern ...] [< archive]\n"
" cpio -p [-adlLmuv] destination-directory "
"< name-list\n", stderr);
exit(1);
}
#endif