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