Debian Codegraph @

gen_subs.c

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