/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1992 Keith Muller.4* Copyright (c) 1992, 19935* The Regents of the University of California. All rights reserved.6*7* This code is derived from software contributed to Berkeley by8* Keith Muller of the University of California, San Diego.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions and the following disclaimer.15* 2. Redistributions in binary form must reproduce the above copyright16* notice, this list of conditions and the following disclaimer in the17* documentation and/or other materials provided with the distribution.18* 3. Neither the name of the University nor the names of its contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*/3435#include <sys/types.h>36#include <sys/time.h>37#include <sys/stat.h>38#include <langinfo.h>39#include <stdint.h>40#include <stdio.h>41#include <string.h>42#include "pax.h"43#include "extern.h"4445/*46* a collection of general purpose subroutines used by pax47*/4849/*50* constants used by ls_list() when printing out archive members51*/52#define MODELEN 2053#define DATELEN 6454#define SIXMONTHS ((365 / 2) * 86400)55#define CURFRMTM "%b %e %H:%M"56#define OLDFRMTM "%b %e %Y"57#define CURFRMTD "%e %b %H:%M"58#define OLDFRMTD "%e %b %Y"5960static int d_first = -1;6162/*63* ls_list()64* list the members of an archive in ls format65*/6667void68ls_list(ARCHD *arcn, time_t now, FILE *fp)69{70struct stat *sbp;71char f_mode[MODELEN];72char f_date[DATELEN];73const char *timefrmt;7475/*76* if not verbose, just print the file name77*/78if (!vflag) {79(void)fprintf(fp, "%s\n", arcn->name);80(void)fflush(fp);81return;82}8384if (d_first < 0)85d_first = (*nl_langinfo(D_MD_ORDER) == 'd');86/*87* user wants long mode88*/89sbp = &(arcn->sb);90strmode(sbp->st_mode, f_mode);9192/*93* time format based on age compared to the time pax was started.94*/95if ((sbp->st_mtime + SIXMONTHS) <= now)96timefrmt = d_first ? OLDFRMTD : OLDFRMTM;97else98timefrmt = d_first ? CURFRMTD : CURFRMTM;99100/*101* print file mode, link count, uid, gid and time102*/103if (strftime(f_date,DATELEN,timefrmt,localtime(&(sbp->st_mtime))) == 0)104f_date[0] = '\0';105(void)fprintf(fp, "%s%2ju %-12s %-12s ", f_mode,106(uintmax_t)sbp->st_nlink,107name_uid(sbp->st_uid, 1), name_gid(sbp->st_gid, 1));108109/*110* print device id's for devices, or sizes for other nodes111*/112if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK))113(void)fprintf(fp, "%4lu,%4lu ", (unsigned long)MAJOR(sbp->st_rdev),114(unsigned long)MINOR(sbp->st_rdev));115else {116(void)fprintf(fp, "%9ju ", (uintmax_t)sbp->st_size);117}118119/*120* print name and link info for hard and soft links121*/122(void)fprintf(fp, "%s %s", f_date, arcn->name);123if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))124(void)fprintf(fp, " == %s\n", arcn->ln_name);125else if (arcn->type == PAX_SLK)126(void)fprintf(fp, " => %s\n", arcn->ln_name);127else128(void)putc('\n', fp);129(void)fflush(fp);130return;131}132133/*134* tty_ls()135* print a short summary of file to tty.136*/137138void139ls_tty(ARCHD *arcn)140{141char f_date[DATELEN];142char f_mode[MODELEN];143const char *timefrmt;144145if (d_first < 0)146d_first = (*nl_langinfo(D_MD_ORDER) == 'd');147148if ((arcn->sb.st_mtime + SIXMONTHS) <= time(NULL))149timefrmt = d_first ? OLDFRMTD : OLDFRMTM;150else151timefrmt = d_first ? CURFRMTD : CURFRMTM;152153/*154* convert time to string, and print155*/156if (strftime(f_date, DATELEN, timefrmt,157localtime(&(arcn->sb.st_mtime))) == 0)158f_date[0] = '\0';159strmode(arcn->sb.st_mode, f_mode);160tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name);161return;162}163164/*165* l_strncpy()166* copy src to dest up to len chars (stopping at first '\0').167* when src is shorter than len, pads to len with '\0'.168* Return:169* number of chars copied. (Note this is a real performance win over170* doing a strncpy(), a strlen(), and then a possible memset())171*/172173int174l_strncpy(char *dest, const char *src, int len)175{176char *stop;177char *start;178179stop = dest + len;180start = dest;181while ((dest < stop) && (*src != '\0'))182*dest++ = *src++;183len = dest - start;184while (dest < stop)185*dest++ = '\0';186return(len);187}188189/*190* asc_ul()191* convert hex/octal character string into a u_long. We do not have to192* check for overflow! (the headers in all supported formats are not large193* enough to create an overflow).194* NOTE: strings passed to us are NOT TERMINATED.195* Return:196* unsigned long value197*/198199u_long200asc_ul(char *str, int len, int base)201{202char *stop;203u_long tval = 0;204205stop = str + len;206207/*208* skip over leading blanks and zeros209*/210while ((str < stop) && ((*str == ' ') || (*str == '0')))211++str;212213/*214* for each valid digit, shift running value (tval) over to next digit215* and add next digit216*/217if (base == HEX) {218while (str < stop) {219if ((*str >= '0') && (*str <= '9'))220tval = (tval << 4) + (*str++ - '0');221else if ((*str >= 'A') && (*str <= 'F'))222tval = (tval << 4) + 10 + (*str++ - 'A');223else if ((*str >= 'a') && (*str <= 'f'))224tval = (tval << 4) + 10 + (*str++ - 'a');225else226break;227}228} else {229while ((str < stop) && (*str >= '0') && (*str <= '7'))230tval = (tval << 3) + (*str++ - '0');231}232return(tval);233}234235/*236* ul_asc()237* convert an unsigned long into an hex/oct ascii string. pads with LEADING238* ascii 0's to fill string completely239* NOTE: the string created is NOT TERMINATED.240*/241242int243ul_asc(u_long val, char *str, int len, int base)244{245char *pt;246u_long digit;247248/*249* WARNING str is not '\0' terminated by this routine250*/251pt = str + len - 1;252253/*254* do a tailwise conversion (start at right most end of string to place255* least significant digit). Keep shifting until conversion value goes256* to zero (all digits were converted)257*/258if (base == HEX) {259while (pt >= str) {260if ((digit = (val & 0xf)) < 10)261*pt-- = '0' + (char)digit;262else263*pt-- = 'a' + (char)(digit - 10);264if ((val = (val >> 4)) == (u_long)0)265break;266}267} else {268while (pt >= str) {269*pt-- = '0' + (char)(val & 0x7);270if ((val = (val >> 3)) == (u_long)0)271break;272}273}274275/*276* pad with leading ascii ZEROS. We return -1 if we ran out of space.277*/278while (pt >= str)279*pt-- = '0';280if (val != (u_long)0)281return(-1);282return(0);283}284285/*286* asc_uqd()287* convert hex/octal character string into a u_quad_t. We do not have to288* check for overflow! (the headers in all supported formats are not large289* enough to create an overflow).290* NOTE: strings passed to us are NOT TERMINATED.291* Return:292* u_quad_t value293*/294295u_quad_t296asc_uqd(char *str, int len, int base)297{298char *stop;299u_quad_t tval = 0;300301stop = str + len;302303/*304* skip over leading blanks and zeros305*/306while ((str < stop) && ((*str == ' ') || (*str == '0')))307++str;308309/*310* for each valid digit, shift running value (tval) over to next digit311* and add next digit312*/313if (base == HEX) {314while (str < stop) {315if ((*str >= '0') && (*str <= '9'))316tval = (tval << 4) + (*str++ - '0');317else if ((*str >= 'A') && (*str <= 'F'))318tval = (tval << 4) + 10 + (*str++ - 'A');319else if ((*str >= 'a') && (*str <= 'f'))320tval = (tval << 4) + 10 + (*str++ - 'a');321else322break;323}324} else {325while ((str < stop) && (*str >= '0') && (*str <= '7'))326tval = (tval << 3) + (*str++ - '0');327}328return(tval);329}330331/*332* uqd_asc()333* convert an u_quad_t into a hex/oct ascii string. pads with LEADING334* ascii 0's to fill string completely335* NOTE: the string created is NOT TERMINATED.336*/337338int339uqd_asc(u_quad_t val, char *str, int len, int base)340{341char *pt;342u_quad_t digit;343344/*345* WARNING str is not '\0' terminated by this routine346*/347pt = str + len - 1;348349/*350* do a tailwise conversion (start at right most end of string to place351* least significant digit). Keep shifting until conversion value goes352* to zero (all digits were converted)353*/354if (base == HEX) {355while (pt >= str) {356if ((digit = (val & 0xf)) < 10)357*pt-- = '0' + (char)digit;358else359*pt-- = 'a' + (char)(digit - 10);360if ((val = (val >> 4)) == (u_quad_t)0)361break;362}363} else {364while (pt >= str) {365*pt-- = '0' + (char)(val & 0x7);366if ((val = (val >> 3)) == (u_quad_t)0)367break;368}369}370371/*372* pad with leading ascii ZEROS. We return -1 if we ran out of space.373*/374while (pt >= str)375*pt-- = '0';376if (val != (u_quad_t)0)377return(-1);378return(0);379}380381382