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