print.c (88584) | print.c (88586) |
---|---|
1/* 2 * Copyright (c) 1989, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Michael Fischbein. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 25 unchanged lines hidden (view full) --- 34 * SUCH DAMAGE. 35 */ 36 37#ifndef lint 38#if 0 39static char sccsid[] = "@(#)print.c 8.4 (Berkeley) 4/17/94"; 40#else 41static const char rcsid[] = | 1/* 2 * Copyright (c) 1989, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Michael Fischbein. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 25 unchanged lines hidden (view full) --- 34 * SUCH DAMAGE. 35 */ 36 37#ifndef lint 38#if 0 39static char sccsid[] = "@(#)print.c 8.4 (Berkeley) 4/17/94"; 40#else 41static const char rcsid[] = |
42 "$FreeBSD: head/bin/ls/print.c 88584 2001-12-28 18:20:23Z joe $"; | 42 "$FreeBSD: head/bin/ls/print.c 88586 2001-12-28 18:43:32Z joe $"; |
43#endif 44#endif /* not lint */ 45 46#include <sys/param.h> 47#include <sys/stat.h> 48 49#include <err.h> 50#include <errno.h> --- 24 unchanged lines hidden (view full) --- 75static int colortype __P((mode_t)); 76#endif 77 78#define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT) 79 80#ifdef COLORLS 81/* Most of these are taken from <sys/stat.h> */ 82typedef enum Colors { | 43#endif 44#endif /* not lint */ 45 46#include <sys/param.h> 47#include <sys/stat.h> 48 49#include <err.h> 50#include <errno.h> --- 24 unchanged lines hidden (view full) --- 75static int colortype __P((mode_t)); 76#endif 77 78#define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT) 79 80#ifdef COLORLS 81/* Most of these are taken from <sys/stat.h> */ 82typedef enum Colors { |
83 C_DIR, /* directory */ 84 C_LNK, /* symbolic link */ 85 C_SOCK, /* socket */ 86 C_FIFO, /* pipe */ 87 C_EXEC, /* executable */ 88 C_BLK, /* block special */ 89 C_CHR, /* character special */ 90 C_SUID, /* setuid executable */ 91 C_SGID, /* setgid executable */ 92 C_WSDIR, /* directory writeble to others, with sticky bit */ 93 C_WDIR, /* directory writeble to others, without sticky bit */ 94 C_NUMCOLORS /* just a place-holder */ 95} Colors ; | 83 C_DIR, /* directory */ 84 C_LNK, /* symbolic link */ 85 C_SOCK, /* socket */ 86 C_FIFO, /* pipe */ 87 C_EXEC, /* executable */ 88 C_BLK, /* block special */ 89 C_CHR, /* character special */ 90 C_SUID, /* setuid executable */ 91 C_SGID, /* setgid executable */ 92 C_WSDIR, /* directory writeble to others, with sticky bit */ 93 C_WDIR, /* directory writeble to others, without sticky bit */ 94 C_NUMCOLORS /* just a place-holder */ 95} Colors; |
96 97char *defcolors = "exfxcxdxbxegedabagacad"; 98 99/* colors for file types */ 100static struct { | 96 97char *defcolors = "exfxcxdxbxegedabagacad"; 98 99/* colors for file types */ 100static struct { |
101 int num[2]; 102 int bold; | 101 int num[2]; 102 int bold; |
103} colors[C_NUMCOLORS]; 104 105#endif 106 107void 108printscol(dp) | 103} colors[C_NUMCOLORS]; 104 105#endif 106 107void 108printscol(dp) |
109 DISPLAY *dp; | 109 DISPLAY *dp; |
110{ | 110{ |
111 FTSENT *p; | 111 FTSENT *p; |
112 113 for (p = dp->list; p; p = p->fts_link) { 114 if (IS_NOPRINT(p)) 115 continue; 116 (void)printaname(p, dp->s_inode, dp->s_block); 117 (void)putchar('\n'); 118 } 119} --- 13 unchanged lines hidden (view full) --- 133 return printf("%s", name); 134} 135 136void 137printlong(dp) 138 DISPLAY *dp; 139{ 140 struct stat *sp; | 112 113 for (p = dp->list; p; p = p->fts_link) { 114 if (IS_NOPRINT(p)) 115 continue; 116 (void)printaname(p, dp->s_inode, dp->s_block); 117 (void)putchar('\n'); 118 } 119} --- 13 unchanged lines hidden (view full) --- 133 return printf("%s", name); 134} 135 136void 137printlong(dp) 138 DISPLAY *dp; 139{ 140 struct stat *sp; |
141 FTSENT *p; 142 NAMES *np; 143 char buf[20]; | 141 FTSENT *p; 142 NAMES *np; 143 char buf[20]; |
144#ifdef COLORLS | 144#ifdef COLORLS |
145 int color_printed = 0; | 145 int color_printed = 0; |
146#endif 147 148 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) 149 (void)printf("total %lu\n", howmany(dp->btotal, blocksize)); 150 151 for (p = dp->list; p; p = p->fts_link) { 152 if (IS_NOPRINT(p)) 153 continue; --- 45 unchanged lines hidden (view full) --- 199 if (S_ISLNK(sp->st_mode)) 200 printlink(p); 201 (void)putchar('\n'); 202 } 203} 204 205void 206printcol(dp) | 146#endif 147 148 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) 149 (void)printf("total %lu\n", howmany(dp->btotal, blocksize)); 150 151 for (p = dp->list; p; p = p->fts_link) { 152 if (IS_NOPRINT(p)) 153 continue; --- 45 unchanged lines hidden (view full) --- 199 if (S_ISLNK(sp->st_mode)) 200 printlink(p); 201 (void)putchar('\n'); 202 } 203} 204 205void 206printcol(dp) |
207 DISPLAY *dp; | 207 DISPLAY *dp; |
208{ 209 extern int termwidth; 210 static FTSENT **array; 211 static int lastentries = -1; | 208{ 209 extern int termwidth; 210 static FTSENT **array; 211 static int lastentries = -1; |
212 FTSENT *p; 213 int base, chcnt, cnt, col, colwidth, num; 214 int endcol, numcols, numrows, row; 215 int tabwidth; | 212 FTSENT *p; 213 int base; 214 int chcnt; 215 int cnt; 216 int col; 217 int colwidth; 218 int endcol; 219 int num; 220 int numcols; 221 int numrows; 222 int row; 223 int tabwidth; |
216 217 if (f_notabs) 218 tabwidth = 1; 219 else 220 tabwidth = 8; 221 222 /* 223 * Have to do random access in the linked list -- build a table --- 51 unchanged lines hidden (view full) --- 275} 276 277/* 278 * print [inode] [size] name 279 * return # of characters printed, no trailing characters. 280 */ 281static int 282printaname(p, inodefield, sizefield) | 224 225 if (f_notabs) 226 tabwidth = 1; 227 else 228 tabwidth = 8; 229 230 /* 231 * Have to do random access in the linked list -- build a table --- 51 unchanged lines hidden (view full) --- 283} 284 285/* 286 * print [inode] [size] name 287 * return # of characters printed, no trailing characters. 288 */ 289static int 290printaname(p, inodefield, sizefield) |
283 FTSENT *p; 284 u_long sizefield, inodefield; | 291 FTSENT *p; 292 u_long inodefield; 293 u_long sizefield; |
285{ 286 struct stat *sp; | 294{ 295 struct stat *sp; |
287 int chcnt; | 296 int chcnt; |
288#ifdef COLORLS | 297#ifdef COLORLS |
289 int color_printed = 0; | 298 int color_printed = 0; |
290#endif 291 292 sp = p->fts_statp; 293 chcnt = 0; 294 if (f_inode) 295 chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino); 296 if (f_size) 297 chcnt += printf("%*qd ", --- 9 unchanged lines hidden (view full) --- 307#endif 308 if (f_type) 309 chcnt += printtype(sp->st_mode); 310 return (chcnt); 311} 312 313static void 314printtime(ftime) | 299#endif 300 301 sp = p->fts_statp; 302 chcnt = 0; 303 if (f_inode) 304 chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino); 305 if (f_size) 306 chcnt += printf("%*qd ", --- 9 unchanged lines hidden (view full) --- 316#endif 317 if (f_type) 318 chcnt += printtype(sp->st_mode); 319 return (chcnt); 320} 321 322static void 323printtime(ftime) |
315 time_t ftime; | 324 time_t ftime; |
316{ | 325{ |
317 char longstring[80]; 318 static time_t now; 319 const char *format; 320 static int d_first = -1; | 326 char longstring[80]; 327 static time_t now; 328 const char *format; 329 static int d_first = -1; |
321 322 if (d_first < 0) 323 d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 324 if (now == 0) 325 now = time(NULL); 326 327#define SIXMONTHS ((365 / 2) * 86400) 328 if (f_sectime) --- 6 unchanged lines hidden (view full) --- 335 /* mmm dd yyyy || dd mmm yyyy */ 336 format = d_first ? "%e %b %Y " : "%b %e %Y "; 337 strftime(longstring, sizeof(longstring), format, localtime(&ftime)); 338 fputs(longstring, stdout); 339} 340 341static int 342printtype(mode) | 330 331 if (d_first < 0) 332 d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 333 if (now == 0) 334 now = time(NULL); 335 336#define SIXMONTHS ((365 / 2) * 86400) 337 if (f_sectime) --- 6 unchanged lines hidden (view full) --- 344 /* mmm dd yyyy || dd mmm yyyy */ 345 format = d_first ? "%e %b %Y " : "%b %e %Y "; 346 strftime(longstring, sizeof(longstring), format, localtime(&ftime)); 347 fputs(longstring, stdout); 348} 349 350static int 351printtype(mode) |
343 u_int mode; | 352 u_int mode; |
344{ 345 switch (mode & S_IFMT) { 346 case S_IFDIR: 347 (void)putchar('/'); 348 return (1); 349 case S_IFIFO: 350 (void)putchar('|'); 351 return (1); --- 12 unchanged lines hidden (view full) --- 364 return (1); 365 } 366 return (0); 367} 368 369#ifdef COLORLS 370static int 371putch(c) | 353{ 354 switch (mode & S_IFMT) { 355 case S_IFDIR: 356 (void)putchar('/'); 357 return (1); 358 case S_IFIFO: 359 (void)putchar('|'); 360 return (1); --- 12 unchanged lines hidden (view full) --- 373 return (1); 374 } 375 return (0); 376} 377 378#ifdef COLORLS 379static int 380putch(c) |
372 int c; | 381 int c; |
373{ 374 (void) putchar(c); 375 return 0; 376} 377 378static int 379writech(c) | 382{ 383 (void) putchar(c); 384 return 0; 385} 386 387static int 388writech(c) |
380 int c; | 389 int c; |
381{ | 390{ |
382 char tmp = c; | 391 char tmp = c; |
383 384 (void) write(STDOUT_FILENO, &tmp, 1); 385 return 0; 386} 387 388static void 389printcolor(c) | 392 393 (void) write(STDOUT_FILENO, &tmp, 1); 394 return 0; 395} 396 397static void 398printcolor(c) |
390 Colors c; | 399 Colors c; |
391{ | 400{ |
392 char *ansiseq; | 401 char *ansiseq; |
393 394 if (colors[c].bold) 395 tputs(enter_bold, 1, putch); 396 397 if (colors[c].num[0] != -1) { 398 ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]); 399 if (ansiseq) 400 tputs(ansiseq, 1, putch); 401 } 402 403 if (colors[c].num[1] != -1) { 404 ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]); 405 if (ansiseq) 406 tputs(ansiseq, 1, putch); 407 } 408} 409 410static void 411endcolor(sig) | 402 403 if (colors[c].bold) 404 tputs(enter_bold, 1, putch); 405 406 if (colors[c].num[0] != -1) { 407 ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]); 408 if (ansiseq) 409 tputs(ansiseq, 1, putch); 410 } 411 412 if (colors[c].num[1] != -1) { 413 ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]); 414 if (ansiseq) 415 tputs(ansiseq, 1, putch); 416 } 417} 418 419static void 420endcolor(sig) |
412 int sig; | 421 int sig; |
413{ 414 tputs(ansi_coloff, 1, sig ? writech : putch); 415 tputs(attrs_off, 1, sig ? writech : putch); 416} 417 418static int 419colortype(mode) | 422{ 423 tputs(ansi_coloff, 1, sig ? writech : putch); 424 tputs(attrs_off, 1, sig ? writech : putch); 425} 426 427static int 428colortype(mode) |
420 mode_t mode; | 429 mode_t mode; |
421{ 422 switch(mode & S_IFMT) { 423 case S_IFDIR: 424 if (mode & S_IWOTH) 425 if (mode & S_ISTXT) 426 printcolor(C_WSDIR); 427 else 428 printcolor(C_WDIR); --- 25 unchanged lines hidden (view full) --- 454 printcolor(C_EXEC); 455 return(1); 456 } 457 return(0); 458} 459 460void 461parsecolors(cs) | 430{ 431 switch(mode & S_IFMT) { 432 case S_IFDIR: 433 if (mode & S_IWOTH) 434 if (mode & S_ISTXT) 435 printcolor(C_WSDIR); 436 else 437 printcolor(C_WDIR); --- 25 unchanged lines hidden (view full) --- 463 printcolor(C_EXEC); 464 return(1); 465 } 466 return(0); 467} 468 469void 470parsecolors(cs) |
462char *cs; | 471 char *cs; |
463{ | 472{ |
464 int i, j, len; 465 char c[2]; 466 short legacy_warn = 0; | 473 int i; 474 int j; 475 int len; 476 char c[2]; 477 short legacy_warn = 0; |
467 | 478 |
468 if (cs == NULL) cs = ""; /* LSCOLORS not set */ | 479 if (cs == NULL) 480 cs = ""; /* LSCOLORS not set */ |
469 len = strlen(cs); 470 for (i = 0 ; i < C_NUMCOLORS ; i++) { 471 colors[i].bold = 0; 472 | 481 len = strlen(cs); 482 for (i = 0 ; i < C_NUMCOLORS ; i++) { 483 colors[i].bold = 0; 484 |
473 if (len <= 2*i) { 474 c[0] = defcolors[2*i]; 475 c[1] = defcolors[2*i+1]; | 485 if (len <= 2 * i) { 486 c[0] = defcolors[2 * i]; 487 c[1] = defcolors[2 * i + 1]; |
476 } 477 else { | 488 } 489 else { |
478 c[0] = cs[2*i]; 479 c[1] = cs[2*i+1]; | 490 c[0] = cs[2 * i]; 491 c[1] = cs[2 * i + 1]; |
480 } 481 for (j = 0 ; j < 2 ; j++) { 482 /* Legacy colours used 0-7 */ 483 if (c[j] >= '0' && c[j] <= '7') { 484 colors[i].num[j] = c[j] - '0'; 485 if (!legacy_warn) { 486 fprintf(stderr, 487 "warn: colors are now defined " --- 15 unchanged lines hidden (view full) --- 503 colors[i].num[j] = -1; 504 } 505 } 506 } 507} 508 509void 510colorquit(sig) | 492 } 493 for (j = 0 ; j < 2 ; j++) { 494 /* Legacy colours used 0-7 */ 495 if (c[j] >= '0' && c[j] <= '7') { 496 colors[i].num[j] = c[j] - '0'; 497 if (!legacy_warn) { 498 fprintf(stderr, 499 "warn: colors are now defined " --- 15 unchanged lines hidden (view full) --- 515 colors[i].num[j] = -1; 516 } 517 } 518 } 519} 520 521void 522colorquit(sig) |
511 int sig; | 523 int sig; |
512{ 513 endcolor(sig); 514 515 (void) signal(sig, SIG_DFL); 516 (void) kill(getpid(), sig); 517} 518#endif /*COLORLS*/ 519 520static void 521printlink(p) | 524{ 525 endcolor(sig); 526 527 (void) signal(sig, SIG_DFL); 528 (void) kill(getpid(), sig); 529} 530#endif /*COLORLS*/ 531 532static void 533printlink(p) |
522 FTSENT *p; | 534 FTSENT *p; |
523{ | 535{ |
524 int lnklen; 525 char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1]; | 536 int lnklen; 537 char name[MAXPATHLEN + 1]; 538 char path[MAXPATHLEN + 1]; |
526 527 if (p->fts_level == FTS_ROOTLEVEL) 528 (void)snprintf(name, sizeof(name), "%s", p->fts_name); 529 else 530 (void)snprintf(name, sizeof(name), 531 "%s/%s", p->fts_parent->fts_accpath, p->fts_name); 532 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) { 533 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno)); 534 return; 535 } 536 path[lnklen] = '\0'; 537 (void)printf(" -> "); 538 printname(path); 539} | 539 540 if (p->fts_level == FTS_ROOTLEVEL) 541 (void)snprintf(name, sizeof(name), "%s", p->fts_name); 542 else 543 (void)snprintf(name, sizeof(name), 544 "%s/%s", p->fts_parent->fts_accpath, p->fts_name); 545 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) { 546 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno)); 547 return; 548 } 549 path[lnklen] = '\0'; 550 (void)printf(" -> "); 551 printname(path); 552} |