xref: /freebsd/bin/pax/gen_subs.c (revision a5921bc3653e2e286715e6fe8d473ec0d02da38c)
1 /*-
2  * Copyright (c) 1992 Keith Muller.
3  * Copyright (c) 1992, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Keith Muller of the University of California, San Diego.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)gen_subs.c	8.1 (Berkeley) 5/31/93";
37 #endif
38 #endif /* not lint */
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include <sys/types.h>
43 #include <sys/time.h>
44 #include <sys/stat.h>
45 #include <langinfo.h>
46 #include <stdint.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include "pax.h"
50 #include "extern.h"
51 
52 /*
53  * a collection of general purpose subroutines used by pax
54  */
55 
56 /*
57  * constants used by ls_list() when printing out archive members
58  */
59 #define MODELEN 20
60 #define DATELEN 64
61 #define SIXMONTHS	 ((365 / 2) * 86400)
62 #define CURFRMTM	"%b %e %H:%M"
63 #define OLDFRMTM	"%b %e  %Y"
64 #define CURFRMTD	"%e %b %H:%M"
65 #define OLDFRMTD	"%e %b  %Y"
66 
67 static int d_first = -1;
68 
69 /*
70  * ls_list()
71  *	list the members of an archive in ls format
72  */
73 
74 void
75 ls_list(ARCHD *arcn, time_t now, FILE *fp)
76 {
77 	struct stat *sbp;
78 	char f_mode[MODELEN];
79 	char f_date[DATELEN];
80 	const char *timefrmt;
81 
82 	/*
83 	 * if not verbose, just print the file name
84 	 */
85 	if (!vflag) {
86 		(void)fprintf(fp, "%s\n", arcn->name);
87 		(void)fflush(fp);
88 		return;
89 	}
90 
91 	if (d_first < 0)
92 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
93 	/*
94 	 * user wants long mode
95 	 */
96 	sbp = &(arcn->sb);
97 	strmode(sbp->st_mode, f_mode);
98 
99 	/*
100 	 * time format based on age compared to the time pax was started.
101 	 */
102 	if ((sbp->st_mtime + SIXMONTHS) <= now)
103 		timefrmt = d_first ? OLDFRMTD : OLDFRMTM;
104 	else
105 		timefrmt = d_first ? CURFRMTD : CURFRMTM;
106 
107 	/*
108 	 * print file mode, link count, uid, gid and time
109 	 */
110 	if (strftime(f_date,DATELEN,timefrmt,localtime(&(sbp->st_mtime))) == 0)
111 		f_date[0] = '\0';
112 	(void)fprintf(fp, "%s%2u %-12s %-12s ", f_mode, sbp->st_nlink,
113 		name_uid(sbp->st_uid, 1), name_gid(sbp->st_gid, 1));
114 
115 	/*
116 	 * print device id's for devices, or sizes for other nodes
117 	 */
118 	if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK))
119 #		ifdef NET2_STAT
120 		(void)fprintf(fp, "%4u,%4u ", MAJOR(sbp->st_rdev),
121 		    MINOR(sbp->st_rdev));
122 #		else
123 		(void)fprintf(fp, "%4lu,%4lu ", (unsigned long)MAJOR(sbp->st_rdev),
124 		    (unsigned long)MINOR(sbp->st_rdev));
125 #		endif
126 	else {
127 #		ifdef NET2_STAT
128 		(void)fprintf(fp, "%9lu ", sbp->st_size);
129 #		else
130 		(void)fprintf(fp, "%9ju ", (uintmax_t)sbp->st_size);
131 #		endif
132 	}
133 
134 	/*
135 	 * print name and link info for hard and soft links
136 	 */
137 	(void)fprintf(fp, "%s %s", f_date, arcn->name);
138 	if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
139 		(void)fprintf(fp, " == %s\n", arcn->ln_name);
140 	else if (arcn->type == PAX_SLK)
141 		(void)fprintf(fp, " => %s\n", arcn->ln_name);
142 	else
143 		(void)putc('\n', fp);
144 	(void)fflush(fp);
145 	return;
146 }
147 
148 /*
149  * tty_ls()
150  * 	print a short summary of file to tty.
151  */
152 
153 void
154 ls_tty(ARCHD *arcn)
155 {
156 	char f_date[DATELEN];
157 	char f_mode[MODELEN];
158 	const char *timefrmt;
159 
160 	if (d_first < 0)
161 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
162 
163 	if ((arcn->sb.st_mtime + SIXMONTHS) <= time(NULL))
164 		timefrmt = d_first ? OLDFRMTD : OLDFRMTM;
165 	else
166 		timefrmt = d_first ? CURFRMTD : CURFRMTM;
167 
168 	/*
169 	 * convert time to string, and print
170 	 */
171 	if (strftime(f_date, DATELEN, timefrmt,
172 	    localtime(&(arcn->sb.st_mtime))) == 0)
173 		f_date[0] = '\0';
174 	strmode(arcn->sb.st_mode, f_mode);
175 	tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name);
176 	return;
177 }
178 
179 /*
180  * l_strncpy()
181  *	copy src to dest up to len chars (stopping at first '\0').
182  *	when src is shorter than len, pads to len with '\0'.
183  * Return:
184  *	number of chars copied. (Note this is a real performance win over
185  *	doing a strncpy(), a strlen(), and then a possible memset())
186  */
187 
188 int
189 l_strncpy(char *dest, const char *src, int len)
190 {
191 	char *stop;
192 	char *start;
193 
194 	stop = dest + len;
195 	start = dest;
196 	while ((dest < stop) && (*src != '\0'))
197 		*dest++ = *src++;
198 	len = dest - start;
199 	while (dest < stop)
200 		*dest++ = '\0';
201 	return(len);
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 #ifndef NET2_STAT
301 /*
302  * asc_uqd()
303  *	convert hex/octal character string into a u_quad_t. We do not have to
304  *	check for overflow! (the headers in all supported formats are not large
305  *	enough to create an overflow).
306  *	NOTE: strings passed to us are NOT TERMINATED.
307  * Return:
308  *	u_quad_t value
309  */
310 
311 u_quad_t
312 asc_uqd(char *str, int len, int base)
313 {
314 	char *stop;
315 	u_quad_t 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  * uqd_asc()
349  *	convert an u_quad_t into a hex/oct ascii string. pads with LEADING
350  *	ascii 0's to fill string completely
351  *	NOTE: the string created is NOT TERMINATED.
352  */
353 
354 int
355 uqd_asc(u_quad_t val, char *str, int len, int base)
356 {
357 	char *pt;
358 	u_quad_t 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)) == (u_quad_t)0)
377 				break;
378 		}
379 	} else {
380 		while (pt >= str) {
381 			*pt-- = '0' + (char)(val & 0x7);
382 			if ((val = (val >> 3)) == (u_quad_t)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 != (u_quad_t)0)
393 		return(-1);
394 	return(0);
395 }
396 #endif
397