xref: /netbsd-src/bin/pax/gen_subs.c (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1 /*	$NetBSD: gen_subs.c,v 1.35 2011/08/14 10:49:58 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1992 Keith Muller.
5  * Copyright (c) 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Keith Muller of the University of California, San Diego.
10  *
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.
22  *
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.
34  */
35 
36 #if HAVE_NBTOOL_CONFIG_H
37 #include "nbtool_config.h"
38 #endif
39 
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.35 2011/08/14 10:49:58 christos Exp $");
46 #endif
47 #endif /* not lint */
48 
49 #include <sys/types.h>
50 #include <sys/time.h>
51 #include <sys/stat.h>
52 #include <sys/param.h>
53 
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>
64 
65 #include "pax.h"
66 #include "extern.h"
67 
68 /*
69  * a collection of general purpose subroutines used by pax
70  */
71 
72 /*
73  * constants used by ls_list() when printing out archive members
74  */
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
84 
85 /*
86  * convert time to string
87  */
88 static void
89 formattime(char *buf, size_t buflen, time_t when)
90 {
91 	int error;
92 	struct tm tm;
93 	(void)localtime_r(&when, &tm);
94 
95 	if (when + SIXMONTHS <= time(NULL))
96 		error = strftime(buf, buflen, OLDFRMT, &tm);
97 	else
98 		error = strftime(buf, buflen, CURFRMT, &tm);
99 
100 	if (error == 0)
101 		buf[0] = '\0';
102 }
103 
104 /*
105  * ls_list()
106  *	list the members of an archive in ls format
107  */
108 
109 void
110 ls_list(ARCHD *arcn, time_t now, FILE *fp)
111 {
112 	struct stat *sbp;
113 	char f_mode[MODELEN];
114 	char f_date[DATELEN];
115 	const char *user, *group;
116 
117 	/*
118 	 * if not verbose, just print the file name
119 	 */
120 	if (!vflag) {
121 		(void)fprintf(fp, "%s\n", arcn->name);
122 		(void)fflush(fp);
123 		return;
124 	}
125 
126 	/*
127 	 * user wants long mode
128 	 */
129 	sbp = &(arcn->sb);
130 	strmode(sbp->st_mode, f_mode);
131 
132 	/*
133 	 * time format based on age compared to the time pax was started.
134 	 */
135 	formattime(f_date, sizeof(f_date), arcn->sb.st_mtime);
136 	/*
137 	 * print file mode, link count, uid, gid and time
138 	 */
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 : "");
144 
145 	/*
146 	 * print device id's for devices, or sizes for other nodes
147 	 */
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);
153 	}
154 
155 	/*
156 	 * print name and link info for hard and soft links
157 	 */
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);
166 }
167 
168 /*
169  * tty_ls()
170  *	print a short summary of file to tty.
171  */
172 
173 void
174 ls_tty(ARCHD *arcn)
175 {
176 	char f_date[DATELEN];
177 	char f_mode[MODELEN];
178 
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;
183 }
184 
185 void
186 safe_print(const char *str, FILE *fp)
187 {
188 	char visbuf[5];
189 	const char *cp;
190 
191 	/*
192 	 * if printing to a tty, use vis(3) to print special characters.
193 	 */
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);
198 		}
199 	} else {
200 		(void)fputs(str, fp);
201 	}
202 }
203 
204 /*
205  * asc_ul()
206  *	convert hex/octal character string into a u_long. 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  *	unsigned long value
212  */
213 
214 u_long
215 asc_ul(char *str, int len, int base)
216 {
217 	char *stop;
218 	u_long tval = 0;
219 
220 	stop = str + len;
221 
222 	/*
223 	 * skip over leading blanks and zeros
224 	 */
225 	while ((str < stop) && ((*str == ' ') || (*str == '0')))
226 		++str;
227 
228 	/*
229 	 * for each valid digit, shift running value (tval) over to next digit
230 	 * and add next digit
231 	 */
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;
242 		}
243 	} else {
244 		while ((str < stop) && (*str >= '0') && (*str <= '7'))
245 			tval = (tval << 3) + (*str++ - '0');
246 	}
247 	return tval;
248 }
249 
250 /*
251  * ul_asc()
252  *	convert an unsigned long 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.
255  */
256 
257 int
258 ul_asc(u_long val, char *str, int len, int base)
259 {
260 	char *pt;
261 	u_long digit;
262 
263 	/*
264 	 * WARNING str is not '\0' terminated by this routine
265 	 */
266 	pt = str + len - 1;
267 
268 	/*
269 	 * do a tailwise conversion (start at right most end of string to place
270 	 * least significant digit). Keep shifting until conversion value goes
271 	 * to zero (all digits were converted)
272 	 */
273 	if (base == HEX) {
274 		while (pt >= str) {
275 			if ((digit = (val & 0xf)) < 10)
276 				*pt-- = '0' + (char)digit;
277 			else
278 				*pt-- = 'a' + (char)(digit - 10);
279 			if ((val = (val >> 4)) == (u_long)0)
280 				break;
281 		}
282 	} else {
283 		while (pt >= str) {
284 			*pt-- = '0' + (char)(val & 0x7);
285 			if ((val = (val >> 3)) == (u_long)0)
286 				break;
287 		}
288 	}
289 
290 	/*
291 	 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
292 	 */
293 	while (pt >= str)
294 		*pt-- = '0';
295 	if (val != (u_long)0)
296 		return -1;
297 	return 0;
298 }
299 
300 #if !defined(_LP64)
301 /*
302  * asc_ull()
303  *	convert hex/octal character string into a unsigned long long. We do
304  *	not have to to check for overflow! (the headers in all supported
305  *	formats are not large enough to create an overflow).
306  *	NOTE: strings passed to us are NOT TERMINATED.
307  * Return:
308  *	unsigned long long value
309  */
310 
311 unsigned long long
312 asc_ull(char *str, int len, int base)
313 {
314 	char *stop;
315 	unsigned long long tval = 0;
316 
317 	stop = str + len;
318 
319 	/*
320 	 * skip over leading blanks and zeros
321 	 */
322 	while ((str < stop) && ((*str == ' ') || (*str == '0')))
323 		++str;
324 
325 	/*
326 	 * for each valid digit, shift running value (tval) over to next digit
327 	 * and add next digit
328 	 */
329 	if (base == HEX) {
330 		while (str < stop) {
331 			if ((*str >= '0') && (*str <= '9'))
332 				tval = (tval << 4) + (*str++ - '0');
333 			else if ((*str >= 'A') && (*str <= 'F'))
334 				tval = (tval << 4) + 10 + (*str++ - 'A');
335 			else if ((*str >= 'a') && (*str <= 'f'))
336 				tval = (tval << 4) + 10 + (*str++ - 'a');
337 			else
338 				break;
339 		}
340 	} else {
341 		while ((str < stop) && (*str >= '0') && (*str <= '7'))
342 			tval = (tval << 3) + (*str++ - '0');
343 	}
344 	return tval;
345 }
346 
347 /*
348  * ull_asc()
349  *	convert an unsigned long long into a hex/oct ascii string. pads with
350  *	LEADING ascii 0's to fill string completely
351  *	NOTE: the string created is NOT TERMINATED.
352  */
353 
354 int
355 ull_asc(unsigned long long val, char *str, int len, int base)
356 {
357 	char *pt;
358 	unsigned long long digit;
359 
360 	/*
361 	 * WARNING str is not '\0' terminated by this routine
362 	 */
363 	pt = str + len - 1;
364 
365 	/*
366 	 * do a tailwise conversion (start at right most end of string to place
367 	 * least significant digit). Keep shifting until conversion value goes
368 	 * to zero (all digits were converted)
369 	 */
370 	if (base == HEX) {
371 		while (pt >= str) {
372 			if ((digit = (val & 0xf)) < 10)
373 				*pt-- = '0' + (char)digit;
374 			else
375 				*pt-- = 'a' + (char)(digit - 10);
376 			if ((val = (val >> 4)) == (unsigned long long)0)
377 				break;
378 		}
379 	} else {
380 		while (pt >= str) {
381 			*pt-- = '0' + (char)(val & 0x7);
382 			if ((val = (val >> 3)) == (unsigned long long)0)
383 				break;
384 		}
385 	}
386 
387 	/*
388 	 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
389 	 */
390 	while (pt >= str)
391 		*pt-- = '0';
392 	if (val != (unsigned long long)0)
393 		return -1;
394 	return 0;
395 }
396 #endif
397 
398 int
399 check_Aflag(void)
400 {
401 
402 	if (Aflag > 0)
403 		return 1;
404 	if (Aflag == 0) {
405 		Aflag = -1;
406 		tty_warn(0,
407 		 "Removing leading / from absolute path names in the archive");
408 	}
409 	return 0;
410 }
411