1 /* $OpenBSD: gen_subs.c,v 1.32 2016/08/26 05:06:14 guenther 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
37 #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 SECSPERDAY (24 * 60 * 60)
62 #define SIXMONTHS (SECSPERDAY * 365 / 2)
63 #define CURFRMT "%b %e %H:%M"
64 #define OLDFRMT "%b %e %Y"
66 #define TIMEFMT(t, now) \
67 (((t) + SIXMONTHS <= (now) || (t) > (now)) ? OLDFRMT : CURFRMT)
71 * list the members of an archive in ls format
75 ls_list(ARCHD
*arcn
, time_t now
, FILE *fp
)
82 term
= zeroflag
? '\0' : '\n'; /* path termination character */
85 * if not verbose, just print the file name
89 (void)fputs(arcn
->name
, fp
);
91 safe_print(arcn
->name
, fp
);
98 * user wants long mode
101 strmode(sbp
->st_mode
, f_mode
);
104 * print file mode, link count, uid, gid and time
106 if (strftime(f_date
, sizeof(f_date
), TIMEFMT(sbp
->st_mtime
, now
),
107 localtime(&(sbp
->st_mtime
))) == 0)
109 (void)fprintf(fp
, "%s%2u %-*.*s %-*.*s ", f_mode
, sbp
->st_nlink
,
110 NAME_WIDTH
, UT_NAMESIZE
, user_from_uid(sbp
->st_uid
, 0),
111 NAME_WIDTH
, UT_NAMESIZE
, group_from_gid(sbp
->st_gid
, 0));
114 * print device id's for devices, or sizes for other nodes
116 if ((arcn
->type
== PAX_CHR
) || (arcn
->type
== PAX_BLK
))
117 (void)fprintf(fp
, "%4lu, %4lu ",
118 (unsigned long)MAJOR(sbp
->st_rdev
),
119 (unsigned long)MINOR(sbp
->st_rdev
));
121 (void)fprintf(fp
, "%9llu ", sbp
->st_size
);
125 * print name and link info for hard and soft links
127 (void)fputs(f_date
, fp
);
129 safe_print(arcn
->name
, fp
);
130 if (PAX_IS_HARDLINK(arcn
->type
)) {
132 safe_print(arcn
->ln_name
, fp
);
133 } else if (arcn
->type
== PAX_SLK
) {
135 safe_print(arcn
->ln_name
, fp
);
137 (void)putc(term
, fp
);
143 * print a short summary of file to tty.
149 char f_date
[DATELEN
];
150 char f_mode
[MODELEN
];
151 time_t now
= time(NULL
);
154 * convert time to string, and print
156 if (strftime(f_date
, DATELEN
, TIMEFMT(arcn
->sb
.st_mtime
, now
),
157 localtime(&(arcn
->sb
.st_mtime
))) == 0)
159 strmode(arcn
->sb
.st_mode
, f_mode
);
160 tty_prnt("%s%s %s\n", f_mode
, f_date
, arcn
->name
);
164 safe_print(const char *str
, FILE *fp
)
170 * if printing to a tty, use vis(3) to print special characters.
172 if (isatty(fileno(fp
))) {
173 for (cp
= str
; *cp
; cp
++) {
174 (void)vis(visbuf
, cp
[0], VIS_CSTYLE
, cp
[1]);
175 (void)fputs(visbuf
, fp
);
178 (void)fputs(str
, fp
);
184 * convert hex/octal character string into a u_long. We do not have to
185 * check for overflow! (the headers in all supported formats are not large
186 * enough to create an overflow).
187 * NOTE: strings passed to us are NOT TERMINATED.
189 * unsigned long value
193 asc_ul(char *str
, int len
, int base
)
201 * skip over leading blanks and zeros
203 while ((str
< stop
) && ((*str
== ' ') || (*str
== '0')))
207 * for each valid digit, shift running value (tval) over to next digit
212 if ((*str
>= '0') && (*str
<= '9'))
213 tval
= (tval
<< 4) + (*str
++ - '0');
214 else if ((*str
>= 'A') && (*str
<= 'F'))
215 tval
= (tval
<< 4) + 10 + (*str
++ - 'A');
216 else if ((*str
>= 'a') && (*str
<= 'f'))
217 tval
= (tval
<< 4) + 10 + (*str
++ - 'a');
222 while ((str
< stop
) && (*str
>= '0') && (*str
<= '7'))
223 tval
= (tval
<< 3) + (*str
++ - '0');
230 * convert an unsigned long into an hex/oct ascii string. pads with LEADING
231 * ascii 0's to fill string completely
232 * NOTE: the string created is NOT TERMINATED.
236 ul_asc(u_long val
, char *str
, int len
, int base
)
242 * WARNING str is not '\0' terminated by this routine
247 * do a tailwise conversion (start at right most end of string to place
248 * least significant digit). Keep shifting until conversion value goes
249 * to zero (all digits were converted)
253 if ((digit
= (val
& 0xf)) < 10)
254 *pt
-- = '0' + (char)digit
;
256 *pt
-- = 'a' + (char)(digit
- 10);
263 *pt
-- = '0' + (char)(val
& 0x7);
271 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
282 * Convert hex/octal character string into a unsigned long long.
283 * We do not have to check for overflow! (The headers in all
284 * supported formats are not large enough to create an overflow).
285 * NOTE: strings passed to us are NOT TERMINATED.
287 * unsigned long long value
291 asc_ull(char *str
, int len
, int base
)
294 unsigned long long tval
= 0;
299 * skip over leading blanks and zeros
301 while ((str
< stop
) && ((*str
== ' ') || (*str
== '0')))
305 * for each valid digit, shift running value (tval) over to next digit
310 if ((*str
>= '0') && (*str
<= '9'))
311 tval
= (tval
<< 4) + (*str
++ - '0');
312 else if ((*str
>= 'A') && (*str
<= 'F'))
313 tval
= (tval
<< 4) + 10 + (*str
++ - 'A');
314 else if ((*str
>= 'a') && (*str
<= 'f'))
315 tval
= (tval
<< 4) + 10 + (*str
++ - 'a');
320 while ((str
< stop
) && (*str
>= '0') && (*str
<= '7'))
321 tval
= (tval
<< 3) + (*str
++ - '0');
328 * Convert an unsigned long long into a hex/oct ascii string.
329 * Pads with LEADING ascii 0's to fill string completely
330 * NOTE: the string created is NOT TERMINATED.
334 ull_asc(unsigned long long val
, char *str
, int len
, int base
)
337 unsigned long long digit
;
340 * WARNING str is not '\0' terminated by this routine
345 * do a tailwise conversion (start at right most end of string to place
346 * least significant digit). Keep shifting until conversion value goes
347 * to zero (all digits were converted)
351 if ((digit
= (val
& 0xf)) < 10)
352 *pt
-- = '0' + (char)digit
;
354 *pt
-- = 'a' + (char)(digit
- 10);
361 *pt
-- = '0' + (char)(val
& 0x7);
369 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
379 * Copy at max min(bufz, fieldsz) chars from field to buf, stopping
380 * at the first NUL char. NUL terminate buf if there is room left.
383 fieldcpy(char *buf
, size_t bufsz
, const char *field
, size_t fieldsz
)
386 const char *q
= field
;
391 while (i
< fieldsz
&& *q
!= '\0') {