1 /* $OpenBSD: gen_subs.c,v 1.21 2012/12/04 02:24:45 deraadt 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 #include <sys/types.h> 38 #include <sys/time.h> 39 #include <sys/stat.h> 40 #include <stdio.h> 41 #include <tzfile.h> 42 #include <utmp.h> 43 #include <unistd.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include <vis.h> 47 #include "pax.h" 48 #include "extern.h" 49 50 /* 51 * a collection of general purpose subroutines used by pax 52 */ 53 54 /* 55 * constants used by ls_list() when printing out archive members 56 */ 57 #define MODELEN 20 58 #define DATELEN 64 59 #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY) 60 #define CURFRMT "%b %e %H:%M" 61 #define OLDFRMT "%b %e %Y" 62 #define NAME_WIDTH 8 63 64 /* 65 * ls_list() 66 * list the members of an archive in ls format 67 */ 68 69 void 70 ls_list(ARCHD *arcn, time_t now, FILE *fp) 71 { 72 struct stat *sbp; 73 char f_mode[MODELEN]; 74 char f_date[DATELEN]; 75 const char *timefrmt; 76 int term; 77 78 term = zeroflag ? '\0' : '\n'; /* path termination character */ 79 80 /* 81 * if not verbose, just print the file name 82 */ 83 if (!vflag) { 84 if (zeroflag) 85 (void)fputs(arcn->name, fp); 86 else 87 safe_print(arcn->name, fp); 88 (void)putc(term, fp); 89 (void)fflush(fp); 90 return; 91 } 92 93 /* 94 * user wants long mode 95 */ 96 sbp = &(arcn->sb); 97 strmode(sbp->st_mode, f_mode); 98 99 if (ltmfrmt == NULL) { 100 /* 101 * no locale specified format. time format based on age 102 * compared to the time pax was started. 103 */ 104 if ((sbp->st_mtime + SIXMONTHS) <= now) 105 timefrmt = OLDFRMT; 106 else 107 timefrmt = CURFRMT; 108 } else 109 timefrmt = ltmfrmt; 110 111 /* 112 * print file mode, link count, uid, gid and time 113 */ 114 if (strftime(f_date,DATELEN,timefrmt,localtime(&(sbp->st_mtime))) == 0) 115 f_date[0] = '\0'; 116 (void)fprintf(fp, "%s%2u %-*.*s %-*.*s ", f_mode, sbp->st_nlink, 117 NAME_WIDTH, UT_NAMESIZE, name_uid(sbp->st_uid, 1), 118 NAME_WIDTH, UT_NAMESIZE, name_gid(sbp->st_gid, 1)); 119 120 /* 121 * print device id's for devices, or sizes for other nodes 122 */ 123 if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK)) 124 # ifdef LONG_OFF_T 125 (void)fprintf(fp, "%4u,%4u ", MAJOR(sbp->st_rdev), 126 # else 127 (void)fprintf(fp, "%4lu,%4lu ", (unsigned long)MAJOR(sbp->st_rdev), 128 # endif 129 (unsigned long)MINOR(sbp->st_rdev)); 130 else { 131 # ifdef LONG_OFF_T 132 (void)fprintf(fp, "%9lu ", sbp->st_size); 133 # else 134 (void)fprintf(fp, "%9qu ", sbp->st_size); 135 # endif 136 } 137 138 /* 139 * print name and link info for hard and soft links 140 */ 141 (void)fputs(f_date, fp); 142 (void)putc(' ', fp); 143 safe_print(arcn->name, fp); 144 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG)) { 145 fputs(" == ", fp); 146 safe_print(arcn->ln_name, fp); 147 } else if (arcn->type == PAX_SLK) { 148 fputs(" -> ", fp); 149 safe_print(arcn->ln_name, fp); 150 } 151 (void)putc(term, 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 (ltmfrmt == NULL) { 169 /* 170 * no locale specified format 171 */ 172 if ((arcn->sb.st_mtime + SIXMONTHS) <= time(NULL)) 173 timefrmt = OLDFRMT; 174 else 175 timefrmt = CURFRMT; 176 } else 177 timefrmt = ltmfrmt; 178 179 /* 180 * convert time to string, and print 181 */ 182 if (strftime(f_date, DATELEN, timefrmt, 183 localtime(&(arcn->sb.st_mtime))) == 0) 184 f_date[0] = '\0'; 185 strmode(arcn->sb.st_mode, f_mode); 186 tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name); 187 return; 188 } 189 190 void 191 safe_print(const char *str, FILE *fp) 192 { 193 char visbuf[5]; 194 const char *cp; 195 196 /* 197 * if printing to a tty, use vis(3) to print special characters. 198 */ 199 if (isatty(fileno(fp))) { 200 for (cp = str; *cp; cp++) { 201 (void)vis(visbuf, cp[0], VIS_CSTYLE, cp[1]); 202 (void)fputs(visbuf, fp); 203 } 204 } else { 205 (void)fputs(str, fp); 206 } 207 } 208 209 /* 210 * asc_ul() 211 * convert hex/octal character string into a u_long. We do not have to 212 * check for overflow! (the headers in all supported formats are not large 213 * enough to create an overflow). 214 * NOTE: strings passed to us are NOT TERMINATED. 215 * Return: 216 * unsigned long value 217 */ 218 219 u_long 220 asc_ul(char *str, int len, int base) 221 { 222 char *stop; 223 u_long tval = 0; 224 225 stop = str + len; 226 227 /* 228 * skip over leading blanks and zeros 229 */ 230 while ((str < stop) && ((*str == ' ') || (*str == '0'))) 231 ++str; 232 233 /* 234 * for each valid digit, shift running value (tval) over to next digit 235 * and add next digit 236 */ 237 if (base == HEX) { 238 while (str < stop) { 239 if ((*str >= '0') && (*str <= '9')) 240 tval = (tval << 4) + (*str++ - '0'); 241 else if ((*str >= 'A') && (*str <= 'F')) 242 tval = (tval << 4) + 10 + (*str++ - 'A'); 243 else if ((*str >= 'a') && (*str <= 'f')) 244 tval = (tval << 4) + 10 + (*str++ - 'a'); 245 else 246 break; 247 } 248 } else { 249 while ((str < stop) && (*str >= '0') && (*str <= '7')) 250 tval = (tval << 3) + (*str++ - '0'); 251 } 252 return(tval); 253 } 254 255 /* 256 * ul_asc() 257 * convert an unsigned long into an hex/oct ascii string. pads with LEADING 258 * ascii 0's to fill string completely 259 * NOTE: the string created is NOT TERMINATED. 260 */ 261 262 int 263 ul_asc(u_long val, char *str, int len, int base) 264 { 265 char *pt; 266 u_long digit; 267 268 /* 269 * WARNING str is not '\0' terminated by this routine 270 */ 271 pt = str + len - 1; 272 273 /* 274 * do a tailwise conversion (start at right most end of string to place 275 * least significant digit). Keep shifting until conversion value goes 276 * to zero (all digits were converted) 277 */ 278 if (base == HEX) { 279 while (pt >= str) { 280 if ((digit = (val & 0xf)) < 10) 281 *pt-- = '0' + (char)digit; 282 else 283 *pt-- = 'a' + (char)(digit - 10); 284 if ((val = (val >> 4)) == (u_long)0) 285 break; 286 } 287 } else { 288 while (pt >= str) { 289 *pt-- = '0' + (char)(val & 0x7); 290 if ((val = (val >> 3)) == (u_long)0) 291 break; 292 } 293 } 294 295 /* 296 * pad with leading ascii ZEROS. We return -1 if we ran out of space. 297 */ 298 while (pt >= str) 299 *pt-- = '0'; 300 if (val != (u_long)0) 301 return(-1); 302 return(0); 303 } 304 305 #ifndef LONG_OFF_T 306 /* 307 * asc_uqd() 308 * convert hex/octal character string into a u_quad_t. We do not have to 309 * check for overflow! (the headers in all supported formats are not large 310 * enough to create an overflow). 311 * NOTE: strings passed to us are NOT TERMINATED. 312 * Return: 313 * u_quad_t value 314 */ 315 316 u_quad_t 317 asc_uqd(char *str, int len, int base) 318 { 319 char *stop; 320 u_quad_t tval = 0; 321 322 stop = str + len; 323 324 /* 325 * skip over leading blanks and zeros 326 */ 327 while ((str < stop) && ((*str == ' ') || (*str == '0'))) 328 ++str; 329 330 /* 331 * for each valid digit, shift running value (tval) over to next digit 332 * and add next digit 333 */ 334 if (base == HEX) { 335 while (str < stop) { 336 if ((*str >= '0') && (*str <= '9')) 337 tval = (tval << 4) + (*str++ - '0'); 338 else if ((*str >= 'A') && (*str <= 'F')) 339 tval = (tval << 4) + 10 + (*str++ - 'A'); 340 else if ((*str >= 'a') && (*str <= 'f')) 341 tval = (tval << 4) + 10 + (*str++ - 'a'); 342 else 343 break; 344 } 345 } else { 346 while ((str < stop) && (*str >= '0') && (*str <= '7')) 347 tval = (tval << 3) + (*str++ - '0'); 348 } 349 return(tval); 350 } 351 352 /* 353 * uqd_asc() 354 * convert an u_quad_t into a hex/oct ascii string. pads with LEADING 355 * ascii 0's to fill string completely 356 * NOTE: the string created is NOT TERMINATED. 357 */ 358 359 int 360 uqd_asc(u_quad_t val, char *str, int len, int base) 361 { 362 char *pt; 363 u_quad_t digit; 364 365 /* 366 * WARNING str is not '\0' terminated by this routine 367 */ 368 pt = str + len - 1; 369 370 /* 371 * do a tailwise conversion (start at right most end of string to place 372 * least significant digit). Keep shifting until conversion value goes 373 * to zero (all digits were converted) 374 */ 375 if (base == HEX) { 376 while (pt >= str) { 377 if ((digit = (val & 0xf)) < 10) 378 *pt-- = '0' + (char)digit; 379 else 380 *pt-- = 'a' + (char)(digit - 10); 381 if ((val = (val >> 4)) == (u_quad_t)0) 382 break; 383 } 384 } else { 385 while (pt >= str) { 386 *pt-- = '0' + (char)(val & 0x7); 387 if ((val = (val >> 3)) == (u_quad_t)0) 388 break; 389 } 390 } 391 392 /* 393 * pad with leading ascii ZEROS. We return -1 if we ran out of space. 394 */ 395 while (pt >= str) 396 *pt-- = '0'; 397 if (val != (u_quad_t)0) 398 return(-1); 399 return(0); 400 } 401 #endif 402 403 /* 404 * Copy at max min(bufz, fieldsz) chars from field to buf, stopping 405 * at the first NUL char. NUL terminate buf if there is room left. 406 */ 407 size_t 408 fieldcpy(char *buf, size_t bufsz, const char *field, size_t fieldsz) 409 { 410 char *p = buf; 411 const char *q = field; 412 size_t i = 0; 413 414 if (fieldsz > bufsz) 415 fieldsz = bufsz; 416 while (i < fieldsz && *q != '\0') { 417 *p++ = *q++; 418 i++; 419 } 420 if (i < bufsz) 421 *p = '\0'; 422 return(i); 423 } 424