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