1 /*	$OpenBSD: gen_subs.c,v 1.20 2009/10/27 23:59:22 deraadt Exp $	*/
2 /*	$NetBSD: gen_subs.c,v 1.5 1995/03/21 09:07:26 cgd Exp $	*/
3 
4 /*-
5  * Copyright (c) 2012
6  *	Thorsten Glaser <tg@debian.org>
7  * Copyright (c) 1992 Keith Muller.
8  * Copyright (c) 1992, 1993
9  *	The Regents of the University of California.  All rights reserved.
10  *
11  * This code is derived from software contributed to Berkeley by
12  * Keith Muller of the University of California, San Diego.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #include <sys/param.h>
40 #include <sys/time.h>
41 #include <sys/stat.h>
42 #include <stdio.h>
43 #ifdef __INTERIX
44 #include <utmpx.h>
45 #else
46 #include <utmp.h>
47 #endif
48 #include <unistd.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <time.h>
52 #ifdef HAVE_VIS
53 #include <vis.h>
54 #endif
55 #include "pax.h"
56 #include "extern.h"
57 
58 __RCSID("$MirOS: src/bin/pax/gen_subs.c,v 1.15 2012/06/05 20:20:39 tg Exp $");
59 
60 /*
61  * a collection of general purpose subroutines used by pax
62  */
63 
64 /*
65  * constants used by ls_list() when printing out archive members
66  */
67 #define MODELEN 20
68 #define DATELEN 64
69 #define SIXMONTHS	 ((DAYSPERNYEAR / 2) * SECSPERDAY)
70 #define CURFRMT		"%b %e %H:%M"
71 #define OLDFRMT		"%b %e  %Y"
72 #define NAME_WIDTH	8
73 
74 /*
75  * ls_list()
76  *	list the members of an archive in ls format
77  */
78 
79 void
ls_list(ARCHD * arcn,time_t now,FILE * fp)80 ls_list(ARCHD *arcn, time_t now, FILE *fp)
81 {
82 	struct stat *sbp;
83 	char f_mode[MODELEN];
84 	char f_date[DATELEN];
85 	const char *timefrmt;
86 	int term;
87 
88 	term = zeroflag ? '\0' : '\n';	/* path termination character */
89 
90 	/*
91 	 * if not verbose, just print the file name
92 	 */
93 	if (!vflag) {
94 		if (zeroflag)
95 			(void)fputs(arcn->name, fp);
96 		else
97 			safe_print(arcn->name, fp);
98 		(void)putc(term, fp);
99 		(void)fflush(fp);
100 		return;
101 	}
102 
103 	/*
104 	 * user wants long mode
105 	 */
106 	sbp = &(arcn->sb);
107 	strmode(sbp->st_mode, f_mode);
108 
109 	if (ltmfrmt == NULL) {
110 		/*
111 		 * no locale specified format. time format based on age
112 		 * compared to the time pax was started.
113 		 */
114 		if ((sbp->st_mtime + SIXMONTHS) <= now)
115 			timefrmt = OLDFRMT;
116 		else
117 			timefrmt = CURFRMT;
118 	} else
119 		timefrmt = ltmfrmt;
120 
121 	/*
122 	 * print file mode, link count, uid, gid and time
123 	 */
124 	if (strftime(f_date,DATELEN,timefrmt,localtime(&(sbp->st_mtime))) == 0)
125 		f_date[0] = '\0';
126 	(void)fprintf(fp, "%s%2u %-*.*s %-*.*s ", f_mode,
127 		(unsigned)sbp->st_nlink,
128 		NAME_WIDTH, UT_NAMESIZE, name_uid(sbp->st_uid, 1),
129 		NAME_WIDTH, UT_NAMESIZE, name_gid(sbp->st_gid, 1));
130 
131 	/*
132 	 * print device id's for devices, or sizes for other nodes
133 	 */
134 	if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK))
135 		(void)fprintf(fp, "%4lu,%4lu ", (unsigned long)MAJOR(sbp->st_rdev),
136 		    (unsigned long)MINOR(sbp->st_rdev));
137 	else
138 		(void)fprintf(fp, "%9" OT_FMT " ", (ot_type)sbp->st_size);
139 
140 	/*
141 	 * print name and link info for hard and soft links
142 	 */
143 	(void)fputs(f_date, fp);
144 	(void)putc(' ', fp);
145 	safe_print(arcn->name, fp);
146 	if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG)) {
147 		fputs(" == ", fp);
148 		safe_print(arcn->ln_name, fp);
149 	} else if (arcn->type == PAX_SLK) {
150 		fputs(" -> ", fp);
151 		safe_print(arcn->ln_name, fp);
152 	}
153 	(void)putc(term, fp);
154 	(void)fflush(fp);
155 	return;
156 }
157 
158 /*
159  * tty_ls()
160  *	print a short summary of file to tty.
161  */
162 
163 void
ls_tty(ARCHD * arcn)164 ls_tty(ARCHD *arcn)
165 {
166 	char f_date[DATELEN];
167 	char f_mode[MODELEN];
168 	const char *timefrmt;
169 
170 	if (ltmfrmt == NULL) {
171 		/*
172 		 * no locale specified format
173 		 */
174 		if ((arcn->sb.st_mtime + SIXMONTHS) <= time(NULL))
175 			timefrmt = OLDFRMT;
176 		else
177 			timefrmt = CURFRMT;
178 	} else
179 		timefrmt = ltmfrmt;
180 
181 	/*
182 	 * convert time to string, and print
183 	 */
184 	if (strftime(f_date, DATELEN, timefrmt,
185 	    localtime(&(arcn->sb.st_mtime))) == 0)
186 		f_date[0] = '\0';
187 	strmode(arcn->sb.st_mode, f_mode);
188 	tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name);
189 	return;
190 }
191 
192 void
safe_print(const char * str,FILE * fp)193 safe_print(const char *str, FILE *fp)
194 {
195 #ifdef HAVE_VIS
196 	char visbuf[5];
197 	const char *cp;
198 
199 	/*
200 	 * if printing to a tty, use vis(3) to print special characters.
201 	 */
202 	if (isatty(fileno(fp))) {
203 		for (cp = str; *cp; cp++) {
204 			(void)vis(visbuf, cp[0], VIS_CSTYLE, cp[1]);
205 			(void)fputs(visbuf, fp);
206 		}
207 	} else
208 #endif
209 		(void)fputs(str, fp);
210 }
211 
212 /*
213  * asc_ul()
214  *	convert hex/octal character string into a u_long. We do not have to
215  *	check for overflow! (the headers in all supported formats are not large
216  *	enough to create an overflow).
217  *	NOTE: strings passed to us are NOT TERMINATED.
218  * Return:
219  *	unsigned long value
220  */
221 
222 u_long
asc_ul(char * str,int len,int base)223 asc_ul(char *str, int len, int base)
224 {
225 	char *stop;
226 	u_long tval = 0;
227 
228 	stop = str + len;
229 
230 	/*
231 	 * skip over leading blanks and zeros
232 	 */
233 	while ((str < stop) && ((*str == ' ') || (*str == '0')))
234 		++str;
235 
236 	/*
237 	 * for each valid digit, shift running value (tval) over to next digit
238 	 * and add next digit
239 	 */
240 	if (base == HEX) {
241 		while (str < stop) {
242 			if ((*str >= '0') && (*str <= '9'))
243 				tval = (tval << 4) + (*str++ - '0');
244 			else if ((*str >= 'A') && (*str <= 'F'))
245 				tval = (tval << 4) + 10 + (*str++ - 'A');
246 			else if ((*str >= 'a') && (*str <= 'f'))
247 				tval = (tval << 4) + 10 + (*str++ - 'a');
248 			else
249 				break;
250 		}
251 	} else {
252 		while ((str < stop) && (*str >= '0') && (*str <= '7'))
253 			tval = (tval << 3) + (*str++ - '0');
254 	}
255 	return(tval);
256 }
257 
258 /*
259  * ul_asc()
260  *	convert an unsigned long into an hex/oct ascii string. pads with LEADING
261  *	ascii 0's to fill string completely
262  *	NOTE: the string created is NOT TERMINATED.
263  */
264 
265 int
ul_asc(u_long val,char * str,int len,int base)266 ul_asc(u_long val, char *str, int len, int base)
267 {
268 	char *pt;
269 	u_long digit;
270 
271 	/*
272 	 * WARNING str is not '\0' terminated by this routine
273 	 */
274 	pt = str + len - 1;
275 
276 	/*
277 	 * do a tailwise conversion (start at right most end of string to place
278 	 * least significant digit). Keep shifting until conversion value goes
279 	 * to zero (all digits were converted)
280 	 */
281 	if (base == HEX) {
282 		while (pt >= str) {
283 			if ((digit = (val & 0xf)) < 10)
284 				*pt-- = '0' + (char)digit;
285 			else
286 				*pt-- = 'a' + (char)(digit - 10);
287 			if ((val = (val >> 4)) == (u_long)0)
288 				break;
289 		}
290 	} else {
291 		while (pt >= str) {
292 			*pt-- = '0' + (char)(val & 0x7);
293 			if ((val = (val >> 3)) == (u_long)0)
294 				break;
295 		}
296 	}
297 
298 	/*
299 	 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
300 	 */
301 	while (pt >= str)
302 		*pt-- = '0';
303 	if (val != (u_long)0)
304 		return(-1);
305 	return(0);
306 }
307 
308 /*
309  * asc_ot()
310  *	convert hex/octal character string into a ot_type. We do not have
311  *	to check for overflow! (the headers in all supported formats are
312  *	not large enough to create an overflow).
313  *	NOTE: strings passed to us are NOT TERMINATED.
314  * Return:
315  *	ot_type value
316  */
317 
318 ot_type
asc_ot(char * str,int len,int base)319 asc_ot(char *str, int len, int base)
320 {
321 	char *stop;
322 	ot_type tval = 0;
323 
324 	stop = str + len;
325 
326 	/*
327 	 * skip over leading blanks and zeros
328 	 */
329 	while ((str < stop) && ((*str == ' ') || (*str == '0')))
330 		++str;
331 
332 	/*
333 	 * for each valid digit, shift running value (tval) over to next digit
334 	 * and add next digit
335 	 */
336 	if (base == HEX) {
337 		while (str < stop) {
338 			if ((*str >= '0') && (*str <= '9'))
339 				tval = (tval << 4) + (*str++ - '0');
340 			else if ((*str >= 'A') && (*str <= 'F'))
341 				tval = (tval << 4) + 10 + (*str++ - 'A');
342 			else if ((*str >= 'a') && (*str <= 'f'))
343 				tval = (tval << 4) + 10 + (*str++ - 'a');
344 			else
345 				break;
346 		}
347 	} else {
348 		while ((str < stop) && (*str >= '0') && (*str <= '7'))
349 			tval = (tval << 3) + (*str++ - '0');
350 	}
351 	return (tval);
352 }
353 
354 /*
355  * ot_asc()
356  *	convert an ot_type into a hex/oct ascii string.
357  *	pads with LEADING ascii 0s to fill string completely.
358  *	NOTE: the string created is NOT TERMINATED.
359  */
360 
361 int
ot_asc(ot_type val,char * str,int len,int base)362 ot_asc(ot_type val, char *str, int len, int base)
363 {
364 	char *pt;
365 	ot_type digit;
366 
367 	/*
368 	 * WARNING str is not '\0' terminated by this routine
369 	 */
370 	pt = str + len - 1;
371 
372 	/*
373 	 * do a tailwise conversion (start at right most end of string to place
374 	 * least significant digit). Keep shifting until conversion value goes
375 	 * to zero (all digits were converted)
376 	 */
377 	if (base == HEX) {
378 		while (pt >= str) {
379 			if ((digit = (val & 0xf)) < 10)
380 				*pt-- = '0' + (char)digit;
381 			else
382 				*pt-- = 'a' + (char)(digit - 10);
383 			if ((val = (val >> 4)) == (ot_type)0)
384 				break;
385 		}
386 	} else {
387 		while (pt >= str) {
388 			*pt-- = '0' + (char)(val & 0x7);
389 			if ((val = (val >> 3)) == (ot_type)0)
390 				break;
391 		}
392 	}
393 
394 	/*
395 	 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
396 	 */
397 	while (pt >= str)
398 		*pt-- = '0';
399 	if (val != (ot_type)0)
400 		return (-1);
401 	return (0);
402 }
403 
404 /*
405  * Copy at max min(bufz, fieldsz) chars from field to buf, stopping
406  * at the first NUL char. NUL terminate buf if there is room left.
407  */
408 size_t
fieldcpy(char * buf,size_t bufsz,const char * field,size_t fieldsz)409 fieldcpy(char *buf, size_t bufsz, const char *field, size_t fieldsz)
410 {
411 	char *p = buf;
412 	const char *q = field;
413 	size_t i = 0;
414 
415 	if (fieldsz > bufsz)
416 		fieldsz = bufsz;
417 	while (i < fieldsz && *q != '\0') {
418 		*p++ = *q++;
419 		i++;
420 	}
421 	if (i < bufsz)
422 		*p = '\0';
423 	return(i);
424 }
425 
426 #ifndef HAVE_STRMODE
427 #include ".linked/strmode.inc"
428 #endif
429 
430 #ifndef HAVE_STRLCPY
431 #undef WIDEC
432 #define OUTSIDE_OF_LIBKERN
433 #define L_strlcat
434 #define L_strlcpy
435 #include ".linked/strlfun.inc"
436 #endif
437