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