aboutsummaryrefslogtreecommitdiff
path: root/unix/boot/rtar/rtar.c
blob: 6ef2e37e438bffc4169d68b283ed58d77d59ee53 (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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
 */

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

#define NOKNET
#define	import_spp
#define	import_knames
#include <iraf.h>

#include "../bootProto.h"


/*
 * RTAR -- Read a UNIX tar format tape containing files with legal IRAF
 * virtual filenames.  Map tape filenames to host system filenames using
 * IRAF filename mapping if the tape does not contain legal host system
 * filenames.
 *
 * Switches:
 *		a	advance to first file in filelist before doing
 *			  anything.  useful for restarting an aborted
 *			  operation.  first file is not otherwise used.
 *		b	generate only C style binary byte stream output
 *			  files (default is to write a text file when
 *			  the input stream is text).
 *		d	print debug messages
 *		e	exclude, rather than include, listed files
 *		f	read from named file rather than stdin
 *		l	do not try to resolve links by a file copy
 *		m	do not restore file modify times
 *		n	do not strip tailing blank lines from text files
 *		o	omit binary files (e.g. when foreign host has
 *			  incompatible binary file format)
 *		p	omit the given pathname prefix when creating files
 *		r	replace existing file at extraction
 *		t	print name of each file matched
 *		u	do not attempt to restore user id
 *		v	verbose; print full description of each file
 *		x	extract files (extract everything if no files
 *			  listed or if -e is set)
 *
 * Switches must be given in a group, in any order, e.g.:
 *
 *	rtar -xetvf tarfile sys/osb sys/os lib/config.h$
 *
 * would extract all files from tarfile with names not beginning with sys/os
 * or sys/osb or with names not equal to lib/config.h, printing a verbose
 * description of each file extracted.  If an exclude filename does not end
 * with a $ all files with the given string as a prefix are excluded.
 */

#define TBLOCK		512
#define NBLOCK		20
#define NAMSIZ		100
#define	MAXERR		20
#define	MAXTRYS		100
#define	MAXLINELEN	256
#define	SZ_TAPEBUFFER	(TBLOCK * NBLOCK)
#define	EOS		'\0'
#define	ERR		(-1)
#define	OK		0
#define	RWXR_XR_X	0755
#define	SZ_PADBUF	8196
#define ctrlcode(c)	((c) >= '\007' && (c) <= '\017')

#define	LF_LINK		1
#define	LF_SYMLINK	2
#define	LF_DIR		5

/* File header structure.  One of these precedes each file on the tape.
 * Each file occupies an integral number of TBLOCK size logical blocks
 * on the tape.  The number of logical blocks per physical block is variable,
 * with at most NBLOCK logical blocks per physical tape block.  Two zero
 * blocks mark the end of the tar file.
 */
union hblock {
	char dummy[TBLOCK];
	struct header {
		char name[NAMSIZ];	/* NULL delimited		*/
		char mode[8];		/* octal, ascii			*/
		char uid[8];
		char gid[8];
		char size[12];
		char mtime[12];
		char chksum[8];
		char linkflag;
		char linkname[NAMSIZ];
	} dbuf;
};


/* Decoded file header.
 */
struct fheader {
	char	name[NAMSIZ];
	int	mode;
	int	uid;
	int	gid;
	int	isdir;
	long	size;
	long	mtime;
	long	chksum;
	int	linkflag;
	char	linkname[NAMSIZ];
};


static int advance;		/* Advance to named file		*/
static int stripblanks;		/* strip blank padding at end of file	*/
static int debug;		/* Print debugging messages		*/
static int binaryout;		/* make only binary byte stream files	*/
static int omitbinary;		/* omit binary files (do not write)	*/
static int extract;		/* Extract files from the tape		*/
static int replace;		/* Replace existing files		*/
static int exclude;		/* Excluded named files			*/
static int printfnames;		/* Print file names			*/
static int verbose;		/* Print everything			*/
static int links;		/* Defeat copy to resolve link		*/
static int setmtime;		/* Restore file modify times		*/
static int rsetuid;		/* Restore file user id			*/

static char *pathprefix = NULL;
static int len_pathprefix = 0;
static struct fheader *curfil;
static int eof;
static int nerrs;
static char *first_file;
static char tapeblock[SZ_TAPEBUFFER];
static char *nextblock;
static int nblocks;

extern int  ZZSTRT (void);
extern int  ZZSTOP (void);

extern  int tape_open (char *fname, int mode);
extern  int tape_close (int fd);
extern  int tape_read (int fd, char *buf, int nbytes);

static int   matchfile (char *fname, register char **files);
static int   getheader (int in, register struct fheader *fh);
static int   cchksum (register char *p, register int nbyte);
static void  printheader (FILE *out, register struct fheader *fh, int verbose);
static int   filetype (int in, struct fheader *fh);
static int   newfile (char *fname, int mode, int uid, int gid, int type);
static int   checkdir (register char *path, int mode, int uid, int gid);
static void  copyfile (int in, int out, struct fheader *fh, int ftype);
static void  strip_blanks (int in, int out, long nbytes);
static void  skipfile (int in, struct fheader *fh);
static char *getblock (int in);




char	*getblock();


/* MAIN -- "rtar [xtvlef] [names]".  The default operation is to extract all
 * files from the tar format standard input in quiet mode.
 */
int main (int argc, char *argv[])
{
	struct	fheader fh;
	char	**argp;
	char	*ip;
	int	in = 0, out;
	int	ftype;
	int	ch;

	ZZSTRT();		/* initialize the IRAF kernel	*/

	advance		= 0;
	debug		= 0;
	binaryout	= 0;
	omitbinary	= 0;
	extract		= 0;
	replace		= 0;
	exclude		= 0;
	printfnames	= 0;
	verbose		= 0;
	links		= 0;
	setmtime	= 1;
	rsetuid		= 1;
	stripblanks	= 1;	/* strip blanks at end of file by default */

	/* Get parameters.  Argp is left pointing at the list of files to be
	 * extracted (default all if no files named).
	 */
	argp = &argv[1];
	if (argc <= 1)
	    extract++;
	else {
	    while (*argp && **argp == '-') {
		ip = *argp++ + 1;
		while ((ch = *ip++) != EOS) {
		    switch (ch) {
		    case 'a':
			advance++;
			break;
		    case 'n':
			stripblanks = 0;
			break;
		    case 'x':
			extract++;
			break;
		    case 'b':
			binaryout++;
			break;
		    case 'd':
			debug++;
			break;
		    case 'e':
			exclude++;
			break;
		    case 'r':
			replace++;
			break;
		    case 't':
			printfnames++;
			break;
		    case 'v':
			printfnames++;
			verbose++;
			break;
		    case 'l':
			links++;
			break;
		    case 'm':
			setmtime = 0;
			break;
		    case 'u':
			rsetuid = 0;
			break;
		    case 'o':
			omitbinary++;
			break;
		    case 'p':
			if (*argp != NULL) {
			    pathprefix = *argp++;
			    len_pathprefix = strlen (pathprefix);
			}
			break;
		    case 'f':
			if (*argp == NULL) {
			    fprintf (stderr, "missing filename argument\n");
			    exit (OSOK+1);
			}
			in = tape_open (*argp, 0);
			if (in == ERR) {
			    fprintf (stderr, "cannot open `%s'\n", *argp);
			    ZZSTOP();
			    exit (OSOK+1);
			}
			argp++;
			break;
		    default:
			fprintf (stderr, "Warning: unknown switch `%c'\n", ch);
			fflush (stderr);
			break;
		    }
		}
	    }
	}

	/* If advancing to a file get the name of the file.  This file name
	 * occurs at the beginning of the file list but is not part of the list.
	 * Only full filenames are permitted here.
	 */
	if (advance)
	    first_file = *argp++;

	/* Step along through the tar format file.  Read file header and if
	 * file is in list and extraction is enabled, extract file.
	 */
	while (getheader (in, &fh) != EOF) {
	    curfil = &fh;
	    if (advance) {
		if (strcmp (fh.name, first_file) == 0) {
		    if (debug)
			fprintf (stderr, "match\n");
		    advance = 0;
		} else {
		    if (debug)
			printheader (stderr, &fh, verbose);
		    skipfile (in, &fh);
		    continue;
		}
	    }

	    if (matchfile (fh.name, argp) == exclude) {
		if (debug)
		    fprintf (stderr, "skip file `%s'\n", fh.name);
		skipfile (in, &fh);
		continue;
	    }

	    if (printfnames) {
		printheader (stdout, &fh, verbose);
		fflush (stdout);
	    }

	    if (fh.linkflag == LF_SYMLINK || fh.linkflag == LF_LINK) {
		/* No file follows header if file is a link.  Try to resolve
		 * the link by copying the original file, assuming it has been
		 * read from the tape.
		 */
		if (extract) {
		    if (fh.linkflag == LF_SYMLINK) {
			if (replace)
			    os_delete (fh.name);
			if (symlink (fh.linkname, fh.name) != 0) {
			    fprintf (stderr,
				"Cannot make symbolic link %s -> %s\n",
				fh.name, fh.linkname);
			}
		    } else if (fh.linkflag == LF_LINK && !links) {
			if (replace)
			    os_delete (fh.name);
			if (os_fcopy (fh.linkname, fh.name) == ERR) {
			    fprintf (stderr, "Copy `%s' to `%s' fails\n",
				fh.linkname, fh.name);
			} else {
			    os_setfmode (fh.name, fh.mode);
			    if (rsetuid)
				os_setowner (fh.name, fh.uid, fh.gid);
			    if (setmtime)
				os_setmtime (fh.name, fh.mtime);
			}
		    } else {
			fprintf (stderr,
			    "Warning: cannot make link `%s' to `%s'\n",
			    fh.name, fh.linkname);
		    }
		}
		continue;
	    }

	    if (extract) {
		ftype = filetype (in, &fh);
		if (fh.size > 0 && ftype == BINARY_FILE && omitbinary) {
		    if (printfnames)
			fprintf (stderr, "omit binary file `%s'\n", fh.name);
		    skipfile (in, &fh);
		    continue;
		}
		out = newfile (fh.name, fh.mode, fh.uid, fh.gid, ftype);
		if (out == ERR) {
		    fprintf (stderr, "cannot create file `%s'\n", fh.name);
		    skipfile (in, &fh);
		    continue;
		}
		if (!fh.isdir) {
		    copyfile (in, out, &fh, ftype);
		    os_close (out);
		}
		os_setfmode (fh.name, fh.mode);
		if (rsetuid)
		    os_setowner (fh.name, fh.uid, fh.gid);
		if (setmtime)
		    os_setmtime (fh.name, fh.mtime);
	    } else
		skipfile (in, &fh);
	}

	/* End of TAR file normally occurs when a zero tape block is read;
	 * this is not the same as the physical end of file, leading to
	 * problems when reading from sequential devices (e.g. pipes and
	 * magtape).  Advance to the physical end of file before exiting.
	 */
	if (!eof)
	    while (tape_read (in, tapeblock, SZ_TAPEBUFFER) > 0)
		;
	if (in)
	    tape_close (in);

	ZZSTOP();
	exit (OSOK);

	return (0);
}


/* MATCHFILE -- Search the filelist for the named file.  If the file list
 * is empty anything is a match.  If the list element ends with a $ an
 * exact match is required (excluding the $), otherwise we have a match if
 * the list element is a prefix of the filename.
 */
static int
matchfile (
    char *fname,		/* filename to be compared to list	*/
    register char **files	/* pointer to array of fname pointers	*/
)
{
	register char *fn, *ln;
	register int firstchar;

	if (*files == NULL)
	    return (1);

	firstchar = *fname;
	do {
	    if (**files++ == firstchar) {
		for (fn=fname, ln = *(files-1);  *ln && *ln == *fn++;  )
		    ln++;
		if (*ln == EOS)
		    return (1);
		else if (*ln == '$' && *(fn-1) == EOS)
		    return (1);
	    }
	} while (*files);

	return (0);
}


/* GETHEADER -- Read the next file block and attempt to interpret it as a
 * file header.  A checksum error on the file header is fatal and usually
 * indicates that the tape is not positioned to the beginning of a file.
 * If we have a legal header, decode the character valued fields into binary.
 */
static int
getheader (
    int	in,				/* input file			*/
    register struct fheader *fh		/* decoded file header (output)	*/
)
{
	register char *ip, *op;
	register int n;
	union	hblock *hb;
	int	tape_checksum, ntrys;

	for (ntrys=0;  ;  ntrys++) {
	    if ((hb = (union hblock *)getblock (in)) == NULL)
		return (EOF);

	    /* Decode the checksum value saved in the file header and then
	     * overwrite the field with blanks, as the field was blank when
	     * the checksum was originally computed.  Compute the actual
	     * checksum as the sum of all bytes in the header block.  If the
	     * sum is zero this indicates the end of the tar file, otherwise
	     * the checksums must match.
	     */
	    if (*hb->dbuf.chksum == '\0' && cchksum ((char *)hb, TBLOCK) == 0)
		return (EOF);
	    else
		sscanf (hb->dbuf.chksum, "%o", &tape_checksum);

	    for (ip=hb->dbuf.chksum, n=8;  --n >= 0;  )
		*ip++ = ' ';
	    if (cchksum ((char *)hb, TBLOCK) != tape_checksum) {
		/* If a checksum error occurs try to advance to the next
		 * header block.
		 */
		if (ntrys == 0) {
		    fprintf (stderr,
			"rtar: file header checksum error %o != %o\n",
	    		cchksum ((char *)hb, TBLOCK), tape_checksum);
		} else if (ntrys >= MAXTRYS) {
		    fprintf (stderr, "cannot recover from checksum error\n");
		    exit (OSOK+1);
		}
	    } else
		break;
	}

	if (ntrys > 1)
	    fprintf (stderr, "found next file following checksum error\n");

	/* Decode the ascii header fields into the output file header
	 * structure.
	 */
	for (ip=hb->dbuf.name, op=fh->name;  (*op++ = *ip++);  )
	    ;
	fh->isdir = (*(op-2) == '/');

	sscanf (hb->dbuf.mode,     "%o",  &fh->mode);
	sscanf (hb->dbuf.uid,      "%o",  &fh->uid);
	sscanf (hb->dbuf.gid,      "%o",  &fh->gid);
	sscanf (hb->dbuf.size,     "%lo", &fh->size);
	sscanf (hb->dbuf.mtime,    "%lo", &fh->mtime);

	n = hb->dbuf.linkflag;
	if (n >= '0' && n <= '9')
	    fh->linkflag = n - '0';
	else
	    fh->linkflag = 0;

	if (fh->linkflag)
	    strcpy (fh->linkname, hb->dbuf.linkname);
	
	return (TBLOCK);
}


/* CCHKSUM -- Compute the checksum of a byte array.
 */
static int
cchksum (
    register char *p,
    register int   nbytes
)
{
	register int	sum;

	for (sum=0;  --nbytes >= 0;  )
	    sum += *p++;

	return (sum);
}


struct _modebits {
	int	code;
	char	ch;
} modebits[] = {
    { 040000,	'd' },
    { 0400,	'r' },
    { 0200,	'w' },
    { 0100,	'x' },
    { 040,	'r' },
    { 020,	'w' },
    { 010,	'x' },
    { 04,	'r' },
    { 02,	'w' },
    { 01,	'x' },
    { 0,	0   }
};


/* PRINTHEADER -- Print the file header in either short or long (verbose)
 * format, e.g.:
 *		drwxr-xr-x  9 tody         1024 Nov  3 17:53 .
 */
static void
printheader (
    FILE  *out,				/* output file			*/
    register struct fheader *fh,	/* file header struct		*/
    int	  verbose			/* long format output		*/
)
{
	register struct	_modebits *mp;
	char	*tp, *ctime();

	if (!verbose) {
	    fprintf (out, "%s\n", fh->name);
	    return;
	}

	for (mp=modebits;  mp->code;  mp++)
	    fprintf (out, "%c", mp->code & fh->mode ? mp->ch : '-');

	tp = ctime (&fh->mtime);
	fprintf (out, "%3d %4d %2d %8ld %-12.12s %-4.4s %s",
	    fh->linkflag,
	    fh->uid,
	    fh->gid,
	    fh->size,
	    tp + 4, tp + 20,
	    fh->name);

	if (fh->linkflag && *fh->linkname)
	    fprintf (out, " -> %s\n", fh->linkname);
	else
	    fprintf (out, "\n");
}


/* FILETYPE -- Determine the file type (text, binary, or directory) of the
 * next file on the input stream.  Directory files are easy; the tar format
 * identifies directories unambiguously.  Discriminating between text and
 * binary files is not possible in general because UNIX does not make such
 * a distinction, but in practice we can apply a heuristic which will work
 * in nearly all cases.  This can be overriden, producing only binary byte
 * stream files as output, by a command line switch.
 */
static int
filetype (
    int	   in,			/* input file			*/
    struct fheader *fh		/* decoded file header		*/
)
{
	register char	*cp;
	register int	n, ch;
	int	newline_seen, nchars;

	/* Easy cases first.
	 */
	if (fh->isdir)
	    return (DIRECTORY_FILE);
	else if (fh->size == 0 || binaryout)
	    return (BINARY_FILE);

	/* Get a pointer to the first block of the input file and set the
	 * input pointers back so that the block is returned by the next
	 * call to getblock.
	 */
	if ((cp = getblock (in)) == NULL)
	    return (BINARY_FILE);
	nextblock -= TBLOCK;
	nblocks++;

	/* Examine the data to see if it is text.  The simple heuristic
	 * used requires that all characters be either printable ascii
	 * or common control codes.
	 */
	n = nchars = (fh->size < TBLOCK) ? fh->size : TBLOCK;
	for (newline_seen=0;  --n >= 0;  ) {
	    ch = *cp++;
	    if (ch == '\n')
		newline_seen++;
	    else if (!isprint(ch) && !isspace(ch) && !ctrlcode(ch))
		break;
	}

	if (n >= 0 || (nchars > MAXLINELEN && !newline_seen))
	    return (BINARY_FILE);
	else
	    return (TEXT_FILE);
}


/* NEWFILE -- Try to open a new file for writing, creating the new file
 * with the mode bits given.  Create all directories leading to the file if
 * necessary (and possible).
 */
static int
newfile (
    char *fname,		/* pathname of file		*/
    int	  mode,			/* file mode bits		*/
    int	  uid, int gid,		/* file owner, group codes	*/
    int	  type 			/* text, binary, directory	*/
)
{
	int	fd;
	char	*cp;
	char	*rindex();

	if (len_pathprefix && strncmp(fname,pathprefix,len_pathprefix) == 0)
	    fname += len_pathprefix;

	if (debug)
	    fprintf (stderr, "newfile `%s':\n", fname);

	if (checkdir (fname, mode, uid, gid) == ERR)
	    return (ERR);

	if (type == DIRECTORY_FILE) {
	    cp = rindex (fname, '/');
	    if (cp && *(cp+1) == EOS)
		*cp = EOS;
	    fd = os_createdir (fname, mode);

	    /* Ignore any error creating directory, as this may just mean
	     * that the directory already exists.  If the directory does
	     * not exist and cannot be created, there will be plenty of
	     * other errors when we try to write files into it.
	     */
	    fd = OK;

	} else {
	    if (replace)
		os_delete (fname);
	    fd = os_createfile (fname, mode, type);
	}

	return (fd);
}


/* CHECKDIR -- Verify that all the directories in the pathname of a file
 * exist.  If they do not exist, try to create them.
 */
static int
checkdir (
    register char *path,
    int	mode,
    int	uid, int gid
)
{
	register char	*cp;
	char	*rindex();

	/* Quick check to see if the directory exists.
	 */
	if ((cp = rindex (path, '/')) == NULL)
	    return (OK);

	*cp = EOS;
	if (os_access (path, 0, DIRECTORY_FILE) == YES) {
	    *cp = '/';
	    return (OK);
	}
	*cp = '/';

	/* The directory cannot be accessed.  Try to make all directories
	 * in the pathname.  If the file is itself a directory leave its
	 * creation until later.
	 */
	for (cp=path;  *cp;  cp++) {
	    if (*cp != '/')
		continue;
	    if (*(cp+1) == EOS)
		return (OK);

	    *cp = EOS;
	    if (os_access (path, 0, DIRECTORY_FILE) == NO) {
		if (os_createdir (path, RWXR_XR_X) == ERR) {
		    fprintf (stderr, "cannot create directory `%s'\n", path);
		    *cp = '/';
		    return (ERR);
		} else
		    os_setowner (path, uid, gid);
	    }
	    *cp = '/';
	}

	return (OK);
}


/* COPYFILE -- Copy bytes from the input (tar) file to the output file.
 * Each file consists of a integral number of TBLOCK size blocks on the
 * input file.
 */
static void
copyfile (
    int	   in,			/* input file			*/
    int	   out,			/* output file			*/
    struct fheader *fh,		/* file header structure	*/
    int	   ftype		/* text or binary file		*/
)
{
	long	nbytes = fh->size;
	int	nblocks = 0, maxpad;
	char	*bp;


	/* Link files are zero length on the tape. */
	if (fh->linkflag)
	    return;

	if (ftype == BINARY_FILE || !stripblanks)
	    maxpad = 0;
	else
	    maxpad = SZ_PADBUF;

	/* Copy all but the last MAXPAD characters if the file is a text file
	 * and stripping is enabled.
	 */
	while (nbytes > maxpad && (bp = getblock (in)) != NULL)
	    if (os_write (out, bp, nbytes<TBLOCK ? (int)nbytes:TBLOCK) == ERR) {
		fprintf (stderr, "Warning: file write error on `%s'\n",
		    curfil->name);
		if (nerrs++ > MAXERR) {
		    fprintf (stderr, "Too many errors\n");
		    exit (OSOK+1);
		}
	    } else {
		nbytes -= TBLOCK;
		nblocks++;
	    }

	/* Strip whitespace at end of file added by WTAR when the archive was
	 * created.
	 */
	if (nbytes > 0)
	    strip_blanks (in, out, nbytes);

	if (debug)
	    fprintf (stderr, "%d blocks written\n", nblocks);
}


/* STRIP_BLANKS -- Read the remaining file data into the pad buffer.
 * Write out the remaining data, minus any extra blanks or empty blank lines
 * at the end of the file.  Some versions of WTAR (e.g., VMS) do not know
 * the actual size of a text file and have to pad with blanks at the end to
 * make the file the size noted in the file header.
 */
static void
strip_blanks (int in, int out, long nbytes)
{
	register char	*ip, *op;
	char	padbuf[SZ_PADBUF+10];
	char	*lastnl;
	int	n;

	/* Fill buffer.
	 */
	op = padbuf;
	while (nbytes > 0 && (ip = getblock (in)) != NULL) {
	    n = nbytes < TBLOCK ? (int)nbytes : TBLOCK;
	    os_amovb (ip, op, n + sizeof(XCHAR)-1);
	    nbytes -= n;
	    op += n;
	}

	/* Backspace from the end of the buffer until the last nonblank line
	 * is found.
	 */
	lastnl = op - 1;
	for (ip=lastnl;  ip > padbuf;  --ip)
	    if (*ip == '\n')
		lastnl = ip;
	    else if (*ip != ' ')
		break;

	/* Write out everything up to and including the newline at the end of
	 * the last line containing anything but blanks.
	 */
	os_write (out, padbuf, lastnl - padbuf + 1);
}


/* SKIPFILE -- Skip the indicated number of bytes on the input (tar) file.
 */
static void
skipfile (
    int	   in,			/* input file			*/
    struct fheader *fh 		/* file header			*/
)
{
	register long nbytes = fh->size;

	/* Link files are zero length on the tape. */
	if (fh->linkflag)
	    return;

	while (nbytes > 0 && getblock (in) != NULL)
	    nbytes -= TBLOCK;
}


/* GETBLOCK -- Return a pointer to the next file block of size TBLOCK bytes
 * in the input file.
 */
static char *
getblock (int in)
{
	char	*bp;
	int	nbytes;

	for (;;) {
	    if (eof)
		return (NULL);
	    else if (--nblocks >= 0) {
		bp = nextblock;
		nextblock += TBLOCK;
		return (bp);
	    }

	    if ((nbytes = tape_read (in, tapeblock, SZ_TAPEBUFFER)) < TBLOCK)
		eof++;
	    else {
		nblocks = (nbytes + TBLOCK-1) / TBLOCK;
		nextblock = tapeblock;
	    }
	}
}