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