2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1992 Keith Muller.
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Keith Muller of the University of California, San Diego.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
36 #include <sys/types.h>
47 * a collection of general purpose subroutines used by pax
51 * constants used by ls_list() when printing out archive members
55 #define SIXMONTHS ((365 / 2) * 86400)
56 #define CURFRMTM "%b %e %H:%M"
57 #define OLDFRMTM "%b %e %Y"
58 #define CURFRMTD "%e %b %H:%M"
59 #define OLDFRMTD "%e %b %Y"
61 static int d_first = -1;
65 * list the members of an archive in ls format
69 ls_list(ARCHD *arcn, time_t now, FILE *fp)
77 * if not verbose, just print the file name
80 (void)fprintf(fp, "%s\n", arcn->name);
86 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
88 * user wants long mode
91 strmode(sbp->st_mode, f_mode);
94 * time format based on age compared to the time pax was started.
96 if ((sbp->st_mtime + SIXMONTHS) <= now)
97 timefrmt = d_first ? OLDFRMTD : OLDFRMTM;
99 timefrmt = d_first ? CURFRMTD : CURFRMTM;
102 * print file mode, link count, uid, gid and time
104 if (strftime(f_date,DATELEN,timefrmt,localtime(&(sbp->st_mtime))) == 0)
106 (void)fprintf(fp, "%s%2ju %-12s %-12s ", f_mode,
107 (uintmax_t)sbp->st_nlink,
108 name_uid(sbp->st_uid, 1), name_gid(sbp->st_gid, 1));
111 * print device id's for devices, or sizes for other nodes
113 if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK))
114 (void)fprintf(fp, "%4lu,%4lu ", (unsigned long)MAJOR(sbp->st_rdev),
115 (unsigned long)MINOR(sbp->st_rdev));
117 (void)fprintf(fp, "%9ju ", (uintmax_t)sbp->st_size);
121 * print name and link info for hard and soft links
123 (void)fprintf(fp, "%s %s", f_date, arcn->name);
124 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
125 (void)fprintf(fp, " == %s\n", arcn->ln_name);
126 else if (arcn->type == PAX_SLK)
127 (void)fprintf(fp, " => %s\n", arcn->ln_name);
129 (void)putc('\n', fp);
136 * print a short summary of file to tty.
142 char f_date[DATELEN];
143 char f_mode[MODELEN];
144 const char *timefrmt;
147 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
149 if ((arcn->sb.st_mtime + SIXMONTHS) <= time(NULL))
150 timefrmt = d_first ? OLDFRMTD : OLDFRMTM;
152 timefrmt = d_first ? CURFRMTD : CURFRMTM;
155 * convert time to string, and print
157 if (strftime(f_date, DATELEN, timefrmt,
158 localtime(&(arcn->sb.st_mtime))) == 0)
160 strmode(arcn->sb.st_mode, f_mode);
161 tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name);
167 * copy src to dest up to len chars (stopping at first '\0').
168 * when src is shorter than len, pads to len with '\0'.
170 * number of chars copied. (Note this is a real performance win over
171 * doing a strncpy(), a strlen(), and then a possible memset())
175 l_strncpy(char *dest, const char *src, int len)
182 while ((dest < stop) && (*src != '\0'))
192 * convert hex/octal character string into a u_long. We do not have to
193 * check for overflow! (the headers in all supported formats are not large
194 * enough to create an overflow).
195 * NOTE: strings passed to us are NOT TERMINATED.
197 * unsigned long value
201 asc_ul(char *str, int len, int base)
209 * skip over leading blanks and zeros
211 while ((str < stop) && ((*str == ' ') || (*str == '0')))
215 * for each valid digit, shift running value (tval) over to next digit
220 if ((*str >= '0') && (*str <= '9'))
221 tval = (tval << 4) + (*str++ - '0');
222 else if ((*str >= 'A') && (*str <= 'F'))
223 tval = (tval << 4) + 10 + (*str++ - 'A');
224 else if ((*str >= 'a') && (*str <= 'f'))
225 tval = (tval << 4) + 10 + (*str++ - 'a');
230 while ((str < stop) && (*str >= '0') && (*str <= '7'))
231 tval = (tval << 3) + (*str++ - '0');
238 * convert an unsigned long into an hex/oct ascii string. pads with LEADING
239 * ascii 0's to fill string completely
240 * NOTE: the string created is NOT TERMINATED.
244 ul_asc(u_long val, char *str, int len, int base)
250 * WARNING str is not '\0' terminated by this routine
255 * do a tailwise conversion (start at right most end of string to place
256 * least significant digit). Keep shifting until conversion value goes
257 * to zero (all digits were converted)
261 if ((digit = (val & 0xf)) < 10)
262 *pt-- = '0' + (char)digit;
264 *pt-- = 'a' + (char)(digit - 10);
265 if ((val = (val >> 4)) == (u_long)0)
270 *pt-- = '0' + (char)(val & 0x7);
271 if ((val = (val >> 3)) == (u_long)0)
277 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
281 if (val != (u_long)0)
288 * convert hex/octal character string into a u_quad_t. We do not have to
289 * check for overflow! (the headers in all supported formats are not large
290 * enough to create an overflow).
291 * NOTE: strings passed to us are NOT TERMINATED.
297 asc_uqd(char *str, int len, int base)
305 * skip over leading blanks and zeros
307 while ((str < stop) && ((*str == ' ') || (*str == '0')))
311 * for each valid digit, shift running value (tval) over to next digit
316 if ((*str >= '0') && (*str <= '9'))
317 tval = (tval << 4) + (*str++ - '0');
318 else if ((*str >= 'A') && (*str <= 'F'))
319 tval = (tval << 4) + 10 + (*str++ - 'A');
320 else if ((*str >= 'a') && (*str <= 'f'))
321 tval = (tval << 4) + 10 + (*str++ - 'a');
326 while ((str < stop) && (*str >= '0') && (*str <= '7'))
327 tval = (tval << 3) + (*str++ - '0');
334 * convert an u_quad_t into a hex/oct ascii string. pads with LEADING
335 * ascii 0's to fill string completely
336 * NOTE: the string created is NOT TERMINATED.
340 uqd_asc(u_quad_t val, char *str, int len, int base)
346 * WARNING str is not '\0' terminated by this routine
351 * do a tailwise conversion (start at right most end of string to place
352 * least significant digit). Keep shifting until conversion value goes
353 * to zero (all digits were converted)
357 if ((digit = (val & 0xf)) < 10)
358 *pt-- = '0' + (char)digit;
360 *pt-- = 'a' + (char)(digit - 10);
361 if ((val = (val >> 4)) == (u_quad_t)0)
366 *pt-- = '0' + (char)(val & 0x7);
367 if ((val = (val >> 3)) == (u_quad_t)0)
373 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
377 if (val != (u_quad_t)0)