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