]>
git.saurik.com Git - apple/file_cmds.git/blob - pax/gen_subs.c
36bbb41d55eddee0f298024ac13880ae3b6a7f51
1 /* $OpenBSD: gen_subs.c,v 1.17 2003/06/13 17:51:14 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
[] __attribute__((__unused__
)) = "$OpenBSD: gen_subs.c,v 1.17 2003/06/13 17:51:14 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 CURFRMT "%b %e %H:%M"
70 #define OLDFRMT "%b %e %Y"
75 * format date of file for printing
79 format_date(char * f_date
, time_t file_time
, time_t current
)
83 if (ltmfrmt
== NULL
) {
85 * no locale specified format.
87 /* use the same format that ls -l uses */
88 if ((file_time
+ SIXMONTHS
) <= current
|| file_time
> current
)
96 * convert time to string for printing
98 if (strftime(f_date
,DATELEN
,timefrmt
,localtime((const time_t *)&(file_time
))) == 0)
104 * list the members of an archive in ls format
108 ls_list(ARCHD
*arcn
, time_t now
, FILE *fp
)
111 char f_mode
[MODELEN
];
112 char f_date
[DATELEN
];
115 term
= zeroflag
? '\0' : '\n'; /* path termination character */
118 * if not verbose, just print the file name
122 (void)fputs(arcn
->name
, fp
);
124 safe_print(arcn
->name
, fp
);
125 (void)putc(term
, fp
);
130 if (pax_list_opt_format
) {
131 pax_format_list_output(arcn
, now
, fp
, term
);
135 * user wants long mode
138 strmode(sbp
->st_mode
, f_mode
);
140 format_date(&f_date
[0], sbp
->st_mtime
, now
);
143 * print file mode, link count, uid, gid and time
145 (void)fprintf(fp
, "%s%2u %-*.*s %-*.*s ", f_mode
, sbp
->st_nlink
,
146 NAME_WIDTH
, UT_NAMESIZE
, name_uid(sbp
->st_uid
, 1),
147 NAME_WIDTH
, UT_NAMESIZE
, name_gid(sbp
->st_gid
, 1));
150 * print device id's for devices, or sizes for other nodes
152 if ((arcn
->type
== PAX_CHR
) || (arcn
->type
== PAX_BLK
))
154 (void)fprintf(fp
, "%4u,%4u ", MAJOR(sbp
->st_rdev
),
156 (void)fprintf(fp
, "%4lu,%4lu ", (unsigned long)MAJOR(sbp
->st_rdev
),
158 (unsigned long)MINOR(sbp
->st_rdev
));
161 (void)fprintf(fp
, "%9lu ", sbp
->st_size
);
163 (void)fprintf(fp
, "%9qu ", sbp
->st_size
);
168 * print name and link info for hard and soft links
170 (void)fputs(f_date
, fp
);
172 safe_print(arcn
->name
, fp
);
173 if ((arcn
->type
== PAX_HLK
) || (arcn
->type
== PAX_HRG
)) {
175 safe_print(arcn
->ln_name
, fp
);
176 } else if (arcn
->type
== PAX_SLK
) {
178 safe_print(arcn
->ln_name
, fp
);
180 (void)putc(term
, fp
);
187 * print a short summary of file to tty.
193 char f_date
[DATELEN
];
194 char f_mode
[MODELEN
];
196 format_date(&f_date
[0], arcn
->sb
.st_mtime
, time(NULL
));
198 strmode(arcn
->sb
.st_mode
, f_mode
);
199 tty_prnt("%s%s %s\n", f_mode
, f_date
, arcn
->name
);
204 safe_print(const char *str
, FILE *fp
)
210 * if printing to a tty, use vis(3) to print special characters.
212 if (isatty(fileno(fp
))) {
213 for (cp
= str
; *cp
; cp
++) {
214 (void)vis(visbuf
, cp
[0], VIS_CSTYLE
, cp
[1]);
215 (void)fputs(visbuf
, fp
);
218 (void)fputs(str
, fp
);
224 * convert hex/octal character string into a u_long. We do not have to
225 * check for overflow! (the headers in all supported formats are not large
226 * enough to create an overflow).
227 * NOTE: strings passed to us are NOT TERMINATED.
229 * unsigned long value
233 asc_ul(char *str
, int len
, int base
)
241 * skip over leading blanks and zeros
243 while ((str
< stop
) && ((*str
== ' ') || (*str
== '0')))
247 * for each valid digit, shift running value (tval) over to next digit
252 if ((*str
>= '0') && (*str
<= '9'))
253 tval
= (tval
<< 4) + (*str
++ - '0');
254 else if ((*str
>= 'A') && (*str
<= 'F'))
255 tval
= (tval
<< 4) + 10 + (*str
++ - 'A');
256 else if ((*str
>= 'a') && (*str
<= 'f'))
257 tval
= (tval
<< 4) + 10 + (*str
++ - 'a');
262 while ((str
< stop
) && (*str
>= '0') && (*str
<= '7'))
263 tval
= (tval
<< 3) + (*str
++ - '0');
270 * convert an unsigned long into an hex/oct ascii string. pads with LEADING
271 * ascii 0's to fill string completely
272 * NOTE: the string created is NOT TERMINATED.
276 ul_asc(u_long val
, char *str
, int len
, int base
)
282 * WARNING str is not '\0' terminated by this routine
287 * do a tailwise conversion (start at right most end of string to place
288 * least significant digit). Keep shifting until conversion value goes
289 * to zero (all digits were converted)
293 if ((digit
= (val
& 0xf)) < 10)
294 *pt
-- = '0' + (char)digit
;
296 *pt
-- = 'a' + (char)(digit
- 10);
297 if ((val
= (val
>> 4)) == (u_long
)0)
302 *pt
-- = '0' + (char)(val
& 0x7);
303 if ((val
= (val
>> 3)) == (u_long
)0)
309 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
313 if (val
!= (u_long
)0)
321 * convert hex/octal character string into a u_quad_t. We do not have to
322 * check for overflow! (the headers in all supported formats are not large
323 * enough to create an overflow).
324 * NOTE: strings passed to us are NOT TERMINATED.
330 asc_uqd(char *str
, int len
, int base
)
338 * skip over leading blanks and zeros
340 while ((str
< stop
) && ((*str
== ' ') || (*str
== '0')))
344 * for each valid digit, shift running value (tval) over to next digit
349 if ((*str
>= '0') && (*str
<= '9'))
350 tval
= (tval
<< 4) + (*str
++ - '0');
351 else if ((*str
>= 'A') && (*str
<= 'F'))
352 tval
= (tval
<< 4) + 10 + (*str
++ - 'A');
353 else if ((*str
>= 'a') && (*str
<= 'f'))
354 tval
= (tval
<< 4) + 10 + (*str
++ - 'a');
359 while ((str
< stop
) && (*str
>= '0') && (*str
<= '7'))
360 tval
= (tval
<< 3) + (*str
++ - '0');
367 * convert an u_quad_t into a hex/oct ascii string. pads with LEADING
368 * ascii 0's to fill string completely
369 * NOTE: the string created is NOT TERMINATED.
373 uqd_asc(u_quad_t val
, char *str
, int len
, int base
)
379 * WARNING str is not '\0' terminated by this routine
384 * do a tailwise conversion (start at right most end of string to place
385 * least significant digit). Keep shifting until conversion value goes
386 * to zero (all digits were converted)
390 if ((digit
= (val
& 0xf)) < 10)
391 *pt
-- = '0' + (char)digit
;
393 *pt
-- = 'a' + (char)(digit
- 10);
394 if ((val
= (val
>> 4)) == (u_quad_t
)0)
399 *pt
-- = '0' + (char)(val
& 0x7);
400 if ((val
= (val
>> 3)) == (u_quad_t
)0)
406 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
410 if (val
!= (u_quad_t
)0)