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
40 static char sccsid[] = "@(#)gen_subs.c 8.1 (Berkeley) 5/31/93";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/types.h>
60 * a collection of general purpose subroutines used by pax
64 * constants used by ls_list() when printing out archive members
68 #define SIXMONTHS ((365 / 2) * 86400)
69 #define CURFRMTM "%b %e %H:%M"
70 #define OLDFRMTM "%b %e %Y"
71 #define CURFRMTD "%e %b %H:%M"
72 #define OLDFRMTD "%e %b %Y"
78 static int d_first = -1;
82 * list the members of an archive in ls format
86 ls_list(ARCHD *arcn, time_t now, FILE *fp)
94 * if not verbose, just print the file name
97 (void)fprintf(fp, "%s\n", arcn->name);
103 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
105 * user wants long mode
108 strmode(sbp->st_mode, f_mode);
111 * time format based on age compared to the time pax was started.
113 if ((sbp->st_mtime + SIXMONTHS) <= now)
114 timefrmt = d_first ? OLDFRMTD : OLDFRMTM;
116 timefrmt = d_first ? CURFRMTD : CURFRMTM;
119 * print file mode, link count, uid, gid and time
121 if (strftime(f_date,DATELEN,timefrmt,localtime(&(sbp->st_mtime))) == 0)
123 (void)fprintf(fp, "%s%2u %-*s %-*s ", f_mode, sbp->st_nlink,
124 UT_NAMESIZE, name_uid(sbp->st_uid, 1), UT_GRPSIZE,
125 name_gid(sbp->st_gid, 1));
128 * print device id's for devices, or sizes for other nodes
130 if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK))
132 (void)fprintf(fp, "%4u,%4u ", MAJOR(sbp->st_rdev),
133 MINOR(sbp->st_rdev));
135 (void)fprintf(fp, "%4lu,%4lu ", (unsigned long)MAJOR(sbp->st_rdev),
136 (unsigned long)MINOR(sbp->st_rdev));
140 (void)fprintf(fp, "%9lu ", sbp->st_size);
142 (void)fprintf(fp, "%9ju ", (uintmax_t)sbp->st_size);
147 * print name and link info for hard and soft links
149 (void)fprintf(fp, "%s %s", f_date, arcn->name);
150 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
151 (void)fprintf(fp, " == %s\n", arcn->ln_name);
152 else if (arcn->type == PAX_SLK)
153 (void)fprintf(fp, " => %s\n", arcn->ln_name);
155 (void)putc('\n', fp);
162 * print a short summary of file to tty.
168 char f_date[DATELEN];
169 char f_mode[MODELEN];
173 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
175 if ((arcn->sb.st_mtime + SIXMONTHS) <= time(NULL))
176 timefrmt = d_first ? OLDFRMTD : OLDFRMTM;
178 timefrmt = d_first ? CURFRMTD : CURFRMTM;
181 * convert time to string, and print
183 if (strftime(f_date, DATELEN, timefrmt,
184 localtime(&(arcn->sb.st_mtime))) == 0)
186 strmode(arcn->sb.st_mode, f_mode);
187 tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name);
193 * copy src to dest up to len chars (stopping at first '\0').
194 * when src is shorter than len, pads to len with '\0'.
196 * number of chars copied. (Note this is a real performance win over
197 * doing a strncpy(), a strlen(), and then a possible memset())
201 l_strncpy(char *dest, char *src, int len)
208 while ((dest < stop) && (*src != '\0'))
218 * convert hex/octal character string into a u_long. We do not have to
219 * check for overflow! (the headers in all supported formats are not large
220 * enough to create an overflow).
221 * NOTE: strings passed to us are NOT TERMINATED.
223 * unsigned long value
227 asc_ul(char *str, int len, int base)
235 * skip over leading blanks and zeros
237 while ((str < stop) && ((*str == ' ') || (*str == '0')))
241 * for each valid digit, shift running value (tval) over to next digit
246 if ((*str >= '0') && (*str <= '9'))
247 tval = (tval << 4) + (*str++ - '0');
248 else if ((*str >= 'A') && (*str <= 'F'))
249 tval = (tval << 4) + 10 + (*str++ - 'A');
250 else if ((*str >= 'a') && (*str <= 'f'))
251 tval = (tval << 4) + 10 + (*str++ - 'a');
256 while ((str < stop) && (*str >= '0') && (*str <= '7'))
257 tval = (tval << 3) + (*str++ - '0');
264 * convert an unsigned long into an hex/oct ascii string. pads with LEADING
265 * ascii 0's to fill string completely
266 * NOTE: the string created is NOT TERMINATED.
270 ul_asc(u_long val, char *str, int len, int base)
276 * WARNING str is not '\0' terminated by this routine
281 * do a tailwise conversion (start at right most end of string to place
282 * least significant digit). Keep shifting until conversion value goes
283 * to zero (all digits were converted)
287 if ((digit = (val & 0xf)) < 10)
288 *pt-- = '0' + (char)digit;
290 *pt-- = 'a' + (char)(digit - 10);
291 if ((val = (val >> 4)) == (u_long)0)
296 *pt-- = '0' + (char)(val & 0x7);
297 if ((val = (val >> 3)) == (u_long)0)
303 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
307 if (val != (u_long)0)
315 * convert hex/octal character string into a u_quad_t. We do not have to
316 * check for overflow! (the headers in all supported formats are not large
317 * enough to create an overflow).
318 * NOTE: strings passed to us are NOT TERMINATED.
324 asc_uqd(char *str, int len, int base)
332 * skip over leading blanks and zeros
334 while ((str < stop) && ((*str == ' ') || (*str == '0')))
338 * for each valid digit, shift running value (tval) over to next digit
343 if ((*str >= '0') && (*str <= '9'))
344 tval = (tval << 4) + (*str++ - '0');
345 else if ((*str >= 'A') && (*str <= 'F'))
346 tval = (tval << 4) + 10 + (*str++ - 'A');
347 else if ((*str >= 'a') && (*str <= 'f'))
348 tval = (tval << 4) + 10 + (*str++ - 'a');
353 while ((str < stop) && (*str >= '0') && (*str <= '7'))
354 tval = (tval << 3) + (*str++ - '0');
361 * convert an u_quad_t into a hex/oct ascii string. pads with LEADING
362 * ascii 0's to fill string completely
363 * NOTE: the string created is NOT TERMINATED.
367 uqd_asc(u_quad_t val, char *str, int len, int base)
373 * WARNING str is not '\0' terminated by this routine
378 * do a tailwise conversion (start at right most end of string to place
379 * least significant digit). Keep shifting until conversion value goes
380 * to zero (all digits were converted)
384 if ((digit = (val & 0xf)) < 10)
385 *pt-- = '0' + (char)digit;
387 *pt-- = 'a' + (char)(digit - 10);
388 if ((val = (val >> 4)) == (u_quad_t)0)
393 *pt-- = '0' + (char)(val & 0x7);
394 if ((val = (val >> 3)) == (u_quad_t)0)
400 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
404 if (val != (u_quad_t)0)