]>
git.saurik.com Git - apple/file_cmds.git/blob - pax/gen_subs.c
79438e0f637e928eeee241fbea3274069cf5af00
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 CURFRMTM "%b %e %H:%M"
70 #define OLDFRMTM "%b %e %Y"
71 #define CURFRMTD "%e %b %H:%M"
72 #define OLDFRMTD "%e %b %Y"
75 static int d_first
= -1;
79 * list the members of an archive in ls format
83 ls_list(ARCHD
*arcn
, time_t now
, FILE *fp
)
91 term
= zeroflag
? '\0' : '\n'; /* path termination character */
94 * if not verbose, just print the file name
98 (void)fputs(arcn
->name
, fp
);
100 safe_print(arcn
->name
, fp
);
101 (void)putc(term
, fp
);
106 if (pax_list_opt_format
) {
107 pax_format_list_output(arcn
, now
, fp
, term
);
112 d_first
= (*nl_langinfo(D_MD_ORDER
) == 'd');
114 * user wants long mode
117 strmode(sbp
->st_mode
, f_mode
);
120 * time format based on age compared to the time pax was started.
122 if ((sbp
->st_mtime
+ SIXMONTHS
) <= now
||
124 timefrmt
= d_first
? OLDFRMTD
: OLDFRMTM
;
126 timefrmt
= d_first
? CURFRMTD
: CURFRMTM
;
129 * print file mode, link count, uid, gid and time
131 if (strftime(f_date
,DATELEN
,timefrmt
,localtime(&(sbp
->st_mtime
))) == 0)
133 #define UT_NAMESIZE 8
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
;
187 d_first
= (*nl_langinfo(D_MD_ORDER
) == 'd');
189 if ((arcn
->sb
.st_mtime
+ SIXMONTHS
) <= time(NULL
))
190 timefrmt
= d_first
? OLDFRMTD
: OLDFRMTM
;
192 timefrmt
= d_first
? CURFRMTD
: CURFRMTM
;
195 * convert time to string, and print
197 if (strftime(f_date
, DATELEN
, timefrmt
,
198 localtime(&(arcn
->sb
.st_mtime
))) == 0)
200 strmode(arcn
->sb
.st_mode
, f_mode
);
201 tty_prnt("%s%s %s\n", f_mode
, f_date
, arcn
->name
);
206 safe_print(const char *str
, FILE *fp
)
212 * if printing to a tty, use vis(3) to print special characters.
214 if (isatty(fileno(fp
))) {
215 for (cp
= str
; *cp
; cp
++) {
216 (void)vis(visbuf
, cp
[0], VIS_CSTYLE
, cp
[1]);
217 (void)fputs(visbuf
, fp
);
220 (void)fputs(str
, fp
);
226 * convert hex/octal character string into a u_long. We do not have to
227 * check for overflow! (the headers in all supported formats are not large
228 * enough to create an overflow).
229 * NOTE: strings passed to us are NOT TERMINATED.
231 * unsigned long value
235 asc_ul(char *str
, int len
, int base
)
243 * skip over leading blanks and zeros
245 while ((str
< stop
) && ((*str
== ' ') || (*str
== '0')))
249 * for each valid digit, shift running value (tval) over to next digit
254 if ((*str
>= '0') && (*str
<= '9'))
255 tval
= (tval
<< 4) + (*str
++ - '0');
256 else if ((*str
>= 'A') && (*str
<= 'F'))
257 tval
= (tval
<< 4) + 10 + (*str
++ - 'A');
258 else if ((*str
>= 'a') && (*str
<= 'f'))
259 tval
= (tval
<< 4) + 10 + (*str
++ - 'a');
264 while ((str
< stop
) && (*str
>= '0') && (*str
<= '7'))
265 tval
= (tval
<< 3) + (*str
++ - '0');
272 * convert an unsigned long into an hex/oct ascii string. pads with LEADING
273 * ascii 0's to fill string completely
274 * NOTE: the string created is NOT TERMINATED.
278 ul_asc(u_long val
, char *str
, int len
, int base
)
284 * WARNING str is not '\0' terminated by this routine
289 * do a tailwise conversion (start at right most end of string to place
290 * least significant digit). Keep shifting until conversion value goes
291 * to zero (all digits were converted)
295 if ((digit
= (val
& 0xf)) < 10)
296 *pt
-- = '0' + (char)digit
;
298 *pt
-- = 'a' + (char)(digit
- 10);
299 if ((val
= (val
>> 4)) == (u_long
)0)
304 *pt
-- = '0' + (char)(val
& 0x7);
305 if ((val
= (val
>> 3)) == (u_long
)0)
311 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
315 if (val
!= (u_long
)0)
323 * convert hex/octal character string into a u_quad_t. We do not have to
324 * check for overflow! (the headers in all supported formats are not large
325 * enough to create an overflow).
326 * NOTE: strings passed to us are NOT TERMINATED.
332 asc_uqd(char *str
, int len
, int base
)
340 * skip over leading blanks and zeros
342 while ((str
< stop
) && ((*str
== ' ') || (*str
== '0')))
346 * for each valid digit, shift running value (tval) over to next digit
351 if ((*str
>= '0') && (*str
<= '9'))
352 tval
= (tval
<< 4) + (*str
++ - '0');
353 else if ((*str
>= 'A') && (*str
<= 'F'))
354 tval
= (tval
<< 4) + 10 + (*str
++ - 'A');
355 else if ((*str
>= 'a') && (*str
<= 'f'))
356 tval
= (tval
<< 4) + 10 + (*str
++ - 'a');
361 while ((str
< stop
) && (*str
>= '0') && (*str
<= '7'))
362 tval
= (tval
<< 3) + (*str
++ - '0');
369 * convert an u_quad_t into a hex/oct ascii string. pads with LEADING
370 * ascii 0's to fill string completely
371 * NOTE: the string created is NOT TERMINATED.
375 uqd_asc(u_quad_t val
, char *str
, int len
, int base
)
381 * WARNING str is not '\0' terminated by this routine
386 * do a tailwise conversion (start at right most end of string to place
387 * least significant digit). Keep shifting until conversion value goes
388 * to zero (all digits were converted)
392 if ((digit
= (val
& 0xf)) < 10)
393 *pt
-- = '0' + (char)digit
;
395 *pt
-- = 'a' + (char)(digit
- 10);
396 if ((val
= (val
>> 4)) == (u_quad_t
)0)
401 *pt
-- = '0' + (char)(val
& 0x7);
402 if ((val
= (val
>> 3)) == (u_quad_t
)0)
408 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
412 if (val
!= (u_quad_t
)0)
419 * Copy at max min(bufz, fieldsz) chars from field to buf, stopping
420 * at the first NUL char. NUL terminate buf if there is room left.
423 fieldcpy(char *buf
, size_t bufsz
, const char *field
, size_t fieldsz
)
426 const char *q
= field
;
431 while (i
< fieldsz
&& *q
!= '\0') {