]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - bin/pax/gen_subs.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / bin / pax / gen_subs.c
1 /*-
2  * Copyright (c) 1992 Keith Muller.
3  * Copyright (c) 1992, 1993
4  *      The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Keith Muller of the University of California, San Diego.
8  *
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.
20  *
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.
32  */
33
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 */
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
42 #include <sys/types.h>
43 #include <sys/time.h>
44 #include <sys/stat.h>
45 #include <langinfo.h>
46 #include <stdint.h>
47 #include <stdio.h>
48 #include <utmp.h>
49 #include <unistd.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include "pax.h"
53 #include "extern.h"
54
55 /*
56  * a collection of general purpose subroutines used by pax
57  */
58
59 /*
60  * constants used by ls_list() when printing out archive members
61  */
62 #define MODELEN 20
63 #define DATELEN 64
64 #define SIXMONTHS        ((365 / 2) * 86400)
65 #define CURFRMTM        "%b %e %H:%M"
66 #define OLDFRMTM        "%b %e  %Y"
67 #define CURFRMTD        "%e %b %H:%M"
68 #define OLDFRMTD        "%e %b  %Y"
69 #ifndef UT_NAMESIZE
70 #define UT_NAMESIZE     8
71 #endif
72 #define UT_GRPSIZE      6
73
74 static int d_first = -1;
75
76 /*
77  * ls_list()
78  *      list the members of an archive in ls format
79  */
80
81 void
82 ls_list(ARCHD *arcn, time_t now, FILE *fp)
83 {
84         struct stat *sbp;
85         char f_mode[MODELEN];
86         char f_date[DATELEN];
87         const char *timefrmt;
88
89         /*
90          * if not verbose, just print the file name
91          */
92         if (!vflag) {
93                 (void)fprintf(fp, "%s\n", arcn->name);
94                 (void)fflush(fp);
95                 return;
96         }
97
98         if (d_first < 0)
99                 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
100         /*
101          * user wants long mode
102          */
103         sbp = &(arcn->sb);
104         strmode(sbp->st_mode, f_mode);
105
106         /*
107          * time format based on age compared to the time pax was started.
108          */
109         if ((sbp->st_mtime + SIXMONTHS) <= now)
110                 timefrmt = d_first ? OLDFRMTD : OLDFRMTM;
111         else
112                 timefrmt = d_first ? CURFRMTD : CURFRMTM;
113
114         /*
115          * print file mode, link count, uid, gid and time
116          */
117         if (strftime(f_date,DATELEN,timefrmt,localtime(&(sbp->st_mtime))) == 0)
118                 f_date[0] = '\0';
119         (void)fprintf(fp, "%s%2u %-*s %-*s ", f_mode, sbp->st_nlink,
120                 UT_NAMESIZE, name_uid(sbp->st_uid, 1), UT_GRPSIZE,
121                 name_gid(sbp->st_gid, 1));
122
123         /*
124          * print device id's for devices, or sizes for other nodes
125          */
126         if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK))
127 #               ifdef NET2_STAT
128                 (void)fprintf(fp, "%4u,%4u ", MAJOR(sbp->st_rdev),
129                     MINOR(sbp->st_rdev));
130 #               else
131                 (void)fprintf(fp, "%4lu,%4lu ", (unsigned long)MAJOR(sbp->st_rdev),
132                     (unsigned long)MINOR(sbp->st_rdev));
133 #               endif
134         else {
135 #               ifdef NET2_STAT
136                 (void)fprintf(fp, "%9lu ", sbp->st_size);
137 #               else
138                 (void)fprintf(fp, "%9ju ", (uintmax_t)sbp->st_size);
139 #               endif
140         }
141
142         /*
143          * print name and link info for hard and soft links
144          */
145         (void)fprintf(fp, "%s %s", f_date, arcn->name);
146         if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
147                 (void)fprintf(fp, " == %s\n", arcn->ln_name);
148         else if (arcn->type == PAX_SLK)
149                 (void)fprintf(fp, " => %s\n", arcn->ln_name);
150         else
151                 (void)putc('\n', fp);
152         (void)fflush(fp);
153         return;
154 }
155
156 /*
157  * tty_ls()
158  *      print a short summary of file to tty.
159  */
160
161 void
162 ls_tty(ARCHD *arcn)
163 {
164         char f_date[DATELEN];
165         char f_mode[MODELEN];
166         const char *timefrmt;
167
168         if (d_first < 0)
169                 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
170
171         if ((arcn->sb.st_mtime + SIXMONTHS) <= time(NULL))
172                 timefrmt = d_first ? OLDFRMTD : OLDFRMTM;
173         else
174                 timefrmt = d_first ? CURFRMTD : CURFRMTM;
175
176         /*
177          * convert time to string, and print
178          */
179         if (strftime(f_date, DATELEN, timefrmt,
180             localtime(&(arcn->sb.st_mtime))) == 0)
181                 f_date[0] = '\0';
182         strmode(arcn->sb.st_mode, f_mode);
183         tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name);
184         return;
185 }
186
187 /*
188  * l_strncpy()
189  *      copy src to dest up to len chars (stopping at first '\0').
190  *      when src is shorter than len, pads to len with '\0'. 
191  * Return:
192  *      number of chars copied. (Note this is a real performance win over
193  *      doing a strncpy(), a strlen(), and then a possible memset())
194  */
195
196 int
197 l_strncpy(char *dest, const char *src, int len)
198 {
199         char *stop;
200         char *start;
201
202         stop = dest + len;
203         start = dest;
204         while ((dest < stop) && (*src != '\0'))
205                 *dest++ = *src++;
206         len = dest - start;
207         while (dest < stop)
208                 *dest++ = '\0';
209         return(len);
210 }
211
212 /*
213  * asc_ul()
214  *      convert hex/octal character string into a u_long. We do not have to
215  *      check for overflow! (the headers in all supported formats are not large
216  *      enough to create an overflow).
217  *      NOTE: strings passed to us are NOT TERMINATED.
218  * Return:
219  *      unsigned long value
220  */
221
222 u_long
223 asc_ul(char *str, int len, int base)
224 {
225         char *stop;
226         u_long tval = 0;
227
228         stop = str + len;
229
230         /*
231          * skip over leading blanks and zeros
232          */
233         while ((str < stop) && ((*str == ' ') || (*str == '0')))
234                 ++str;
235
236         /*
237          * for each valid digit, shift running value (tval) over to next digit
238          * and add next digit
239          */
240         if (base == HEX) {
241                 while (str < stop) {
242                         if ((*str >= '0') && (*str <= '9'))
243                                 tval = (tval << 4) + (*str++ - '0');
244                         else if ((*str >= 'A') && (*str <= 'F'))
245                                 tval = (tval << 4) + 10 + (*str++ - 'A');
246                         else if ((*str >= 'a') && (*str <= 'f'))
247                                 tval = (tval << 4) + 10 + (*str++ - 'a');
248                         else
249                                 break;
250                 }
251         } else {
252                 while ((str < stop) && (*str >= '0') && (*str <= '7'))
253                         tval = (tval << 3) + (*str++ - '0');
254         }
255         return(tval);
256 }
257
258 /*
259  * ul_asc()
260  *      convert an unsigned long into an hex/oct ascii string. pads with LEADING
261  *      ascii 0's to fill string completely
262  *      NOTE: the string created is NOT TERMINATED.
263  */
264
265 int
266 ul_asc(u_long val, char *str, int len, int base)
267 {
268         char *pt;
269         u_long digit;
270
271         /*
272          * WARNING str is not '\0' terminated by this routine
273          */
274         pt = str + len - 1;
275
276         /*
277          * do a tailwise conversion (start at right most end of string to place
278          * least significant digit). Keep shifting until conversion value goes
279          * to zero (all digits were converted)
280          */
281         if (base == HEX) {
282                 while (pt >= str) {
283                         if ((digit = (val & 0xf)) < 10)
284                                 *pt-- = '0' + (char)digit;
285                         else
286                                 *pt-- = 'a' + (char)(digit - 10);
287                         if ((val = (val >> 4)) == (u_long)0)
288                                 break;
289                 }
290         } else {
291                 while (pt >= str) {
292                         *pt-- = '0' + (char)(val & 0x7);
293                         if ((val = (val >> 3)) == (u_long)0)
294                                 break;
295                 }
296         }
297
298         /*
299          * pad with leading ascii ZEROS. We return -1 if we ran out of space.
300          */
301         while (pt >= str)
302                 *pt-- = '0';
303         if (val != (u_long)0)
304                 return(-1);
305         return(0);
306 }
307
308 #ifndef NET2_STAT
309 /*
310  * asc_uqd()
311  *      convert hex/octal character string into a u_quad_t. We do not have to
312  *      check for overflow! (the headers in all supported formats are not large
313  *      enough to create an overflow).
314  *      NOTE: strings passed to us are NOT TERMINATED.
315  * Return:
316  *      u_quad_t value
317  */
318
319 u_quad_t
320 asc_uqd(char *str, int len, int base)
321 {
322         char *stop;
323         u_quad_t tval = 0;
324
325         stop = str + len;
326
327         /*
328          * skip over leading blanks and zeros
329          */
330         while ((str < stop) && ((*str == ' ') || (*str == '0')))
331                 ++str;
332
333         /*
334          * for each valid digit, shift running value (tval) over to next digit
335          * and add next digit
336          */
337         if (base == HEX) {
338                 while (str < stop) {
339                         if ((*str >= '0') && (*str <= '9'))
340                                 tval = (tval << 4) + (*str++ - '0');
341                         else if ((*str >= 'A') && (*str <= 'F'))
342                                 tval = (tval << 4) + 10 + (*str++ - 'A');
343                         else if ((*str >= 'a') && (*str <= 'f'))
344                                 tval = (tval << 4) + 10 + (*str++ - 'a');
345                         else
346                                 break;
347                 }
348         } else {
349                 while ((str < stop) && (*str >= '0') && (*str <= '7'))
350                         tval = (tval << 3) + (*str++ - '0');
351         }
352         return(tval);
353 }
354
355 /*
356  * uqd_asc()
357  *      convert an u_quad_t into a hex/oct ascii string. pads with LEADING
358  *      ascii 0's to fill string completely
359  *      NOTE: the string created is NOT TERMINATED.
360  */
361
362 int
363 uqd_asc(u_quad_t val, char *str, int len, int base)
364 {
365         char *pt;
366         u_quad_t digit;
367
368         /*
369          * WARNING str is not '\0' terminated by this routine
370          */
371         pt = str + len - 1;
372
373         /*
374          * do a tailwise conversion (start at right most end of string to place
375          * least significant digit). Keep shifting until conversion value goes
376          * to zero (all digits were converted)
377          */
378         if (base == HEX) {
379                 while (pt >= str) {
380                         if ((digit = (val & 0xf)) < 10)
381                                 *pt-- = '0' + (char)digit;
382                         else
383                                 *pt-- = 'a' + (char)(digit - 10);
384                         if ((val = (val >> 4)) == (u_quad_t)0)
385                                 break;
386                 }
387         } else {
388                 while (pt >= str) {
389                         *pt-- = '0' + (char)(val & 0x7);
390                         if ((val = (val >> 3)) == (u_quad_t)0)
391                                 break;
392                 }
393         }
394
395         /*
396          * pad with leading ascii ZEROS. We return -1 if we ran out of space.
397          */
398         while (pt >= str)
399                 *pt-- = '0';
400         if (val != (u_quad_t)0)
401                 return(-1);
402         return(0);
403 }
404 #endif