xref: /freebsd/bin/pax/gen_subs.c (revision 42249ef2)
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 __FBSDID("$FreeBSD$");
43 
44 #include <sys/types.h>
45 #include <sys/time.h>
46 #include <sys/stat.h>
47 #include <langinfo.h>
48 #include <stdint.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include "pax.h"
52 #include "extern.h"
53 
54 /*
55  * a collection of general purpose subroutines used by pax
56  */
57 
58 /*
59  * constants used by ls_list() when printing out archive members
60  */
61 #define MODELEN 20
62 #define DATELEN 64
63 #define SIXMONTHS	 ((365 / 2) * 86400)
64 #define CURFRMTM	"%b %e %H:%M"
65 #define OLDFRMTM	"%b %e  %Y"
66 #define CURFRMTD	"%e %b %H:%M"
67 #define OLDFRMTD	"%e %b  %Y"
68 
69 static int d_first = -1;
70 
71 /*
72  * ls_list()
73  *	list the members of an archive in ls format
74  */
75 
76 void
77 ls_list(ARCHD *arcn, time_t now, FILE *fp)
78 {
79 	struct stat *sbp;
80 	char f_mode[MODELEN];
81 	char f_date[DATELEN];
82 	const char *timefrmt;
83 
84 	/*
85 	 * if not verbose, just print the file name
86 	 */
87 	if (!vflag) {
88 		(void)fprintf(fp, "%s\n", arcn->name);
89 		(void)fflush(fp);
90 		return;
91 	}
92 
93 	if (d_first < 0)
94 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
95 	/*
96 	 * user wants long mode
97 	 */
98 	sbp = &(arcn->sb);
99 	strmode(sbp->st_mode, f_mode);
100 
101 	/*
102 	 * time format based on age compared to the time pax was started.
103 	 */
104 	if ((sbp->st_mtime + SIXMONTHS) <= now)
105 		timefrmt = d_first ? OLDFRMTD : OLDFRMTM;
106 	else
107 		timefrmt = d_first ? CURFRMTD : CURFRMTM;
108 
109 	/*
110 	 * print file mode, link count, uid, gid and time
111 	 */
112 	if (strftime(f_date,DATELEN,timefrmt,localtime(&(sbp->st_mtime))) == 0)
113 		f_date[0] = '\0';
114 	(void)fprintf(fp, "%s%2ju %-12s %-12s ", f_mode,
115 		(uintmax_t)sbp->st_nlink,
116 		name_uid(sbp->st_uid, 1), name_gid(sbp->st_gid, 1));
117 
118 	/*
119 	 * print device id's for devices, or sizes for other nodes
120 	 */
121 	if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK))
122 #		ifdef NET2_STAT
123 		(void)fprintf(fp, "%4u,%4u ", MAJOR(sbp->st_rdev),
124 		    MINOR(sbp->st_rdev));
125 #		else
126 		(void)fprintf(fp, "%4lu,%4lu ", (unsigned long)MAJOR(sbp->st_rdev),
127 		    (unsigned long)MINOR(sbp->st_rdev));
128 #		endif
129 	else {
130 #		ifdef NET2_STAT
131 		(void)fprintf(fp, "%9lu ", sbp->st_size);
132 #		else
133 		(void)fprintf(fp, "%9ju ", (uintmax_t)sbp->st_size);
134 #		endif
135 	}
136 
137 	/*
138 	 * print name and link info for hard and soft links
139 	 */
140 	(void)fprintf(fp, "%s %s", f_date, arcn->name);
141 	if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
142 		(void)fprintf(fp, " == %s\n", arcn->ln_name);
143 	else if (arcn->type == PAX_SLK)
144 		(void)fprintf(fp, " => %s\n", arcn->ln_name);
145 	else
146 		(void)putc('\n', fp);
147 	(void)fflush(fp);
148 	return;
149 }
150 
151 /*
152  * tty_ls()
153  * 	print a short summary of file to tty.
154  */
155 
156 void
157 ls_tty(ARCHD *arcn)
158 {
159 	char f_date[DATELEN];
160 	char f_mode[MODELEN];
161 	const char *timefrmt;
162 
163 	if (d_first < 0)
164 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
165 
166 	if ((arcn->sb.st_mtime + SIXMONTHS) <= time(NULL))
167 		timefrmt = d_first ? OLDFRMTD : OLDFRMTM;
168 	else
169 		timefrmt = d_first ? CURFRMTD : CURFRMTM;
170 
171 	/*
172 	 * convert time to string, and print
173 	 */
174 	if (strftime(f_date, DATELEN, timefrmt,
175 	    localtime(&(arcn->sb.st_mtime))) == 0)
176 		f_date[0] = '\0';
177 	strmode(arcn->sb.st_mode, f_mode);
178 	tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name);
179 	return;
180 }
181 
182 /*
183  * l_strncpy()
184  *	copy src to dest up to len chars (stopping at first '\0').
185  *	when src is shorter than len, pads to len with '\0'.
186  * Return:
187  *	number of chars copied. (Note this is a real performance win over
188  *	doing a strncpy(), a strlen(), and then a possible memset())
189  */
190 
191 int
192 l_strncpy(char *dest, const char *src, int len)
193 {
194 	char *stop;
195 	char *start;
196 
197 	stop = dest + len;
198 	start = dest;
199 	while ((dest < stop) && (*src != '\0'))
200 		*dest++ = *src++;
201 	len = dest - start;
202 	while (dest < stop)
203 		*dest++ = '\0';
204 	return(len);
205 }
206 
207 /*
208  * asc_ul()
209  *	convert hex/octal character string into a u_long. We do not have to
210  *	check for overflow! (the headers in all supported formats are not large
211  *	enough to create an overflow).
212  *	NOTE: strings passed to us are NOT TERMINATED.
213  * Return:
214  *	unsigned long value
215  */
216 
217 u_long
218 asc_ul(char *str, int len, int base)
219 {
220 	char *stop;
221 	u_long tval = 0;
222 
223 	stop = str + len;
224 
225 	/*
226 	 * skip over leading blanks and zeros
227 	 */
228 	while ((str < stop) && ((*str == ' ') || (*str == '0')))
229 		++str;
230 
231 	/*
232 	 * for each valid digit, shift running value (tval) over to next digit
233 	 * and add next digit
234 	 */
235 	if (base == HEX) {
236 		while (str < stop) {
237 			if ((*str >= '0') && (*str <= '9'))
238 				tval = (tval << 4) + (*str++ - '0');
239 			else if ((*str >= 'A') && (*str <= 'F'))
240 				tval = (tval << 4) + 10 + (*str++ - 'A');
241 			else if ((*str >= 'a') && (*str <= 'f'))
242 				tval = (tval << 4) + 10 + (*str++ - 'a');
243 			else
244 				break;
245 		}
246 	} else {
247  		while ((str < stop) && (*str >= '0') && (*str <= '7'))
248 			tval = (tval << 3) + (*str++ - '0');
249 	}
250 	return(tval);
251 }
252 
253 /*
254  * ul_asc()
255  *	convert an unsigned long into an hex/oct ascii string. pads with LEADING
256  *	ascii 0's to fill string completely
257  *	NOTE: the string created is NOT TERMINATED.
258  */
259 
260 int
261 ul_asc(u_long val, char *str, int len, int base)
262 {
263 	char *pt;
264 	u_long digit;
265 
266 	/*
267 	 * WARNING str is not '\0' terminated by this routine
268 	 */
269 	pt = str + len - 1;
270 
271 	/*
272 	 * do a tailwise conversion (start at right most end of string to place
273 	 * least significant digit). Keep shifting until conversion value goes
274 	 * to zero (all digits were converted)
275 	 */
276 	if (base == HEX) {
277 		while (pt >= str) {
278 			if ((digit = (val & 0xf)) < 10)
279 				*pt-- = '0' + (char)digit;
280 			else
281 				*pt-- = 'a' + (char)(digit - 10);
282 			if ((val = (val >> 4)) == (u_long)0)
283 				break;
284 		}
285 	} else {
286 		while (pt >= str) {
287 			*pt-- = '0' + (char)(val & 0x7);
288 			if ((val = (val >> 3)) == (u_long)0)
289 				break;
290 		}
291 	}
292 
293 	/*
294 	 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
295 	 */
296 	while (pt >= str)
297 		*pt-- = '0';
298 	if (val != (u_long)0)
299 		return(-1);
300 	return(0);
301 }
302 
303 #ifndef NET2_STAT
304 /*
305  * asc_uqd()
306  *	convert hex/octal character string into a u_quad_t. We do not have to
307  *	check for overflow! (the headers in all supported formats are not large
308  *	enough to create an overflow).
309  *	NOTE: strings passed to us are NOT TERMINATED.
310  * Return:
311  *	u_quad_t value
312  */
313 
314 u_quad_t
315 asc_uqd(char *str, int len, int base)
316 {
317 	char *stop;
318 	u_quad_t tval = 0;
319 
320 	stop = str + len;
321 
322 	/*
323 	 * skip over leading blanks and zeros
324 	 */
325 	while ((str < stop) && ((*str == ' ') || (*str == '0')))
326 		++str;
327 
328 	/*
329 	 * for each valid digit, shift running value (tval) over to next digit
330 	 * and add next digit
331 	 */
332 	if (base == HEX) {
333 		while (str < stop) {
334 			if ((*str >= '0') && (*str <= '9'))
335 				tval = (tval << 4) + (*str++ - '0');
336 			else if ((*str >= 'A') && (*str <= 'F'))
337 				tval = (tval << 4) + 10 + (*str++ - 'A');
338 			else if ((*str >= 'a') && (*str <= 'f'))
339 				tval = (tval << 4) + 10 + (*str++ - 'a');
340 			else
341 				break;
342 		}
343 	} else {
344  		while ((str < stop) && (*str >= '0') && (*str <= '7'))
345 			tval = (tval << 3) + (*str++ - '0');
346 	}
347 	return(tval);
348 }
349 
350 /*
351  * uqd_asc()
352  *	convert an u_quad_t into a hex/oct ascii string. pads with LEADING
353  *	ascii 0's to fill string completely
354  *	NOTE: the string created is NOT TERMINATED.
355  */
356 
357 int
358 uqd_asc(u_quad_t val, char *str, int len, int base)
359 {
360 	char *pt;
361 	u_quad_t digit;
362 
363 	/*
364 	 * WARNING str is not '\0' terminated by this routine
365 	 */
366 	pt = str + len - 1;
367 
368 	/*
369 	 * do a tailwise conversion (start at right most end of string to place
370 	 * least significant digit). Keep shifting until conversion value goes
371 	 * to zero (all digits were converted)
372 	 */
373 	if (base == HEX) {
374 		while (pt >= str) {
375 			if ((digit = (val & 0xf)) < 10)
376 				*pt-- = '0' + (char)digit;
377 			else
378 				*pt-- = 'a' + (char)(digit - 10);
379 			if ((val = (val >> 4)) == (u_quad_t)0)
380 				break;
381 		}
382 	} else {
383 		while (pt >= str) {
384 			*pt-- = '0' + (char)(val & 0x7);
385 			if ((val = (val >> 3)) == (u_quad_t)0)
386 				break;
387 		}
388 	}
389 
390 	/*
391 	 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
392 	 */
393 	while (pt >= str)
394 		*pt-- = '0';
395 	if (val != (u_quad_t)0)
396 		return(-1);
397 	return(0);
398 }
399 #endif
400