minor fixes for safecopy & safemap tests
[minix.git] / commands / pax / gen_subs.c
blob5785a694a14a3eb4fdd73855ce0585f393460e57
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 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)gen_subs.c 8.1 (Berkeley) 5/31/93";
37 #endif
38 #endif /* not lint */
40 #include <sys/types.h>
41 #include <sys/time.h>
42 #include <sys/stat.h>
43 #include <stdint.h>
44 #include <stdio.h>
45 #include <utmp.h>
46 #include <unistd.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <time.h>
50 #include "pax.h"
51 #include "extern.h"
54 * a collection of general purpose subroutines used by pax
58 * constants used by ls_list() when printing out archive members
60 #define MODELEN 20
61 #define DATELEN 64
62 #define SIXMONTHS ((365 / 2) * 86400)
63 #define CURFRMTM "%b %e %H:%M"
64 #define OLDFRMTM "%b %e %Y"
65 #define CURFRMTD "%e %b %H:%M"
66 #define OLDFRMTD "%e %b %Y"
67 #ifndef UT_NAMESIZE
68 #define UT_NAMESIZE 8
69 #endif
70 #define UT_GRPSIZE 6
72 static int d_first = -1;
75 * ls_list()
76 * list the members of an archive in ls format
79 void
80 ls_list(ARCHD *arcn, time_t now, FILE *fp)
82 struct stat *sbp;
83 char f_mode[MODELEN];
84 char f_date[DATELEN];
85 const char *timefrmt;
88 * if not verbose, just print the file name
90 if (!vflag) {
91 (void)fprintf(fp, "%s\n", arcn->name);
92 (void)fflush(fp);
93 return;
96 if (d_first < 0)
97 d_first = 0;
99 * user wants long mode
101 sbp = &(arcn->sb);
102 #if 0
103 strmode(sbp->st_mode, f_mode);
104 #else
105 strcpy(f_mode, "");
106 #endif
109 * time format based on age compared to the time pax was started.
111 if ((sbp->st_mtime + SIXMONTHS) <= now)
112 timefrmt = d_first ? OLDFRMTD : OLDFRMTM;
113 else
114 timefrmt = d_first ? CURFRMTD : CURFRMTM;
117 * print file mode, link count, uid, gid and time
119 #if 0
120 if (strftime(f_date,DATELEN,timefrmt,localtime(&(sbp->st_mtime))) == 0)
121 #endif
122 f_date[0] = '\0';
123 (void)fprintf(fp, "%s%2u %-*s %-*s ", f_mode, sbp->st_nlink,
124 UT_NAMESIZE, name_uid(sbp->st_uid, 1), UT_GRPSIZE,
125 name_gid(sbp->st_gid, 1));
128 * print device id's for devices, or sizes for other nodes
130 if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK))
131 # ifdef NET2_STAT
132 (void)fprintf(fp, "%4u,%4u ", major(sbp->st_rdev),
133 minor(sbp->st_rdev));
134 # else
135 (void)fprintf(fp, "%4lu,%4lu ", (unsigned long)major(sbp->st_rdev),
136 (unsigned long)minor(sbp->st_rdev));
137 # endif
138 else {
139 # ifdef NET2_STAT
140 (void)fprintf(fp, "%9lu ", sbp->st_size);
141 # else
142 (void)fprintf(fp, "%9ju ", (uintmax_t)sbp->st_size);
143 # endif
147 * print name and link info for hard and soft links
149 (void)fprintf(fp, "%s %s", f_date, arcn->name);
150 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
151 (void)fprintf(fp, " == %s\n", arcn->ln_name);
152 else if (arcn->type == PAX_SLK)
153 (void)fprintf(fp, " => %s\n", arcn->ln_name);
154 else
155 (void)putc('\n', fp);
156 (void)fflush(fp);
157 return;
161 * tty_ls()
162 * print a short summary of file to tty.
165 void
166 ls_tty(ARCHD *arcn)
168 char f_date[DATELEN];
169 char f_mode[MODELEN];
170 const char *timefrmt;
172 if (d_first < 0)
173 d_first = 0;
175 if ((arcn->sb.st_mtime + SIXMONTHS) <= time(NULL))
176 timefrmt = d_first ? OLDFRMTD : OLDFRMTM;
177 else
178 timefrmt = d_first ? CURFRMTD : CURFRMTM;
181 * convert time to string, and print
183 #if 0
184 if (strftime(f_date, DATELEN, timefrmt,
185 localtime(&(arcn->sb.st_mtime))) == 0)
186 #endif
187 f_date[0] = '\0';
188 #if 0
189 strmode(arcn->sb.st_mode, f_mode);
190 #else
191 strcpy(f_mode, "");
192 #endif
193 tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name);
194 return;
198 * l_strncpy()
199 * copy src to dest up to len chars (stopping at first '\0').
200 * when src is shorter than len, pads to len with '\0'.
201 * Return:
202 * number of chars copied. (Note this is a real performance win over
203 * doing a strncpy(), a strlen(), and then a possible memset())
207 l_strncpy(char *dest, const char *src, int len)
209 char *stop;
210 char *start;
212 stop = dest + len;
213 start = dest;
214 while ((dest < stop) && (*src != '\0'))
215 *dest++ = *src++;
216 len = dest - start;
217 while (dest < stop)
218 *dest++ = '\0';
219 return(len);
223 * asc_ul()
224 * convert hex/octal character string into a u_long. We do not have to
225 * check for overflow! (the headers in all supported formats are not large
226 * enough to create an overflow).
227 * NOTE: strings passed to us are NOT TERMINATED.
228 * Return:
229 * unsigned long value
232 u_long
233 asc_ul(char *str, int len, int base)
235 char *stop;
236 u_long tval = 0;
238 stop = str + len;
241 * skip over leading blanks and zeros
243 while ((str < stop) && ((*str == ' ') || (*str == '0')))
244 ++str;
247 * for each valid digit, shift running value (tval) over to next digit
248 * and add next digit
250 if (base == HEX) {
251 while (str < stop) {
252 if ((*str >= '0') && (*str <= '9'))
253 tval = (tval << 4) + (*str++ - '0');
254 else if ((*str >= 'A') && (*str <= 'F'))
255 tval = (tval << 4) + 10 + (*str++ - 'A');
256 else if ((*str >= 'a') && (*str <= 'f'))
257 tval = (tval << 4) + 10 + (*str++ - 'a');
258 else
259 break;
261 } else {
262 while ((str < stop) && (*str >= '0') && (*str <= '7'))
263 tval = (tval << 3) + (*str++ - '0');
265 return(tval);
269 * ul_asc()
270 * convert an unsigned long into an hex/oct ascii string. pads with LEADING
271 * ascii 0's to fill string completely
272 * NOTE: the string created is NOT TERMINATED.
276 ul_asc(u_long val, char *str, int len, int base)
278 char *pt;
279 u_long digit;
282 * WARNING str is not '\0' terminated by this routine
284 pt = str + len - 1;
287 * do a tailwise conversion (start at right most end of string to place
288 * least significant digit). Keep shifting until conversion value goes
289 * to zero (all digits were converted)
291 if (base == HEX) {
292 while (pt >= str) {
293 if ((digit = (val & 0xf)) < 10)
294 *pt-- = '0' + (char)digit;
295 else
296 *pt-- = 'a' + (char)(digit - 10);
297 if ((val = (val >> 4)) == (u_long)0)
298 break;
300 } else {
301 while (pt >= str) {
302 *pt-- = '0' + (char)(val & 0x7);
303 if ((val = (val >> 3)) == (u_long)0)
304 break;
309 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
311 while (pt >= str)
312 *pt-- = '0';
313 if (val != (u_long)0)
314 return(-1);
315 return(0);
318 #ifndef NET2_STAT
320 * asc_uqd()
321 * convert hex/octal character string into a u_quad_t. We do not have to
322 * check for overflow! (the headers in all supported formats are not large
323 * enough to create an overflow).
324 * NOTE: strings passed to us are NOT TERMINATED.
325 * Return:
326 * u_quad_t value
329 u_quad_t
330 asc_uqd(char *str, int len, int base)
332 char *stop;
333 u_quad_t tval = 0;
335 stop = str + len;
338 * skip over leading blanks and zeros
340 while ((str < stop) && ((*str == ' ') || (*str == '0')))
341 ++str;
344 * for each valid digit, shift running value (tval) over to next digit
345 * and add next digit
347 if (base == HEX) {
348 while (str < stop) {
349 if ((*str >= '0') && (*str <= '9'))
350 tval = (tval << 4) + (*str++ - '0');
351 else if ((*str >= 'A') && (*str <= 'F'))
352 tval = (tval << 4) + 10 + (*str++ - 'A');
353 else if ((*str >= 'a') && (*str <= 'f'))
354 tval = (tval << 4) + 10 + (*str++ - 'a');
355 else
356 break;
358 } else {
359 while ((str < stop) && (*str >= '0') && (*str <= '7'))
360 tval = (tval << 3) + (*str++ - '0');
362 return(tval);
366 * uqd_asc()
367 * convert an u_quad_t into a hex/oct ascii string. pads with LEADING
368 * ascii 0's to fill string completely
369 * NOTE: the string created is NOT TERMINATED.
373 uqd_asc(u_quad_t val, char *str, int len, int base)
375 char *pt;
376 u_quad_t digit;
379 * WARNING str is not '\0' terminated by this routine
381 pt = str + len - 1;
384 * do a tailwise conversion (start at right most end of string to place
385 * least significant digit). Keep shifting until conversion value goes
386 * to zero (all digits were converted)
388 if (base == HEX) {
389 while (pt >= str) {
390 if ((digit = (val & 0xf)) < 10)
391 *pt-- = '0' + (char)digit;
392 else
393 *pt-- = 'a' + (char)(digit - 10);
394 if ((val = (val >> 4)) == (u_quad_t)0)
395 break;
397 } else {
398 while (pt >= str) {
399 *pt-- = '0' + (char)(val & 0x7);
400 if ((val = (val >> 3)) == (u_quad_t)0)
401 break;
406 * pad with leading ascii ZEROS. We return -1 if we ran out of space.
408 while (pt >= str)
409 *pt-- = '0';
410 if (val != (u_quad_t)0)
411 return(-1);
412 return(0);
414 #endif