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