2 * Copyright (c) 1992 Keith Muller.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Keith Muller of the University of California, San Diego.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)gen_subs.c 8.1 (Berkeley) 5/31/93
38 * $FreeBSD: src/bin/pax/gen_subs.c,v 1.12.2.4 2002/03/12 17:49:17 phantom Exp $
39 * $DragonFly: src/bin/pax/gen_subs.c,v 1.2 2003/06/17 04:22:50 dillon Exp $
42 #include <sys/types.h>
55 * a collection of general purpose subroutines used by pax
59 * constants used by ls_list() when printing out archive members
63 #define SIXMONTHS ((365 / 2) * 86400)
64 #define CURFRMTM "%b %e %H:%M"
65 #define OLDFRMTM "%b %e %Y"
66 #define CURFRMTD "%e %b %H:%M"
67 #define OLDFRMTD "%e %b %Y"
73 static int d_first = -1;
77 * list the members of an archive in ls format
82 ls_list(register ARCHD *arcn, time_t now, FILE *fp)
85 ls_list(arcn, now, fp)
91 register struct stat *sbp;
97 * if not verbose, just print the file name
100 (void)fprintf(fp, "%s\n", arcn->name);
106 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
108 * user wants long mode
111 strmode(sbp->st_mode, f_mode);
114 * time format based on age compared to the time pax was started.
116 if ((sbp->st_mtime + SIXMONTHS) <= now)
117 timefrmt = d_first ? OLDFRMTD : OLDFRMTM;
119 timefrmt = d_first ? CURFRMTD : CURFRMTM;
122 * print file mode, link count, uid, gid and time
124 if (strftime(f_date,DATELEN,timefrmt,localtime(&(sbp->st_mtime))) == 0)
126 (void)fprintf(fp, "%s%2u %-*s %-*s ", f_mode, sbp->st_nlink,
127 UT_NAMESIZE, name_uid(sbp->st_uid, 1), UT_GRPSIZE,
128 name_gid(sbp->st_gid, 1));
131 * print device id's for devices, or sizes for other nodes
133 if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK))
135 (void)fprintf(fp, "%4u,%4u ", MAJOR(sbp->st_rdev),
136 MINOR(sbp->st_rdev));
138 (void)fprintf(fp, "%4lu,%4lu ", (unsigned long)MAJOR(sbp->st_rdev),
139 (unsigned long)MINOR(sbp->st_rdev));
143 (void)fprintf(fp, "%9lu ", sbp->st_size);
145 (void)fprintf(fp, "%9qu ", sbp->st_size);
150 * print name and link info for hard and soft links
152 (void)fprintf(fp, "%s %s", f_date, arcn->name);
153 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
154 (void)fprintf(fp, " == %s\n", arcn->ln_name);
155 else if (arcn->type == PAX_SLK)
156 (void)fprintf(fp, " => %s\n", arcn->ln_name);
158 (void)putc('\n', fp);
165 * print a short summary of file to tty.
170 ls_tty(register ARCHD *arcn)
174 register ARCHD *arcn;
177 char f_date[DATELEN];
178 char f_mode[MODELEN];
182 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
184 if ((arcn->sb.st_mtime + SIXMONTHS) <= time((time_t *)NULL))
185 timefrmt = d_first ? OLDFRMTD : OLDFRMTM;
187 timefrmt = d_first ? CURFRMTD : CURFRMTM;
190 * convert time to string, and print
192 if (strftime(f_date, DATELEN, timefrmt,
193 localtime(&(arcn->sb.st_mtime))) == 0)
195 strmode(arcn->sb.st_mode, f_mode);
196 tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name);
202 * copy src to dest up to len chars (stopping at first '\0').
203 * when src is shorter than len, pads to len with '\0'.
205 * number of chars copied. (Note this is a real performance win over
206 * doing a strncpy(), a strlen(), and then a possible memset())
211 l_strncpy(register char *dest, register char *src, int len)
214 l_strncpy(dest, src, len)
221 register char *start;
225 while ((dest < stop) && (*src != '\0'))
235 * convert hex/octal character string into a u_long. We do not have to
236 * check for overflow! (the headers in all supported formats are not large
237 * enough to create an overflow).
238 * NOTE: strings passed to us are NOT TERMINATED.
240 * unsigned long value
245 asc_ul(register char *str, int len, register int base)
248 asc_ul(str, len, base)
260 * skip over leading blanks and zeros
262 while ((str < stop) && ((*str == ' ') || (*str == '0')))
266 * for each valid digit, shift running value (tval) over to next digit
271 if ((*str >= '0') && (*str <= '9'))
272 tval = (tval << 4) + (*str++ - '0');
273 else if ((*str >= 'A') && (*str <= 'F'))
274 tval = (tval << 4) + 10 + (*str++ - 'A');
275 else if ((*str >= 'a') && (*str <= 'f'))
276 tval = (tval << 4) + 10 + (*str++ - 'a');
281 while ((str < stop) && (*str >= '0') && (*str <= '7'))
282 tval = (tval << 3) + (*str++ - '0');
289 * convert an unsigned long into an hex/oct ascii string. pads with LEADING
290 * ascii 0's to fill string completely
291 * NOTE: the string created is NOT TERMINATED.
296 ul_asc(u_long val, register char *str, register int len, register int base)
299 ul_asc(val, str, len, base)
310 * WARNING str is not '\0' terminated by this routine
315 * do a tailwise conversion (start at right most end of string to place
316 * least significant digit). Keep shifting until conversion value goes
317 * to zero (all digits were converted)
321 if ((digit = (val & 0xf)) < 10)
322 *pt-- = '0' + (char)digit;
324 *pt-- = 'a' + (char)(digit - 10);
325 if ((val = (val >> 4)) == (u_long)0)
330 *pt-- = '0' + (char)(val & 0x7);
331 if ((val = (val >> 3)) == (u_long)0)
337 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
341 if (val != (u_long)0)
349 * convert hex/octal character string into a u_quad_t. We do not have to
350 * check for overflow! (the headers in all supported formats are not large
351 * enough to create an overflow).
352 * NOTE: strings passed to us are NOT TERMINATED.
359 asc_uqd(register char *str, int len, register int base)
362 asc_uqd(str, len, base)
374 * skip over leading blanks and zeros
376 while ((str < stop) && ((*str == ' ') || (*str == '0')))
380 * for each valid digit, shift running value (tval) over to next digit
385 if ((*str >= '0') && (*str <= '9'))
386 tval = (tval << 4) + (*str++ - '0');
387 else if ((*str >= 'A') && (*str <= 'F'))
388 tval = (tval << 4) + 10 + (*str++ - 'A');
389 else if ((*str >= 'a') && (*str <= 'f'))
390 tval = (tval << 4) + 10 + (*str++ - 'a');
395 while ((str < stop) && (*str >= '0') && (*str <= '7'))
396 tval = (tval << 3) + (*str++ - '0');
403 * convert an u_quad_t into a hex/oct ascii string. pads with LEADING
404 * ascii 0's to fill string completely
405 * NOTE: the string created is NOT TERMINATED.
410 uqd_asc(u_quad_t val, register char *str, register int len, register int base)
413 uqd_asc(val, str, len, base)
424 * WARNING str is not '\0' terminated by this routine
429 * do a tailwise conversion (start at right most end of string to place
430 * least significant digit). Keep shifting until conversion value goes
431 * to zero (all digits were converted)
435 if ((digit = (val & 0xf)) < 10)
436 *pt-- = '0' + (char)digit;
438 *pt-- = 'a' + (char)(digit - 10);
439 if ((val = (val >> 4)) == (u_quad_t)0)
444 *pt-- = '0' + (char)(val & 0x7);
445 if ((val = (val >> 3)) == (u_quad_t)0)
451 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
455 if (val != (u_quad_t)0)