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