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