summaryrefslogtreecommitdiffstats
path: root/private/posix/programs/pax/tar.c
blob: 7d091dff3bb26becd1e8c71582b08c6d7b0ca6ba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* $Source: /u/mark/src/pax/RCS/tar.c,v $
 *
 * $Revision: 1.2 $
 *
 * tar.c - tar specific functions for archive handling
 *
 * DESCRIPTION
 *
 *	These routines provide a tar conforming interface to the pax
 *	program.
 *
 * AUTHOR
 *
 *	Mark H. Colburn, NAPS International (mark@jhereg.mn.org)
 *
 * Sponsored by The USENIX Association for public distribution. 
 *
 * Copyright (c) 1989 Mark H. Colburn.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice is duplicated in all such 
 * forms and that any documentation, advertising materials, and other 
 * materials related to such distribution and use acknowledge that the 
 * software was developed by Mark H. Colburn and sponsored by The 
 * USENIX Association. 
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * $Log:	tar.c,v $
 * Revision 1.2  89/02/12  10:06:05  mark
 * 1.2 release fixes
 * 
 * Revision 1.1  88/12/23  18:02:38  mark
 * Initial revision
 * 
 */

#ifndef lint
static char *ident = "$Id: tar.c,v 1.2 89/02/12 10:06:05 mark Exp $";
static char *copyright ="Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.";
#endif /* not lint */

/* Headers */

#include "pax.h"


/* Defines */

#define DEF_BLOCKING	20	/* default blocking factor for extract */


/* Function Prototypes */

#ifdef __STDC__

static int taropt(int , char **, char *);
static void usage(void);

#else /* !__STDC__ */

static int taropt();
static void usage();

#endif /* __STDC__ */


/* do_tar - main routine for tar. 
 *
 * DESCRIPTION
 *
 *	Provides a tar interface to the PAX program.  All tar standard
 *	command line options are supported.
 *
 * PARAMETERS
 *
 *	int argc	- argument count (argc from main) 
 *	char **argv	- argument list (argv from main) 
 *
 * RETURNS
 *
 *	zero
 */

#ifdef __STDC__

void do_tar(int argc, char **argv)  /* Xn */

#else

void do_tar(argc, argv)  /* Xn */
int             argc;		/* argument count (argc from main) */
char          **argv;		/* argument list (argv from main) */

#endif
{
    int             c;		/* Option letter */

    /* Set default option values */
    names_from_stdin = 0;
#ifdef DF_TRACE_DEBUG
printf("DF_TRACE_DEBUG: void do_tar() in tar.c\n");
#endif
    ar_file = getenv("TAPE");	/* From environment, or */
    if (ar_file == 0) {
	ar_file = DEF_AR_FILE;	/* From Makefile */
    }

    /*
     * set up the flags to reflect the default pax inteface.  Unfortunately
     * the pax interface has several options which are completely opposite
     * of the tar and/or cpio interfaces...
     */
    f_unconditional = 1;
    f_mtime = 1;
    f_dir_create = 1;
    blocking = 0;
    ar_interface = TAR;
    ar_format = TAR;
    msgfile=stderr;

    /* Parse options */
    while ((c = taropt(argc, argv, "b:cf:hlmortuvwx")) != EOF) {
	switch (c) {
	case 'b':		/* specify blocking factor */
	    /* 
	     * FIXME - we should use a conversion routine that does
	     * some kind of reasonable error checking, but...
	     */
	    blocking = atoi(optarg);
	    break;
	case 'c':		/* create a new archive */
	    f_create = 1;
	    break;
	case 'f':		/* specify input/output file */
	    ar_file = optarg;
	    break;
	case 'h':
	    f_follow_links = 1;	/* follow symbolic links */
	    break;
	case 'l':		/* report unresolved links */
	    f_linksleft = 1;
	    break;
	case 'm':		/* don't restore modification times */
	    f_modified = 1;
	    break;
	case 'o':		/* take on user's group rather than
				 * archives */
	    break;
	case 'r':		/* named files are appended to archive */
	    f_append = 1;
	    break;
	case 't':
	    f_list = 1;		/* list files in archive */
	    break;
	case 'u':		/* named files are added to archive */
	    f_newer = 1;
	    break;
	case 'v':		/* verbose mode */
	    f_verbose = 1;
	    break;
	case 'w':		/* user interactive mode */
	    f_disposition = 1;
	    break;
	case 'x':		/* named files are extracted from archive */
	    f_extract = 1;
	    break;
	case '?':
	    usage();
	    exit(EX_ARGSBAD);
	}
    }

    /* check command line argument sanity */
    if (f_create + f_extract + f_list + f_append + f_newer != 1) {
	(void) fprintf(stderr,
	   "%s: you must specify exactly one of the c, t, r, u or x options\n",
		       myname);
	usage();
	exit(EX_ARGSBAD);
    }

    /* set the blocking factor, if not set by the user */
    if (blocking == 0) {
#ifdef USG
	if (f_extract || f_list) {
	    blocking = DEF_BLOCKING;
	    fprintf(stderr, "Tar: blocksize = %d\n", blocking);
	} else {
	    blocking = 1;
	}
#else /* !USG */
	blocking = 20;
#endif /* USG */
    }
    blocksize = blocking * BLOCKSIZE;
    buf_allocate((OFFSET) blocksize);

    if (f_create) {
	open_archive(AR_WRITE);
	create_archive();	/* create the archive */
    } else if (f_extract) {
	open_archive(AR_READ);
	read_archive();		/* extract files from archive */
    } else if (f_list) {
	open_archive(AR_READ);
	read_archive();		/* read and list contents of archive */
    } else if (f_append) {
	open_archive(AR_APPEND);
	append_archive();	/* append files to archive */
    }
    
    if (f_linksleft) {		
	linkleft(); 		/* report any unresolved links */ 
    }
#if 0  /* Xn */
    
    return (0);
#endif  /* Xn */
}


/* taropt -  tar specific getopt
 *
 * DESCRIPTION
 *
 * 	Plug-compatible replacement for getopt() for parsing tar-like
 * 	arguments.  If the first argument begins with "-", it uses getopt;
 * 	otherwise, it uses the old rules used by tar, dump, and ps.
 *
 * PARAMETERS
 *
 *	int argc	- argument count (argc from main) 
 *	char **argv	- argument list (argv from main) 
 *	char *optstring	- sring which describes allowable options
 *
 * RETURNS
 *
 *	Returns the next option character in the option string(s).  If the
 *	option requires an argument and an argument was given, the argument
 *	is pointed to by "optarg".  If no option character was found,
 *	returns an EOF.
 *
 */

#ifdef __STDC__

static int taropt(int argc, char **argv, char *optstring)

#else

static int taropt(argc, argv, optstring)
int             argc;
char          **argv;
char           *optstring;

#endif
{
    extern char    *optarg;	/* Points to next arg */
    extern int      optind;	/* Global argv index */
    static char    *key = NULL;	/* Points to next keyletter */  /* Xn */
    static char     use_getopt = 0;	/* !=0 if argv[1][0] was '-' */  /* Xn */
    char            c;
    char           *place;

#ifdef DF_TRACE_DEBUG
printf("DF_TRACE_DEBUG: static int taropt() in tar.c\n");
#endif
    optarg = (char *)NULL;

    if (key == (char *)NULL) {		/* First time */
	if (argc < 2)
	    return EOF;
	key = argv[1];
	if (*key == '-')
	    use_getopt++;
	else
	    optind = 2;
    }
    if (use_getopt) {
#ifdef _POSIX2_SOURCE  /* Xn */
	return getopt(argc, (const char * const *) argv, optstring);  /* Xn */
#else  /* Xn */
	return getopt(argc, argv, optstring);
#endif  /* Xn */
    }

    c = *key++;
    if (c == '\0') {
	key--;
	return EOF;
    }
    place = strchr(optstring, c);

    if (place == (char *)NULL || c == ':') {
	fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
	return ('?');
    }
    place++;
    if (*place == ':') {
	if (optind < argc) {
	    optarg = argv[optind];
	    optind++;
	} else {
	    fprintf(stderr, "%s: %c argument missing\n",
		    argv[0], c);
	    return ('?');
	}
    }
    return (c);
}


/* usage - print a helpful message and exit
 *
 * DESCRIPTION
 *
 *	Usage prints out the usage message for the TAR interface and then
 *	exits with a non-zero termination status.  This is used when a user
 *	has provided non-existant or incompatible command line arguments.
 *
 * RETURNS
 *
 *	Returns an exit status of 1 to the parent process.
 *
 */

#ifdef __STDC__

static void usage(void)

#else

static void usage()

#endif
{
#ifdef DF_TRACE_DEBUG
printf("DF_TRACE_DEBUG: static void usage() in tar.c\n");
#endif
    fprintf(stderr, "Usage: %s -c[bfvw] device block filename..\n", myname);
    fprintf(stderr, "       %s -r[bvw] device block [filename...]\n", myname);
    fprintf(stderr, "       %s -t[vf] device\n", myname);
    fprintf(stderr, "       %s -u[bvw] device block [filename...]\n", myname);
    fprintf(stderr, "       %s -x[flmovw] device [filename...]\n", myname);
    exit(1);
}