1 /* $NetBSD: gen_subs.c,v 1.36 2012/08/09 08:09:21 christos 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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #if HAVE_NBTOOL_CONFIG_H
37 #include "nbtool_config.h"
38 #endif
39
40 #include <sys/cdefs.h>
41 #if !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.36 2012/08/09 08:09:21 christos 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 <vis.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <time.h>
62 #include <tzfile.h>
63 #include <unistd.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 * convert time to string
87 */
88 static void
formattime(char * buf,size_t buflen,time_t when)89 formattime(char *buf, size_t buflen, time_t when)
90 {
91 int error;
92 struct tm tm;
93 (void)localtime_r(&when, &tm);
94
95 if (when + SIXMONTHS <= time(NULL))
96 error = strftime(buf, buflen, OLDFRMT, &tm);
97 else
98 error = strftime(buf, buflen, CURFRMT, &tm);
99
100 if (error == 0)
101 buf[0] = '\0';
102 }
103
104 /*
105 * ls_list()
106 * list the members of an archive in ls format
107 */
108
109 void
ls_list(ARCHD * arcn,time_t now,FILE * fp)110 ls_list(ARCHD *arcn, time_t now, FILE *fp)
111 {
112 struct stat *sbp;
113 char f_mode[MODELEN];
114 char f_date[DATELEN];
115 const char *user, *group;
116
117 /*
118 * if not verbose, just print the file name
119 */
120 if (!vflag) {
121 (void)fprintf(fp, "%s\n", arcn->name);
122 (void)fflush(fp);
123 return;
124 }
125
126 /*
127 * user wants long mode
128 */
129 sbp = &(arcn->sb);
130 strmode(sbp->st_mode, f_mode);
131
132 /*
133 * time format based on age compared to the time pax was started.
134 */
135 formattime(f_date, sizeof(f_date), arcn->sb.st_mtime);
136 /*
137 * print file mode, link count, uid, gid and time
138 */
139 user = user_from_uid(sbp->st_uid, 0);
140 group = group_from_gid(sbp->st_gid, 0);
141 (void)fprintf(fp, "%s%2lu %-*s %-*s ", f_mode,
142 (unsigned long)sbp->st_nlink,
143 UT_NAMESIZE, user ? user : "", UT_GRPSIZE, group ? group : "");
144
145 /*
146 * print device id's for devices, or sizes for other nodes
147 */
148 if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK))
149 (void)fprintf(fp, "%4lu,%4lu ", (long) MAJOR(sbp->st_rdev),
150 (long) MINOR(sbp->st_rdev));
151 else {
152 (void)fprintf(fp, OFFT_FP("9") " ", (OFFT_T)sbp->st_size);
153 }
154
155 /*
156 * print name and link info for hard and soft links
157 */
158 (void)fprintf(fp, "%s %s", f_date, arcn->name);
159 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
160 (void)fprintf(fp, " == %s\n", arcn->ln_name);
161 else if (arcn->type == PAX_SLK)
162 (void)fprintf(fp, " -> %s\n", arcn->ln_name);
163 else
164 (void)fputc('\n', fp);
165 (void)fflush(fp);
166 }
167
168 /*
169 * tty_ls()
170 * print a short summary of file to tty.
171 */
172
173 void
ls_tty(ARCHD * arcn)174 ls_tty(ARCHD *arcn)
175 {
176 char f_date[DATELEN];
177 char f_mode[MODELEN];
178
179 formattime(f_date, sizeof(f_date), arcn->sb.st_mtime);
180 strmode(arcn->sb.st_mode, f_mode);
181 tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name);
182 return;
183 }
184
185 void
safe_print(const char * str,FILE * fp)186 safe_print(const char *str, FILE *fp)
187 {
188 char visbuf[5];
189 const char *cp;
190
191 /*
192 * if printing to a tty, use vis(3) to print special characters.
193 */
194 if (isatty(fileno(fp))) {
195 for (cp = str; *cp; cp++) {
196 (void)vis(visbuf, cp[0], VIS_CSTYLE, cp[1]);
197 (void)fputs(visbuf, fp);
198 }
199 } else {
200 (void)fputs(str, fp);
201 }
202 }
203
204 /*
205 * asc_u32()
206 * convert hex/octal character string into a uint32_t. We do not have to
207 * check for overflow! (the headers in all supported formats are not large
208 * enough to create an overflow).
209 * NOTE: strings passed to us are NOT TERMINATED.
210 * Return:
211 * uint32_t value
212 */
213
214 uint32_t
asc_u32(char * str,int len,int base)215 asc_u32(char *str, int len, int base)
216 {
217 char *stop;
218 uint32_t tval = 0;
219
220 stop = str + len;
221
222 /*
223 * skip over leading blanks and zeros
224 */
225 while ((str < stop) && ((*str == ' ') || (*str == '0')))
226 ++str;
227
228 /*
229 * for each valid digit, shift running value (tval) over to next digit
230 * and add next digit
231 */
232 if (base == HEX) {
233 while (str < stop) {
234 if ((*str >= '0') && (*str <= '9'))
235 tval = (tval << 4) + (*str++ - '0');
236 else if ((*str >= 'A') && (*str <= 'F'))
237 tval = (tval << 4) + 10 + (*str++ - 'A');
238 else if ((*str >= 'a') && (*str <= 'f'))
239 tval = (tval << 4) + 10 + (*str++ - 'a');
240 else
241 break;
242 }
243 } else {
244 while ((str < stop) && (*str >= '0') && (*str <= '7'))
245 tval = (tval << 3) + (*str++ - '0');
246 }
247 return tval;
248 }
249
250 /*
251 * u32_asc()
252 * convert an uintmax_t into an hex/oct ascii string. pads with LEADING
253 * ascii 0's to fill string completely
254 * NOTE: the string created is NOT TERMINATED.
255 */
256
257 int
u32_asc(uintmax_t val,char * str,int len,int base)258 u32_asc(uintmax_t val, char *str, int len, int base)
259 {
260 char *pt;
261 uint32_t digit;
262 uintmax_t p;
263
264 p = val & TOP_HALF;
265 if (p && p != TOP_HALF)
266 return -1;
267
268 val &= BOTTOM_HALF;
269
270 /*
271 * WARNING str is not '\0' terminated by this routine
272 */
273 pt = str + len - 1;
274
275 /*
276 * do a tailwise conversion (start at right most end of string to place
277 * least significant digit). Keep shifting until conversion value goes
278 * to zero (all digits were converted)
279 */
280 if (base == HEX) {
281 while (pt >= str) {
282 if ((digit = (val & 0xf)) < 10)
283 *pt-- = '0' + (char)digit;
284 else
285 *pt-- = 'a' + (char)(digit - 10);
286 if ((val = (val >> 4)) == (u_long)0)
287 break;
288 }
289 } else {
290 while (pt >= str) {
291 *pt-- = '0' + (char)(val & 0x7);
292 if ((val = (val >> 3)) == 0)
293 break;
294 }
295 }
296
297 /*
298 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
299 */
300 while (pt >= str)
301 *pt-- = '0';
302 if (val != 0)
303 return -1;
304 return 0;
305 }
306
307 /*
308 * asc_umax()
309 * convert hex/octal character string into a uintmax. We do
310 * not have to to check for overflow! (the headers in all supported
311 * formats are not large enough to create an overflow).
312 * NOTE: strings passed to us are NOT TERMINATED.
313 * Return:
314 * uintmax_t value
315 */
316
317 uintmax_t
asc_umax(char * str,int len,int base)318 asc_umax(char *str, int len, int base)
319 {
320 char *stop;
321 uintmax_t tval = 0;
322
323 stop = str + len;
324
325 /*
326 * skip over leading blanks and zeros
327 */
328 while ((str < stop) && ((*str == ' ') || (*str == '0')))
329 ++str;
330
331 /*
332 * for each valid digit, shift running value (tval) over to next digit
333 * and add next digit
334 */
335 if (base == HEX) {
336 while (str < stop) {
337 if ((*str >= '0') && (*str <= '9'))
338 tval = (tval << 4) + (*str++ - '0');
339 else if ((*str >= 'A') && (*str <= 'F'))
340 tval = (tval << 4) + 10 + (*str++ - 'A');
341 else if ((*str >= 'a') && (*str <= 'f'))
342 tval = (tval << 4) + 10 + (*str++ - 'a');
343 else
344 break;
345 }
346 } else {
347 while ((str < stop) && (*str >= '0') && (*str <= '7'))
348 tval = (tval << 3) + (*str++ - '0');
349 }
350 return tval;
351 }
352
353 /*
354 * umax_asc()
355 * convert an uintmax_t into a hex/oct ascii string. pads with
356 * LEADING ascii 0's to fill string completely
357 * NOTE: the string created is NOT TERMINATED.
358 */
359
360 int
umax_asc(uintmax_t val,char * str,int len,int base)361 umax_asc(uintmax_t val, char *str, int len, int base)
362 {
363 char *pt;
364 uintmax_t digit;
365
366 /*
367 * WARNING str is not '\0' terminated by this routine
368 */
369 pt = str + len - 1;
370
371 /*
372 * do a tailwise conversion (start at right most end of string to place
373 * least significant digit). Keep shifting until conversion value goes
374 * to zero (all digits were converted)
375 */
376 if (base == HEX) {
377 while (pt >= str) {
378 if ((digit = (val & 0xf)) < 10)
379 *pt-- = '0' + (char)digit;
380 else
381 *pt-- = 'a' + (char)(digit - 10);
382 if ((val = (val >> 4)) == 0)
383 break;
384 }
385 } else {
386 while (pt >= str) {
387 *pt-- = '0' + (char)(val & 0x7);
388 if ((val = (val >> 3)) == 0)
389 break;
390 }
391 }
392
393 /*
394 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
395 */
396 while (pt >= str)
397 *pt-- = '0';
398 if (val != 0)
399 return -1;
400 return 0;
401 }
402
403 int
check_Aflag(void)404 check_Aflag(void)
405 {
406
407 if (Aflag > 0)
408 return 1;
409 if (Aflag == 0) {
410 Aflag = -1;
411 tty_warn(0,
412 "Removing leading / from absolute path names in the archive");
413 }
414 return 0;
415 }
416