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
33 * @(#)gen_subs.c 8.1 (Berkeley) 5/31/93
34 * $FreeBSD: src/bin/pax/gen_subs.c,v 1.12.2.4 2002/03/12 17:49:17 phantom Exp $
37 #include <sys/types.h>
51 * a collection of general purpose subroutines used by pax
55 * constants used by ls_list() when printing out archive members
59 #define SIXMONTHS ((365 / 2) * 86400)
60 #define CURFRMTM "%b %e %H:%M"
61 #define OLDFRMTM "%b %e %Y"
62 #define CURFRMTD "%e %b %H:%M"
63 #define OLDFRMTD "%e %b %Y"
65 static int d_first = -1;
69 * list the members of an archive in ls format
73 ls_list(ARCHD *arcn, time_t now, FILE *fp)
79 const char *user, *group;
82 * if not verbose, just print the file name
85 fprintf(fp, "%s\n", arcn->name);
91 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
93 * user wants long mode
96 strmode(sbp->st_mode, f_mode);
99 * time format based on age compared to the time pax was started.
101 if ((sbp->st_mtime + SIXMONTHS) <= now)
102 timefrmt = d_first ? OLDFRMTD : OLDFRMTM;
104 timefrmt = d_first ? CURFRMTD : CURFRMTM;
107 * print file mode, link count, uid, gid and time
109 if (strftime(f_date,DATELEN,timefrmt,localtime(&(sbp->st_mtime))) == 0)
111 user = user_from_uid(sbp->st_uid, 0);
112 group = group_from_gid(sbp->st_gid, 0);
113 fprintf(fp, "%s%2u %-16s %-6s ", f_mode, sbp->st_nlink,
114 user ? user : "", group ? group : "");
117 * print device id's for devices, or sizes for other nodes
119 if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK))
120 fprintf(fp, "%4lu,%4lu ", (unsigned long)MAJOR(sbp->st_rdev),
121 (unsigned long)MINOR(sbp->st_rdev));
123 fprintf(fp, "%9jd ", (intmax_t)sbp->st_size);
127 * print name and link info for hard and soft links
129 fprintf(fp, "%s %s", f_date, arcn->name);
130 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
131 fprintf(fp, " == %s\n", arcn->ln_name);
132 else if (arcn->type == PAX_SLK)
133 fprintf(fp, " => %s\n", arcn->ln_name);
142 * print a short summary of file to tty.
148 char f_date[DATELEN];
149 char f_mode[MODELEN];
153 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
155 if ((arcn->sb.st_mtime + SIXMONTHS) <= time(NULL))
156 timefrmt = d_first ? OLDFRMTD : OLDFRMTM;
158 timefrmt = d_first ? CURFRMTD : CURFRMTM;
161 * convert time to string, and print
163 if (strftime(f_date, DATELEN, timefrmt,
164 localtime(&(arcn->sb.st_mtime))) == 0)
166 strmode(arcn->sb.st_mode, f_mode);
167 tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name);
173 * copy src to dest up to len chars (stopping at first '\0').
174 * when src is shorter than len, pads to len with '\0'.
176 * number of chars copied. (Note this is a real performance win over
177 * doing a strncpy(), a strlen(), and then a possible memset())
181 l_strncpy(char *dest, const char *src, int len)
188 while ((dest < stop) && (*src != '\0'))
198 * convert hex/octal character string into a u_long. We do not have to
199 * check for overflow! (the headers in all supported formats are not large
200 * enough to create an overflow).
201 * NOTE: strings passed to us are NOT TERMINATED.
203 * unsigned long value
207 asc_ul(char *str, int len, int base)
215 * skip over leading blanks and zeros
217 while ((str < stop) && ((*str == ' ') || (*str == '0')))
221 * for each valid digit, shift running value (tval) over to next digit
226 if ((*str >= '0') && (*str <= '9'))
227 tval = (tval << 4) + (*str++ - '0');
228 else if ((*str >= 'A') && (*str <= 'F'))
229 tval = (tval << 4) + 10 + (*str++ - 'A');
230 else if ((*str >= 'a') && (*str <= 'f'))
231 tval = (tval << 4) + 10 + (*str++ - 'a');
236 while ((str < stop) && (*str >= '0') && (*str <= '7'))
237 tval = (tval << 3) + (*str++ - '0');
244 * convert an unsigned long into an hex/oct ascii string. pads with LEADING
245 * ascii 0's to fill string completely
246 * NOTE: the string created is NOT TERMINATED.
250 ul_asc(u_long val, char *str, int len, int base)
256 * WARNING str is not '\0' terminated by this routine
261 * do a tailwise conversion (start at right most end of string to place
262 * least significant digit). Keep shifting until conversion value goes
263 * to zero (all digits were converted)
267 if ((digit = (val & 0xf)) < 10)
268 *pt-- = '0' + (char)digit;
270 *pt-- = 'a' + (char)(digit - 10);
271 if ((val = (val >> 4)) == (u_long)0)
276 *pt-- = '0' + (char)(val & 0x7);
277 if ((val = (val >> 3)) == (u_long)0)
283 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
287 if (val != (u_long)0)
294 * convert hex/octal character string into a u_quad_t. We do not have to
295 * check for overflow! (the headers in all supported formats are not large
296 * enough to create an overflow).
297 * NOTE: strings passed to us are NOT TERMINATED.
303 asc_uqd(char *str, int len, int base)
311 * skip over leading blanks and zeros
313 while ((str < stop) && ((*str == ' ') || (*str == '0')))
317 * for each valid digit, shift running value (tval) over to next digit
322 if ((*str >= '0') && (*str <= '9'))
323 tval = (tval << 4) + (*str++ - '0');
324 else if ((*str >= 'A') && (*str <= 'F'))
325 tval = (tval << 4) + 10 + (*str++ - 'A');
326 else if ((*str >= 'a') && (*str <= 'f'))
327 tval = (tval << 4) + 10 + (*str++ - 'a');
332 while ((str < stop) && (*str >= '0') && (*str <= '7'))
333 tval = (tval << 3) + (*str++ - '0');
340 * convert an u_quad_t into a hex/oct ascii string. pads with LEADING
341 * ascii 0's to fill string completely
342 * NOTE: the string created is NOT TERMINATED.
346 uqd_asc(u_quad_t val, char *str, int len, int base)
352 * WARNING str is not '\0' terminated by this routine
357 * do a tailwise conversion (start at right most end of string to place
358 * least significant digit). Keep shifting until conversion value goes
359 * to zero (all digits were converted)
363 if ((digit = (val & 0xf)) < 10)
364 *pt-- = '0' + (char)digit;
366 *pt-- = 'a' + (char)(digit - 10);
367 if ((val = (val >> 4)) == (u_quad_t)0)
372 *pt-- = '0' + (char)(val & 0x7);
373 if ((val = (val >> 3)) == (u_quad_t)0)
379 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
383 if (val != (u_quad_t)0)