| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
#include <sys/types.h> |
| 40 |
#if HAVE_BOTH_TIME_H |
| 41 |
#include <sys/time.h> |
| 42 |
#include <time.h> |
| 43 |
#elif HAVE_SYS_TIME_H |
| 44 |
#include <sys/time.h> |
| 45 |
#elif HAVE_TIME_H |
| 46 |
#include <time.h> |
| 47 |
#endif |
| 48 |
#include <sys/stat.h> |
| 49 |
#if HAVE_GRP_H |
| 50 |
#include <grp.h> |
| 51 |
#endif |
| 52 |
#include <pwd.h> |
| 53 |
#include <stdio.h> |
| 54 |
#include <stdlib.h> |
| 55 |
#include <string.h> |
| 56 |
#if HAVE_STRINGS_H |
| 57 |
#include <strings.h> |
| 58 |
#endif |
| 59 |
#include <unistd.h> |
| 60 |
#if HAVE_UTMP_H |
| 61 |
#include <utmp.h> |
| 62 |
#elif HAVE_UTMPX_H |
| 63 |
#include <utmpx.h> |
| 64 |
#endif |
| 65 |
#if HAVE_VIS_H |
| 66 |
#include <vis.h> |
| 67 |
#endif |
| 68 |
|
| 69 |
#include "pax.h" |
| 70 |
#include "extern.h" |
| 71 |
|
| 72 |
__RCSID("$MirOS: src/bin/pax/gen_subs.c,v 1.22 2021/09/23 21:15:49 tg Exp $"); |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
#define MODELEN 20 |
| 82 |
#define DATELEN 64 |
| 83 |
#define SIXMONTHS (24 * 60 * 60 * 365 / 2) |
| 84 |
#define CURFRMT "%b %e %H:%M" |
| 85 |
#define OLDFRMT "%b %e %Y" |
| 86 |
#define NAME_WIDTH 8 |
| 87 |
#define TIMEFMT(t) \ |
| 88 |
(((t) + SIXMONTHS <= now || (t) > now) ? OLDFRMT : CURFRMT) |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
void |
| 96 |
ls_list(ARCHD *arcn, FILE *fp) |
| 97 |
{ |
| 98 |
struct stat *sbp; |
| 99 |
char f_mode[MODELEN]; |
| 100 |
char f_date[DATELEN]; |
| 101 |
int term; |
| 102 |
|
| 103 |
term = zeroflag ? '\0' : '\n'; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
if (!vflag) { |
| 109 |
if (zeroflag) |
| 110 |
(void)fputs(arcn->name, fp); |
| 111 |
else |
| 112 |
safe_print(arcn->name, fp); |
| 113 |
(void)putc(term, fp); |
| 114 |
(void)fflush(fp); |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
sbp = &(arcn->sb); |
| 122 |
strmode(sbp->st_mode, f_mode); |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
if (strftime(f_date, sizeof(f_date), TIMEFMT(sbp->st_mtime), |
| 128 |
localtime(&(sbp->st_mtime))) == 0) |
| 129 |
f_date[0] = '\0'; |
| 130 |
(void)fprintf(fp, "%s%2u %-*.*s %-*.*s ", f_mode, |
| 131 |
(unsigned int)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 |
|
| 137 |
|
| 138 |
if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK)) |
| 139 |
(void)fprintf(fp, "%4lu, %4lu ", |
| 140 |
(unsigned long)MAJOR(sbp->st_rdev), |
| 141 |
(unsigned long)MINOR(sbp->st_rdev)); |
| 142 |
else |
| 143 |
(void)fprintf(fp, "%9" OT_FMT " ", sbp->st_size); |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
(void)fputs(f_date, fp); |
| 149 |
(void)putc(' ', fp); |
| 150 |
safe_print(arcn->name, fp); |
| 151 |
if (PAX_IS_HARDLINK(arcn->type)) { |
| 152 |
fputs(" == ", fp); |
| 153 |
safe_print(arcn->ln_name, fp); |
| 154 |
} else if (arcn->type == PAX_SLK) { |
| 155 |
fputs(" -> ", fp); |
| 156 |
safe_print(arcn->ln_name, fp); |
| 157 |
} |
| 158 |
(void)putc(term, fp); |
| 159 |
(void)fflush(fp); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
void |
| 168 |
ls_tty(ARCHD *arcn) |
| 169 |
{ |
| 170 |
char f_date[DATELEN]; |
| 171 |
char f_mode[MODELEN]; |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
if (strftime(f_date, DATELEN, TIMEFMT(arcn->sb.st_mtime), |
| 177 |
localtime(&(arcn->sb.st_mtime))) == 0) |
| 178 |
f_date[0] = '\0'; |
| 179 |
strmode(arcn->sb.st_mode, f_mode); |
| 180 |
tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name); |
| 181 |
} |
| 182 |
|
| 183 |
void |
| 184 |
safe_print(const char *str, FILE *fp) |
| 185 |
{ |
| 186 |
#if HAVE_VIS_H |
| 187 |
char visbuf[5]; |
| 188 |
const char *cp; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
if (isatty(fileno(fp))) { |
| 194 |
for (cp = str; *cp; cp++) { |
| 195 |
(void)vis(visbuf, cp[0], VIS_CSTYLE, cp[1]); |
| 196 |
(void)fputs(visbuf, fp); |
| 197 |
} |
| 198 |
} else |
| 199 |
#endif |
| 200 |
(void)fputs(str, fp); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
u_long |
| 214 |
asc_ul(char *str, int len, int base) |
| 215 |
{ |
| 216 |
char *stop; |
| 217 |
u_long tval = 0; |
| 218 |
|
| 219 |
stop = str + len; |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
while ((str < stop) && ((*str == ' ') || (*str == '0'))) |
| 225 |
++str; |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
if (base == HEX) { |
| 232 |
while (str < stop) { |
| 233 |
if ((*str >= '0') && (*str <= '9')) |
| 234 |
tval = (tval << 4) + (*str++ - '0'); |
| 235 |
else if ((*str >= 'A') && (*str <= 'F')) |
| 236 |
tval = (tval << 4) + 10 + (*str++ - 'A'); |
| 237 |
else if ((*str >= 'a') && (*str <= 'f')) |
| 238 |
tval = (tval << 4) + 10 + (*str++ - 'a'); |
| 239 |
else |
| 240 |
break; |
| 241 |
} |
| 242 |
} else { |
| 243 |
while ((str < stop) && (*str >= '0') && (*str <= '7')) |
| 244 |
tval = (tval << 3) + (*str++ - '0'); |
| 245 |
} |
| 246 |
return(tval); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
int |
| 257 |
ul_asc(u_long val, char *str, int len, int base) |
| 258 |
{ |
| 259 |
char *pt; |
| 260 |
u_long digit; |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
pt = str + len - 1; |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
if (base == HEX) { |
| 273 |
while (pt >= str) { |
| 274 |
if ((digit = (val & 0xf)) < 10) |
| 275 |
*pt-- = '0' + (char)digit; |
| 276 |
else |
| 277 |
*pt-- = 'a' + (char)(digit - 10); |
| 278 |
val >>= 4; |
| 279 |
if (val == 0) |
| 280 |
break; |
| 281 |
} |
| 282 |
} else { |
| 283 |
while (pt >= str) { |
| 284 |
*pt-- = '0' + (char)(val & 0x7); |
| 285 |
val >>= 3; |
| 286 |
if (val == 0) |
| 287 |
break; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
while (pt >= str) |
| 295 |
*pt-- = '0'; |
| 296 |
if (val != 0) |
| 297 |
return(-1); |
| 298 |
return(0); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
unsigned long long |
| 312 |
asc_ull(char *str, int len, int base) |
| 313 |
{ |
| 314 |
char *stop; |
| 315 |
unsigned long long tval = 0; |
| 316 |
|
| 317 |
stop = str + len; |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
while ((str < stop) && ((*str == ' ') || (*str == '0'))) |
| 323 |
++str; |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
if (base == HEX) { |
| 330 |
while (str < stop) { |
| 331 |
if ((*str >= '0') && (*str <= '9')) |
| 332 |
tval = (tval << 4) + (*str++ - '0'); |
| 333 |
else if ((*str >= 'A') && (*str <= 'F')) |
| 334 |
tval = (tval << 4) + 10 + (*str++ - 'A'); |
| 335 |
else if ((*str >= 'a') && (*str <= 'f')) |
| 336 |
tval = (tval << 4) + 10 + (*str++ - 'a'); |
| 337 |
else |
| 338 |
break; |
| 339 |
} |
| 340 |
} else { |
| 341 |
while ((str < stop) && (*str >= '0') && (*str <= '7')) |
| 342 |
tval = (tval << 3) + (*str++ - '0'); |
| 343 |
} |
| 344 |
return(tval); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
int |
| 355 |
ull_asc(unsigned long long val, char *str, int len, int base) |
| 356 |
{ |
| 357 |
char *pt; |
| 358 |
unsigned long long digit; |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
pt = str + len - 1; |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
if (base == HEX) { |
| 371 |
while (pt >= str) { |
| 372 |
if ((digit = (val & 0xf)) < 10) |
| 373 |
*pt-- = '0' + (char)digit; |
| 374 |
else |
| 375 |
*pt-- = 'a' + (char)(digit - 10); |
| 376 |
val >>= 4; |
| 377 |
if (val == 0) |
| 378 |
break; |
| 379 |
} |
| 380 |
} else { |
| 381 |
while (pt >= str) { |
| 382 |
*pt-- = '0' + (char)(val & 0x7); |
| 383 |
val >>= 3; |
| 384 |
if (val == 0) |
| 385 |
break; |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
while (pt >= str) |
| 393 |
*pt-- = '0'; |
| 394 |
if (val != 0) |
| 395 |
return(-1); |
| 396 |
return(0); |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
size_t |
| 404 |
fieldcpy(char *buf, size_t bufsz, const char *field, size_t fieldsz) |
| 405 |
{ |
| 406 |
char *p = buf; |
| 407 |
const char *q = field; |
| 408 |
size_t i = 0; |
| 409 |
|
| 410 |
if (fieldsz > bufsz) |
| 411 |
fieldsz = bufsz; |
| 412 |
while (i < fieldsz && *q != '\0') { |
| 413 |
*p++ = *q++; |
| 414 |
i++; |
| 415 |
} |
| 416 |
if (i < bufsz) |
| 417 |
*p = '\0'; |
| 418 |
return(i); |
| 419 |
} |