1 /* $Source: /u/mark/src/pax/RCS/pax.c,v $
7 * Pax is the archiver described in IEEE P1003.2. It is an archiver
8 * which understands both tar and cpio archives and has a new interface.
12 * pax -[cimopuvy] [-f archive] [-s replstr] [-t device] [pattern...]
13 * pax -r [-cimopuvy] [-f archive] [-s replstr] [-t device] [pattern...]
14 * pax -w [-adimuvy] [-b blocking] [-f archive] [-s replstr]...]
15 * [-t device][-x format][pathname...]
16 * pax -r -w [-ilmopuvy][-s replstr][pathname...] directory
20 * PAX - POSIX conforming tar and cpio archive handler. This
21 * program implements POSIX conformant versions of tar, cpio and pax
22 * archive handlers for UNIX. These handlers have defined befined
23 * by the IEEE P1003.2 commitee.
27 * A number of different compile time configuration options are
28 * available, please see the Makefile and config.h for more details.
32 * Mark H. Colburn, NAPS International (mark@jhereg.mn.org)
35 * Sponsored by The USENIX Association for public distribution.
37 * Copyright (c) 1989 Mark H. Colburn.
38 * All rights reserved.
40 * Redistribution and use in source and binary forms are permitted
41 * provided that the above copyright notice is duplicated in all such
42 * forms and that any documentation, advertising materials, and other
43 * materials related to such distribution and use acknowledge that the
44 * software was developed * by Mark H. Colburn and sponsored by The
47 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
48 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
49 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
52 * Revision 1.2 89/02/12 10:05:17 mark
55 * Revision 1.1 88/12/23 18:02:23 mark
61 static char *ident = "$Id: pax.c,v 1.2 89/02/12 10:05:17 mark Exp $";
62 static char *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
72 /* Globally Available Identifiers */
74 char *ar_file; /* File containing name of archive */
75 char *bufend; /* End of data within archive buffer */
76 char *bufstart; /* Archive buffer */
77 char *bufidx; /* Archive buffer index */
78 char *myname; /* name of executable (argv[0]) */
79 char **n_argv; /* Argv used by name routines */
80 int n_argc; /* Argc used by name routines */
81 int archivefd; /* Archive file descriptor */
82 int blocking; /* Size of each block, in records */
83 int gid; /* Group ID */
84 int head_standard; /* true if archive is POSIX format */
85 int ar_interface; /* defines interface we are using */
86 int ar_format; /* defines current archve format */
87 int mask; /* File creation mask */
88 int ttyf; /* For interactive queries */
89 int uid; /* User ID */
90 int names_from_stdin; /* names for files are from stdin */
91 OFFSET total; /* Total number of bytes transferred */
92 short f_access_time; /* Reset access times of input files */
93 short areof; /* End of input volume reached */
94 short f_dir_create; /* Create missing directories */
95 short f_append; /* Add named files to end of archive */
96 short f_create; /* create a new archive */
97 short f_extract; /* Extract named files from archive */
98 short f_follow_links; /* follow symbolic links */
99 short f_interactive; /* Interactivly extract files */
100 short f_linksleft; /* Report on unresolved links */
101 short f_list; /* List files on the archive */
102 short f_modified; /* Don't restore modification times */
103 short f_verbose; /* Turn on verbose mode */
104 short f_link; /* link files where possible */
105 short f_owner; /* extract files as the user */
106 short f_pass; /* pass files between directories */
107 short f_newer; /* append files to archive if newer */
108 short f_disposition; /* ask for file disposition */
109 short f_reverse_match; /* Reverse sense of pattern match */
110 short f_mtime; /* Retain file modification time */
111 short f_unconditional; /* Copy unconditionally */
112 time_t now = 0; /* Current time */
113 uint arvolume; /* Volume number */
114 uint blocksize = BLOCKSIZE; /* Archive block size */
115 FILE *msgfile; /* message outpu file stdout/stderr */
116 Replstr *rplhead = (Replstr *)NULL; /* head of replstr list */
117 Replstr *rpltail; /* pointer to tail of replstr list */
120 /* Function Prototypes */
124 static void usage(void);
125 static OFFSET pax_optsize(char *);
127 #else /* !__STDC__ */
130 static OFFSET pax_optsize();
132 #endif /* __STDC__ */
135 /* main - main routine for handling all archive formats.
139 * Set up globals and call the proper interface as specified by the user.
143 * int argc - count of user supplied arguments
144 * char **argv - user supplied arguments
148 * Returns an exit code of 0 to the parent process.
153 int main(int argc, char **argv)
163 /* strip the pathname off of the name of the executable */
164 if ((myname = strrchr(argv[0], '/')) != (char *)NULL) {
170 /* set upt for collecting other command line arguments */
171 name_init(argc, argv);
173 /* get all our necessary information */
177 now = time((time_t *) 0);
179 /* open terminal for interactive queries */
182 if (strcmp(myname, "tar")==0) {
184 } else if (strcmp(myname, "cpio")==0) {
194 /* do_pax - provide a PAX conformant user interface for archive handling
198 * Process the command line parameters given, doing some minimal sanity
199 * checking, and then launch the specified archiving functions.
203 * int ac - A count of arguments in av. Should be passed argc
205 * char **av - A pointer to an argument list. Should be passed
210 * Normally returns 0. If an error occurs, -1 is returned
211 * and state is set to reflect the error.
217 int do_pax(int ac, char **av)
222 int ac; /* argument counter */
223 char **av; /* arguments */
231 /* default input/output file for PAX is STDIN/STDOUT */
235 * set up the flags to reflect the default pax inteface. Unfortunately
236 * the pax interface has several options which are completely opposite
237 * of the tar and/or cpio interfaces...
246 ar_format = TAR; /* default interface if none given for -w */
249 while ((c = getopt(ac, av, "ab:cdf:ilmoprs:t:uvwx:y")) != EOF) {
256 if ((blocksize = pax_optsize(optarg)) == 0) {
257 fatal("Bad block size");
267 if (blocksize == 0) {
269 blocksize = 1 * BLOCKSIZE;
302 if (blocksize == 0) {
304 blocksize = 10 * BLOCKSIZE;
325 if (strcmp(optarg, "ustar") == 0) {
327 } else if (strcmp(optarg, "cpio") == 0) {
341 if (blocksize == 0) {
343 blocksize = blocking * BLOCKSIZE;
345 buf_allocate((OFFSET) blocksize);
347 if (f_extract || f_list) {
348 open_archive(AR_READ);
351 } else if (f_create) {
352 if (optind >= n_argc) {
353 names_from_stdin++; /* args from stdin */
355 open_archive(AR_WRITE);
357 } else if (f_append) {
358 open_archive(AR_APPEND);
361 } else if (f_pass && optind < n_argc) {
362 dirname = n_argv[--n_argc];
363 if (LSTAT(dirname, &st) < 0) {
366 if ((st.sb_mode & S_IFMT) != S_IFDIR) {
367 fatal("Not a directory");
369 if (optind >= n_argc) {
370 names_from_stdin++; /* args from stdin */
381 /* get_archive_type - determine input archive type from archive header
385 * reads the first block of the archive and determines the archive
386 * type from the data. If the archive type cannot be determined,
387 * processing stops, and a 1 is returned to the caller. If verbose
388 * mode is on, then the archive type will be printed on the standard
389 * error device as it is determined.
393 * be able to understand TAR and CPIO magic numbers
398 void get_archive_type(void)
402 void get_archive_type()
406 if (ar_read() != 0) {
407 fatal("Unable to determine archive type.");
409 if (strncmp(bufstart, "070707", 6) == 0) {
412 fputs("CPIO format archive\n", stderr);
414 } else if (strncmp(&bufstart[257], "ustar", 5) == 0) {
417 fputs("USTAR format archive\n", stderr);
425 /* pax_optsize - interpret a size argument
429 * Recognizes suffixes for blocks (512-bytes), k-bytes and megabytes.
430 * Also handles simple expressions containing '+' for addition.
434 * char *str - A pointer to the string to interpret
438 * Normally returns the value represented by the expression in the
443 * If the string cannot be interpretted, the program will fail, since
444 * the buffering will be incorrect.
450 static OFFSET pax_optsize(char *str)
454 static OFFSET pax_optsize(str)
455 char *str; /* pointer to string to interpret */
460 OFFSET number; /* temporary storage for current number */
461 OFFSET result; /* cumulative total to be returned to caller */
467 while (*idx >= '0' && *idx <= '9')
468 number = number * 10 + *idx++ - '0';
471 result += number * 512L;
474 result += number * 1024L;
477 result += number * 1024L * 1024L;
491 fatal("Unrecognizable value");
497 /* usage - print a helpful message and exit
501 * Usage prints out the usage message for the PAX interface and then
502 * exits with a non-zero termination status. This is used when a user
503 * has provided non-existant or incompatible command line arguments.
507 * Returns an exit status of 1 to the parent process.
513 static void usage(void)
521 fprintf(stderr, "Usage: %s -[cimopuvy] [-f archive] [-s replstr] [-t device] [pattern...]\n",
523 fprintf(stderr, " %s -r [-cimopuvy] [-f archive] [-s replstr] [-t device] [pattern...]\n",
525 fprintf(stderr, " %s -w [-adimuvy] [-b blocking] [-f archive] [-s replstr]\n [-t device] [-x format] [pathname...]\n",
527 fprintf(stderr, " %s -r -w [-ilmopuvy] [-s replstr] [pathname...] directory\n",