1 /* $NetBSD: gen_subs.c,v 1.36 2012/08/09 08:09:21 christos Exp $ */
4 * Copyright (c) 1992 Keith Muller.
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Keith Muller of the University of California, San Diego.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #if HAVE_NBTOOL_CONFIG_H
37 #include "nbtool_config.h"
40 #include <sys/cdefs.h>
43 static char sccsid
[] = "@(#)gen_subs.c 8.1 (Berkeley) 5/31/93";
45 __RCSID("$NetBSD: gen_subs.c,v 1.36 2012/08/09 08:09:21 christos Exp $");
49 #include <sys/types.h>
52 #include <sys/param.h>
69 * a collection of general purpose subroutines used by pax
73 * constants used by ls_list() when printing out archive members
77 #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
78 #define CURFRMT "%b %e %H:%M"
79 #define OLDFRMT "%b %e %Y"
86 * convert time to string
89 formattime(char *buf
, size_t buflen
, time_t when
)
93 (void)localtime_r(&when
, &tm
);
95 if (when
+ SIXMONTHS
<= time(NULL
))
96 error
= strftime(buf
, buflen
, OLDFRMT
, &tm
);
98 error
= strftime(buf
, buflen
, CURFRMT
, &tm
);
106 * list the members of an archive in ls format
110 ls_list(ARCHD
*arcn
, time_t now
, FILE *fp
)
113 char f_mode
[MODELEN
];
114 char f_date
[DATELEN
];
115 const char *user
, *group
;
118 * if not verbose, just print the file name
121 (void)fprintf(fp
, "%s\n", arcn
->name
);
127 * user wants long mode
130 strmode(sbp
->st_mode
, f_mode
);
133 * time format based on age compared to the time pax was started.
135 formattime(f_date
, sizeof(f_date
), arcn
->sb
.st_mtime
);
137 * print file mode, link count, uid, gid and time
139 user
= user_from_uid(sbp
->st_uid
, 0);
140 group
= group_from_gid(sbp
->st_gid
, 0);
141 (void)fprintf(fp
, "%s%2lu %-*s %-*s ", f_mode
,
142 (unsigned long)sbp
->st_nlink
,
143 UT_NAMESIZE
, user
? user
: "", UT_GRPSIZE
, group
? group
: "");
146 * print device id's for devices, or sizes for other nodes
148 if ((arcn
->type
== PAX_CHR
) || (arcn
->type
== PAX_BLK
))
149 (void)fprintf(fp
, "%4lu,%4lu ", (long) MAJOR(sbp
->st_rdev
),
150 (long) MINOR(sbp
->st_rdev
));
152 (void)fprintf(fp
, OFFT_FP("9") " ", (OFFT_T
)sbp
->st_size
);
156 * print name and link info for hard and soft links
158 (void)fprintf(fp
, "%s %s", f_date
, arcn
->name
);
159 if ((arcn
->type
== PAX_HLK
) || (arcn
->type
== PAX_HRG
))
160 (void)fprintf(fp
, " == %s\n", arcn
->ln_name
);
161 else if (arcn
->type
== PAX_SLK
)
162 (void)fprintf(fp
, " -> %s\n", arcn
->ln_name
);
164 (void)fputc('\n', fp
);
170 * print a short summary of file to tty.
176 char f_date
[DATELEN
];
177 char f_mode
[MODELEN
];
179 formattime(f_date
, sizeof(f_date
), arcn
->sb
.st_mtime
);
180 strmode(arcn
->sb
.st_mode
, f_mode
);
181 tty_prnt("%s%s %s\n", f_mode
, f_date
, arcn
->name
);
186 safe_print(const char *str
, FILE *fp
)
192 * if printing to a tty, use vis(3) to print special characters.
194 if (isatty(fileno(fp
))) {
195 for (cp
= str
; *cp
; cp
++) {
196 (void)vis(visbuf
, cp
[0], VIS_CSTYLE
, cp
[1]);
197 (void)fputs(visbuf
, fp
);
200 (void)fputs(str
, fp
);
206 * convert hex/octal character string into a uint32_t. We do not have to
207 * check for overflow! (the headers in all supported formats are not large
208 * enough to create an overflow).
209 * NOTE: strings passed to us are NOT TERMINATED.
215 asc_u32(char *str
, int len
, int base
)
223 * skip over leading blanks and zeros
225 while ((str
< stop
) && ((*str
== ' ') || (*str
== '0')))
229 * for each valid digit, shift running value (tval) over to next digit
234 if ((*str
>= '0') && (*str
<= '9'))
235 tval
= (tval
<< 4) + (*str
++ - '0');
236 else if ((*str
>= 'A') && (*str
<= 'F'))
237 tval
= (tval
<< 4) + 10 + (*str
++ - 'A');
238 else if ((*str
>= 'a') && (*str
<= 'f'))
239 tval
= (tval
<< 4) + 10 + (*str
++ - 'a');
244 while ((str
< stop
) && (*str
>= '0') && (*str
<= '7'))
245 tval
= (tval
<< 3) + (*str
++ - '0');
252 * convert an uintmax_t into an hex/oct ascii string. pads with LEADING
253 * ascii 0's to fill string completely
254 * NOTE: the string created is NOT TERMINATED.
258 u32_asc(uintmax_t val
, char *str
, int len
, int base
)
265 if (p
&& p
!= TOP_HALF
)
271 * WARNING str is not '\0' terminated by this routine
276 * do a tailwise conversion (start at right most end of string to place
277 * least significant digit). Keep shifting until conversion value goes
278 * to zero (all digits were converted)
282 if ((digit
= (val
& 0xf)) < 10)
283 *pt
-- = '0' + (char)digit
;
285 *pt
-- = 'a' + (char)(digit
- 10);
286 if ((val
= (val
>> 4)) == (u_long
)0)
291 *pt
-- = '0' + (char)(val
& 0x7);
292 if ((val
= (val
>> 3)) == 0)
298 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
309 * convert hex/octal character string into a uintmax. We do
310 * not have to to check for overflow! (the headers in all supported
311 * formats are not large enough to create an overflow).
312 * NOTE: strings passed to us are NOT TERMINATED.
318 asc_umax(char *str
, int len
, int base
)
326 * skip over leading blanks and zeros
328 while ((str
< stop
) && ((*str
== ' ') || (*str
== '0')))
332 * for each valid digit, shift running value (tval) over to next digit
337 if ((*str
>= '0') && (*str
<= '9'))
338 tval
= (tval
<< 4) + (*str
++ - '0');
339 else if ((*str
>= 'A') && (*str
<= 'F'))
340 tval
= (tval
<< 4) + 10 + (*str
++ - 'A');
341 else if ((*str
>= 'a') && (*str
<= 'f'))
342 tval
= (tval
<< 4) + 10 + (*str
++ - 'a');
347 while ((str
< stop
) && (*str
>= '0') && (*str
<= '7'))
348 tval
= (tval
<< 3) + (*str
++ - '0');
355 * convert an uintmax_t into a hex/oct ascii string. pads with
356 * LEADING ascii 0's to fill string completely
357 * NOTE: the string created is NOT TERMINATED.
361 umax_asc(uintmax_t val
, char *str
, int len
, int base
)
367 * WARNING str is not '\0' terminated by this routine
372 * do a tailwise conversion (start at right most end of string to place
373 * least significant digit). Keep shifting until conversion value goes
374 * to zero (all digits were converted)
378 if ((digit
= (val
& 0xf)) < 10)
379 *pt
-- = '0' + (char)digit
;
381 *pt
-- = 'a' + (char)(digit
- 10);
382 if ((val
= (val
>> 4)) == 0)
387 *pt
-- = '0' + (char)(val
& 0x7);
388 if ((val
= (val
>> 3)) == 0)
394 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
412 "Removing leading / from absolute path names in the archive");