1 |
|
|
/* $OpenBSD: gen_subs.c,v 1.28 2015/03/17 03:23:17 guenther Exp $ */ |
2 |
|
|
/* $NetBSD: gen_subs.c,v 1.5 1995/03/21 09:07:26 cgd Exp $ */ |
3 |
|
|
|
4 |
|
|
/*- |
5 |
|
|
* Copyright (c) 1992 Keith Muller. |
6 |
|
|
* Copyright (c) 1992, 1993 |
7 |
|
|
* The Regents of the University of California. All rights reserved. |
8 |
|
|
* |
9 |
|
|
* This code is derived from software contributed to Berkeley by |
10 |
|
|
* Keith Muller of the University of California, San Diego. |
11 |
|
|
* |
12 |
|
|
* Redistribution and use in source and binary forms, with or without |
13 |
|
|
* modification, are permitted provided that the following conditions |
14 |
|
|
* are met: |
15 |
|
|
* 1. Redistributions of source code must retain the above copyright |
16 |
|
|
* notice, this list of conditions and the following disclaimer. |
17 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
18 |
|
|
* notice, this list of conditions and the following disclaimer in the |
19 |
|
|
* documentation and/or other materials provided with the distribution. |
20 |
|
|
* 3. Neither the name of the University nor the names of its contributors |
21 |
|
|
* may be used to endorse or promote products derived from this software |
22 |
|
|
* without specific prior written permission. |
23 |
|
|
* |
24 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
25 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
26 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
27 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
28 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
29 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
30 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
31 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
32 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
33 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 |
|
|
* SUCH DAMAGE. |
35 |
|
|
*/ |
36 |
|
|
|
37 |
|
|
#include <sys/types.h> |
38 |
|
|
#include <sys/time.h> |
39 |
|
|
#include <sys/stat.h> |
40 |
|
|
#include <stdio.h> |
41 |
|
|
#include <utmp.h> |
42 |
|
|
#include <unistd.h> |
43 |
|
|
#include <stdlib.h> |
44 |
|
|
#include <string.h> |
45 |
|
|
#include <vis.h> |
46 |
|
|
#include "pax.h" |
47 |
|
|
#include "extern.h" |
48 |
|
|
|
49 |
|
|
/* |
50 |
|
|
* a collection of general purpose subroutines used by pax |
51 |
|
|
*/ |
52 |
|
|
|
53 |
|
|
/* |
54 |
|
|
* constants used by ls_list() when printing out archive members |
55 |
|
|
*/ |
56 |
|
|
#define MODELEN 20 |
57 |
|
|
#define DATELEN 64 |
58 |
|
|
#define SECSPERDAY (24 * 60 * 60) |
59 |
|
|
#define SIXMONTHS (SECSPERDAY * 365 / 2) |
60 |
|
|
#define CURFRMT "%b %e %H:%M" |
61 |
|
|
#define OLDFRMT "%b %e %Y" |
62 |
|
|
#define NAME_WIDTH 8 |
63 |
|
|
#define TIMEFMT(t, now) \ |
64 |
|
|
(((t) + SIXMONTHS <= (now) || (t) > (now)) ? OLDFRMT : CURFRMT) |
65 |
|
|
|
66 |
|
|
/* |
67 |
|
|
* ls_list() |
68 |
|
|
* list the members of an archive in ls format |
69 |
|
|
*/ |
70 |
|
|
|
71 |
|
|
void |
72 |
|
|
ls_list(ARCHD *arcn, time_t now, FILE *fp) |
73 |
|
100 |
{ |
74 |
|
|
struct stat *sbp; |
75 |
|
|
char f_mode[MODELEN]; |
76 |
|
|
char f_date[DATELEN]; |
77 |
|
|
int term; |
78 |
|
|
|
79 |
✗✓ |
100 |
term = zeroflag ? '\0' : '\n'; /* path termination character */ |
80 |
|
|
|
81 |
|
|
/* |
82 |
|
|
* if not verbose, just print the file name |
83 |
|
|
*/ |
84 |
✓✗ |
100 |
if (!vflag) { |
85 |
✗✓ |
100 |
if (zeroflag) |
86 |
|
|
(void)fputs(arcn->name, fp); |
87 |
|
|
else |
88 |
|
100 |
safe_print(arcn->name, fp); |
89 |
✓✗ |
100 |
(void)putc(term, fp); |
90 |
|
100 |
(void)fflush(fp); |
91 |
|
100 |
return; |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
/* |
95 |
|
|
* user wants long mode |
96 |
|
|
*/ |
97 |
|
|
sbp = &(arcn->sb); |
98 |
|
|
strmode(sbp->st_mode, f_mode); |
99 |
|
|
|
100 |
|
|
/* |
101 |
|
|
* print file mode, link count, uid, gid and time |
102 |
|
|
*/ |
103 |
|
|
if (strftime(f_date, sizeof(f_date), TIMEFMT(sbp->st_mtime, now), |
104 |
|
|
localtime(&(sbp->st_mtime))) == 0) |
105 |
|
|
f_date[0] = '\0'; |
106 |
|
|
(void)fprintf(fp, "%s%2u %-*.*s %-*.*s ", f_mode, sbp->st_nlink, |
107 |
|
|
NAME_WIDTH, UT_NAMESIZE, name_uid(sbp->st_uid, 1), |
108 |
|
|
NAME_WIDTH, UT_NAMESIZE, name_gid(sbp->st_gid, 1)); |
109 |
|
|
|
110 |
|
|
/* |
111 |
|
|
* print device id's for devices, or sizes for other nodes |
112 |
|
|
*/ |
113 |
|
|
if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK)) |
114 |
|
|
(void)fprintf(fp, "%4lu, %4lu ", |
115 |
|
|
(unsigned long)MAJOR(sbp->st_rdev), |
116 |
|
|
(unsigned long)MINOR(sbp->st_rdev)); |
117 |
|
|
else { |
118 |
|
|
(void)fprintf(fp, "%9llu ", sbp->st_size); |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
/* |
122 |
|
|
* print name and link info for hard and soft links |
123 |
|
|
*/ |
124 |
|
|
(void)fputs(f_date, fp); |
125 |
|
|
(void)putc(' ', fp); |
126 |
|
|
safe_print(arcn->name, fp); |
127 |
|
|
if (PAX_IS_HARDLINK(arcn->type)) { |
128 |
|
|
fputs(" == ", fp); |
129 |
|
|
safe_print(arcn->ln_name, fp); |
130 |
|
|
} else if (arcn->type == PAX_SLK) { |
131 |
|
|
fputs(" -> ", fp); |
132 |
|
|
safe_print(arcn->ln_name, fp); |
133 |
|
|
} |
134 |
|
|
(void)putc(term, fp); |
135 |
|
|
(void)fflush(fp); |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
/* |
139 |
|
|
* tty_ls() |
140 |
|
|
* print a short summary of file to tty. |
141 |
|
|
*/ |
142 |
|
|
|
143 |
|
|
void |
144 |
|
|
ls_tty(ARCHD *arcn) |
145 |
|
|
{ |
146 |
|
|
char f_date[DATELEN]; |
147 |
|
|
char f_mode[MODELEN]; |
148 |
|
|
time_t now = time(NULL); |
149 |
|
|
|
150 |
|
|
/* |
151 |
|
|
* convert time to string, and print |
152 |
|
|
*/ |
153 |
|
|
if (strftime(f_date, DATELEN, TIMEFMT(arcn->sb.st_mtime, now), |
154 |
|
|
localtime(&(arcn->sb.st_mtime))) == 0) |
155 |
|
|
f_date[0] = '\0'; |
156 |
|
|
strmode(arcn->sb.st_mode, f_mode); |
157 |
|
|
tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name); |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
void |
161 |
|
|
safe_print(const char *str, FILE *fp) |
162 |
|
100 |
{ |
163 |
|
|
char visbuf[5]; |
164 |
|
|
const char *cp; |
165 |
|
|
|
166 |
|
|
/* |
167 |
|
|
* if printing to a tty, use vis(3) to print special characters. |
168 |
|
|
*/ |
169 |
✓✗✗✓
|
100 |
if (isatty(fileno(fp))) { |
170 |
|
|
for (cp = str; *cp; cp++) { |
171 |
|
|
(void)vis(visbuf, cp[0], VIS_CSTYLE, cp[1]); |
172 |
|
|
(void)fputs(visbuf, fp); |
173 |
|
|
} |
174 |
|
|
} else { |
175 |
|
100 |
(void)fputs(str, fp); |
176 |
|
|
} |
177 |
|
100 |
} |
178 |
|
|
|
179 |
|
|
/* |
180 |
|
|
* asc_ul() |
181 |
|
|
* convert hex/octal character string into a u_long. We do not have to |
182 |
|
|
* check for overflow! (the headers in all supported formats are not large |
183 |
|
|
* enough to create an overflow). |
184 |
|
|
* NOTE: strings passed to us are NOT TERMINATED. |
185 |
|
|
* Return: |
186 |
|
|
* unsigned long value |
187 |
|
|
*/ |
188 |
|
|
|
189 |
|
|
u_long |
190 |
|
|
asc_ul(char *str, int len, int base) |
191 |
|
466 |
{ |
192 |
|
|
char *stop; |
193 |
|
466 |
u_long tval = 0; |
194 |
|
|
|
195 |
|
466 |
stop = str + len; |
196 |
|
|
|
197 |
|
|
/* |
198 |
|
|
* skip over leading blanks and zeros |
199 |
|
|
*/ |
200 |
✓✗✓✓
|
2262 |
while ((str < stop) && ((*str == ' ') || (*str == '0'))) |
201 |
|
1330 |
++str; |
202 |
|
|
|
203 |
|
|
/* |
204 |
|
|
* for each valid digit, shift running value (tval) over to next digit |
205 |
|
|
* and add next digit |
206 |
|
|
*/ |
207 |
✗✓ |
466 |
if (base == HEX) { |
208 |
|
|
while (str < stop) { |
209 |
|
|
if ((*str >= '0') && (*str <= '9')) |
210 |
|
|
tval = (tval << 4) + (*str++ - '0'); |
211 |
|
|
else if ((*str >= 'A') && (*str <= 'F')) |
212 |
|
|
tval = (tval << 4) + 10 + (*str++ - 'A'); |
213 |
|
|
else if ((*str >= 'a') && (*str <= 'f')) |
214 |
|
|
tval = (tval << 4) + 10 + (*str++ - 'a'); |
215 |
|
|
else |
216 |
|
|
break; |
217 |
|
|
} |
218 |
|
|
} else { |
219 |
✓✗✓✓ ✓✗ |
2264 |
while ((str < stop) && (*str >= '0') && (*str <= '7')) |
220 |
|
1798 |
tval = (tval << 3) + (*str++ - '0'); |
221 |
|
|
} |
222 |
|
466 |
return(tval); |
223 |
|
|
} |
224 |
|
|
|
225 |
|
|
/* |
226 |
|
|
* ul_asc() |
227 |
|
|
* convert an unsigned long into an hex/oct ascii string. pads with LEADING |
228 |
|
|
* ascii 0's to fill string completely |
229 |
|
|
* NOTE: the string created is NOT TERMINATED. |
230 |
|
|
*/ |
231 |
|
|
|
232 |
|
|
int |
233 |
|
|
ul_asc(u_long val, char *str, int len, int base) |
234 |
|
|
{ |
235 |
|
|
char *pt; |
236 |
|
|
u_long digit; |
237 |
|
|
|
238 |
|
|
/* |
239 |
|
|
* WARNING str is not '\0' terminated by this routine |
240 |
|
|
*/ |
241 |
|
|
pt = str + len - 1; |
242 |
|
|
|
243 |
|
|
/* |
244 |
|
|
* do a tailwise conversion (start at right most end of string to place |
245 |
|
|
* least significant digit). Keep shifting until conversion value goes |
246 |
|
|
* to zero (all digits were converted) |
247 |
|
|
*/ |
248 |
|
|
if (base == HEX) { |
249 |
|
|
while (pt >= str) { |
250 |
|
|
if ((digit = (val & 0xf)) < 10) |
251 |
|
|
*pt-- = '0' + (char)digit; |
252 |
|
|
else |
253 |
|
|
*pt-- = 'a' + (char)(digit - 10); |
254 |
|
|
if ((val = (val >> 4)) == (u_long)0) |
255 |
|
|
break; |
256 |
|
|
} |
257 |
|
|
} else { |
258 |
|
|
while (pt >= str) { |
259 |
|
|
*pt-- = '0' + (char)(val & 0x7); |
260 |
|
|
if ((val = (val >> 3)) == (u_long)0) |
261 |
|
|
break; |
262 |
|
|
} |
263 |
|
|
} |
264 |
|
|
|
265 |
|
|
/* |
266 |
|
|
* pad with leading ascii ZEROS. We return -1 if we ran out of space. |
267 |
|
|
*/ |
268 |
|
|
while (pt >= str) |
269 |
|
|
*pt-- = '0'; |
270 |
|
|
if (val != (u_long)0) |
271 |
|
|
return(-1); |
272 |
|
|
return(0); |
273 |
|
|
} |
274 |
|
|
|
275 |
|
|
/* |
276 |
|
|
* asc_uqd() |
277 |
|
|
* convert hex/octal character string into a u_quad_t. We do not have to |
278 |
|
|
* check for overflow! (the headers in all supported formats are not large |
279 |
|
|
* enough to create an overflow). |
280 |
|
|
* NOTE: strings passed to us are NOT TERMINATED. |
281 |
|
|
* Return: |
282 |
|
|
* u_quad_t value |
283 |
|
|
*/ |
284 |
|
|
|
285 |
|
|
u_quad_t |
286 |
|
|
asc_uqd(char *str, int len, int base) |
287 |
|
240 |
{ |
288 |
|
|
char *stop; |
289 |
|
240 |
u_quad_t tval = 0; |
290 |
|
|
|
291 |
|
240 |
stop = str + len; |
292 |
|
|
|
293 |
|
|
/* |
294 |
|
|
* skip over leading blanks and zeros |
295 |
|
|
*/ |
296 |
✓✓✓✓
|
1788 |
while ((str < stop) && ((*str == ' ') || (*str == '0'))) |
297 |
|
1308 |
++str; |
298 |
|
|
|
299 |
|
|
/* |
300 |
|
|
* for each valid digit, shift running value (tval) over to next digit |
301 |
|
|
* and add next digit |
302 |
|
|
*/ |
303 |
✗✓ |
240 |
if (base == HEX) { |
304 |
|
|
while (str < stop) { |
305 |
|
|
if ((*str >= '0') && (*str <= '9')) |
306 |
|
|
tval = (tval << 4) + (*str++ - '0'); |
307 |
|
|
else if ((*str >= 'A') && (*str <= 'F')) |
308 |
|
|
tval = (tval << 4) + 10 + (*str++ - 'A'); |
309 |
|
|
else if ((*str >= 'a') && (*str <= 'f')) |
310 |
|
|
tval = (tval << 4) + 10 + (*str++ - 'a'); |
311 |
|
|
else |
312 |
|
|
break; |
313 |
|
|
} |
314 |
|
|
} else { |
315 |
✓✓✓✓ ✓✗ |
1586 |
while ((str < stop) && (*str >= '0') && (*str <= '7')) |
316 |
|
1346 |
tval = (tval << 3) + (*str++ - '0'); |
317 |
|
|
} |
318 |
|
240 |
return(tval); |
319 |
|
|
} |
320 |
|
|
|
321 |
|
|
/* |
322 |
|
|
* uqd_asc() |
323 |
|
|
* convert an u_quad_t into a hex/oct ascii string. pads with LEADING |
324 |
|
|
* ascii 0's to fill string completely |
325 |
|
|
* NOTE: the string created is NOT TERMINATED. |
326 |
|
|
*/ |
327 |
|
|
|
328 |
|
|
int |
329 |
|
|
uqd_asc(u_quad_t val, char *str, int len, int base) |
330 |
|
|
{ |
331 |
|
|
char *pt; |
332 |
|
|
u_quad_t digit; |
333 |
|
|
|
334 |
|
|
/* |
335 |
|
|
* WARNING str is not '\0' terminated by this routine |
336 |
|
|
*/ |
337 |
|
|
pt = str + len - 1; |
338 |
|
|
|
339 |
|
|
/* |
340 |
|
|
* do a tailwise conversion (start at right most end of string to place |
341 |
|
|
* least significant digit). Keep shifting until conversion value goes |
342 |
|
|
* to zero (all digits were converted) |
343 |
|
|
*/ |
344 |
|
|
if (base == HEX) { |
345 |
|
|
while (pt >= str) { |
346 |
|
|
if ((digit = (val & 0xf)) < 10) |
347 |
|
|
*pt-- = '0' + (char)digit; |
348 |
|
|
else |
349 |
|
|
*pt-- = 'a' + (char)(digit - 10); |
350 |
|
|
if ((val = (val >> 4)) == (u_quad_t)0) |
351 |
|
|
break; |
352 |
|
|
} |
353 |
|
|
} else { |
354 |
|
|
while (pt >= str) { |
355 |
|
|
*pt-- = '0' + (char)(val & 0x7); |
356 |
|
|
if ((val = (val >> 3)) == (u_quad_t)0) |
357 |
|
|
break; |
358 |
|
|
} |
359 |
|
|
} |
360 |
|
|
|
361 |
|
|
/* |
362 |
|
|
* pad with leading ascii ZEROS. We return -1 if we ran out of space. |
363 |
|
|
*/ |
364 |
|
|
while (pt >= str) |
365 |
|
|
*pt-- = '0'; |
366 |
|
|
if (val != (u_quad_t)0) |
367 |
|
|
return(-1); |
368 |
|
|
return(0); |
369 |
|
|
} |
370 |
|
|
|
371 |
|
|
/* |
372 |
|
|
* Copy at max min(bufz, fieldsz) chars from field to buf, stopping |
373 |
|
|
* at the first NUL char. NUL terminate buf if there is room left. |
374 |
|
|
*/ |
375 |
|
|
size_t |
376 |
|
|
fieldcpy(char *buf, size_t bufsz, const char *field, size_t fieldsz) |
377 |
|
322 |
{ |
378 |
|
322 |
char *p = buf; |
379 |
|
322 |
const char *q = field; |
380 |
|
322 |
size_t i = 0; |
381 |
|
|
|
382 |
✓✓ |
322 |
if (fieldsz > bufsz) |
383 |
|
22 |
fieldsz = bufsz; |
384 |
✓✓✓✓
|
12126 |
while (i < fieldsz && *q != '\0') { |
385 |
|
11804 |
*p++ = *q++; |
386 |
|
11804 |
i++; |
387 |
|
|
} |
388 |
✓✓ |
322 |
if (i < bufsz) |
389 |
|
316 |
*p = '\0'; |
390 |
|
322 |
return(i); |
391 |
|
|
} |