kernel debug: priv can be NULL early on
[minix.git] / bin / pax / gen_subs.c
blob1ffa95a3656ca977caff241a219373d017d1d22a
1 /* $NetBSD: gen_subs.c,v 1.36 2012/08/09 08:09:21 christos Exp $ */
3 /*-
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
13 * are met:
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
33 * SUCH DAMAGE.
36 #if HAVE_NBTOOL_CONFIG_H
37 #include "nbtool_config.h"
38 #endif
40 #include <sys/cdefs.h>
41 #if !defined(lint)
42 #if 0
43 static char sccsid[] = "@(#)gen_subs.c 8.1 (Berkeley) 5/31/93";
44 #else
45 __RCSID("$NetBSD: gen_subs.c,v 1.36 2012/08/09 08:09:21 christos Exp $");
46 #endif
47 #endif /* not lint */
49 #include <sys/types.h>
50 #include <sys/time.h>
51 #include <sys/stat.h>
52 #include <sys/param.h>
54 #include <ctype.h>
55 #include <grp.h>
56 #include <pwd.h>
57 #include <vis.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <time.h>
62 #include <tzfile.h>
63 #include <unistd.h>
65 #include "pax.h"
66 #include "extern.h"
69 * a collection of general purpose subroutines used by pax
73 * constants used by ls_list() when printing out archive members
75 #define MODELEN 20
76 #define DATELEN 64
77 #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
78 #define CURFRMT "%b %e %H:%M"
79 #define OLDFRMT "%b %e %Y"
80 #ifndef UT_NAMESIZE
81 #define UT_NAMESIZE 8
82 #endif
83 #define UT_GRPSIZE 6
86 * convert time to string
88 static void
89 formattime(char *buf, size_t buflen, time_t when)
91 int error;
92 struct tm tm;
93 (void)localtime_r(&when, &tm);
95 if (when + SIXMONTHS <= time(NULL))
96 error = strftime(buf, buflen, OLDFRMT, &tm);
97 else
98 error = strftime(buf, buflen, CURFRMT, &tm);
100 if (error == 0)
101 buf[0] = '\0';
105 * ls_list()
106 * list the members of an archive in ls format
109 void
110 ls_list(ARCHD *arcn, time_t now, FILE *fp)
112 struct stat *sbp;
113 char f_mode[MODELEN];
114 char f_date[DATELEN];
115 const char *user, *group;
118 * if not verbose, just print the file name
120 if (!vflag) {
121 (void)fprintf(fp, "%s\n", arcn->name);
122 (void)fflush(fp);
123 return;
127 * user wants long mode
129 sbp = &(arcn->sb);
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));
151 else {
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);
163 else
164 (void)fputc('\n', fp);
165 (void)fflush(fp);
169 * tty_ls()
170 * print a short summary of file to tty.
173 void
174 ls_tty(ARCHD *arcn)
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);
182 return;
185 void
186 safe_print(const char *str, FILE *fp)
188 char visbuf[5];
189 const char *cp;
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);
199 } else {
200 (void)fputs(str, fp);
205 * asc_u32()
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.
210 * Return:
211 * uint32_t value
214 uint32_t
215 asc_u32(char *str, int len, int base)
217 char *stop;
218 uint32_t tval = 0;
220 stop = str + len;
223 * skip over leading blanks and zeros
225 while ((str < stop) && ((*str == ' ') || (*str == '0')))
226 ++str;
229 * for each valid digit, shift running value (tval) over to next digit
230 * and add next digit
232 if (base == HEX) {
233 while (str < stop) {
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');
240 else
241 break;
243 } else {
244 while ((str < stop) && (*str >= '0') && (*str <= '7'))
245 tval = (tval << 3) + (*str++ - '0');
247 return tval;
251 * u32_asc()
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)
260 char *pt;
261 uint32_t digit;
262 uintmax_t p;
264 p = val & TOP_HALF;
265 if (p && p != TOP_HALF)
266 return -1;
268 val &= BOTTOM_HALF;
271 * WARNING str is not '\0' terminated by this routine
273 pt = str + len - 1;
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)
280 if (base == HEX) {
281 while (pt >= str) {
282 if ((digit = (val & 0xf)) < 10)
283 *pt-- = '0' + (char)digit;
284 else
285 *pt-- = 'a' + (char)(digit - 10);
286 if ((val = (val >> 4)) == (u_long)0)
287 break;
289 } else {
290 while (pt >= str) {
291 *pt-- = '0' + (char)(val & 0x7);
292 if ((val = (val >> 3)) == 0)
293 break;
298 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
300 while (pt >= str)
301 *pt-- = '0';
302 if (val != 0)
303 return -1;
304 return 0;
308 * asc_umax()
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.
313 * Return:
314 * uintmax_t value
317 uintmax_t
318 asc_umax(char *str, int len, int base)
320 char *stop;
321 uintmax_t tval = 0;
323 stop = str + len;
326 * skip over leading blanks and zeros
328 while ((str < stop) && ((*str == ' ') || (*str == '0')))
329 ++str;
332 * for each valid digit, shift running value (tval) over to next digit
333 * and add next digit
335 if (base == HEX) {
336 while (str < stop) {
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');
343 else
344 break;
346 } else {
347 while ((str < stop) && (*str >= '0') && (*str <= '7'))
348 tval = (tval << 3) + (*str++ - '0');
350 return tval;
354 * umax_asc()
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)
363 char *pt;
364 uintmax_t digit;
367 * WARNING str is not '\0' terminated by this routine
369 pt = str + len - 1;
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)
376 if (base == HEX) {
377 while (pt >= str) {
378 if ((digit = (val & 0xf)) < 10)
379 *pt-- = '0' + (char)digit;
380 else
381 *pt-- = 'a' + (char)(digit - 10);
382 if ((val = (val >> 4)) == 0)
383 break;
385 } else {
386 while (pt >= str) {
387 *pt-- = '0' + (char)(val & 0x7);
388 if ((val = (val >> 3)) == 0)
389 break;
394 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
396 while (pt >= str)
397 *pt-- = '0';
398 if (val != 0)
399 return -1;
400 return 0;
404 check_Aflag(void)
407 if (Aflag > 0)
408 return 1;
409 if (Aflag == 0) {
410 Aflag = -1;
411 tty_warn(0,
412 "Removing leading / from absolute path names in the archive");
414 return 0;