Home | History | Annotate | Line # | Download | only in pax
      1 /*	$NetBSD: gen_subs.c,v 1.37 2018/11/30 00:53:11 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1992 Keith Muller.
      5  * Copyright (c) 1992, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * This code is derived from software contributed to Berkeley by
      9  * Keith Muller of the University of California, San Diego.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #if HAVE_NBTOOL_CONFIG_H
     37 #include "nbtool_config.h"
     38 #endif
     39 
     40 #include <sys/cdefs.h>
     41 #if !defined(lint)
     42 #if 0
     43 static char sccsid[] = "@(#)gen_subs.c	8.1 (Berkeley) 5/31/93";
     44 #else
     45 __RCSID("$NetBSD: gen_subs.c,v 1.37 2018/11/30 00:53:11 christos Exp $");
     46 #endif
     47 #endif /* not lint */
     48 
     49 #include <sys/types.h>
     50 #include <sys/time.h>
     51 #include <sys/stat.h>
     52 #include <sys/param.h>
     53 
     54 #include <ctype.h>
     55 #include <grp.h>
     56 #include <pwd.h>
     57 #include <vis.h>
     58 #include <stdio.h>
     59 #include <stdlib.h>
     60 #include <string.h>
     61 #include <time.h>
     62 #include <tzfile.h>
     63 #include <unistd.h>
     64 
     65 #include "pax.h"
     66 #include "extern.h"
     67 
     68 /*
     69  * a collection of general purpose subroutines used by pax
     70  */
     71 
     72 /*
     73  * constants used by ls_list() when printing out archive members
     74  */
     75 #define MODELEN 20
     76 #define DATELEN 64
     77 #define SIXMONTHS	 ((DAYSPERNYEAR / 2) * SECSPERDAY)
     78 #define CURFRMT		"%b %e %H:%M"
     79 #define OLDFRMT		"%b %e  %Y"
     80 #ifndef UT_NAMESIZE
     81 #define UT_NAMESIZE	8
     82 #endif
     83 #define UT_GRPSIZE	6
     84 
     85 /*
     86  * convert time to string
     87  */
     88 static void
     89 formattime(char *buf, size_t buflen, time_t when)
     90 {
     91 	int error;
     92 	struct tm tm;
     93 	(void)localtime_r(&when, &tm);
     94 
     95 	if (when + SIXMONTHS <= time(NULL))
     96 		error = strftime(buf, buflen, OLDFRMT, &tm);
     97 	else
     98 		error = strftime(buf, buflen, CURFRMT, &tm);
     99 
    100 	if (error == 0)
    101 		buf[0] = '\0';
    102 }
    103 
    104 /*
    105  * ls_list()
    106  *	list the members of an archive in ls format
    107  */
    108 
    109 void
    110 ls_list(ARCHD *arcn, time_t now, FILE *fp)
    111 {
    112 	struct stat *sbp;
    113 	char f_mode[MODELEN];
    114 	char f_date[DATELEN];
    115 	const char *user, *group;
    116 
    117 	/*
    118 	 * if not verbose, just print the file name
    119 	 */
    120 	if (!vflag) {
    121 		(void)fprintf(fp, "%s\n", arcn->name);
    122 		(void)fflush(fp);
    123 		return;
    124 	}
    125 
    126 	/*
    127 	 * user wants long mode
    128 	 */
    129 	sbp = &(arcn->sb);
    130 	strmode(sbp->st_mode, f_mode);
    131 
    132 	/*
    133 	 * time format based on age compared to the time pax was started.
    134 	 */
    135 	formattime(f_date, sizeof(f_date), arcn->sb.st_mtime);
    136 	/*
    137 	 * print file mode, link count, uid, gid and time
    138 	 */
    139 	user = user_from_uid(sbp->st_uid, 0);
    140 	group = group_from_gid(sbp->st_gid, 0);
    141 	(void)fprintf(fp, "%s%2lu %-*s %-*s ", f_mode,
    142 	    (unsigned long)sbp->st_nlink,
    143 	    UT_NAMESIZE, user ? user : "", UT_GRPSIZE, group ? group : "");
    144 
    145 	/*
    146 	 * print device id's for devices, or sizes for other nodes
    147 	 */
    148 	if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK))
    149 		(void)fprintf(fp, "%4lu,%4lu ", (long) MAJOR(sbp->st_rdev),
    150 		    (long) MINOR(sbp->st_rdev));
    151 	else {
    152 		(void)fprintf(fp, OFFT_FP("9") " ", (OFFT_T)sbp->st_size);
    153 	}
    154 
    155 	/*
    156 	 * print name and link info for hard and soft links
    157 	 */
    158 	(void)fprintf(fp, "%s %s", f_date, arcn->name);
    159 	if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
    160 		(void)fprintf(fp, " == %s\n", arcn->ln_name);
    161 	else if (arcn->type == PAX_SLK)
    162 		(void)fprintf(fp, " -> %s\n", arcn->ln_name);
    163 	else
    164 		(void)fputc('\n', fp);
    165 	(void)fflush(fp);
    166 }
    167 
    168 /*
    169  * tty_ls()
    170  *	print a short summary of file to tty.
    171  */
    172 
    173 void
    174 ls_tty(ARCHD *arcn)
    175 {
    176 	char f_date[DATELEN];
    177 	char f_mode[MODELEN];
    178 
    179 	formattime(f_date, sizeof(f_date), arcn->sb.st_mtime);
    180 	strmode(arcn->sb.st_mode, f_mode);
    181 	tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name);
    182 	return;
    183 }
    184 
    185 void
    186 safe_print(const char *str, FILE *fp)
    187 {
    188 	char visbuf[5];
    189 	const char *cp;
    190 
    191 	/*
    192 	 * if printing to a tty, use vis(3) to print special characters.
    193 	 */
    194 	if (isatty(fileno(fp))) {
    195 		for (cp = str; *cp; cp++) {
    196 			(void)vis(visbuf, cp[0], VIS_CSTYLE, cp[1]);
    197 			(void)fputs(visbuf, fp);
    198 		}
    199 	} else {
    200 		(void)fputs(str, fp);
    201 	}
    202 }
    203 
    204 /*
    205  * asc_u32()
    206  *	convert hex/octal character string into a uint32_t. We do not have to
    207  *	check for overflow! (the headers in all supported formats are not large
    208  *	enough to create an overflow).
    209  *	NOTE: strings passed to us are NOT TERMINATED.
    210  * Return:
    211  *	uint32_t value
    212  */
    213 
    214 uint32_t
    215 asc_u32(char *str, int len, int base)
    216 {
    217 	char *stop;
    218 	uint32_t tval = 0;
    219 
    220 	stop = str + len;
    221 
    222 	/*
    223 	 * skip over leading blanks and zeros
    224 	 */
    225 	while ((str < stop) && ((*str == ' ') || (*str == '0')))
    226 		++str;
    227 
    228 	/*
    229 	 * for each valid digit, shift running value (tval) over to next digit
    230 	 * and add next digit
    231 	 */
    232 	if (base == HEX) {
    233 		while (str < stop) {
    234 			if ((*str >= '0') && (*str <= '9'))
    235 				tval = (tval << 4) + (*str++ - '0');
    236 			else if ((*str >= 'A') && (*str <= 'F'))
    237 				tval = (tval << 4) + 10 + (*str++ - 'A');
    238 			else if ((*str >= 'a') && (*str <= 'f'))
    239 				tval = (tval << 4) + 10 + (*str++ - 'a');
    240 			else
    241 				break;
    242 		}
    243 	} else {
    244 		while ((str < stop) && (*str >= '0') && (*str <= '7'))
    245 			tval = (tval << 3) + (*str++ - '0');
    246 	}
    247 	return tval;
    248 }
    249 
    250 /*
    251  * u32_asc()
    252  *	convert an uintmax_t into an hex/oct ascii string. pads with LEADING
    253  *	ascii 0's to fill string completely
    254  *	NOTE: the string created is NOT TERMINATED.
    255  */
    256 
    257 int
    258 u32_asc(uintmax_t val, char *str, int len, int base)
    259 {
    260 	char *pt;
    261 	uint32_t digit;
    262 	uintmax_t p;
    263 
    264 	p = val & TOP_HALF;
    265 	if (p && p != TOP_HALF)
    266 		return -1;
    267 
    268 	val &= BOTTOM_HALF;
    269 
    270 	/*
    271 	 * WARNING str is not '\0' terminated by this routine
    272 	 */
    273 	pt = str + len - 1;
    274 
    275 	/*
    276 	 * do a tailwise conversion (start at right most end of string to place
    277 	 * least significant digit). Keep shifting until conversion value goes
    278 	 * to zero (all digits were converted)
    279 	 */
    280 	if (base == HEX) {
    281 		while (pt >= str) {
    282 			if ((digit = (val & 0xf)) < 10)
    283 				*pt-- = '0' + (char)digit;
    284 			else
    285 				*pt-- = 'a' + (char)(digit - 10);
    286 			if ((val = (val >> 4)) == (u_long)0)
    287 				break;
    288 		}
    289 	} else {
    290 		while (pt >= str) {
    291 			*pt-- = '0' + (char)(val & 0x7);
    292 			if ((val = (val >> 3)) == 0)
    293 				break;
    294 		}
    295 	}
    296 
    297 	/*
    298 	 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
    299 	 */
    300 	while (pt >= str)
    301 		*pt-- = '0';
    302 	if (val != 0)
    303 		return -1;
    304 	return 0;
    305 }
    306 
    307 /*
    308  * asc_umax()
    309  *	convert hex/octal/base-256 value into a uintmax.
    310  *	NOTE: strings passed to us are NOT TERMINATED.
    311  * Return:
    312  *	uintmax_t value; UINTMAX_MAX for overflow/negative
    313  */
    314 
    315 uintmax_t
    316 asc_umax(char *str, int len, int base)
    317 {
    318 	char *stop;
    319 	uintmax_t tval = 0;
    320 
    321 	stop = str + len;
    322 
    323 	/*
    324 	 * if the highest bit of first byte is set, it's base-256 encoded
    325 	 * (base-256 is basically (n-1)-bit big endian signed
    326 	 */
    327 	if (str < stop && (*str & 0x80)) {
    328 		/*
    329 		 * uintmax_t can't be negative, so fail on negative numbers
    330 		 */
    331 		if (*str & 0x40)
    332 			return UINTMAX_MAX;
    333 
    334 		tval = *str++ & 0x3f;
    335 		while (str < stop) {
    336 			/*
    337 			 * check for overflow
    338 			 */
    339 			if (tval > (UINTMAX_MAX/256))
    340 				return UINTMAX_MAX;
    341 			tval = (tval << 8) | ((*str++) & 0xFF);
    342 		}
    343 
    344 		return tval;
    345 	}
    346 
    347 	/*
    348 	 * skip over leading blanks and zeros
    349 	 */
    350 	while ((str < stop) && ((*str == ' ') || (*str == '0')))
    351 		++str;
    352 
    353 	/*
    354 	 * for each valid digit, shift running value (tval) over to next digit
    355 	 * and add next digit
    356 	 */
    357 	if (base == HEX) {
    358 		while (str < stop) {
    359 			if ((*str >= '0') && (*str <= '9'))
    360 				tval = (tval << 4) + (*str++ - '0');
    361 			else if ((*str >= 'A') && (*str <= 'F'))
    362 				tval = (tval << 4) + 10 + (*str++ - 'A');
    363 			else if ((*str >= 'a') && (*str <= 'f'))
    364 				tval = (tval << 4) + 10 + (*str++ - 'a');
    365 			else
    366 				break;
    367 		}
    368 	} else {
    369 		while ((str < stop) && (*str >= '0') && (*str <= '7'))
    370 			tval = (tval << 3) + (*str++ - '0');
    371 	}
    372 	return tval;
    373 }
    374 
    375 /*
    376  * umax_asc()
    377  *	convert an uintmax_t into a hex/oct ascii string. pads with
    378  *	LEADING ascii 0's to fill string completely
    379  *	NOTE: the string created is NOT TERMINATED.
    380  */
    381 
    382 int
    383 umax_asc(uintmax_t val, char *str, int len, int base)
    384 {
    385 	char *pt;
    386 	uintmax_t digit;
    387 
    388 	/*
    389 	 * WARNING str is not '\0' terminated by this routine
    390 	 */
    391 	pt = str + len - 1;
    392 
    393 	/*
    394 	 * do a tailwise conversion (start at right most end of string to place
    395 	 * least significant digit). Keep shifting until conversion value goes
    396 	 * to zero (all digits were converted)
    397 	 */
    398 	if (base == HEX) {
    399 		while (pt >= str) {
    400 			if ((digit = (val & 0xf)) < 10)
    401 				*pt-- = '0' + (char)digit;
    402 			else
    403 				*pt-- = 'a' + (char)(digit - 10);
    404 			if ((val = (val >> 4)) == 0)
    405 				break;
    406 		}
    407 	} else {
    408 		while (pt >= str) {
    409 			*pt-- = '0' + (char)(val & 0x7);
    410 			if ((val = (val >> 3)) == 0)
    411 				break;
    412 		}
    413 	}
    414 
    415 	/*
    416 	 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
    417 	 */
    418 	while (pt >= str)
    419 		*pt-- = '0';
    420 	if (val != 0)
    421 		return -1;
    422 	return 0;
    423 }
    424 
    425 int
    426 check_Aflag(void)
    427 {
    428 
    429 	if (Aflag > 0)
    430 		return 1;
    431 	if (Aflag == 0) {
    432 		Aflag = -1;
    433 		tty_warn(0,
    434 		 "Removing leading / from absolute path names in the archive");
    435 	}
    436 	return 0;
    437 }
    438