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