xref: /freebsd/bin/pax/gen_subs.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)gen_subs.c	8.1 (Berkeley) 5/31/93";
39 #endif
40 #endif /* not lint */
41 #include <sys/cdefs.h>
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%2ju %-12s %-12s ", f_mode,
113 		(uintmax_t)sbp->st_nlink,
114 		name_uid(sbp->st_uid, 1), name_gid(sbp->st_gid, 1));
115 
116 	/*
117 	 * print device id's for devices, or sizes for other nodes
118 	 */
119 	if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK))
120 		(void)fprintf(fp, "%4lu,%4lu ", (unsigned long)MAJOR(sbp->st_rdev),
121 		    (unsigned long)MINOR(sbp->st_rdev));
122 	else {
123 		(void)fprintf(fp, "%9ju ", (uintmax_t)sbp->st_size);
124 	}
125 
126 	/*
127 	 * print name and link info for hard and soft links
128 	 */
129 	(void)fprintf(fp, "%s %s", f_date, arcn->name);
130 	if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
131 		(void)fprintf(fp, " == %s\n", arcn->ln_name);
132 	else if (arcn->type == PAX_SLK)
133 		(void)fprintf(fp, " => %s\n", arcn->ln_name);
134 	else
135 		(void)putc('\n', fp);
136 	(void)fflush(fp);
137 	return;
138 }
139 
140 /*
141  * tty_ls()
142  * 	print a short summary of file to tty.
143  */
144 
145 void
146 ls_tty(ARCHD *arcn)
147 {
148 	char f_date[DATELEN];
149 	char f_mode[MODELEN];
150 	const char *timefrmt;
151 
152 	if (d_first < 0)
153 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
154 
155 	if ((arcn->sb.st_mtime + SIXMONTHS) <= time(NULL))
156 		timefrmt = d_first ? OLDFRMTD : OLDFRMTM;
157 	else
158 		timefrmt = d_first ? CURFRMTD : CURFRMTM;
159 
160 	/*
161 	 * convert time to string, and print
162 	 */
163 	if (strftime(f_date, DATELEN, timefrmt,
164 	    localtime(&(arcn->sb.st_mtime))) == 0)
165 		f_date[0] = '\0';
166 	strmode(arcn->sb.st_mode, f_mode);
167 	tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name);
168 	return;
169 }
170 
171 /*
172  * l_strncpy()
173  *	copy src to dest up to len chars (stopping at first '\0').
174  *	when src is shorter than len, pads to len with '\0'.
175  * Return:
176  *	number of chars copied. (Note this is a real performance win over
177  *	doing a strncpy(), a strlen(), and then a possible memset())
178  */
179 
180 int
181 l_strncpy(char *dest, const char *src, int len)
182 {
183 	char *stop;
184 	char *start;
185 
186 	stop = dest + len;
187 	start = dest;
188 	while ((dest < stop) && (*src != '\0'))
189 		*dest++ = *src++;
190 	len = dest - start;
191 	while (dest < stop)
192 		*dest++ = '\0';
193 	return(len);
194 }
195 
196 /*
197  * asc_ul()
198  *	convert hex/octal character string into a u_long. We do not have to
199  *	check for overflow! (the headers in all supported formats are not large
200  *	enough to create an overflow).
201  *	NOTE: strings passed to us are NOT TERMINATED.
202  * Return:
203  *	unsigned long value
204  */
205 
206 u_long
207 asc_ul(char *str, int len, int base)
208 {
209 	char *stop;
210 	u_long tval = 0;
211 
212 	stop = str + len;
213 
214 	/*
215 	 * skip over leading blanks and zeros
216 	 */
217 	while ((str < stop) && ((*str == ' ') || (*str == '0')))
218 		++str;
219 
220 	/*
221 	 * for each valid digit, shift running value (tval) over to next digit
222 	 * and add next digit
223 	 */
224 	if (base == HEX) {
225 		while (str < stop) {
226 			if ((*str >= '0') && (*str <= '9'))
227 				tval = (tval << 4) + (*str++ - '0');
228 			else if ((*str >= 'A') && (*str <= 'F'))
229 				tval = (tval << 4) + 10 + (*str++ - 'A');
230 			else if ((*str >= 'a') && (*str <= 'f'))
231 				tval = (tval << 4) + 10 + (*str++ - 'a');
232 			else
233 				break;
234 		}
235 	} else {
236  		while ((str < stop) && (*str >= '0') && (*str <= '7'))
237 			tval = (tval << 3) + (*str++ - '0');
238 	}
239 	return(tval);
240 }
241 
242 /*
243  * ul_asc()
244  *	convert an unsigned long into an hex/oct ascii string. pads with LEADING
245  *	ascii 0's to fill string completely
246  *	NOTE: the string created is NOT TERMINATED.
247  */
248 
249 int
250 ul_asc(u_long val, char *str, int len, int base)
251 {
252 	char *pt;
253 	u_long digit;
254 
255 	/*
256 	 * WARNING str is not '\0' terminated by this routine
257 	 */
258 	pt = str + len - 1;
259 
260 	/*
261 	 * do a tailwise conversion (start at right most end of string to place
262 	 * least significant digit). Keep shifting until conversion value goes
263 	 * to zero (all digits were converted)
264 	 */
265 	if (base == HEX) {
266 		while (pt >= str) {
267 			if ((digit = (val & 0xf)) < 10)
268 				*pt-- = '0' + (char)digit;
269 			else
270 				*pt-- = 'a' + (char)(digit - 10);
271 			if ((val = (val >> 4)) == (u_long)0)
272 				break;
273 		}
274 	} else {
275 		while (pt >= str) {
276 			*pt-- = '0' + (char)(val & 0x7);
277 			if ((val = (val >> 3)) == (u_long)0)
278 				break;
279 		}
280 	}
281 
282 	/*
283 	 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
284 	 */
285 	while (pt >= str)
286 		*pt-- = '0';
287 	if (val != (u_long)0)
288 		return(-1);
289 	return(0);
290 }
291 
292 /*
293  * asc_uqd()
294  *	convert hex/octal character string into a u_quad_t. We do not have to
295  *	check for overflow! (the headers in all supported formats are not large
296  *	enough to create an overflow).
297  *	NOTE: strings passed to us are NOT TERMINATED.
298  * Return:
299  *	u_quad_t value
300  */
301 
302 u_quad_t
303 asc_uqd(char *str, int len, int base)
304 {
305 	char *stop;
306 	u_quad_t tval = 0;
307 
308 	stop = str + len;
309 
310 	/*
311 	 * skip over leading blanks and zeros
312 	 */
313 	while ((str < stop) && ((*str == ' ') || (*str == '0')))
314 		++str;
315 
316 	/*
317 	 * for each valid digit, shift running value (tval) over to next digit
318 	 * and add next digit
319 	 */
320 	if (base == HEX) {
321 		while (str < stop) {
322 			if ((*str >= '0') && (*str <= '9'))
323 				tval = (tval << 4) + (*str++ - '0');
324 			else if ((*str >= 'A') && (*str <= 'F'))
325 				tval = (tval << 4) + 10 + (*str++ - 'A');
326 			else if ((*str >= 'a') && (*str <= 'f'))
327 				tval = (tval << 4) + 10 + (*str++ - 'a');
328 			else
329 				break;
330 		}
331 	} else {
332  		while ((str < stop) && (*str >= '0') && (*str <= '7'))
333 			tval = (tval << 3) + (*str++ - '0');
334 	}
335 	return(tval);
336 }
337 
338 /*
339  * uqd_asc()
340  *	convert an u_quad_t into a hex/oct ascii string. pads with LEADING
341  *	ascii 0's to fill string completely
342  *	NOTE: the string created is NOT TERMINATED.
343  */
344 
345 int
346 uqd_asc(u_quad_t val, char *str, int len, int base)
347 {
348 	char *pt;
349 	u_quad_t digit;
350 
351 	/*
352 	 * WARNING str is not '\0' terminated by this routine
353 	 */
354 	pt = str + len - 1;
355 
356 	/*
357 	 * do a tailwise conversion (start at right most end of string to place
358 	 * least significant digit). Keep shifting until conversion value goes
359 	 * to zero (all digits were converted)
360 	 */
361 	if (base == HEX) {
362 		while (pt >= str) {
363 			if ((digit = (val & 0xf)) < 10)
364 				*pt-- = '0' + (char)digit;
365 			else
366 				*pt-- = 'a' + (char)(digit - 10);
367 			if ((val = (val >> 4)) == (u_quad_t)0)
368 				break;
369 		}
370 	} else {
371 		while (pt >= str) {
372 			*pt-- = '0' + (char)(val & 0x7);
373 			if ((val = (val >> 3)) == (u_quad_t)0)
374 				break;
375 		}
376 	}
377 
378 	/*
379 	 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
380 	 */
381 	while (pt >= str)
382 		*pt-- = '0';
383 	if (val != (u_quad_t)0)
384 		return(-1);
385 	return(0);
386 }
387