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