1 /* $OpenBSD: gen_subs.c,v 1.19 2007/04/04 21:55:10 millert 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. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static const char sccsid[] = "@(#)gen_subs.c 8.1 (Berkeley) 5/31/93";
41 static const char rcsid[] = "$OpenBSD: gen_subs.c,v 1.19 2007/04/04 21:55:10 millert Exp $";
45 #include <sys/types.h>
48 #include <sys/param.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 ((DAYSPERNYEAR / 2) * SECSPERDAY)
69 #define CURFRMT "%b %e %H:%M"
70 #define OLDFRMT "%b %e %Y"
75 * list the members of an archive in ls format
79 ls_list(ARCHD *arcn, time_t now, FILE *fp)
87 term = zeroflag ? '\0' : '\n'; /* path termination character */
90 * if not verbose, just print the file name
94 (void)fputs(arcn->name, fp);
96 safe_print(arcn->name, fp);
103 * user wants long mode
106 strmode(sbp->st_mode, f_mode);
108 if (ltmfrmt == NULL) {
110 * no locale specified format. time format based on age
111 * compared to the time pax was started.
113 if ((sbp->st_mtime + SIXMONTHS) <= now)
121 * print file mode, link count, uid, gid and time
123 if (strftime(f_date,DATELEN,timefrmt,localtime(&(sbp->st_mtime))) == 0)
125 (void)fprintf(fp, "%s%2u %-*.*s %-*.*s ", f_mode, sbp->st_nlink,
126 NAME_WIDTH, UT_NAMESIZE, name_uid(sbp->st_uid, 1),
127 NAME_WIDTH, UT_NAMESIZE, name_gid(sbp->st_gid, 1));
130 * print device id's for devices, or sizes for other nodes
132 if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK))
134 (void)fprintf(fp, "%4u,%4u ", MAJOR(sbp->st_rdev),
136 (void)fprintf(fp, "%4lu,%4lu ", (unsigned long)MAJOR(sbp->st_rdev),
138 (unsigned long)MINOR(sbp->st_rdev));
141 (void)fprintf(fp, "%9lu ", sbp->st_size);
143 (void)fprintf(fp, "%9qu ", sbp->st_size);
148 * print name and link info for hard and soft links
150 (void)fputs(f_date, fp);
152 safe_print(arcn->name, fp);
153 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG)) {
155 safe_print(arcn->ln_name, fp);
156 } else if (arcn->type == PAX_SLK) {
158 safe_print(arcn->ln_name, fp);
160 (void)putc(term, fp);
167 * print a short summary of file to tty.
173 char f_date[DATELEN];
174 char f_mode[MODELEN];
175 const char *timefrmt;
177 if (ltmfrmt == NULL) {
179 * no locale specified format
181 if ((arcn->sb.st_mtime + SIXMONTHS) <= time(NULL))
189 * convert time to string, and print
191 if (strftime(f_date, DATELEN, timefrmt,
192 localtime(&(arcn->sb.st_mtime))) == 0)
194 strmode(arcn->sb.st_mode, f_mode);
195 tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name);
200 safe_print(const char *str, FILE *fp)
206 * if printing to a tty, use vis(3) to print special characters.
208 if (isatty(fileno(fp))) {
209 for (cp = str; *cp; cp++) {
210 (void)vis(visbuf, cp[0], VIS_CSTYLE, cp[1]);
211 (void)fputs(visbuf, fp);
214 (void)fputs(str, fp);
220 * convert hex/octal character string into a u_long. We do not have to
221 * check for overflow! (the headers in all supported formats are not large
222 * enough to create an overflow).
223 * NOTE: strings passed to us are NOT TERMINATED.
225 * unsigned long value
229 asc_ul(char *str, int len, int base)
237 * skip over leading blanks and zeros
239 while ((str < stop) && ((*str == ' ') || (*str == '0')))
243 * for each valid digit, shift running value (tval) over to next digit
248 if ((*str >= '0') && (*str <= '9'))
249 tval = (tval << 4) + (*str++ - '0');
250 else if ((*str >= 'A') && (*str <= 'F'))
251 tval = (tval << 4) + 10 + (*str++ - 'A');
252 else if ((*str >= 'a') && (*str <= 'f'))
253 tval = (tval << 4) + 10 + (*str++ - 'a');
258 while ((str < stop) && (*str >= '0') && (*str <= '7'))
259 tval = (tval << 3) + (*str++ - '0');
266 * convert an unsigned long into an hex/oct ascii string. pads with LEADING
267 * ascii 0's to fill string completely
268 * NOTE: the string created is NOT TERMINATED.
272 ul_asc(u_long val, char *str, int len, int base)
278 * WARNING str is not '\0' terminated by this routine
283 * do a tailwise conversion (start at right most end of string to place
284 * least significant digit). Keep shifting until conversion value goes
285 * to zero (all digits were converted)
289 if ((digit = (val & 0xf)) < 10)
290 *pt-- = '0' + (char)digit;
292 *pt-- = 'a' + (char)(digit - 10);
293 if ((val = (val >> 4)) == (u_long)0)
298 *pt-- = '0' + (char)(val & 0x7);
299 if ((val = (val >> 3)) == (u_long)0)
305 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
309 if (val != (u_long)0)
317 * convert hex/octal character string into a u_quad_t. We do not have to
318 * check for overflow! (the headers in all supported formats are not large
319 * enough to create an overflow).
320 * NOTE: strings passed to us are NOT TERMINATED.
326 asc_uqd(char *str, int len, int base)
334 * skip over leading blanks and zeros
336 while ((str < stop) && ((*str == ' ') || (*str == '0')))
340 * for each valid digit, shift running value (tval) over to next digit
345 if ((*str >= '0') && (*str <= '9'))
346 tval = (tval << 4) + (*str++ - '0');
347 else if ((*str >= 'A') && (*str <= 'F'))
348 tval = (tval << 4) + 10 + (*str++ - 'A');
349 else if ((*str >= 'a') && (*str <= 'f'))
350 tval = (tval << 4) + 10 + (*str++ - 'a');
355 while ((str < stop) && (*str >= '0') && (*str <= '7'))
356 tval = (tval << 3) + (*str++ - '0');
363 * convert an u_quad_t into a hex/oct ascii string. pads with LEADING
364 * ascii 0's to fill string completely
365 * NOTE: the string created is NOT TERMINATED.
369 uqd_asc(u_quad_t val, char *str, int len, int base)
375 * WARNING str is not '\0' terminated by this routine
380 * do a tailwise conversion (start at right most end of string to place
381 * least significant digit). Keep shifting until conversion value goes
382 * to zero (all digits were converted)
386 if ((digit = (val & 0xf)) < 10)
387 *pt-- = '0' + (char)digit;
389 *pt-- = 'a' + (char)(digit - 10);
390 if ((val = (val >> 4)) == (u_quad_t)0)
395 *pt-- = '0' + (char)(val & 0x7);
396 if ((val = (val >> 3)) == (u_quad_t)0)
402 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
406 if (val != (u_quad_t)0)
413 * Copy at max min(bufz, fieldsz) chars from field to buf, stopping
414 * at the first NUL char. NUL terminate buf if there is room left.
417 fieldcpy(char *buf, size_t bufsz, const char *field, size_t fieldsz)
420 const char *q = field;
425 while (i < fieldsz && *q != '\0') {