print.c revision 105375
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 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. 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#if 0 38#ifndef lint 39static char sccsid[] = "@(#)print.c 8.4 (Berkeley) 4/17/94"; 40#endif /* not lint */ 41#endif 42#include <sys/cdefs.h> 43__FBSDID("$FreeBSD: head/bin/ls/print.c 105375 2002-10-18 04:06:59Z dd $"); 44 45#include <sys/param.h> 46#include <sys/stat.h> 47 48#include <err.h> 49#include <errno.h> 50#include <fts.h> 51#include <math.h> 52#include <langinfo.h> 53#include <stdio.h> 54#include <stdlib.h> 55#include <string.h> 56#include <time.h> 57#include <unistd.h> 58#ifdef COLORLS 59#include <ctype.h> 60#include <termcap.h> 61#include <signal.h> 62#endif 63 64#include "ls.h" 65#include "extern.h" 66 67static int printaname(FTSENT *, u_long, u_long); 68static void printlink(FTSENT *); 69static void printtime(time_t); 70static int printtype(u_int); 71static void printsize(size_t, off_t); 72#ifdef COLORLS 73static void endcolor(int); 74static int colortype(mode_t); 75#endif 76 77#define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT) 78 79#define KILO_SZ(n) (n) 80#define MEGA_SZ(n) ((n) * (n)) 81#define GIGA_SZ(n) ((n) * (n) * (n)) 82#define TERA_SZ(n) ((n) * (n) * (n) * (n)) 83#define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n)) 84 85#define KILO_2_SZ (KILO_SZ(1024ULL)) 86#define MEGA_2_SZ (MEGA_SZ(1024ULL)) 87#define GIGA_2_SZ (GIGA_SZ(1024ULL)) 88#define TERA_2_SZ (TERA_SZ(1024ULL)) 89#define PETA_2_SZ (PETA_SZ(1024ULL)) 90 91static unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TERA_2_SZ, PETA_2_SZ}; 92 93typedef enum { 94 NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX 95} unit_t; 96static unit_t unit_adjust(double *); 97 98static int unitp[] = {NONE, KILO, MEGA, GIGA, TERA, PETA}; 99 100#ifdef COLORLS 101/* Most of these are taken from <sys/stat.h> */ 102typedef enum Colors { 103 C_DIR, /* directory */ 104 C_LNK, /* symbolic link */ 105 C_SOCK, /* socket */ 106 C_FIFO, /* pipe */ 107 C_EXEC, /* executable */ 108 C_BLK, /* block special */ 109 C_CHR, /* character special */ 110 C_SUID, /* setuid executable */ 111 C_SGID, /* setgid executable */ 112 C_WSDIR, /* directory writeble to others, with sticky 113 * bit */ 114 C_WDIR, /* directory writeble to others, without 115 * sticky bit */ 116 C_NUMCOLORS /* just a place-holder */ 117} Colors; 118 119static const char *defcolors = "exfxcxdxbxegedabagacad"; 120 121/* colors for file types */ 122static struct { 123 int num[2]; 124 int bold; 125} colors[C_NUMCOLORS]; 126#endif 127 128void 129printscol(DISPLAY *dp) 130{ 131 FTSENT *p; 132 133 for (p = dp->list; p; p = p->fts_link) { 134 if (IS_NOPRINT(p)) 135 continue; 136 (void)printaname(p, dp->s_inode, dp->s_block); 137 (void)putchar('\n'); 138 } 139} 140 141/* 142 * print name in current style 143 */ 144static int 145printname(const char *name) 146{ 147 if (f_octal || f_octal_escape) 148 return prn_octal(name); 149 else if (f_nonprint) 150 return prn_printable(name); 151 else 152 return printf("%s", name); 153} 154 155void 156printlong(DISPLAY *dp) 157{ 158 struct stat *sp; 159 FTSENT *p; 160 NAMES *np; 161 char buf[20]; 162#ifdef COLORLS 163 int color_printed = 0; 164#endif 165 166 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) 167 (void)printf("total %lu\n", howmany(dp->btotal, blocksize)); 168 169 for (p = dp->list; p; p = p->fts_link) { 170 if (IS_NOPRINT(p)) 171 continue; 172 sp = p->fts_statp; 173 if (f_inode) 174 (void)printf("%*lu ", dp->s_inode, (u_long)sp->st_ino); 175 if (f_size) 176 (void)printf("%*lld ", 177 dp->s_block, howmany(sp->st_blocks, blocksize)); 178 strmode(sp->st_mode, buf); 179 np = p->fts_pointer; 180 (void)printf("%s %*u %-*s %-*s ", buf, dp->s_nlink, 181 sp->st_nlink, dp->s_user, np->user, dp->s_group, 182 np->group); 183 if (f_flags) 184 (void)printf("%-*s ", dp->s_flags, np->flags); 185 if (f_lomac) 186 (void)printf("%-*s ", dp->s_lattr, np->lattr); 187 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode)) 188 if (minor(sp->st_rdev) > 255 || minor(sp->st_rdev) < 0) 189 (void)printf("%3d, 0x%08x ", 190 major(sp->st_rdev), 191 (u_int)minor(sp->st_rdev)); 192 else 193 (void)printf("%3d, %3d ", 194 major(sp->st_rdev), minor(sp->st_rdev)); 195 else if (dp->bcfile) 196 (void)printf("%*s%*lld ", 197 8 - dp->s_size, "", dp->s_size, sp->st_size); 198 else 199 printsize(dp->s_size, sp->st_size); 200 if (f_accesstime) 201 printtime(sp->st_atime); 202 else if (f_statustime) 203 printtime(sp->st_ctime); 204 else 205 printtime(sp->st_mtime); 206#ifdef COLORLS 207 if (f_color) 208 color_printed = colortype(sp->st_mode); 209#endif 210 (void)printname(p->fts_name); 211#ifdef COLORLS 212 if (f_color && color_printed) 213 endcolor(0); 214#endif 215 if (f_type) 216 (void)printtype(sp->st_mode); 217 if (S_ISLNK(sp->st_mode)) 218 printlink(p); 219 (void)putchar('\n'); 220 } 221} 222 223void 224printstream(DISPLAY *dp) 225{ 226 FTSENT *p; 227 extern int termwidth; 228 int chcnt; 229 230 for (p = dp->list, chcnt = 0; p; p = p->fts_link) { 231 if (p->fts_number == NO_PRINT) 232 continue; 233 if (strlen(p->fts_name) + chcnt + 234 (p->fts_link ? 2 : 0) >= (unsigned)termwidth) { 235 putchar('\n'); 236 chcnt = 0; 237 } 238 chcnt += printaname(p, dp->s_inode, dp->s_block); 239 if (p->fts_link) { 240 printf(", "); 241 chcnt += 2; 242 } 243 } 244 if (chcnt) 245 putchar('\n'); 246} 247 248void 249printcol(DISPLAY *dp) 250{ 251 extern int termwidth; 252 static FTSENT **array; 253 static int lastentries = -1; 254 FTSENT *p; 255 int base; 256 int chcnt; 257 int cnt; 258 int col; 259 int colwidth; 260 int endcol; 261 int num; 262 int numcols; 263 int numrows; 264 int row; 265 int tabwidth; 266 267 if (f_notabs) 268 tabwidth = 1; 269 else 270 tabwidth = 8; 271 272 /* 273 * Have to do random access in the linked list -- build a table 274 * of pointers. 275 */ 276 if (dp->entries > lastentries) { 277 lastentries = dp->entries; 278 if ((array = 279 realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) { 280 warn(NULL); 281 printscol(dp); 282 } 283 } 284 for (p = dp->list, num = 0; p; p = p->fts_link) 285 if (p->fts_number != NO_PRINT) 286 array[num++] = p; 287 288 colwidth = dp->maxlen; 289 if (f_inode) 290 colwidth += dp->s_inode + 1; 291 if (f_size) 292 colwidth += dp->s_block + 1; 293 if (f_type) 294 colwidth += 1; 295 296 colwidth = (colwidth + tabwidth) & ~(tabwidth - 1); 297 if (termwidth < 2 * colwidth) { 298 printscol(dp); 299 return; 300 } 301 numcols = termwidth / colwidth; 302 numrows = num / numcols; 303 if (num % numcols) 304 ++numrows; 305 306 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) 307 (void)printf("total %lu\n", howmany(dp->btotal, blocksize)); 308 309 base = 0; 310 for (row = 0; row < numrows; ++row) { 311 endcol = colwidth; 312 if (!f_sortacross) 313 base = row; 314 for (col = 0, chcnt = 0; col < numcols; ++col) { 315 chcnt += printaname(array[base], dp->s_inode, 316 dp->s_block); 317 if (f_sortacross) 318 base++; 319 else 320 base += numrows; 321 if (base >= num) 322 break; 323 while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1))) 324 <= endcol) { 325 if (f_sortacross && col + 1 >= numcols) 326 break; 327 (void)putchar(f_notabs ? ' ' : '\t'); 328 chcnt = cnt; 329 } 330 endcol += colwidth; 331 } 332 (void)putchar('\n'); 333 } 334} 335 336/* 337 * print [inode] [size] name 338 * return # of characters printed, no trailing characters. 339 */ 340static int 341printaname(FTSENT *p, u_long inodefield, u_long sizefield) 342{ 343 struct stat *sp; 344 int chcnt; 345#ifdef COLORLS 346 int color_printed = 0; 347#endif 348 349 sp = p->fts_statp; 350 chcnt = 0; 351 if (f_inode) 352 chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino); 353 if (f_size) 354 chcnt += printf("%*lld ", 355 (int)sizefield, howmany(sp->st_blocks, blocksize)); 356#ifdef COLORLS 357 if (f_color) 358 color_printed = colortype(sp->st_mode); 359#endif 360 chcnt += printname(p->fts_name); 361#ifdef COLORLS 362 if (f_color && color_printed) 363 endcolor(0); 364#endif 365 if (f_type) 366 chcnt += printtype(sp->st_mode); 367 return (chcnt); 368} 369 370static void 371printtime(time_t ftime) 372{ 373 char longstring[80]; 374 static time_t now; 375 const char *format; 376 static int d_first = -1; 377 378 if (d_first < 0) 379 d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 380 if (now == 0) 381 now = time(NULL); 382 383#define SIXMONTHS ((365 / 2) * 86400) 384 if (f_sectime) 385 /* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */ 386 format = d_first ? "%e %b %T %Y " : "%b %e %T %Y "; 387 else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS) 388 /* mmm dd hh:mm || dd mmm hh:mm */ 389 format = d_first ? "%e %b %R " : "%b %e %R "; 390 else 391 /* mmm dd yyyy || dd mmm yyyy */ 392 format = d_first ? "%e %b %Y " : "%b %e %Y "; 393 strftime(longstring, sizeof(longstring), format, localtime(&ftime)); 394 fputs(longstring, stdout); 395} 396 397static int 398printtype(u_int mode) 399{ 400 401 if (f_slash) { 402 if ((mode & S_IFMT) == S_IFDIR) { 403 (void)putchar('/'); 404 return (1); 405 } 406 return (0); 407 } 408 409 switch (mode & S_IFMT) { 410 case S_IFDIR: 411 (void)putchar('/'); 412 return (1); 413 case S_IFIFO: 414 (void)putchar('|'); 415 return (1); 416 case S_IFLNK: 417 (void)putchar('@'); 418 return (1); 419 case S_IFSOCK: 420 (void)putchar('='); 421 return (1); 422 case S_IFWHT: 423 (void)putchar('%'); 424 return (1); 425 default: 426 break; 427 } 428 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) { 429 (void)putchar('*'); 430 return (1); 431 } 432 return (0); 433} 434 435#ifdef COLORLS 436static int 437putch(int c) 438{ 439 (void)putchar(c); 440 return 0; 441} 442 443static int 444writech(int c) 445{ 446 char tmp = c; 447 448 (void)write(STDOUT_FILENO, &tmp, 1); 449 return 0; 450} 451 452static void 453printcolor(Colors c) 454{ 455 char *ansiseq; 456 457 if (colors[c].bold) 458 tputs(enter_bold, 1, putch); 459 460 if (colors[c].num[0] != -1) { 461 ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]); 462 if (ansiseq) 463 tputs(ansiseq, 1, putch); 464 } 465 if (colors[c].num[1] != -1) { 466 ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]); 467 if (ansiseq) 468 tputs(ansiseq, 1, putch); 469 } 470} 471 472static void 473endcolor(int sig) 474{ 475 tputs(ansi_coloff, 1, sig ? writech : putch); 476 tputs(attrs_off, 1, sig ? writech : putch); 477} 478 479static int 480colortype(mode_t mode) 481{ 482 switch (mode & S_IFMT) { 483 case S_IFDIR: 484 if (mode & S_IWOTH) 485 if (mode & S_ISTXT) 486 printcolor(C_WSDIR); 487 else 488 printcolor(C_WDIR); 489 else 490 printcolor(C_DIR); 491 return (1); 492 case S_IFLNK: 493 printcolor(C_LNK); 494 return (1); 495 case S_IFSOCK: 496 printcolor(C_SOCK); 497 return (1); 498 case S_IFIFO: 499 printcolor(C_FIFO); 500 return (1); 501 case S_IFBLK: 502 printcolor(C_BLK); 503 return (1); 504 case S_IFCHR: 505 printcolor(C_CHR); 506 return (1); 507 } 508 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) { 509 if (mode & S_ISUID) 510 printcolor(C_SUID); 511 else if (mode & S_ISGID) 512 printcolor(C_SGID); 513 else 514 printcolor(C_EXEC); 515 return (1); 516 } 517 return (0); 518} 519 520void 521parsecolors(const char *cs) 522{ 523 int i; 524 int j; 525 int len; 526 char c[2]; 527 short legacy_warn = 0; 528 529 if (cs == NULL) 530 cs = ""; /* LSCOLORS not set */ 531 len = strlen(cs); 532 for (i = 0; i < C_NUMCOLORS; i++) { 533 colors[i].bold = 0; 534 535 if (len <= 2 * i) { 536 c[0] = defcolors[2 * i]; 537 c[1] = defcolors[2 * i + 1]; 538 } else { 539 c[0] = cs[2 * i]; 540 c[1] = cs[2 * i + 1]; 541 } 542 for (j = 0; j < 2; j++) { 543 /* Legacy colours used 0-7 */ 544 if (c[j] >= '0' && c[j] <= '7') { 545 colors[i].num[j] = c[j] - '0'; 546 if (!legacy_warn) { 547 fprintf(stderr, 548 "warn: LSCOLORS should use " 549 "characters a-h instead of 0-9 (" 550 "see the manual page)\n"); 551 } 552 legacy_warn = 1; 553 } else if (c[j] >= 'a' && c[j] <= 'h') 554 colors[i].num[j] = c[j] - 'a'; 555 else if (c[j] >= 'A' && c[j] <= 'H') { 556 colors[i].num[j] = c[j] - 'A'; 557 colors[i].bold = 1; 558 } else if (tolower((unsigned char)c[j] == 'x')) 559 colors[i].num[j] = -1; 560 else { 561 fprintf(stderr, 562 "error: invalid character '%c' in LSCOLORS" 563 " env var\n", c[j]); 564 colors[i].num[j] = -1; 565 } 566 } 567 } 568} 569 570void 571colorquit(int sig) 572{ 573 endcolor(sig); 574 575 (void)signal(sig, SIG_DFL); 576 (void)kill(getpid(), sig); 577} 578 579#endif /* COLORLS */ 580 581static void 582printlink(FTSENT *p) 583{ 584 int lnklen; 585 char name[MAXPATHLEN + 1]; 586 char path[MAXPATHLEN + 1]; 587 588 if (p->fts_level == FTS_ROOTLEVEL) 589 (void)snprintf(name, sizeof(name), "%s", p->fts_name); 590 else 591 (void)snprintf(name, sizeof(name), 592 "%s/%s", p->fts_parent->fts_accpath, p->fts_name); 593 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) { 594 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno)); 595 return; 596 } 597 path[lnklen] = '\0'; 598 (void)printf(" -> "); 599 (void)printname(path); 600} 601 602static void 603printsize(size_t width, off_t bytes) 604{ 605 double dbytes; 606 unit_t unit; 607 608 if (f_humanval) { 609 dbytes = bytes; 610 unit = unit_adjust(&dbytes); 611 612 if (dbytes == 0) 613 (void)printf("%*s ", width, "0B"); 614 else 615 (void)printf("%*.*f%c ", width - 1, dbytes > 10 ? 0 : 1, 616 dbytes, "BKMGTPE"[unit]); 617 } else 618 (void)printf("%*lld ", width, bytes); 619} 620 621/* 622 * Output in "human-readable" format. Uses 3 digits max and puts 623 * unit suffixes at the end. Makes output compact and easy to read, 624 * especially on huge disks. 625 * 626 */ 627unit_t 628unit_adjust(double *val) 629{ 630 double abval; 631 unit_t unit; 632 unsigned int unit_sz; 633 634 abval = fabs(*val); 635 636 unit_sz = abval ? ilogb(abval) / 10 : 0; 637 638 if (unit_sz >= UNIT_MAX) { 639 unit = NONE; 640 } else { 641 unit = unitp[unit_sz]; 642 *val /= (double)vals_base2[unit_sz]; 643 } 644 645 return (unit); 646} 647