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