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
[] __attribute__((__unused__
)) = "$OpenBSD: gen_subs.c,v 1.8 1997/09/01 18:29:51 deraadt Exp $";
49 #include <sys/types.h>
52 #include <sys/param.h>
63 * a collection of general purpose subroutines used by pax
67 * constants used by ls_list() when printing out archive members
71 #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
72 #define CURFRMT "%b %e %H:%M"
73 #define OLDFRMT "%b %e %Y"
81 * list the members of an archive in ls format
86 ls_list(register ARCHD
*arcn
, time_t now
, FILE *fp
)
89 ls_list(arcn
, now
, fp
)
95 register struct stat
*sbp
;
101 * if not verbose, just print the file name
104 (void)fprintf(fp
, "%s\n", arcn
->name
);
110 * user wants long mode
113 strmode(sbp
->st_mode
, f_mode
);
115 if (ltmfrmt
== NULL
) {
117 * no locale specified format. time format based on age
118 * compared to the time pax was started.
120 if ((sbp
->st_mtime
+ SIXMONTHS
) <= now
)
128 * print file mode, link count, uid, gid and time
130 if (strftime(f_date
,DATELEN
,timefrmt
,localtime((const time_t *)&(sbp
->st_mtime
))) == 0)
132 (void)fprintf(fp
, "%s%2u %-*s %-*s ", f_mode
, sbp
->st_nlink
,
133 UT_NAMESIZE
, name_uid(sbp
->st_uid
, 1), UT_GRPSIZE
,
134 name_gid(sbp
->st_gid
, 1));
137 * print device id's for devices, or sizes for other nodes
139 if ((arcn
->type
== PAX_CHR
) || (arcn
->type
== PAX_BLK
))
141 (void)fprintf(fp
, "%4u,%4u ", MAJOR(sbp
->st_rdev
),
143 (void)fprintf(fp
, "%4lu,%4lu ", (unsigned long)MAJOR(sbp
->st_rdev
),
145 (unsigned long)MINOR(sbp
->st_rdev
));
148 (void)fprintf(fp
, "%9lu ", sbp
->st_size
);
150 (void)fprintf(fp
, "%9qu ", sbp
->st_size
);
155 * print name and link info for hard and soft links
157 (void)fprintf(fp
, "%s %s", f_date
, arcn
->name
);
158 if ((arcn
->type
== PAX_HLK
) || (arcn
->type
== PAX_HRG
))
159 (void)fprintf(fp
, " == %s\n", arcn
->ln_name
);
160 else if (arcn
->type
== PAX_SLK
)
161 (void)fprintf(fp
, " => %s\n", arcn
->ln_name
);
163 (void)putc('\n', fp
);
170 * print a short summary of file to tty.
175 ls_tty(register ARCHD
*arcn
)
179 register ARCHD
*arcn
;
182 char f_date
[DATELEN
];
183 char f_mode
[MODELEN
];
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((const time_t *)&(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
);
210 * copy src to dest up to len chars (stopping at first '\0').
211 * when src is shorter than len, pads to len with '\0'.
213 * number of chars copied. (Note this is a real performance win over
214 * doing a strncpy(), a strlen(), and then a possible memset())
219 l_strncpy(register char *dest
, register char *src
, int len
)
222 l_strncpy(dest
, src
, len
)
229 register char *start
;
233 while ((dest
< stop
) && (*src
!= '\0'))
243 * convert hex/octal character string into a u_long. We do not have to
244 * check for overflow! (the headers in all supported formats are not large
245 * enough to create an overflow).
246 * NOTE: strings passed to us are NOT TERMINATED.
248 * unsigned long value
253 asc_ul(register char *str
, int len
, register int base
)
256 asc_ul(str
, len
, base
)
268 * skip over leading blanks and zeros
270 while ((str
< stop
) && ((*str
== ' ') || (*str
== '0')))
274 * for each valid digit, shift running value (tval) over to next digit
279 if ((*str
>= '0') && (*str
<= '9'))
280 tval
= (tval
<< 4) + (*str
++ - '0');
281 else if ((*str
>= 'A') && (*str
<= 'F'))
282 tval
= (tval
<< 4) + 10 + (*str
++ - 'A');
283 else if ((*str
>= 'a') && (*str
<= 'f'))
284 tval
= (tval
<< 4) + 10 + (*str
++ - 'a');
289 while ((str
< stop
) && (*str
>= '0') && (*str
<= '7'))
290 tval
= (tval
<< 3) + (*str
++ - '0');
297 * convert an unsigned long into an hex/oct ascii string. pads with LEADING
298 * ascii 0's to fill string completely
299 * NOTE: the string created is NOT TERMINATED.
304 ul_asc(u_long val
, register char *str
, register int len
, register int base
)
307 ul_asc(val
, str
, len
, base
)
318 * WARNING str is not '\0' terminated by this routine
323 * do a tailwise conversion (start at right most end of string to place
324 * least significant digit). Keep shifting until conversion value goes
325 * to zero (all digits were converted)
329 if ((digit
= (val
& 0xf)) < 10)
330 *pt
-- = '0' + (char)digit
;
332 *pt
-- = 'a' + (char)(digit
- 10);
333 if ((val
= (val
>> 4)) == (u_long
)0)
338 *pt
-- = '0' + (char)(val
& 0x7);
339 if ((val
= (val
>> 3)) == (u_long
)0)
345 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
349 if (val
!= (u_long
)0)
357 * convert hex/octal character string into a u_quad_t. We do not have to
358 * check for overflow! (the headers in all supported formats are not large
359 * enough to create an overflow).
360 * NOTE: strings passed to us are NOT TERMINATED.
367 asc_uqd(register char *str
, int len
, register int base
)
370 asc_uqd(str
, len
, base
)
382 * skip over leading blanks and zeros
384 while ((str
< stop
) && ((*str
== ' ') || (*str
== '0')))
388 * for each valid digit, shift running value (tval) over to next digit
393 if ((*str
>= '0') && (*str
<= '9'))
394 tval
= (tval
<< 4) + (*str
++ - '0');
395 else if ((*str
>= 'A') && (*str
<= 'F'))
396 tval
= (tval
<< 4) + 10 + (*str
++ - 'A');
397 else if ((*str
>= 'a') && (*str
<= 'f'))
398 tval
= (tval
<< 4) + 10 + (*str
++ - 'a');
403 while ((str
< stop
) && (*str
>= '0') && (*str
<= '7'))
404 tval
= (tval
<< 3) + (*str
++ - '0');
411 * convert an u_quad_t into a hex/oct ascii string. pads with LEADING
412 * ascii 0's to fill string completely
413 * NOTE: the string created is NOT TERMINATED.
418 uqd_asc(u_quad_t val
, register char *str
, register int len
, register int base
)
421 uqd_asc(val
, str
, len
, base
)
432 * WARNING str is not '\0' terminated by this routine
437 * do a tailwise conversion (start at right most end of string to place
438 * least significant digit). Keep shifting until conversion value goes
439 * to zero (all digits were converted)
443 if ((digit
= (val
& 0xf)) < 10)
444 *pt
-- = '0' + (char)digit
;
446 *pt
-- = 'a' + (char)(digit
- 10);
447 if ((val
= (val
>> 4)) == (u_quad_t
)0)
452 *pt
-- = '0' + (char)(val
& 0x7);
453 if ((val
= (val
>> 3)) == (u_quad_t
)0)
459 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
463 if (val
!= (u_quad_t
)0)