]>
git.saurik.com Git - apple/file_cmds.git/blob - pax/gen_subs.c
d3b4c16766651f334df698702bc24cb1e50e3427
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
37 #include <sys/cdefs.h>
40 static const char sccsid
[] = "@(#)gen_subs.c 8.1 (Berkeley) 5/31/93";
42 __used
static const char rcsid
[] = "$OpenBSD: gen_subs.c,v 1.19 2007/04/04 21:55:10 millert Exp $";
46 #include <sys/types.h>
49 #include <sys/param.h>
61 * a collection of general purpose subroutines used by pax
65 * constants used by ls_list() when printing out archive members
69 #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
70 #define CURFRMTM "%b %e %H:%M"
71 #define OLDFRMTM "%b %e %Y"
72 #define CURFRMTD "%e %b %H:%M"
73 #define OLDFRMTD "%e %b %Y"
76 static int d_first
= -1;
80 * list the members of an archive in ls format
84 ls_list(ARCHD
*arcn
, time_t now
, FILE *fp
)
92 term
= zeroflag
? '\0' : '\n'; /* path termination character */
95 * if not verbose, just print the file name
99 (void)fputs(arcn
->name
, fp
);
101 safe_print(arcn
->name
, fp
);
102 (void)putc(term
, fp
);
107 if (pax_list_opt_format
) {
108 pax_format_list_output(arcn
, now
, fp
, term
);
113 d_first
= (*nl_langinfo(D_MD_ORDER
) == 'd');
115 * user wants long mode
118 strmode(sbp
->st_mode
, f_mode
);
121 * time format based on age compared to the time pax was started.
123 if ((sbp
->st_mtime
+ SIXMONTHS
) <= now
||
125 timefrmt
= d_first
? OLDFRMTD
: OLDFRMTM
;
127 timefrmt
= d_first
? CURFRMTD
: CURFRMTM
;
130 * print file mode, link count, uid, gid and time
132 if (strftime(f_date
,DATELEN
,timefrmt
,localtime(&(sbp
->st_mtime
))) == 0)
134 #define UT_NAMESIZE 8
135 (void)fprintf(fp
, "%s%2u %-*.*s %-*.*s ", f_mode
, sbp
->st_nlink
,
136 NAME_WIDTH
, UT_NAMESIZE
, name_uid(sbp
->st_uid
, 1),
137 NAME_WIDTH
, UT_NAMESIZE
, name_gid(sbp
->st_gid
, 1));
140 * print device id's for devices, or sizes for other nodes
142 if ((arcn
->type
== PAX_CHR
) || (arcn
->type
== PAX_BLK
))
144 (void)fprintf(fp
, "%4u,%4u ", MAJOR(sbp
->st_rdev
),
146 (void)fprintf(fp
, "%4lu,%4lu ", (unsigned long)MAJOR(sbp
->st_rdev
),
148 (unsigned long)MINOR(sbp
->st_rdev
));
151 (void)fprintf(fp
, "%9lu ", sbp
->st_size
);
153 (void)fprintf(fp
, "%9qu ", sbp
->st_size
);
158 * print name and link info for hard and soft links
160 (void)fputs(f_date
, fp
);
162 safe_print(arcn
->name
, fp
);
163 if ((arcn
->type
== PAX_HLK
) || (arcn
->type
== PAX_HRG
)) {
165 safe_print(arcn
->ln_name
, fp
);
166 } else if (arcn
->type
== PAX_SLK
) {
168 safe_print(arcn
->ln_name
, fp
);
170 (void)putc(term
, fp
);
177 * print a short summary of file to tty.
183 char f_date
[DATELEN
];
184 char f_mode
[MODELEN
];
185 const char *timefrmt
;
188 d_first
= (*nl_langinfo(D_MD_ORDER
) == 'd');
190 if ((arcn
->sb
.st_mtime
+ SIXMONTHS
) <= time(NULL
))
191 timefrmt
= d_first
? OLDFRMTD
: OLDFRMTM
;
193 timefrmt
= d_first
? CURFRMTD
: CURFRMTM
;
196 * convert time to string, and print
198 if (strftime(f_date
, DATELEN
, timefrmt
,
199 localtime(&(arcn
->sb
.st_mtime
))) == 0)
201 strmode(arcn
->sb
.st_mode
, f_mode
);
202 tty_prnt("%s%s %s\n", f_mode
, f_date
, arcn
->name
);
207 safe_print(const char *str
, FILE *fp
)
213 * if printing to a tty, use vis(3) to print special characters.
215 if (isatty(fileno(fp
))) {
216 for (cp
= str
; *cp
; cp
++) {
217 (void)vis(visbuf
, cp
[0], VIS_CSTYLE
, cp
[1]);
218 (void)fputs(visbuf
, fp
);
221 (void)fputs(str
, fp
);
227 * convert hex/octal character string into a u_long. We do not have to
228 * check for overflow! (the headers in all supported formats are not large
229 * enough to create an overflow).
230 * NOTE: strings passed to us are NOT TERMINATED.
232 * unsigned long value
236 asc_ul(char *str
, int len
, int base
)
244 * skip over leading blanks and zeros
246 while ((str
< stop
) && ((*str
== ' ') || (*str
== '0')))
250 * for each valid digit, shift running value (tval) over to next digit
255 if ((*str
>= '0') && (*str
<= '9'))
256 tval
= (tval
<< 4) + (*str
++ - '0');
257 else if ((*str
>= 'A') && (*str
<= 'F'))
258 tval
= (tval
<< 4) + 10 + (*str
++ - 'A');
259 else if ((*str
>= 'a') && (*str
<= 'f'))
260 tval
= (tval
<< 4) + 10 + (*str
++ - 'a');
265 while ((str
< stop
) && (*str
>= '0') && (*str
<= '7'))
266 tval
= (tval
<< 3) + (*str
++ - '0');
273 * convert an unsigned long into an hex/oct ascii string. pads with LEADING
274 * ascii 0's to fill string completely
275 * NOTE: the string created is NOT TERMINATED.
279 ul_asc(u_long val
, char *str
, int len
, int base
)
285 * WARNING str is not '\0' terminated by this routine
290 * do a tailwise conversion (start at right most end of string to place
291 * least significant digit). Keep shifting until conversion value goes
292 * to zero (all digits were converted)
296 if ((digit
= (val
& 0xf)) < 10)
297 *pt
-- = '0' + (char)digit
;
299 *pt
-- = 'a' + (char)(digit
- 10);
300 if ((val
= (val
>> 4)) == (u_long
)0)
305 *pt
-- = '0' + (char)(val
& 0x7);
306 if ((val
= (val
>> 3)) == (u_long
)0)
312 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
316 if (val
!= (u_long
)0)
324 * convert hex/octal character string into a u_quad_t. We do not have to
325 * check for overflow! (the headers in all supported formats are not large
326 * enough to create an overflow).
327 * NOTE: strings passed to us are NOT TERMINATED.
333 asc_uqd(char *str
, int len
, int base
)
341 * skip over leading blanks and zeros
343 while ((str
< stop
) && ((*str
== ' ') || (*str
== '0')))
347 * for each valid digit, shift running value (tval) over to next digit
352 if ((*str
>= '0') && (*str
<= '9'))
353 tval
= (tval
<< 4) + (*str
++ - '0');
354 else if ((*str
>= 'A') && (*str
<= 'F'))
355 tval
= (tval
<< 4) + 10 + (*str
++ - 'A');
356 else if ((*str
>= 'a') && (*str
<= 'f'))
357 tval
= (tval
<< 4) + 10 + (*str
++ - 'a');
362 while ((str
< stop
) && (*str
>= '0') && (*str
<= '7'))
363 tval
= (tval
<< 3) + (*str
++ - '0');
370 * convert an u_quad_t into a hex/oct ascii string. pads with LEADING
371 * ascii 0's to fill string completely
372 * NOTE: the string created is NOT TERMINATED.
376 uqd_asc(u_quad_t val
, char *str
, int len
, int base
)
382 * WARNING str is not '\0' terminated by this routine
387 * do a tailwise conversion (start at right most end of string to place
388 * least significant digit). Keep shifting until conversion value goes
389 * to zero (all digits were converted)
393 if ((digit
= (val
& 0xf)) < 10)
394 *pt
-- = '0' + (char)digit
;
396 *pt
-- = 'a' + (char)(digit
- 10);
397 if ((val
= (val
>> 4)) == (u_quad_t
)0)
402 *pt
-- = '0' + (char)(val
& 0x7);
403 if ((val
= (val
>> 3)) == (u_quad_t
)0)
409 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
413 if (val
!= (u_quad_t
)0)
420 * Copy at max min(bufz, fieldsz) chars from field to buf, stopping
421 * at the first NUL char. NUL terminate buf if there is room left.
424 fieldcpy(char *buf
, size_t bufsz
, const char *field
, size_t fieldsz
)
427 const char *q
= field
;
432 while (i
< fieldsz
&& *q
!= '\0') {