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>
46 #include <sys/sysmacros.h>
50 #include <sys/param.h>
66 * a collection of general purpose subroutines used by pax
70 * constants used by ls_list() when printing out archive members
74 #define DAYSPERNYEAR 365
75 #define SECSPERDAY 86400
77 #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
78 #define CURFRMT "%b %e %H:%M"
79 #define OLDFRMT "%b %e %Y"
84 * list the members of an archive in ls format
88 ls_list(ARCHD *arcn, time_t now, FILE *fp)
96 term = zeroflag ? '\0' : '\n'; /* path termination character */
99 * if not verbose, just print the file name
103 (void)fputs(arcn->name, fp);
105 safe_print(arcn->name, fp);
106 (void)putc(term, fp);
112 * user wants long mode
115 strmode(sbp->st_mode, f_mode);
117 if (ltmfrmt == NULL) {
119 * no locale specified format. time format based on age
120 * compared to the time pax was started.
122 if ((sbp->st_mtime + SIXMONTHS) <= now)
130 * print file mode, link count, uid, gid and time
132 if (strftime(f_date,DATELEN,timefrmt,localtime(&(sbp->st_mtime))) == 0)
134 (void)fprintf(fp, "%s%2u %-*.*s %-*.*s ", f_mode, sbp->st_nlink,
135 NAME_WIDTH, UT_NAMESIZE, name_uid(sbp->st_uid, 1),
136 NAME_WIDTH, UT_NAMESIZE, name_gid(sbp->st_gid, 1));
139 * print device id's for devices, or sizes for other nodes
141 if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK))
143 (void)fprintf(fp, "%4u,%4u ", MAJOR(sbp->st_rdev),
145 (void)fprintf(fp, "%4lu,%4lu ", (unsigned long)MAJOR(sbp->st_rdev),
147 (unsigned long)MINOR(sbp->st_rdev));
150 (void)fprintf(fp, "%9lu ", sbp->st_size);
152 (void)fprintf(fp, "%9qu ", sbp->st_size);
157 * print name and link info for hard and soft links
159 (void)fputs(f_date, fp);
161 safe_print(arcn->name, fp);
162 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG)) {
164 safe_print(arcn->ln_name, fp);
165 } else if (arcn->type == PAX_SLK) {
167 safe_print(arcn->ln_name, fp);
169 (void)putc(term, fp);
176 * print a short summary of file to tty.
182 char f_date[DATELEN];
183 char f_mode[MODELEN];
184 const char *timefrmt;
186 if (ltmfrmt == NULL) {
188 * no locale specified format
190 if ((arcn->sb.st_mtime + SIXMONTHS) <= time(NULL))
198 * convert time to string, and print
200 if (strftime(f_date, DATELEN, timefrmt,
201 localtime(&(arcn->sb.st_mtime))) == 0)
203 strmode(arcn->sb.st_mode, f_mode);
204 tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name);
209 safe_print(const char *str, FILE *fp)
215 * if printing to a tty, use vis(3) to print special characters.
217 if (0 && isatty(fileno(fp))) {
218 for (cp = str; *cp; cp++) {
219 (void)vis(visbuf, cp[0], VIS_CSTYLE, cp[1]);
220 (void)fputs(visbuf, fp);
223 (void)fputs(str, fp);
229 * convert hex/octal character string into a u_long. We do not have to
230 * check for overflow! (the headers in all supported formats are not large
231 * enough to create an overflow).
232 * NOTE: strings passed to us are NOT TERMINATED.
234 * unsigned long value
238 asc_ul(char *str, int len, int base)
246 * skip over leading blanks and zeros
248 while ((str < stop) && ((*str == ' ') || (*str == '0')))
252 * for each valid digit, shift running value (tval) over to next digit
257 if ((*str >= '0') && (*str <= '9'))
258 tval = (tval << 4) + (*str++ - '0');
259 else if ((*str >= 'A') && (*str <= 'F'))
260 tval = (tval << 4) + 10 + (*str++ - 'A');
261 else if ((*str >= 'a') && (*str <= 'f'))
262 tval = (tval << 4) + 10 + (*str++ - 'a');
267 while ((str < stop) && (*str >= '0') && (*str <= '7'))
268 tval = (tval << 3) + (*str++ - '0');
275 * convert an unsigned long into an hex/oct ascii string. pads with LEADING
276 * ascii 0's to fill string completely
277 * NOTE: the string created is NOT TERMINATED.
281 ul_asc(u_long val, char *str, int len, int base)
287 * WARNING str is not '\0' terminated by this routine
292 * do a tailwise conversion (start at right most end of string to place
293 * least significant digit). Keep shifting until conversion value goes
294 * to zero (all digits were converted)
298 if ((digit = (val & 0xf)) < 10)
299 *pt-- = '0' + (char)digit;
301 *pt-- = 'a' + (char)(digit - 10);
302 if ((val = (val >> 4)) == (u_long)0)
307 *pt-- = '0' + (char)(val & 0x7);
308 if ((val = (val >> 3)) == (u_long)0)
314 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
318 if (val != (u_long)0)
326 * convert hex/octal character string into a u_quad_t. We do not have to
327 * check for overflow! (the headers in all supported formats are not large
328 * enough to create an overflow).
329 * NOTE: strings passed to us are NOT TERMINATED.
335 asc_uqd(char *str, int len, int base)
343 * skip over leading blanks and zeros
345 while ((str < stop) && ((*str == ' ') || (*str == '0')))
349 * for each valid digit, shift running value (tval) over to next digit
354 if ((*str >= '0') && (*str <= '9'))
355 tval = (tval << 4) + (*str++ - '0');
356 else if ((*str >= 'A') && (*str <= 'F'))
357 tval = (tval << 4) + 10 + (*str++ - 'A');
358 else if ((*str >= 'a') && (*str <= 'f'))
359 tval = (tval << 4) + 10 + (*str++ - 'a');
364 while ((str < stop) && (*str >= '0') && (*str <= '7'))
365 tval = (tval << 3) + (*str++ - '0');
372 * convert an u_quad_t into a hex/oct ascii string. pads with LEADING
373 * ascii 0's to fill string completely
374 * NOTE: the string created is NOT TERMINATED.
378 uqd_asc(u_quad_t val, char *str, int len, int base)
384 * WARNING str is not '\0' terminated by this routine
389 * do a tailwise conversion (start at right most end of string to place
390 * least significant digit). Keep shifting until conversion value goes
391 * to zero (all digits were converted)
395 if ((digit = (val & 0xf)) < 10)
396 *pt-- = '0' + (char)digit;
398 *pt-- = 'a' + (char)(digit - 10);
399 if ((val = (val >> 4)) == (u_quad_t)0)
404 *pt-- = '0' + (char)(val & 0x7);
405 if ((val = (val >> 3)) == (u_quad_t)0)
411 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
415 if (val != (u_quad_t)0)
422 * Copy at max min(bufz, fieldsz) chars from field to buf, stopping
423 * at the first NUL char. NUL terminate buf if there is room left.
426 fieldcpy(char *buf, size_t bufsz, const char *field, size_t fieldsz)
429 const char *q = field;
434 while (i < fieldsz && *q != '\0') {