Home | History | Annotate | Line # | Download | only in pax
tar.c revision 1.1
      1  1.1  jtc /*-
      2  1.1  jtc  * Copyright (c) 1992 Keith Muller.
      3  1.1  jtc  * Copyright (c) 1992, 1993
      4  1.1  jtc  *	The Regents of the University of California.  All rights reserved.
      5  1.1  jtc  *
      6  1.1  jtc  * This code is derived from software contributed to Berkeley by
      7  1.1  jtc  * Keith Muller of the University of California, San Diego.
      8  1.1  jtc  *
      9  1.1  jtc  * Redistribution and use in source and binary forms, with or without
     10  1.1  jtc  * modification, are permitted provided that the following conditions
     11  1.1  jtc  * are met:
     12  1.1  jtc  * 1. Redistributions of source code must retain the above copyright
     13  1.1  jtc  *    notice, this list of conditions and the following disclaimer.
     14  1.1  jtc  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  jtc  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  jtc  *    documentation and/or other materials provided with the distribution.
     17  1.1  jtc  * 3. All advertising materials mentioning features or use of this software
     18  1.1  jtc  *    must display the following acknowledgement:
     19  1.1  jtc  *	This product includes software developed by the University of
     20  1.1  jtc  *	California, Berkeley and its contributors.
     21  1.1  jtc  * 4. Neither the name of the University nor the names of its contributors
     22  1.1  jtc  *    may be used to endorse or promote products derived from this software
     23  1.1  jtc  *    without specific prior written permission.
     24  1.1  jtc  *
     25  1.1  jtc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  1.1  jtc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  1.1  jtc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  1.1  jtc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  1.1  jtc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  1.1  jtc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  1.1  jtc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  1.1  jtc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  1.1  jtc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  1.1  jtc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  1.1  jtc  * SUCH DAMAGE.
     36  1.1  jtc  */
     37  1.1  jtc 
     38  1.1  jtc #ifndef lint
     39  1.1  jtc static char sccsid[] = "@(#)tar.c	8.2 (Berkeley) 4/18/94";
     40  1.1  jtc #endif /* not lint */
     41  1.1  jtc 
     42  1.1  jtc #include <sys/types.h>
     43  1.1  jtc #include <sys/time.h>
     44  1.1  jtc #include <sys/stat.h>
     45  1.1  jtc #include <sys/param.h>
     46  1.1  jtc #include <string.h>
     47  1.1  jtc #include <stdio.h>
     48  1.1  jtc #include <ctype.h>
     49  1.1  jtc #include <unistd.h>
     50  1.1  jtc #include <stdlib.h>
     51  1.1  jtc #include "pax.h"
     52  1.1  jtc #include "extern.h"
     53  1.1  jtc #include "tar.h"
     54  1.1  jtc 
     55  1.1  jtc /*
     56  1.1  jtc  * Routines for reading, writing and header identify of various versions of tar
     57  1.1  jtc  */
     58  1.1  jtc 
     59  1.1  jtc static u_long tar_chksm __P((register char *, register int));
     60  1.1  jtc static char *name_split __P((register char *, register int));
     61  1.1  jtc static int ul_oct __P((u_long, register char *, register int, int));
     62  1.1  jtc #ifndef NET2_STAT
     63  1.1  jtc static int uqd_oct __P((u_quad_t, register char *, register int, int));
     64  1.1  jtc #endif
     65  1.1  jtc 
     66  1.1  jtc /*
     67  1.1  jtc  * Routines common to all versions of tar
     68  1.1  jtc  */
     69  1.1  jtc 
     70  1.1  jtc static int tar_nodir;			/* do not write dirs under old tar */
     71  1.1  jtc 
     72  1.1  jtc /*
     73  1.1  jtc  * tar_endwr()
     74  1.1  jtc  *	add the tar trailer of two null blocks
     75  1.1  jtc  * Return:
     76  1.1  jtc  *	0 if ok, -1 otherwise (what wr_skip returns)
     77  1.1  jtc  */
     78  1.1  jtc 
     79  1.1  jtc #if __STDC__
     80  1.1  jtc int
     81  1.1  jtc tar_endwr(void)
     82  1.1  jtc #else
     83  1.1  jtc int
     84  1.1  jtc tar_endwr()
     85  1.1  jtc #endif
     86  1.1  jtc {
     87  1.1  jtc 	return(wr_skip((off_t)(NULLCNT*BLKMULT)));
     88  1.1  jtc }
     89  1.1  jtc 
     90  1.1  jtc /*
     91  1.1  jtc  * tar_endrd()
     92  1.1  jtc  *	no cleanup needed here, just return size of trailer (for append)
     93  1.1  jtc  * Return:
     94  1.1  jtc  *	size of trailer (2 * BLKMULT)
     95  1.1  jtc  */
     96  1.1  jtc 
     97  1.1  jtc #if __STDC__
     98  1.1  jtc off_t
     99  1.1  jtc tar_endrd(void)
    100  1.1  jtc #else
    101  1.1  jtc off_t
    102  1.1  jtc tar_endrd()
    103  1.1  jtc #endif
    104  1.1  jtc {
    105  1.1  jtc 	return((off_t)(NULLCNT*BLKMULT));
    106  1.1  jtc }
    107  1.1  jtc 
    108  1.1  jtc /*
    109  1.1  jtc  * tar_trail()
    110  1.1  jtc  *	Called to determine if a header block is a valid trailer. We are passed
    111  1.1  jtc  *	the block, the in_sync flag (which tells us we are in resync mode;
    112  1.1  jtc  *	looking for a valid header), and cnt (which starts at zero) which is
    113  1.1  jtc  *	used to count the number of empty blocks we have seen so far.
    114  1.1  jtc  * Return:
    115  1.1  jtc  *	0 if a valid trailer, -1 if not a valid trailer, or 1 if the block
    116  1.1  jtc  *	could never contain a header.
    117  1.1  jtc  */
    118  1.1  jtc 
    119  1.1  jtc #if __STDC__
    120  1.1  jtc int
    121  1.1  jtc tar_trail(register char *buf, register int in_resync, register int *cnt)
    122  1.1  jtc #else
    123  1.1  jtc int
    124  1.1  jtc tar_trail(buf, in_resync, cnt)
    125  1.1  jtc 	register char *buf;
    126  1.1  jtc 	register int in_resync;
    127  1.1  jtc 	register int *cnt;
    128  1.1  jtc #endif
    129  1.1  jtc {
    130  1.1  jtc 	register int i;
    131  1.1  jtc 
    132  1.1  jtc 	/*
    133  1.1  jtc 	 * look for all zero, trailer is two consecutive blocks of zero
    134  1.1  jtc 	 */
    135  1.1  jtc 	for (i = 0; i < BLKMULT; ++i) {
    136  1.1  jtc 		if (buf[i] != '\0')
    137  1.1  jtc 			break;
    138  1.1  jtc 	}
    139  1.1  jtc 
    140  1.1  jtc 	/*
    141  1.1  jtc 	 * if not all zero it is not a trailer, but MIGHT be a header.
    142  1.1  jtc 	 */
    143  1.1  jtc 	if (i != BLKMULT)
    144  1.1  jtc 		return(-1);
    145  1.1  jtc 
    146  1.1  jtc 	/*
    147  1.1  jtc 	 * When given a zero block, we must be careful!
    148  1.1  jtc 	 * If we are not in resync mode, check for the trailer. Have to watch
    149  1.1  jtc 	 * out that we do not mis-identify file data as the trailer, so we do
    150  1.1  jtc 	 * NOT try to id a trailer during resync mode. During resync mode we
    151  1.1  jtc 	 * might as well throw this block out since a valid header can NEVER be
    152  1.1  jtc 	 * a block of all 0 (we must have a valid file name).
    153  1.1  jtc 	 */
    154  1.1  jtc 	if (!in_resync && (++*cnt >= NULLCNT))
    155  1.1  jtc 		return(0);
    156  1.1  jtc 	return(1);
    157  1.1  jtc }
    158  1.1  jtc 
    159  1.1  jtc /*
    160  1.1  jtc  * ul_oct()
    161  1.1  jtc  *	convert an unsigned long to an octal string. many oddball field
    162  1.1  jtc  *	termination characters are used by the various versions of tar in the
    163  1.1  jtc  *	different fields. term selects which kind to use. str is BLANK padded
    164  1.1  jtc  *	at the front to len. we are unable to use only one format as many old
    165  1.1  jtc  *	tar readers are very cranky about this.
    166  1.1  jtc  * Return:
    167  1.1  jtc  *	0 if the number fit into the string, -1 otherwise
    168  1.1  jtc  */
    169  1.1  jtc 
    170  1.1  jtc #if __STDC__
    171  1.1  jtc static int
    172  1.1  jtc ul_oct(u_long val, register char *str, register int len, int term)
    173  1.1  jtc #else
    174  1.1  jtc static int
    175  1.1  jtc ul_oct(val, str, len, term)
    176  1.1  jtc 	u_long val;
    177  1.1  jtc 	register char *str;
    178  1.1  jtc 	register int len;
    179  1.1  jtc 	int term;
    180  1.1  jtc #endif
    181  1.1  jtc {
    182  1.1  jtc 	register char *pt;
    183  1.1  jtc 
    184  1.1  jtc 	/*
    185  1.1  jtc 	 * term selects the appropriate character(s) for the end of the string
    186  1.1  jtc 	 */
    187  1.1  jtc 	pt = str + len - 1;
    188  1.1  jtc 	switch(term) {
    189  1.1  jtc 	case 3:
    190  1.1  jtc 		*pt-- = '\0';
    191  1.1  jtc 		break;
    192  1.1  jtc 	case 2:
    193  1.1  jtc 		*pt-- = ' ';
    194  1.1  jtc 		*pt-- = '\0';
    195  1.1  jtc 		break;
    196  1.1  jtc 	case 1:
    197  1.1  jtc 		*pt-- = ' ';
    198  1.1  jtc 		break;
    199  1.1  jtc 	case 0:
    200  1.1  jtc 	default:
    201  1.1  jtc 		*pt-- = '\0';
    202  1.1  jtc 		*pt-- = ' ';
    203  1.1  jtc 		break;
    204  1.1  jtc 	}
    205  1.1  jtc 
    206  1.1  jtc 	/*
    207  1.1  jtc 	 * convert and blank pad if there is space
    208  1.1  jtc 	 */
    209  1.1  jtc 	while (pt >= str) {
    210  1.1  jtc 		*pt-- = '0' + (char)(val & 0x7);
    211  1.1  jtc 		if ((val = val >> 3) == (u_long)0)
    212  1.1  jtc 			break;
    213  1.1  jtc 	}
    214  1.1  jtc 
    215  1.1  jtc 	while (pt >= str)
    216  1.1  jtc 		*pt-- = ' ';
    217  1.1  jtc 	if (val != (u_long)0)
    218  1.1  jtc 		return(-1);
    219  1.1  jtc 	return(0);
    220  1.1  jtc }
    221  1.1  jtc 
    222  1.1  jtc #ifndef NET2_STAT
    223  1.1  jtc /*
    224  1.1  jtc  * uqd_oct()
    225  1.1  jtc  *	convert an u_quad_t to an octal string. one of many oddball field
    226  1.1  jtc  *	termination characters are used by the various versions of tar in the
    227  1.1  jtc  *	different fields. term selects which kind to use. str is BLANK padded
    228  1.1  jtc  *	at the front to len. we are unable to use only one format as many old
    229  1.1  jtc  *	tar readers are very cranky about this.
    230  1.1  jtc  * Return:
    231  1.1  jtc  *	0 if the number fit into the string, -1 otherwise
    232  1.1  jtc  */
    233  1.1  jtc 
    234  1.1  jtc #if __STDC__
    235  1.1  jtc static int
    236  1.1  jtc uqd_oct(u_quad_t val, register char *str, register int len, int term)
    237  1.1  jtc #else
    238  1.1  jtc static int
    239  1.1  jtc uqd_oct(val, str, len, term)
    240  1.1  jtc 	u_quad_t val;
    241  1.1  jtc 	register char *str;
    242  1.1  jtc 	register int len;
    243  1.1  jtc 	int term;
    244  1.1  jtc #endif
    245  1.1  jtc {
    246  1.1  jtc 	register char *pt;
    247  1.1  jtc 
    248  1.1  jtc 	/*
    249  1.1  jtc 	 * term selects the appropriate character(s) for the end of the string
    250  1.1  jtc 	 */
    251  1.1  jtc 	pt = str + len - 1;
    252  1.1  jtc 	switch(term) {
    253  1.1  jtc 	case 3:
    254  1.1  jtc 		*pt-- = '\0';
    255  1.1  jtc 		break;
    256  1.1  jtc 	case 2:
    257  1.1  jtc 		*pt-- = ' ';
    258  1.1  jtc 		*pt-- = '\0';
    259  1.1  jtc 		break;
    260  1.1  jtc 	case 1:
    261  1.1  jtc 		*pt-- = ' ';
    262  1.1  jtc 		break;
    263  1.1  jtc 	case 0:
    264  1.1  jtc 	default:
    265  1.1  jtc 		*pt-- = '\0';
    266  1.1  jtc 		*pt-- = ' ';
    267  1.1  jtc 		break;
    268  1.1  jtc 	}
    269  1.1  jtc 
    270  1.1  jtc 	/*
    271  1.1  jtc 	 * convert and blank pad if there is space
    272  1.1  jtc 	 */
    273  1.1  jtc 	while (pt >= str) {
    274  1.1  jtc 		*pt-- = '0' + (char)(val & 0x7);
    275  1.1  jtc 		if ((val = val >> 3) == 0)
    276  1.1  jtc 			break;
    277  1.1  jtc 	}
    278  1.1  jtc 
    279  1.1  jtc 	while (pt >= str)
    280  1.1  jtc 		*pt-- = ' ';
    281  1.1  jtc 	if (val != (u_quad_t)0)
    282  1.1  jtc 		return(-1);
    283  1.1  jtc 	return(0);
    284  1.1  jtc }
    285  1.1  jtc #endif
    286  1.1  jtc 
    287  1.1  jtc /*
    288  1.1  jtc  * tar_chksm()
    289  1.1  jtc  *	calculate the checksum for a tar block counting the checksum field as
    290  1.1  jtc  *	all blanks (BLNKSUM is that value pre-calculated, the sume of 8 blanks).
    291  1.1  jtc  *	NOTE: we use len to short circuit summing 0's on write since we ALWAYS
    292  1.1  jtc  *	pad headers with 0.
    293  1.1  jtc  * Return:
    294  1.1  jtc  *	unsigned long checksum
    295  1.1  jtc  */
    296  1.1  jtc 
    297  1.1  jtc #if __STDC__
    298  1.1  jtc static u_long
    299  1.1  jtc tar_chksm(register char *blk, register int len)
    300  1.1  jtc #else
    301  1.1  jtc static u_long
    302  1.1  jtc tar_chksm(blk, len)
    303  1.1  jtc 	register char *blk;
    304  1.1  jtc 	register int len;
    305  1.1  jtc #endif
    306  1.1  jtc {
    307  1.1  jtc 	register char *stop;
    308  1.1  jtc 	register char *pt;
    309  1.1  jtc 	u_long chksm = BLNKSUM;	/* inital value is checksum field sum */
    310  1.1  jtc 
    311  1.1  jtc 	/*
    312  1.1  jtc 	 * add the part of the block before the checksum field
    313  1.1  jtc 	 */
    314  1.1  jtc 	pt = blk;
    315  1.1  jtc 	stop = blk + CHK_OFFSET;
    316  1.1  jtc 	while (pt < stop)
    317  1.1  jtc 		chksm += (u_long)(*pt++ & 0xff);
    318  1.1  jtc 	/*
    319  1.1  jtc 	 * move past the checksum field and keep going, spec counts the
    320  1.1  jtc 	 * checksum field as the sum of 8 blanks (which is pre-computed as
    321  1.1  jtc 	 * BLNKSUM).
    322  1.1  jtc 	 * ASSUMED: len is greater than CHK_OFFSET. (len is where our 0 padding
    323  1.1  jtc 	 * starts, no point in summing zero's)
    324  1.1  jtc 	 */
    325  1.1  jtc 	pt += CHK_LEN;
    326  1.1  jtc 	stop = blk + len;
    327  1.1  jtc 	while (pt < stop)
    328  1.1  jtc 		chksm += (u_long)(*pt++ & 0xff);
    329  1.1  jtc 	return(chksm);
    330  1.1  jtc }
    331  1.1  jtc 
    332  1.1  jtc /*
    333  1.1  jtc  * Routines for old BSD style tar (also made portable to sysV tar)
    334  1.1  jtc  */
    335  1.1  jtc 
    336  1.1  jtc /*
    337  1.1  jtc  * tar_id()
    338  1.1  jtc  *	determine if a block given to us is a valid tar header (and not a USTAR
    339  1.1  jtc  *	header). We have to be on the lookout for those pesky blocks of	all
    340  1.1  jtc  *	zero's.
    341  1.1  jtc  * Return:
    342  1.1  jtc  *	0 if a tar header, -1 otherwise
    343  1.1  jtc  */
    344  1.1  jtc 
    345  1.1  jtc #if __STDC__
    346  1.1  jtc int
    347  1.1  jtc tar_id(register char *blk, int size)
    348  1.1  jtc #else
    349  1.1  jtc int
    350  1.1  jtc tar_id(blk, size)
    351  1.1  jtc 	register char *blk;
    352  1.1  jtc 	int size;
    353  1.1  jtc #endif
    354  1.1  jtc {
    355  1.1  jtc 	register HD_TAR *hd;
    356  1.1  jtc 	register HD_USTAR *uhd;
    357  1.1  jtc 
    358  1.1  jtc 	if (size < BLKMULT)
    359  1.1  jtc 		return(-1);
    360  1.1  jtc 	hd = (HD_TAR *)blk;
    361  1.1  jtc 	uhd = (HD_USTAR *)blk;
    362  1.1  jtc 
    363  1.1  jtc 	/*
    364  1.1  jtc 	 * check for block of zero's first, a simple and fast test, then make
    365  1.1  jtc 	 * sure this is not a ustar header by looking for the ustar magic
    366  1.1  jtc 	 * cookie. We should use TMAGLEN, but some USTAR archive programs are
    367  1.1  jtc 	 * wrong and create archives missing the \0. Last we check the
    368  1.1  jtc 	 * checksum. If this is ok we have to assume it is a valid header.
    369  1.1  jtc 	 */
    370  1.1  jtc 	if (hd->name[0] == '\0')
    371  1.1  jtc 		return(-1);
    372  1.1  jtc 	if (strncmp(uhd->magic, TMAGIC, TMAGLEN - 1) == 0)
    373  1.1  jtc 		return(-1);
    374  1.1  jtc 	if (asc_ul(hd->chksum,sizeof(hd->chksum),OCT) != tar_chksm(blk,BLKMULT))
    375  1.1  jtc 		return(-1);
    376  1.1  jtc 	return(0);
    377  1.1  jtc }
    378  1.1  jtc 
    379  1.1  jtc /*
    380  1.1  jtc  * tar_opt()
    381  1.1  jtc  *	handle tar format specific -o options
    382  1.1  jtc  * Return:
    383  1.1  jtc  *	0 if ok -1 otherwise
    384  1.1  jtc  */
    385  1.1  jtc 
    386  1.1  jtc #if __STDC__
    387  1.1  jtc int
    388  1.1  jtc tar_opt(void)
    389  1.1  jtc #else
    390  1.1  jtc int
    391  1.1  jtc tar_opt()
    392  1.1  jtc #endif
    393  1.1  jtc {
    394  1.1  jtc 	OPLIST *opt;
    395  1.1  jtc 
    396  1.1  jtc 	while ((opt = opt_next()) != NULL) {
    397  1.1  jtc 		if (strcmp(opt->name, TAR_OPTION) ||
    398  1.1  jtc 		    strcmp(opt->value, TAR_NODIR)) {
    399  1.1  jtc 			warn(1, "Unknown tar format -o option/value pair %s=%s",
    400  1.1  jtc 			    opt->name, opt->value);
    401  1.1  jtc 			warn(1,"%s=%s is the only supported tar format option",
    402  1.1  jtc 			    TAR_OPTION, TAR_NODIR);
    403  1.1  jtc 			return(-1);
    404  1.1  jtc 		}
    405  1.1  jtc 
    406  1.1  jtc 		/*
    407  1.1  jtc 		 * we only support one option, and only when writing
    408  1.1  jtc 		 */
    409  1.1  jtc 		if ((act != APPND) && (act != ARCHIVE)) {
    410  1.1  jtc 			warn(1, "%s=%s is only supported when writing.",
    411  1.1  jtc 			    opt->name, opt->value);
    412  1.1  jtc 			return(-1);
    413  1.1  jtc 		}
    414  1.1  jtc 		tar_nodir = 1;
    415  1.1  jtc 	}
    416  1.1  jtc 	return(0);
    417  1.1  jtc }
    418  1.1  jtc 
    419  1.1  jtc 
    420  1.1  jtc /*
    421  1.1  jtc  * tar_rd()
    422  1.1  jtc  *	extract the values out of block already determined to be a tar header.
    423  1.1  jtc  *	store the values in the ARCHD parameter.
    424  1.1  jtc  * Return:
    425  1.1  jtc  *	0
    426  1.1  jtc  */
    427  1.1  jtc 
    428  1.1  jtc #if __STDC__
    429  1.1  jtc int
    430  1.1  jtc tar_rd(register ARCHD *arcn, register char *buf)
    431  1.1  jtc #else
    432  1.1  jtc int
    433  1.1  jtc tar_rd(arcn, buf)
    434  1.1  jtc 	register ARCHD *arcn;
    435  1.1  jtc 	register char *buf;
    436  1.1  jtc #endif
    437  1.1  jtc {
    438  1.1  jtc 	register HD_TAR *hd;
    439  1.1  jtc 	register char *pt;
    440  1.1  jtc 
    441  1.1  jtc 	/*
    442  1.1  jtc 	 * we only get proper sized buffers passed to us
    443  1.1  jtc 	 */
    444  1.1  jtc 	if (tar_id(buf, BLKMULT) < 0)
    445  1.1  jtc 		return(-1);
    446  1.1  jtc 	arcn->org_name = arcn->name;
    447  1.1  jtc 	arcn->sb.st_nlink = 1;
    448  1.1  jtc 	arcn->pat = NULL;
    449  1.1  jtc 
    450  1.1  jtc 	/*
    451  1.1  jtc 	 * copy out the name and values in the stat buffer
    452  1.1  jtc 	 */
    453  1.1  jtc 	hd = (HD_TAR *)buf;
    454  1.1  jtc 	arcn->nlen = l_strncpy(arcn->name, hd->name, sizeof(hd->name));
    455  1.1  jtc 	arcn->name[arcn->nlen] = '\0';
    456  1.1  jtc 	arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode,sizeof(hd->mode),OCT) &
    457  1.1  jtc 	    0xfff);
    458  1.1  jtc 	arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT);
    459  1.1  jtc 	arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT);
    460  1.1  jtc 	arcn->sb.st_size = (size_t)asc_ul(hd->size, sizeof(hd->size), OCT);
    461  1.1  jtc 	arcn->sb.st_mtime = (time_t)asc_ul(hd->mtime, sizeof(hd->mtime), OCT);
    462  1.1  jtc 	arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
    463  1.1  jtc 
    464  1.1  jtc 	/*
    465  1.1  jtc 	 * have to look at the last character, it may be a '/' and that is used
    466  1.1  jtc 	 * to encode this as a directory
    467  1.1  jtc 	 */
    468  1.1  jtc 	pt = &(arcn->name[arcn->nlen - 1]);
    469  1.1  jtc 	arcn->pad = 0;
    470  1.1  jtc 	arcn->skip = 0;
    471  1.1  jtc 	switch(hd->linkflag) {
    472  1.1  jtc 	case SYMTYPE:
    473  1.1  jtc 		/*
    474  1.1  jtc 		 * symbolic link, need to get the link name and set the type in
    475  1.1  jtc 		 * the st_mode so -v printing will look correct.
    476  1.1  jtc 		 */
    477  1.1  jtc 		arcn->type = PAX_SLK;
    478  1.1  jtc 		arcn->ln_nlen = l_strncpy(arcn->ln_name, hd->linkname,
    479  1.1  jtc 			sizeof(hd->linkname));
    480  1.1  jtc 		arcn->ln_name[arcn->ln_nlen] = '\0';
    481  1.1  jtc 		arcn->sb.st_mode |= S_IFLNK;
    482  1.1  jtc 		break;
    483  1.1  jtc 	case LNKTYPE:
    484  1.1  jtc 		/*
    485  1.1  jtc 		 * hard link, need to get the link name, set the type in the
    486  1.1  jtc 		 * st_mode and st_nlink so -v printing will look better.
    487  1.1  jtc 		 */
    488  1.1  jtc 		arcn->type = PAX_HLK;
    489  1.1  jtc 		arcn->sb.st_nlink = 2;
    490  1.1  jtc 		arcn->ln_nlen = l_strncpy(arcn->ln_name, hd->linkname,
    491  1.1  jtc 			sizeof(hd->linkname));
    492  1.1  jtc 		arcn->ln_name[arcn->ln_nlen] = '\0';
    493  1.1  jtc 
    494  1.1  jtc 		/*
    495  1.1  jtc 		 * no idea of what type this thing really points at, but
    496  1.1  jtc 		 * we set something for printing only.
    497  1.1  jtc 		 */
    498  1.1  jtc 		arcn->sb.st_mode |= S_IFREG;
    499  1.1  jtc 		break;
    500  1.1  jtc 	case AREGTYPE:
    501  1.1  jtc 	case REGTYPE:
    502  1.1  jtc 	default:
    503  1.1  jtc 		/*
    504  1.1  jtc 		 * If we have a trailing / this is a directory and NOT a file.
    505  1.1  jtc 		 */
    506  1.1  jtc 		arcn->ln_name[0] = '\0';
    507  1.1  jtc 		arcn->ln_nlen = 0;
    508  1.1  jtc 		if (*pt == '/') {
    509  1.1  jtc 			/*
    510  1.1  jtc 			 * it is a directory, set the mode for -v printing
    511  1.1  jtc 			 */
    512  1.1  jtc 			arcn->type = PAX_DIR;
    513  1.1  jtc 			arcn->sb.st_mode |= S_IFDIR;
    514  1.1  jtc 			arcn->sb.st_nlink = 2;
    515  1.1  jtc 		} else {
    516  1.1  jtc 			/*
    517  1.1  jtc 			 * have a file that will be followed by data. Set the
    518  1.1  jtc 			 * skip value to the size field and caluculate the size
    519  1.1  jtc 			 * of the padding.
    520  1.1  jtc 			 */
    521  1.1  jtc 			arcn->type = PAX_REG;
    522  1.1  jtc 			arcn->sb.st_mode |= S_IFREG;
    523  1.1  jtc 			arcn->pad = TAR_PAD(arcn->sb.st_size);
    524  1.1  jtc 			arcn->skip = arcn->sb.st_size;
    525  1.1  jtc 		}
    526  1.1  jtc 		break;
    527  1.1  jtc 	}
    528  1.1  jtc 
    529  1.1  jtc 	/*
    530  1.1  jtc 	 * strip off any trailing slash.
    531  1.1  jtc 	 */
    532  1.1  jtc 	if (*pt == '/') {
    533  1.1  jtc 		*pt = '\0';
    534  1.1  jtc 		--arcn->nlen;
    535  1.1  jtc 	}
    536  1.1  jtc 	return(0);
    537  1.1  jtc }
    538  1.1  jtc 
    539  1.1  jtc /*
    540  1.1  jtc  * tar_wr()
    541  1.1  jtc  *	write a tar header for the file specified in the ARCHD to the archive.
    542  1.1  jtc  *	Have to check for file types that cannot be stored and file names that
    543  1.1  jtc  *	are too long. Be careful of the term (last arg) to ul_oct, each field
    544  1.1  jtc  *	of tar has it own spec for the termination character(s).
    545  1.1  jtc  *	ASSUMED: space after header in header block is zero filled
    546  1.1  jtc  * Return:
    547  1.1  jtc  *	0 if file has data to be written after the header, 1 if file has NO
    548  1.1  jtc  *	data to write after the header, -1 if archive write failed
    549  1.1  jtc  */
    550  1.1  jtc 
    551  1.1  jtc #if __STDC__
    552  1.1  jtc int
    553  1.1  jtc tar_wr(register ARCHD *arcn)
    554  1.1  jtc #else
    555  1.1  jtc int
    556  1.1  jtc tar_wr(arcn)
    557  1.1  jtc 	register ARCHD *arcn;
    558  1.1  jtc #endif
    559  1.1  jtc {
    560  1.1  jtc 	register HD_TAR *hd;
    561  1.1  jtc 	int len;
    562  1.1  jtc 	char hdblk[sizeof(HD_TAR)];
    563  1.1  jtc 
    564  1.1  jtc 	/*
    565  1.1  jtc 	 * check for those file system types which tar cannot store
    566  1.1  jtc 	 */
    567  1.1  jtc 	switch(arcn->type) {
    568  1.1  jtc 	case PAX_DIR:
    569  1.1  jtc 		/*
    570  1.1  jtc 		 * user asked that dirs not be written to the archive
    571  1.1  jtc 		 */
    572  1.1  jtc 		if (tar_nodir)
    573  1.1  jtc 			return(1);
    574  1.1  jtc 		break;
    575  1.1  jtc 	case PAX_CHR:
    576  1.1  jtc 		warn(1, "Tar cannot archive a character device %s",
    577  1.1  jtc 		    arcn->org_name);
    578  1.1  jtc 		return(1);
    579  1.1  jtc 	case PAX_BLK:
    580  1.1  jtc 		warn(1, "Tar cannot archive a block device %s", arcn->org_name);
    581  1.1  jtc 		return(1);
    582  1.1  jtc 	case PAX_SCK:
    583  1.1  jtc 		warn(1, "Tar cannot archive a socket %s", arcn->org_name);
    584  1.1  jtc 		return(1);
    585  1.1  jtc 	case PAX_FIF:
    586  1.1  jtc 		warn(1, "Tar cannot archive a fifo %s", arcn->org_name);
    587  1.1  jtc 		return(1);
    588  1.1  jtc 	case PAX_SLK:
    589  1.1  jtc 	case PAX_HLK:
    590  1.1  jtc 	case PAX_HRG:
    591  1.1  jtc 		if (arcn->ln_nlen > sizeof(hd->linkname)) {
    592  1.1  jtc 			warn(1,"Link name too long for tar %s", arcn->ln_name);
    593  1.1  jtc 			return(1);
    594  1.1  jtc 		}
    595  1.1  jtc 		break;
    596  1.1  jtc 	case PAX_REG:
    597  1.1  jtc 	case PAX_CTG:
    598  1.1  jtc 	default:
    599  1.1  jtc 		break;
    600  1.1  jtc 	}
    601  1.1  jtc 
    602  1.1  jtc 	/*
    603  1.1  jtc 	 * check file name len, remember extra char for dirs (the / at the end)
    604  1.1  jtc 	 */
    605  1.1  jtc 	len = arcn->nlen;
    606  1.1  jtc 	if (arcn->type == PAX_DIR)
    607  1.1  jtc 		++len;
    608  1.1  jtc 	if (len > sizeof(hd->name)) {
    609  1.1  jtc 		warn(1, "File name too long for tar %s", arcn->name);
    610  1.1  jtc 		return(1);
    611  1.1  jtc 	}
    612  1.1  jtc 
    613  1.1  jtc 	/*
    614  1.1  jtc 	 * copy the data out of the ARCHD into the tar header based on the type
    615  1.1  jtc 	 * of the file. Remember many tar readers want the unused fields to be
    616  1.1  jtc 	 * padded with zero. We set the linkflag field (type), the linkname
    617  1.1  jtc 	 * (or zero if not used),the size, and set the padding (if any) to be
    618  1.1  jtc 	 * added after the file data (0 for all other types, as they only have
    619  1.1  jtc 	 * a header)
    620  1.1  jtc 	 */
    621  1.1  jtc 	hd = (HD_TAR *)hdblk;
    622  1.1  jtc 	zf_strncpy(hd->name, arcn->name, sizeof(hd->name));
    623  1.1  jtc 	arcn->pad = 0;
    624  1.1  jtc 
    625  1.1  jtc 	if (arcn->type == PAX_DIR) {
    626  1.1  jtc 		/*
    627  1.1  jtc 		 * directories are the same as files, except have a filename
    628  1.1  jtc 		 * that ends with a /, we add the slash here. No data follows,
    629  1.1  jtc 		 * dirs, so no pad.
    630  1.1  jtc 		 */
    631  1.1  jtc 		hd->linkflag = AREGTYPE;
    632  1.1  jtc 		bzero(hd->linkname, sizeof(hd->linkname));
    633  1.1  jtc 		hd->name[len-1] = '/';
    634  1.1  jtc 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
    635  1.1  jtc 			goto out;
    636  1.1  jtc 	} else if (arcn->type == PAX_SLK) {
    637  1.1  jtc 		/*
    638  1.1  jtc 		 * no data follows this file, so no pad
    639  1.1  jtc 		 */
    640  1.1  jtc 		hd->linkflag = SYMTYPE;
    641  1.1  jtc 		zf_strncpy(hd->linkname,arcn->ln_name, sizeof(hd->linkname));
    642  1.1  jtc 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
    643  1.1  jtc 			goto out;
    644  1.1  jtc 	} else if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG)) {
    645  1.1  jtc 		/*
    646  1.1  jtc 		 * no data follows this file, so no pad
    647  1.1  jtc 		 */
    648  1.1  jtc 		hd->linkflag = LNKTYPE;
    649  1.1  jtc 		zf_strncpy(hd->linkname,arcn->ln_name, sizeof(hd->linkname));
    650  1.1  jtc 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1))
    651  1.1  jtc 			goto out;
    652  1.1  jtc 	} else {
    653  1.1  jtc 		/*
    654  1.1  jtc 		 * data follows this file, so set the pad
    655  1.1  jtc 		 */
    656  1.1  jtc 		hd->linkflag = AREGTYPE;
    657  1.1  jtc 		bzero(hd->linkname, sizeof(hd->linkname));
    658  1.1  jtc #		ifdef NET2_STAT
    659  1.1  jtc 		if (ul_oct((u_long)arcn->sb.st_size, hd->size,
    660  1.1  jtc 		    sizeof(hd->size), 1)) {
    661  1.1  jtc #		else
    662  1.1  jtc 		if (uqd_oct((u_quad_t)arcn->sb.st_size, hd->size,
    663  1.1  jtc 		    sizeof(hd->size), 1)) {
    664  1.1  jtc #		endif
    665  1.1  jtc 			warn(1,"File is too large for tar %s", arcn->org_name);
    666  1.1  jtc 			return(1);
    667  1.1  jtc 		}
    668  1.1  jtc 		arcn->pad = TAR_PAD(arcn->sb.st_size);
    669  1.1  jtc 	}
    670  1.1  jtc 
    671  1.1  jtc 	/*
    672  1.1  jtc 	 * copy those fields that are independent of the type
    673  1.1  jtc 	 */
    674  1.1  jtc 	if (ul_oct((u_long)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 0) ||
    675  1.1  jtc 	    ul_oct((u_long)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 0) ||
    676  1.1  jtc 	    ul_oct((u_long)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 0) ||
    677  1.1  jtc 	    ul_oct((u_long)arcn->sb.st_mtime, hd->mtime, sizeof(hd->mtime), 1))
    678  1.1  jtc 		goto out;
    679  1.1  jtc 
    680  1.1  jtc 	/*
    681  1.1  jtc 	 * calculate and add the checksum, then write the header. A return of
    682  1.1  jtc 	 * 0 tells the caller to now write the file data, 1 says no data needs
    683  1.1  jtc 	 * to be written
    684  1.1  jtc 	 */
    685  1.1  jtc 	if (ul_oct(tar_chksm(hdblk, sizeof(HD_TAR)), hd->chksum,
    686  1.1  jtc 	    sizeof(hd->chksum), 2))
    687  1.1  jtc 		goto out;
    688  1.1  jtc 	if (wr_rdbuf(hdblk, sizeof(HD_TAR)) < 0)
    689  1.1  jtc 		return(-1);
    690  1.1  jtc 	if (wr_skip((off_t)(BLKMULT - sizeof(HD_TAR))) < 0)
    691  1.1  jtc 		return(-1);
    692  1.1  jtc 	if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG))
    693  1.1  jtc 		return(0);
    694  1.1  jtc 	return(1);
    695  1.1  jtc 
    696  1.1  jtc     out:
    697  1.1  jtc 	/*
    698  1.1  jtc 	 * header field is out of range
    699  1.1  jtc 	 */
    700  1.1  jtc 	warn(1, "Tar header field is too small for %s", arcn->org_name);
    701  1.1  jtc 	return(1);
    702  1.1  jtc }
    703  1.1  jtc 
    704  1.1  jtc /*
    705  1.1  jtc  * Routines for POSIX ustar
    706  1.1  jtc  */
    707  1.1  jtc 
    708  1.1  jtc /*
    709  1.1  jtc  * ustar_strd()
    710  1.1  jtc  *	initialization for ustar read
    711  1.1  jtc  * Return:
    712  1.1  jtc  *	0 if ok, -1 otherwise
    713  1.1  jtc  */
    714  1.1  jtc 
    715  1.1  jtc #if __STDC__
    716  1.1  jtc int
    717  1.1  jtc ustar_strd(void)
    718  1.1  jtc #else
    719  1.1  jtc int
    720  1.1  jtc ustar_strd()
    721  1.1  jtc #endif
    722  1.1  jtc {
    723  1.1  jtc 	if ((usrtb_start() < 0) || (grptb_start() < 0))
    724  1.1  jtc 		return(-1);
    725  1.1  jtc 	return(0);
    726  1.1  jtc }
    727  1.1  jtc 
    728  1.1  jtc /*
    729  1.1  jtc  * ustar_stwr()
    730  1.1  jtc  *	initialization for ustar write
    731  1.1  jtc  * Return:
    732  1.1  jtc  *	0 if ok, -1 otherwise
    733  1.1  jtc  */
    734  1.1  jtc 
    735  1.1  jtc #if __STDC__
    736  1.1  jtc int
    737  1.1  jtc ustar_stwr(void)
    738  1.1  jtc #else
    739  1.1  jtc int
    740  1.1  jtc ustar_stwr()
    741  1.1  jtc #endif
    742  1.1  jtc {
    743  1.1  jtc 	if ((uidtb_start() < 0) || (gidtb_start() < 0))
    744  1.1  jtc 		return(-1);
    745  1.1  jtc 	return(0);
    746  1.1  jtc }
    747  1.1  jtc 
    748  1.1  jtc /*
    749  1.1  jtc  * ustar_id()
    750  1.1  jtc  *	determine if a block given to us is a valid ustar header. We have to
    751  1.1  jtc  *	be on the lookout for those pesky blocks of all zero's
    752  1.1  jtc  * Return:
    753  1.1  jtc  *	0 if a ustar header, -1 otherwise
    754  1.1  jtc  */
    755  1.1  jtc 
    756  1.1  jtc #if __STDC__
    757  1.1  jtc int
    758  1.1  jtc ustar_id(char *blk, int size)
    759  1.1  jtc #else
    760  1.1  jtc int
    761  1.1  jtc ustar_id(blk, size)
    762  1.1  jtc 	char *blk;
    763  1.1  jtc 	int size;
    764  1.1  jtc #endif
    765  1.1  jtc {
    766  1.1  jtc 	register HD_USTAR *hd;
    767  1.1  jtc 
    768  1.1  jtc 	if (size < BLKMULT)
    769  1.1  jtc 		return(-1);
    770  1.1  jtc 	hd = (HD_USTAR *)blk;
    771  1.1  jtc 
    772  1.1  jtc 	/*
    773  1.1  jtc 	 * check for block of zero's first, a simple and fast test then check
    774  1.1  jtc 	 * ustar magic cookie. We should use TMAGLEN, but some USTAR archive
    775  1.1  jtc 	 * programs are fouled up and create archives missing the \0. Last we
    776  1.1  jtc 	 * check the checksum. If ok we have to assume it is a valid header.
    777  1.1  jtc 	 */
    778  1.1  jtc 	if (hd->name[0] == '\0')
    779  1.1  jtc 		return(-1);
    780  1.1  jtc 	if (strncmp(hd->magic, TMAGIC, TMAGLEN - 1) != 0)
    781  1.1  jtc 		return(-1);
    782  1.1  jtc 	if (asc_ul(hd->chksum,sizeof(hd->chksum),OCT) != tar_chksm(blk,BLKMULT))
    783  1.1  jtc 		return(-1);
    784  1.1  jtc 	return(0);
    785  1.1  jtc }
    786  1.1  jtc 
    787  1.1  jtc /*
    788  1.1  jtc  * ustar_rd()
    789  1.1  jtc  *	extract the values out of block already determined to be a ustar header.
    790  1.1  jtc  *	store the values in the ARCHD parameter.
    791  1.1  jtc  * Return:
    792  1.1  jtc  *	0
    793  1.1  jtc  */
    794  1.1  jtc 
    795  1.1  jtc #if __STDC__
    796  1.1  jtc int
    797  1.1  jtc ustar_rd(register ARCHD *arcn, register char *buf)
    798  1.1  jtc #else
    799  1.1  jtc int
    800  1.1  jtc ustar_rd(arcn, buf)
    801  1.1  jtc 	register ARCHD *arcn;
    802  1.1  jtc 	register char *buf;
    803  1.1  jtc #endif
    804  1.1  jtc {
    805  1.1  jtc 	register HD_USTAR *hd;
    806  1.1  jtc 	register char *dest;
    807  1.1  jtc 	register int cnt = 0;
    808  1.1  jtc 	dev_t devmajor;
    809  1.1  jtc 	dev_t devminor;
    810  1.1  jtc 
    811  1.1  jtc 	/*
    812  1.1  jtc 	 * we only get proper sized buffers
    813  1.1  jtc 	 */
    814  1.1  jtc 	if (ustar_id(buf, BLKMULT) < 0)
    815  1.1  jtc 		return(-1);
    816  1.1  jtc 	arcn->org_name = arcn->name;
    817  1.1  jtc 	arcn->sb.st_nlink = 1;
    818  1.1  jtc 	arcn->pat = NULL;
    819  1.1  jtc 	hd = (HD_USTAR *)buf;
    820  1.1  jtc 
    821  1.1  jtc 	/*
    822  1.1  jtc 	 * see if the filename is split into two parts. if, so joint the parts.
    823  1.1  jtc 	 * we copy the prefix first and add a / between the prefix and name.
    824  1.1  jtc 	 */
    825  1.1  jtc 	dest = arcn->name;
    826  1.1  jtc 	if (*(hd->prefix) != '\0') {
    827  1.1  jtc 		cnt = l_strncpy(arcn->name, hd->prefix, sizeof(hd->prefix));
    828  1.1  jtc 		dest = arcn->name + arcn->nlen;
    829  1.1  jtc 		*dest++ = '/';
    830  1.1  jtc 	}
    831  1.1  jtc 	arcn->nlen = l_strncpy(dest, hd->name, sizeof(hd->name));
    832  1.1  jtc 	arcn->nlen += cnt;
    833  1.1  jtc 	arcn->name[arcn->nlen] = '\0';
    834  1.1  jtc 
    835  1.1  jtc 	/*
    836  1.1  jtc 	 * follow the spec to the letter. we should only have mode bits, strip
    837  1.1  jtc 	 * off all other crud we may be passed.
    838  1.1  jtc 	 */
    839  1.1  jtc 	arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode, sizeof(hd->mode), OCT) &
    840  1.1  jtc 	    0xfff);
    841  1.1  jtc 	arcn->sb.st_size = (size_t)asc_ul(hd->size, sizeof(hd->size), OCT);
    842  1.1  jtc 	arcn->sb.st_mtime = (time_t)asc_ul(hd->mtime, sizeof(hd->mtime), OCT);
    843  1.1  jtc 	arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
    844  1.1  jtc 
    845  1.1  jtc 	/*
    846  1.1  jtc 	 * If we can find the ascii names for gname and uname in the password
    847  1.1  jtc 	 * and group files we will use the uid's and gid they bind. Otherwise
    848  1.1  jtc 	 * we use the uid and gid values stored in the header. (This is what
    849  1.1  jtc 	 * the posix spec wants).
    850  1.1  jtc 	 */
    851  1.1  jtc 	hd->gname[sizeof(hd->gname) - 1] = '\0';
    852  1.1  jtc 	if (gid_name(hd->gname, &(arcn->sb.st_gid)) < 0)
    853  1.1  jtc 		arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT);
    854  1.1  jtc 	hd->uname[sizeof(hd->uname) - 1] = '\0';
    855  1.1  jtc 	if (uid_name(hd->uname, &(arcn->sb.st_uid)) < 0)
    856  1.1  jtc 		arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT);
    857  1.1  jtc 
    858  1.1  jtc 	/*
    859  1.1  jtc 	 * set the defaults, these may be changed depending on the file type
    860  1.1  jtc 	 */
    861  1.1  jtc 	arcn->ln_name[0] = '\0';
    862  1.1  jtc 	arcn->ln_nlen = 0;
    863  1.1  jtc 	arcn->pad = 0;
    864  1.1  jtc 	arcn->skip = 0;
    865  1.1  jtc 	arcn->sb.st_rdev = (dev_t)0;
    866  1.1  jtc 
    867  1.1  jtc 	/*
    868  1.1  jtc 	 * set the mode and PAX type according to the typeflag in the header
    869  1.1  jtc 	 */
    870  1.1  jtc 	switch(hd->typeflag) {
    871  1.1  jtc 	case FIFOTYPE:
    872  1.1  jtc 		arcn->type = PAX_FIF;
    873  1.1  jtc 		arcn->sb.st_mode |= S_IFIFO;
    874  1.1  jtc 		break;
    875  1.1  jtc 	case DIRTYPE:
    876  1.1  jtc 		arcn->type = PAX_DIR;
    877  1.1  jtc 		arcn->sb.st_mode |= S_IFDIR;
    878  1.1  jtc 		arcn->sb.st_nlink = 2;
    879  1.1  jtc 
    880  1.1  jtc 		/*
    881  1.1  jtc 		 * Some programs that create ustar archives append a '/'
    882  1.1  jtc 		 * to the pathname for directories. This clearly violates
    883  1.1  jtc 		 * ustar specs, but we will silently strip it off anyway.
    884  1.1  jtc 		 */
    885  1.1  jtc 		if (arcn->name[arcn->nlen - 1] == '/')
    886  1.1  jtc 			arcn->name[--arcn->nlen] = '\0';
    887  1.1  jtc 		break;
    888  1.1  jtc 	case BLKTYPE:
    889  1.1  jtc 	case CHRTYPE:
    890  1.1  jtc 		/*
    891  1.1  jtc 		 * this type requires the rdev field to be set.
    892  1.1  jtc 		 */
    893  1.1  jtc 		if (hd->typeflag == BLKTYPE) {
    894  1.1  jtc 			arcn->type = PAX_BLK;
    895  1.1  jtc 			arcn->sb.st_mode |= S_IFBLK;
    896  1.1  jtc 		} else {
    897  1.1  jtc 			arcn->type = PAX_CHR;
    898  1.1  jtc 			arcn->sb.st_mode |= S_IFCHR;
    899  1.1  jtc 		}
    900  1.1  jtc 		devmajor = (dev_t)asc_ul(hd->devmajor,sizeof(hd->devmajor),OCT);
    901  1.1  jtc 		devminor = (dev_t)asc_ul(hd->devminor,sizeof(hd->devminor),OCT);
    902  1.1  jtc 		arcn->sb.st_rdev = TODEV(devmajor, devminor);
    903  1.1  jtc 		break;
    904  1.1  jtc 	case SYMTYPE:
    905  1.1  jtc 	case LNKTYPE:
    906  1.1  jtc 		if (hd->typeflag == SYMTYPE) {
    907  1.1  jtc 			arcn->type = PAX_SLK;
    908  1.1  jtc 			arcn->sb.st_mode |= S_IFLNK;
    909  1.1  jtc 		} else {
    910  1.1  jtc 			arcn->type = PAX_HLK;
    911  1.1  jtc 			/*
    912  1.1  jtc 			 * so printing looks better
    913  1.1  jtc 			 */
    914  1.1  jtc 			arcn->sb.st_mode |= S_IFREG;
    915  1.1  jtc 			arcn->sb.st_nlink = 2;
    916  1.1  jtc 		}
    917  1.1  jtc 		/*
    918  1.1  jtc 		 * copy the link name
    919  1.1  jtc 		 */
    920  1.1  jtc 		arcn->ln_nlen = l_strncpy(arcn->ln_name, hd->linkname,
    921  1.1  jtc 			sizeof(hd->linkname));
    922  1.1  jtc 		arcn->ln_name[arcn->ln_nlen] = '\0';
    923  1.1  jtc 		break;
    924  1.1  jtc 	case CONTTYPE:
    925  1.1  jtc 	case AREGTYPE:
    926  1.1  jtc 	case REGTYPE:
    927  1.1  jtc 	default:
    928  1.1  jtc 		/*
    929  1.1  jtc 		 * these types have file data that follows. Set the skip and
    930  1.1  jtc 		 * pad fields.
    931  1.1  jtc 		 */
    932  1.1  jtc 		arcn->type = PAX_REG;
    933  1.1  jtc 		arcn->pad = TAR_PAD(arcn->sb.st_size);
    934  1.1  jtc 		arcn->skip = arcn->sb.st_size;
    935  1.1  jtc 		arcn->sb.st_mode |= S_IFREG;
    936  1.1  jtc 		break;
    937  1.1  jtc 	}
    938  1.1  jtc 	return(0);
    939  1.1  jtc }
    940  1.1  jtc 
    941  1.1  jtc /*
    942  1.1  jtc  * ustar_wr()
    943  1.1  jtc  *	write a ustar header for the file specified in the ARCHD to the archive
    944  1.1  jtc  *	Have to check for file types that cannot be stored and file names that
    945  1.1  jtc  *	are too long. Be careful of the term (last arg) to ul_oct, we only use
    946  1.1  jtc  *	'\0' for the termination character (this is different than picky tar)
    947  1.1  jtc  *	ASSUMED: space after header in header block is zero filled
    948  1.1  jtc  * Return:
    949  1.1  jtc  *	0 if file has data to be written after the header, 1 if file has NO
    950  1.1  jtc  *	data to write after the header, -1 if archive write failed
    951  1.1  jtc  */
    952  1.1  jtc 
    953  1.1  jtc #if __STDC__
    954  1.1  jtc int
    955  1.1  jtc ustar_wr(register ARCHD *arcn)
    956  1.1  jtc #else
    957  1.1  jtc int
    958  1.1  jtc ustar_wr(arcn)
    959  1.1  jtc 	register ARCHD *arcn;
    960  1.1  jtc #endif
    961  1.1  jtc {
    962  1.1  jtc 	register HD_USTAR *hd;
    963  1.1  jtc 	register char *pt;
    964  1.1  jtc 	char hdblk[sizeof(HD_USTAR)];
    965  1.1  jtc 
    966  1.1  jtc 	/*
    967  1.1  jtc 	 * check for those file system types ustar cannot store
    968  1.1  jtc 	 */
    969  1.1  jtc 	if (arcn->type == PAX_SCK) {
    970  1.1  jtc 		warn(1, "Ustar cannot archive a socket %s", arcn->org_name);
    971  1.1  jtc 		return(1);
    972  1.1  jtc 	}
    973  1.1  jtc 
    974  1.1  jtc 	/*
    975  1.1  jtc 	 * check the length of the linkname
    976  1.1  jtc 	 */
    977  1.1  jtc 	if (((arcn->type == PAX_SLK) || (arcn->type == PAX_HLK) ||
    978  1.1  jtc 	    (arcn->type == PAX_HRG)) && (arcn->ln_nlen > sizeof(hd->linkname))){
    979  1.1  jtc 		warn(1, "Link name too long for ustar %s", arcn->ln_name);
    980  1.1  jtc 		return(1);
    981  1.1  jtc 	}
    982  1.1  jtc 
    983  1.1  jtc 	/*
    984  1.1  jtc 	 * split the path name into prefix and name fields (if needed). if
    985  1.1  jtc 	 * pt != arcn->name, the name has to be split
    986  1.1  jtc 	 */
    987  1.1  jtc 	if ((pt = name_split(arcn->name, arcn->nlen)) == NULL) {
    988  1.1  jtc 		warn(1, "File name too long for ustar %s", arcn->name);
    989  1.1  jtc 		return(1);
    990  1.1  jtc 	}
    991  1.1  jtc 	hd = (HD_USTAR *)hdblk;
    992  1.1  jtc 	arcn->pad = 0L;
    993  1.1  jtc 
    994  1.1  jtc 	/*
    995  1.1  jtc 	 * split the name, or zero out the prefix
    996  1.1  jtc 	 */
    997  1.1  jtc 	if (pt != arcn->name) {
    998  1.1  jtc 		/*
    999  1.1  jtc 		 * name was split, pt points at the / where the split is to
   1000  1.1  jtc 		 * occur, we remove the / and copy the first part to the prefix
   1001  1.1  jtc 		 */
   1002  1.1  jtc 		*pt = '\0';
   1003  1.1  jtc 		zf_strncpy(hd->prefix, arcn->name, sizeof(hd->prefix));
   1004  1.1  jtc 		*pt++ = '/';
   1005  1.1  jtc 	} else
   1006  1.1  jtc 		bzero(hd->prefix, sizeof(hd->prefix));
   1007  1.1  jtc 
   1008  1.1  jtc 	/*
   1009  1.1  jtc 	 * copy the name part. this may be the whole path or the part after
   1010  1.1  jtc 	 * the prefix
   1011  1.1  jtc 	 */
   1012  1.1  jtc 	zf_strncpy(hd->name, pt, sizeof(hd->name));
   1013  1.1  jtc 
   1014  1.1  jtc 	/*
   1015  1.1  jtc 	 * set the fields in the header that are type dependent
   1016  1.1  jtc 	 */
   1017  1.1  jtc 	switch(arcn->type) {
   1018  1.1  jtc 	case PAX_DIR:
   1019  1.1  jtc 		hd->typeflag = DIRTYPE;
   1020  1.1  jtc 		bzero(hd->linkname, sizeof(hd->linkname));
   1021  1.1  jtc 		bzero(hd->devmajor, sizeof(hd->devmajor));
   1022  1.1  jtc 		bzero(hd->devminor, sizeof(hd->devminor));
   1023  1.1  jtc 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
   1024  1.1  jtc 			goto out;
   1025  1.1  jtc 		break;
   1026  1.1  jtc 	case PAX_CHR:
   1027  1.1  jtc 	case PAX_BLK:
   1028  1.1  jtc 		if (arcn->type == PAX_CHR)
   1029  1.1  jtc 			hd->typeflag = CHRTYPE;
   1030  1.1  jtc 		else
   1031  1.1  jtc 			hd->typeflag = BLKTYPE;
   1032  1.1  jtc 		bzero(hd->linkname, sizeof(hd->linkname));
   1033  1.1  jtc 		if (ul_oct((u_long)MAJOR(arcn->sb.st_rdev), hd->devmajor,
   1034  1.1  jtc 		   sizeof(hd->devmajor), 3) ||
   1035  1.1  jtc 		   ul_oct((u_long)MINOR(arcn->sb.st_rdev), hd->devminor,
   1036  1.1  jtc 		   sizeof(hd->devminor), 3) ||
   1037  1.1  jtc 		   ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
   1038  1.1  jtc 			goto out;
   1039  1.1  jtc 		break;
   1040  1.1  jtc 	case PAX_FIF:
   1041  1.1  jtc 		hd->typeflag = FIFOTYPE;
   1042  1.1  jtc 		bzero(hd->linkname, sizeof(hd->linkname));
   1043  1.1  jtc 		bzero(hd->devmajor, sizeof(hd->devmajor));
   1044  1.1  jtc 		bzero(hd->devminor, sizeof(hd->devminor));
   1045  1.1  jtc 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
   1046  1.1  jtc 			goto out;
   1047  1.1  jtc 		break;
   1048  1.1  jtc 	case PAX_SLK:
   1049  1.1  jtc 	case PAX_HLK:
   1050  1.1  jtc 	case PAX_HRG:
   1051  1.1  jtc 		if (arcn->type == PAX_SLK)
   1052  1.1  jtc 			hd->typeflag = SYMTYPE;
   1053  1.1  jtc 		else
   1054  1.1  jtc 			hd->typeflag = LNKTYPE;
   1055  1.1  jtc 		zf_strncpy(hd->linkname,arcn->ln_name, sizeof(hd->linkname));
   1056  1.1  jtc 		bzero(hd->devmajor, sizeof(hd->devmajor));
   1057  1.1  jtc 		bzero(hd->devminor, sizeof(hd->devminor));
   1058  1.1  jtc 		if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3))
   1059  1.1  jtc 			goto out;
   1060  1.1  jtc 		break;
   1061  1.1  jtc 	case PAX_REG:
   1062  1.1  jtc 	case PAX_CTG:
   1063  1.1  jtc 	default:
   1064  1.1  jtc 		/*
   1065  1.1  jtc 		 * file data with this type, set the padding
   1066  1.1  jtc 		 */
   1067  1.1  jtc 		if (arcn->type == PAX_CTG)
   1068  1.1  jtc 			hd->typeflag = CONTTYPE;
   1069  1.1  jtc 		else
   1070  1.1  jtc 			hd->typeflag = REGTYPE;
   1071  1.1  jtc 		bzero(hd->linkname, sizeof(hd->linkname));
   1072  1.1  jtc 		bzero(hd->devmajor, sizeof(hd->devmajor));
   1073  1.1  jtc 		bzero(hd->devminor, sizeof(hd->devminor));
   1074  1.1  jtc 		arcn->pad = TAR_PAD(arcn->sb.st_size);
   1075  1.1  jtc #		ifdef NET2_STAT
   1076  1.1  jtc 		if (ul_oct((u_long)arcn->sb.st_size, hd->size,
   1077  1.1  jtc 		    sizeof(hd->size), 3)) {
   1078  1.1  jtc #		else
   1079  1.1  jtc 		if (uqd_oct((u_quad_t)arcn->sb.st_size, hd->size,
   1080  1.1  jtc 		    sizeof(hd->size), 3)) {
   1081  1.1  jtc #		endif
   1082  1.1  jtc 			warn(1,"File is too long for ustar %s",arcn->org_name);
   1083  1.1  jtc 			return(1);
   1084  1.1  jtc 		}
   1085  1.1  jtc 		break;
   1086  1.1  jtc 	}
   1087  1.1  jtc 
   1088  1.1  jtc 	zf_strncpy(hd->magic, TMAGIC, TMAGLEN);
   1089  1.1  jtc 	zf_strncpy(hd->version, TVERSION, TVERSLEN);
   1090  1.1  jtc 
   1091  1.1  jtc 	/*
   1092  1.1  jtc 	 * set the remaining fields. Some versions want all 16 bits of mode
   1093  1.1  jtc 	 * we better humor them (they really do not meet spec though)....
   1094  1.1  jtc 	 */
   1095  1.1  jtc 	if (ul_oct((u_long)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 3) ||
   1096  1.1  jtc 	    ul_oct((u_long)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 3)  ||
   1097  1.1  jtc 	    ul_oct((u_long)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 3) ||
   1098  1.1  jtc 	    ul_oct((u_long)arcn->sb.st_mtime,hd->mtime,sizeof(hd->mtime),3))
   1099  1.1  jtc 		goto out;
   1100  1.1  jtc 	zf_strncpy(hd->uname,name_uid(arcn->sb.st_uid, 0),sizeof(hd->uname));
   1101  1.1  jtc 	zf_strncpy(hd->gname,name_gid(arcn->sb.st_gid, 0),sizeof(hd->gname));
   1102  1.1  jtc 
   1103  1.1  jtc 	/*
   1104  1.1  jtc 	 * calculate and store the checksum write the header to the archive
   1105  1.1  jtc 	 * return 0 tells the caller to now write the file data, 1 says no data
   1106  1.1  jtc 	 * needs to be written
   1107  1.1  jtc 	 */
   1108  1.1  jtc 	if (ul_oct(tar_chksm(hdblk, sizeof(HD_USTAR)), hd->chksum,
   1109  1.1  jtc 	   sizeof(hd->chksum), 3))
   1110  1.1  jtc 		goto out;
   1111  1.1  jtc 	if (wr_rdbuf(hdblk, sizeof(HD_USTAR)) < 0)
   1112  1.1  jtc 		return(-1);
   1113  1.1  jtc 	if (wr_skip((off_t)(BLKMULT - sizeof(HD_USTAR))) < 0)
   1114  1.1  jtc 		return(-1);
   1115  1.1  jtc 	if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG))
   1116  1.1  jtc 		return(0);
   1117  1.1  jtc 	return(1);
   1118  1.1  jtc 
   1119  1.1  jtc     out:
   1120  1.1  jtc     	/*
   1121  1.1  jtc 	 * header field is out of range
   1122  1.1  jtc 	 */
   1123  1.1  jtc 	warn(1, "Ustar header field is too small for %s", arcn->org_name);
   1124  1.1  jtc 	return(1);
   1125  1.1  jtc }
   1126  1.1  jtc 
   1127  1.1  jtc /*
   1128  1.1  jtc  * name_split()
   1129  1.1  jtc  *	see if the name has to be split for storage in a ustar header. We try
   1130  1.1  jtc  *	to fit the entire name in the name field without splitting if we can.
   1131  1.1  jtc  *	The split point is always at a /
   1132  1.1  jtc  * Return
   1133  1.1  jtc  *	character pointer to split point (always the / that is to be removed
   1134  1.1  jtc  *	if the split is not needed, the points is set to the start of the file
   1135  1.1  jtc  *	name (it would violate the spec to split there). A NULL is returned if
   1136  1.1  jtc  *	the file name is too long
   1137  1.1  jtc  */
   1138  1.1  jtc 
   1139  1.1  jtc #if __STDC__
   1140  1.1  jtc static char *
   1141  1.1  jtc name_split(register char *name, register int len)
   1142  1.1  jtc #else
   1143  1.1  jtc static char *
   1144  1.1  jtc name_split(name, len)
   1145  1.1  jtc 	register char *name;
   1146  1.1  jtc 	register int len;
   1147  1.1  jtc #endif
   1148  1.1  jtc {
   1149  1.1  jtc 	register char *start;
   1150  1.1  jtc 
   1151  1.1  jtc 	/*
   1152  1.1  jtc 	 * check to see if the file name is small enough to fit in the name
   1153  1.1  jtc 	 * field. if so just return a pointer to the name.
   1154  1.1  jtc 	 */
   1155  1.1  jtc 	if (len <= TNMSZ)
   1156  1.1  jtc 		return(name);
   1157  1.1  jtc 	if (len > (TPFSZ + TNMSZ + 1))
   1158  1.1  jtc 		return(NULL);
   1159  1.1  jtc 
   1160  1.1  jtc 	/*
   1161  1.1  jtc 	 * we start looking at the biggest sized piece that fits in the name
   1162  1.1  jtc 	 * field. We walk foward looking for a slash to split at. The idea is
   1163  1.1  jtc 	 * to find the biggest piece to fit in the name field (or the smallest
   1164  1.1  jtc 	 * prefix we can find) (the -1 is correct the biggest piece would
   1165  1.1  jtc 	 * include the slash between the two parts that gets thrown away)
   1166  1.1  jtc 	 */
   1167  1.1  jtc 	start = name + len - TNMSZ - 1;
   1168  1.1  jtc 	while ((*start != '\0') && (*start != '/'))
   1169  1.1  jtc 		++start;
   1170  1.1  jtc 
   1171  1.1  jtc 	/*
   1172  1.1  jtc 	 * if we hit the end of the string, this name cannot be split, so we
   1173  1.1  jtc 	 * cannot store this file.
   1174  1.1  jtc 	 */
   1175  1.1  jtc 	if (*start == '\0')
   1176  1.1  jtc 		return(NULL);
   1177  1.1  jtc 	len = start - name;
   1178  1.1  jtc 
   1179  1.1  jtc 	/*
   1180  1.1  jtc 	 * NOTE: /str where the length of str == TNMSZ can not be stored under
   1181  1.1  jtc 	 * the p1003.1-1990 spec for ustar. We could force a prefix of / and
   1182  1.1  jtc 	 * the file would then expand on extract to //str. The len == 0 below
   1183  1.1  jtc 	 * makes this special case follow the spec to the letter.
   1184  1.1  jtc 	 */
   1185  1.1  jtc 	if ((len > TPFSZ) || (len == 0))
   1186  1.1  jtc 		return(NULL);
   1187  1.1  jtc 
   1188  1.1  jtc 	/*
   1189  1.1  jtc 	 * ok have a split point, return it to the caller
   1190  1.1  jtc 	 */
   1191  1.1  jtc 	return(start);
   1192  1.1  jtc }
   1193