o Correct quirks entry for a MP3 player
[dragonfly.git] / bin / pax / gen_subs.c
blob14bf6dbd386e0755484e1365c4721d0f45de40a4
1 /*-
2 * Copyright (c) 1992 Keith Muller.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Keith Muller of the University of California, San Diego.
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.
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.
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.6 2006/09/27 19:18:00 pavalos Exp $
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"
55 * a collection of general purpose subroutines used by pax
59 * constants used by ls_list() when printing out archive members
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
73 static int d_first = -1;
76 * ls_list()
77 * list the members of an archive in ls format
80 void
81 ls_list(ARCHD *arcn, time_t now, FILE *fp)
83 struct stat *sbp;
84 char f_mode[MODELEN];
85 char f_date[DATELEN];
86 char *timefrmt;
89 * if not verbose, just print the file name
91 if (!vflag) {
92 fprintf(fp, "%s\n", arcn->name);
93 fflush(fp);
94 return;
97 if (d_first < 0)
98 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
100 * user wants long mode
102 sbp = &(arcn->sb);
103 strmode(sbp->st_mode, f_mode);
106 * time format based on age compared to the time pax was started.
108 if ((sbp->st_mtime + SIXMONTHS) <= now)
109 timefrmt = d_first ? OLDFRMTD : OLDFRMTM;
110 else
111 timefrmt = d_first ? CURFRMTD : CURFRMTM;
114 * print file mode, link count, uid, gid and time
116 if (strftime(f_date,DATELEN,timefrmt,localtime(&(sbp->st_mtime))) == 0)
117 f_date[0] = '\0';
118 fprintf(fp, "%s%2u %-*s %-*s ", f_mode, sbp->st_nlink,
119 UT_NAMESIZE, name_uid(sbp->st_uid, 1), UT_GRPSIZE,
120 name_gid(sbp->st_gid, 1));
123 * print device id's for devices, or sizes for other nodes
125 if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK))
126 fprintf(fp, "%4lu,%4lu ", (unsigned long)MAJOR(sbp->st_rdev),
127 (unsigned long)MINOR(sbp->st_rdev));
128 else {
129 fprintf(fp, "%9qu ", sbp->st_size);
133 * print name and link info for hard and soft links
135 fprintf(fp, "%s %s", f_date, arcn->name);
136 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
137 fprintf(fp, " == %s\n", arcn->ln_name);
138 else if (arcn->type == PAX_SLK)
139 fprintf(fp, " => %s\n", arcn->ln_name);
140 else
141 putc('\n', fp);
142 fflush(fp);
143 return;
147 * tty_ls()
148 * print a short summary of file to tty.
151 void
152 ls_tty(ARCHD *arcn)
154 char f_date[DATELEN];
155 char f_mode[MODELEN];
156 char *timefrmt;
158 if (d_first < 0)
159 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
161 if ((arcn->sb.st_mtime + SIXMONTHS) <= time((time_t *)NULL))
162 timefrmt = d_first ? OLDFRMTD : OLDFRMTM;
163 else
164 timefrmt = d_first ? CURFRMTD : CURFRMTM;
167 * convert time to string, and print
169 if (strftime(f_date, DATELEN, timefrmt,
170 localtime(&(arcn->sb.st_mtime))) == 0)
171 f_date[0] = '\0';
172 strmode(arcn->sb.st_mode, f_mode);
173 tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name);
174 return;
178 * l_strncpy()
179 * copy src to dest up to len chars (stopping at first '\0').
180 * when src is shorter than len, pads to len with '\0'.
181 * Return:
182 * number of chars copied. (Note this is a real performance win over
183 * doing a strncpy(), a strlen(), and then a possible memset())
187 l_strncpy(char *dest, char *src, int len)
189 char *stop;
190 char *start;
192 stop = dest + len;
193 start = dest;
194 while ((dest < stop) && (*src != '\0'))
195 *dest++ = *src++;
196 len = dest - start;
197 while (dest < stop)
198 *dest++ = '\0';
199 return(len);
203 * asc_ul()
204 * convert hex/octal character string into a u_long. We do not have to
205 * check for overflow! (the headers in all supported formats are not large
206 * enough to create an overflow).
207 * NOTE: strings passed to us are NOT TERMINATED.
208 * Return:
209 * unsigned long value
212 u_long
213 asc_ul(char *str, int len, int base)
215 char *stop;
216 u_long tval = 0;
218 stop = str + len;
221 * skip over leading blanks and zeros
223 while ((str < stop) && ((*str == ' ') || (*str == '0')))
224 ++str;
227 * for each valid digit, shift running value (tval) over to next digit
228 * and add next digit
230 if (base == HEX) {
231 while (str < stop) {
232 if ((*str >= '0') && (*str <= '9'))
233 tval = (tval << 4) + (*str++ - '0');
234 else if ((*str >= 'A') && (*str <= 'F'))
235 tval = (tval << 4) + 10 + (*str++ - 'A');
236 else if ((*str >= 'a') && (*str <= 'f'))
237 tval = (tval << 4) + 10 + (*str++ - 'a');
238 else
239 break;
241 } else {
242 while ((str < stop) && (*str >= '0') && (*str <= '7'))
243 tval = (tval << 3) + (*str++ - '0');
245 return(tval);
249 * ul_asc()
250 * convert an unsigned long into an hex/oct ascii string. pads with LEADING
251 * ascii 0's to fill string completely
252 * NOTE: the string created is NOT TERMINATED.
256 ul_asc(u_long val, char *str, int len, int base)
258 char *pt;
259 u_long digit;
262 * WARNING str is not '\0' terminated by this routine
264 pt = str + len - 1;
267 * do a tailwise conversion (start at right most end of string to place
268 * least significant digit). Keep shifting until conversion value goes
269 * to zero (all digits were converted)
271 if (base == HEX) {
272 while (pt >= str) {
273 if ((digit = (val & 0xf)) < 10)
274 *pt-- = '0' + (char)digit;
275 else
276 *pt-- = 'a' + (char)(digit - 10);
277 if ((val = (val >> 4)) == (u_long)0)
278 break;
280 } else {
281 while (pt >= str) {
282 *pt-- = '0' + (char)(val & 0x7);
283 if ((val = (val >> 3)) == (u_long)0)
284 break;
289 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
291 while (pt >= str)
292 *pt-- = '0';
293 if (val != (u_long)0)
294 return(-1);
295 return(0);
299 * asc_uqd()
300 * convert hex/octal character string into a u_quad_t. We do not have to
301 * check for overflow! (the headers in all supported formats are not large
302 * enough to create an overflow).
303 * NOTE: strings passed to us are NOT TERMINATED.
304 * Return:
305 * u_quad_t value
308 u_quad_t
309 asc_uqd(char *str, int len, int base)
311 char *stop;
312 u_quad_t tval = 0;
314 stop = str + len;
317 * skip over leading blanks and zeros
319 while ((str < stop) && ((*str == ' ') || (*str == '0')))
320 ++str;
323 * for each valid digit, shift running value (tval) over to next digit
324 * and add next digit
326 if (base == HEX) {
327 while (str < stop) {
328 if ((*str >= '0') && (*str <= '9'))
329 tval = (tval << 4) + (*str++ - '0');
330 else if ((*str >= 'A') && (*str <= 'F'))
331 tval = (tval << 4) + 10 + (*str++ - 'A');
332 else if ((*str >= 'a') && (*str <= 'f'))
333 tval = (tval << 4) + 10 + (*str++ - 'a');
334 else
335 break;
337 } else {
338 while ((str < stop) && (*str >= '0') && (*str <= '7'))
339 tval = (tval << 3) + (*str++ - '0');
341 return(tval);
345 * uqd_asc()
346 * convert an u_quad_t into a hex/oct ascii string. pads with LEADING
347 * ascii 0's to fill string completely
348 * NOTE: the string created is NOT TERMINATED.
352 uqd_asc(u_quad_t val, char *str, int len, int base)
354 char *pt;
355 u_quad_t digit;
358 * WARNING str is not '\0' terminated by this routine
360 pt = str + len - 1;
363 * do a tailwise conversion (start at right most end of string to place
364 * least significant digit). Keep shifting until conversion value goes
365 * to zero (all digits were converted)
367 if (base == HEX) {
368 while (pt >= str) {
369 if ((digit = (val & 0xf)) < 10)
370 *pt-- = '0' + (char)digit;
371 else
372 *pt-- = 'a' + (char)(digit - 10);
373 if ((val = (val >> 4)) == (u_quad_t)0)
374 break;
376 } else {
377 while (pt >= str) {
378 *pt-- = '0' + (char)(val & 0x7);
379 if ((val = (val >> 3)) == (u_quad_t)0)
380 break;
385 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
387 while (pt >= str)
388 *pt-- = '0';
389 if (val != (u_quad_t)0)
390 return(-1);
391 return(0);