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. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)gen_subs.c 8.1 (Berkeley) 5/31/93";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/types.h>
53 * a collection of general purpose subroutines used by pax
57 * constants used by ls_list() when printing out archive members
61 #define SIXMONTHS ((365 / 2) * 86400)
62 #define CURFRMTM "%b %e %H:%M"
63 #define OLDFRMTM "%b %e %Y"
64 #define CURFRMTD "%e %b %H:%M"
65 #define OLDFRMTD "%e %b %Y"
67 static int d_first = -1;
71 * list the members of an archive in ls format
75 ls_list(ARCHD *arcn, time_t now, FILE *fp)
83 * if not verbose, just print the file name
86 (void)fprintf(fp, "%s\n", arcn->name);
92 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
94 * user wants long mode
97 strmode(sbp->st_mode, f_mode);
100 * time format based on age compared to the time pax was started.
102 if ((sbp->st_mtime + SIXMONTHS) <= now)
103 timefrmt = d_first ? OLDFRMTD : OLDFRMTM;
105 timefrmt = d_first ? CURFRMTD : CURFRMTM;
108 * print file mode, link count, uid, gid and time
110 if (strftime(f_date,DATELEN,timefrmt,localtime(&(sbp->st_mtime))) == 0)
112 (void)fprintf(fp, "%s%2ju %-12s %-12s ", f_mode,
113 (uintmax_t)sbp->st_nlink,
114 name_uid(sbp->st_uid, 1), name_gid(sbp->st_gid, 1));
117 * print device id's for devices, or sizes for other nodes
119 if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK))
121 (void)fprintf(fp, "%4u,%4u ", MAJOR(sbp->st_rdev),
122 MINOR(sbp->st_rdev));
124 (void)fprintf(fp, "%4lu,%4lu ", (unsigned long)MAJOR(sbp->st_rdev),
125 (unsigned long)MINOR(sbp->st_rdev));
129 (void)fprintf(fp, "%9lu ", sbp->st_size);
131 (void)fprintf(fp, "%9ju ", (uintmax_t)sbp->st_size);
136 * print name and link info for hard and soft links
138 (void)fprintf(fp, "%s %s", f_date, arcn->name);
139 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
140 (void)fprintf(fp, " == %s\n", arcn->ln_name);
141 else if (arcn->type == PAX_SLK)
142 (void)fprintf(fp, " => %s\n", arcn->ln_name);
144 (void)putc('\n', fp);
151 * print a short summary of file to tty.
157 char f_date[DATELEN];
158 char f_mode[MODELEN];
159 const char *timefrmt;
162 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
164 if ((arcn->sb.st_mtime + SIXMONTHS) <= time(NULL))
165 timefrmt = d_first ? OLDFRMTD : OLDFRMTM;
167 timefrmt = d_first ? CURFRMTD : CURFRMTM;
170 * convert time to string, and print
172 if (strftime(f_date, DATELEN, timefrmt,
173 localtime(&(arcn->sb.st_mtime))) == 0)
175 strmode(arcn->sb.st_mode, f_mode);
176 tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name);
182 * copy src to dest up to len chars (stopping at first '\0').
183 * when src is shorter than len, pads to len with '\0'.
185 * number of chars copied. (Note this is a real performance win over
186 * doing a strncpy(), a strlen(), and then a possible memset())
190 l_strncpy(char *dest, const char *src, int len)
197 while ((dest < stop) && (*src != '\0'))
207 * convert hex/octal character string into a u_long. We do not have to
208 * check for overflow! (the headers in all supported formats are not large
209 * enough to create an overflow).
210 * NOTE: strings passed to us are NOT TERMINATED.
212 * unsigned long value
216 asc_ul(char *str, int len, int base)
224 * skip over leading blanks and zeros
226 while ((str < stop) && ((*str == ' ') || (*str == '0')))
230 * for each valid digit, shift running value (tval) over to next digit
235 if ((*str >= '0') && (*str <= '9'))
236 tval = (tval << 4) + (*str++ - '0');
237 else if ((*str >= 'A') && (*str <= 'F'))
238 tval = (tval << 4) + 10 + (*str++ - 'A');
239 else if ((*str >= 'a') && (*str <= 'f'))
240 tval = (tval << 4) + 10 + (*str++ - 'a');
245 while ((str < stop) && (*str >= '0') && (*str <= '7'))
246 tval = (tval << 3) + (*str++ - '0');
253 * convert an unsigned long into an hex/oct ascii string. pads with LEADING
254 * ascii 0's to fill string completely
255 * NOTE: the string created is NOT TERMINATED.
259 ul_asc(u_long val, char *str, int len, int base)
265 * WARNING str is not '\0' terminated by this routine
270 * do a tailwise conversion (start at right most end of string to place
271 * least significant digit). Keep shifting until conversion value goes
272 * to zero (all digits were converted)
276 if ((digit = (val & 0xf)) < 10)
277 *pt-- = '0' + (char)digit;
279 *pt-- = 'a' + (char)(digit - 10);
280 if ((val = (val >> 4)) == (u_long)0)
285 *pt-- = '0' + (char)(val & 0x7);
286 if ((val = (val >> 3)) == (u_long)0)
292 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
296 if (val != (u_long)0)
304 * convert hex/octal character string into a u_quad_t. We do not have to
305 * check for overflow! (the headers in all supported formats are not large
306 * enough to create an overflow).
307 * NOTE: strings passed to us are NOT TERMINATED.
313 asc_uqd(char *str, int len, int base)
321 * skip over leading blanks and zeros
323 while ((str < stop) && ((*str == ' ') || (*str == '0')))
327 * for each valid digit, shift running value (tval) over to next digit
332 if ((*str >= '0') && (*str <= '9'))
333 tval = (tval << 4) + (*str++ - '0');
334 else if ((*str >= 'A') && (*str <= 'F'))
335 tval = (tval << 4) + 10 + (*str++ - 'A');
336 else if ((*str >= 'a') && (*str <= 'f'))
337 tval = (tval << 4) + 10 + (*str++ - 'a');
342 while ((str < stop) && (*str >= '0') && (*str <= '7'))
343 tval = (tval << 3) + (*str++ - '0');
350 * convert an u_quad_t into a hex/oct ascii string. pads with LEADING
351 * ascii 0's to fill string completely
352 * NOTE: the string created is NOT TERMINATED.
356 uqd_asc(u_quad_t val, char *str, int len, int base)
362 * WARNING str is not '\0' terminated by this routine
367 * do a tailwise conversion (start at right most end of string to place
368 * least significant digit). Keep shifting until conversion value goes
369 * to zero (all digits were converted)
373 if ((digit = (val & 0xf)) < 10)
374 *pt-- = '0' + (char)digit;
376 *pt-- = 'a' + (char)(digit - 10);
377 if ((val = (val >> 4)) == (u_quad_t)0)
382 *pt-- = '0' + (char)(val & 0x7);
383 if ((val = (val >> 3)) == (u_quad_t)0)
389 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
393 if (val != (u_quad_t)0)