1 /* $OpenBSD: gen_subs.c,v 1.8 1997/09/01 18:29:51 deraadt Exp $ */
2 /* $NetBSD: gen_subs.c,v 1.5 1995/03/21 09:07:26 cgd Exp $ */
5 * Copyright (c) 1992 Keith Muller.
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * Keith Muller of the University of California, San Diego.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 static char sccsid[] = "@(#)gen_subs.c 8.1 (Berkeley) 5/31/93";
45 static char rcsid[] = "$OpenBSD: gen_subs.c,v 1.8 1997/09/01 18:29:51 deraadt Exp $";
49 #include <sys/types.h>
50 #include <sys/sysmacros.h>
54 #include <sys/param.h>
67 * a collection of general purpose subroutines used by pax
71 * constants used by ls_list() when printing out archive members
75 #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
76 #define CURFRMT "%b %e %H:%M"
77 #define OLDFRMT "%b %e %Y"
85 * list the members of an archive in ls format
90 ls_list(register ARCHD *arcn, time_t now, FILE *fp)
93 ls_list(arcn, now, fp)
99 register struct stat *sbp;
100 char f_mode[MODELEN];
101 char f_date[DATELEN];
105 * if not verbose, just print the file name
108 (void)fprintf(fp, "%s\n", arcn->name);
114 * user wants long mode
117 strmode(sbp->st_mode, f_mode);
119 if (ltmfrmt == NULL) {
121 * no locale specified format. time format based on age
122 * compared to the time pax was started.
124 if ((sbp->st_mtime + SIXMONTHS) <= now)
132 * print file mode, link count, uid, gid and time
134 if (strftime(f_date,DATELEN,timefrmt,localtime(&(sbp->st_mtime))) == 0)
136 (void)fprintf(fp, "%s%2u %-*s %-*s ", f_mode, sbp->st_nlink,
137 UT_NAMESIZE, name_uid(sbp->st_uid, 1), UT_GRPSIZE,
138 name_gid(sbp->st_gid, 1));
141 * print device id's for devices, or sizes for other nodes
143 if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK))
145 (void)fprintf(fp, "%4u,%4u ", MAJOR(sbp->st_rdev),
147 (void)fprintf(fp, "%4lu,%4lu ", (unsigned long)MAJOR(sbp->st_rdev),
149 (unsigned long)MINOR(sbp->st_rdev));
152 (void)fprintf(fp, "%9lu ", sbp->st_size);
154 (void)fprintf(fp, "%9qu ", sbp->st_size);
159 * print name and link info for hard and soft links
161 (void)fprintf(fp, "%s %s", f_date, arcn->name);
162 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
163 (void)fprintf(fp, " == %s\n", arcn->ln_name);
164 else if (arcn->type == PAX_SLK)
165 (void)fprintf(fp, " => %s\n", arcn->ln_name);
167 (void)putc('\n', fp);
174 * print a short summary of file to tty.
179 ls_tty(register ARCHD *arcn)
183 register ARCHD *arcn;
186 char f_date[DATELEN];
187 char f_mode[MODELEN];
190 if (ltmfrmt == NULL) {
192 * no locale specified format
194 if ((arcn->sb.st_mtime + SIXMONTHS) <= time(NULL))
202 * convert time to string, and print
204 if (strftime(f_date, DATELEN, timefrmt,
205 localtime(&(arcn->sb.st_mtime))) == 0)
207 strmode(arcn->sb.st_mode, f_mode);
208 tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name);
214 * copy src to dest up to len chars (stopping at first '\0').
215 * when src is shorter than len, pads to len with '\0'.
217 * number of chars copied. (Note this is a real performance win over
218 * doing a strncpy(), a strlen(), and then a possible memset())
223 l_strncpy(register char *dest, register char *src, int len)
226 l_strncpy(dest, src, len)
233 register char *start;
237 while ((dest < stop) && (*src != '\0'))
247 * convert hex/octal character string into a u_long. We do not have to
248 * check for overflow! (the headers in all supported formats are not large
249 * enough to create an overflow).
250 * NOTE: strings passed to us are NOT TERMINATED.
252 * unsigned long value
257 asc_ul(register char *str, int len, register int base)
260 asc_ul(str, len, base)
272 * skip over leading blanks and zeros
274 while ((str < stop) && ((*str == ' ') || (*str == '0')))
278 * for each valid digit, shift running value (tval) over to next digit
283 if ((*str >= '0') && (*str <= '9'))
284 tval = (tval << 4) + (*str++ - '0');
285 else if ((*str >= 'A') && (*str <= 'F'))
286 tval = (tval << 4) + 10 + (*str++ - 'A');
287 else if ((*str >= 'a') && (*str <= 'f'))
288 tval = (tval << 4) + 10 + (*str++ - 'a');
293 while ((str < stop) && (*str >= '0') && (*str <= '7'))
294 tval = (tval << 3) + (*str++ - '0');
301 * convert an unsigned long into an hex/oct ascii string. pads with LEADING
302 * ascii 0's to fill string completely
303 * NOTE: the string created is NOT TERMINATED.
308 ul_asc(u_long val, register char *str, register int len, register int base)
311 ul_asc(val, str, len, base)
322 * WARNING str is not '\0' terminated by this routine
327 * do a tailwise conversion (start at right most end of string to place
328 * least significant digit). Keep shifting until conversion value goes
329 * to zero (all digits were converted)
333 if ((digit = (val & 0xf)) < 10)
334 *pt-- = '0' + (char)digit;
336 *pt-- = 'a' + (char)(digit - 10);
337 if ((val = (val >> 4)) == (u_long)0)
342 *pt-- = '0' + (char)(val & 0x7);
343 if ((val = (val >> 3)) == (u_long)0)
349 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
353 if (val != (u_long)0)
361 * convert hex/octal character string into a u_quad_t. We do not have to
362 * check for overflow! (the headers in all supported formats are not large
363 * enough to create an overflow).
364 * NOTE: strings passed to us are NOT TERMINATED.
371 asc_uqd(register char *str, int len, register int base)
374 asc_uqd(str, len, base)
386 * skip over leading blanks and zeros
388 while ((str < stop) && ((*str == ' ') || (*str == '0')))
392 * for each valid digit, shift running value (tval) over to next digit
397 if ((*str >= '0') && (*str <= '9'))
398 tval = (tval << 4) + (*str++ - '0');
399 else if ((*str >= 'A') && (*str <= 'F'))
400 tval = (tval << 4) + 10 + (*str++ - 'A');
401 else if ((*str >= 'a') && (*str <= 'f'))
402 tval = (tval << 4) + 10 + (*str++ - 'a');
407 while ((str < stop) && (*str >= '0') && (*str <= '7'))
408 tval = (tval << 3) + (*str++ - '0');
415 * convert an u_quad_t into a hex/oct ascii string. pads with LEADING
416 * ascii 0's to fill string completely
417 * NOTE: the string created is NOT TERMINATED.
422 uqd_asc(u_quad_t val, register char *str, register int len, register int base)
425 uqd_asc(val, str, len, base)
436 * WARNING str is not '\0' terminated by this routine
441 * do a tailwise conversion (start at right most end of string to place
442 * least significant digit). Keep shifting until conversion value goes
443 * to zero (all digits were converted)
447 if ((digit = (val & 0xf)) < 10)
448 *pt-- = '0' + (char)digit;
450 *pt-- = 'a' + (char)(digit - 10);
451 if ((val = (val >> 4)) == (u_quad_t)0)
456 *pt-- = '0' + (char)(val & 0x7);
457 if ((val = (val >> 3)) == (u_quad_t)0)
463 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
467 if (val != (u_quad_t)0)