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. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)print.c 8.4 (Berkeley) 4/17/94 33 * $FreeBSD: src/bin/ls/print.c,v 1.73 2004/06/08 09:27:42 das Exp $ 34 */ 35 36 #include <sys/param.h> 37 #include <sys/stat.h> 38 39 #include <err.h> 40 #include <errno.h> 41 #include <fts.h> 42 #include <langinfo.h> 43 #include <libutil.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <time.h> 48 #include <unistd.h> 49 #include <locale.h> 50 #ifdef COLORLS 51 #include <ctype.h> 52 #include <termcap.h> 53 #include <signal.h> 54 #endif 55 56 #include "ls.h" 57 #include "extern.h" 58 59 static int printaname(const FTSENT *, u_long, u_long); 60 static void printlink(const FTSENT *); 61 static void printtime(const struct timespec); 62 static int printtype(u_int); 63 static void printsize(size_t, off_t); 64 #ifdef COLORLS 65 static void endcolor(int); 66 static int colortype(mode_t); 67 #endif 68 69 #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT) 70 71 #ifdef COLORLS 72 /* Most of these are taken from <sys/stat.h> */ 73 typedef enum Colors { 74 C_DIR, /* directory */ 75 C_LNK, /* symbolic link */ 76 C_SOCK, /* socket */ 77 C_FIFO, /* pipe */ 78 C_EXEC, /* executable */ 79 C_BLK, /* block special */ 80 C_CHR, /* character special */ 81 C_SUID, /* setuid executable */ 82 C_SGID, /* setgid executable */ 83 C_WSDIR, /* directory writeble to others, with sticky 84 * bit */ 85 C_WDIR, /* directory writeble to others, without 86 * sticky bit */ 87 C_NUMCOLORS /* just a place-holder */ 88 } Colors; 89 90 static const char *defcolors = "exfxcxdxbxegedabagacad"; 91 92 /* colors for file types */ 93 static struct { 94 int num[2]; 95 int bold; 96 } colors[C_NUMCOLORS]; 97 #endif 98 99 void 100 printscol(const DISPLAY *dp) 101 { 102 FTSENT *p; 103 104 for (p = dp->list; p; p = p->fts_link) { 105 if (IS_NOPRINT(p)) 106 continue; 107 printaname(p, dp->s_inode, dp->s_block); 108 putchar('\n'); 109 } 110 } 111 112 /* 113 * print name in current style 114 */ 115 int 116 printname(const char *name) 117 { 118 if (f_octal || f_octal_escape) 119 return prn_octal(name); 120 else if (f_nonprint) 121 return prn_printable(name); 122 else 123 return prn_normal(name); 124 } 125 126 void 127 printlong(const DISPLAY *dp) 128 { 129 struct stat *sp; 130 FTSENT *p; 131 NAMES *np; 132 char buf[20]; 133 #ifdef COLORLS 134 int color_printed = 0; 135 #endif 136 137 if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) && 138 (f_longform || f_size)) { 139 printf("total %lu\n", howmany(dp->btotal, blocksize)); 140 } 141 142 for (p = dp->list; p; p = p->fts_link) { 143 if (IS_NOPRINT(p)) 144 continue; 145 sp = p->fts_statp; 146 if (f_inode) 147 printf("%*ju ", dp->s_inode, (uintmax_t)sp->st_ino); 148 if (f_size) 149 printf("%*jd ", 150 dp->s_block, howmany(sp->st_blocks, blocksize)); 151 strmode(sp->st_mode, buf); 152 np = p->fts_pointer; 153 printf("%s %*u %-*s %-*s ", buf, dp->s_nlink, 154 sp->st_nlink, dp->s_user, np->user, dp->s_group, 155 np->group); 156 if (f_fsmid) 157 printf("%s ", np->fsmid); 158 if (f_flags) 159 printf("%-*s ", dp->s_flags, np->flags); 160 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode)) 161 printf("%3d, 0x%08x ", 162 major(sp->st_rdev), (u_int)minor(sp->st_rdev)); 163 else if (dp->bcfile) 164 printf("%*s%*jd ", 165 8 - dp->s_size, "", dp->s_size, (intmax_t)sp->st_size); 166 else 167 printsize(dp->s_size, sp->st_size); 168 if (f_accesstime) 169 printtime(sp->st_atim); 170 else if (f_statustime) 171 printtime(sp->st_ctim); 172 else 173 printtime(sp->st_mtim); 174 putchar(' '); 175 #ifdef COLORLS 176 if (f_color) 177 color_printed = colortype(sp->st_mode); 178 #endif 179 printname(p->fts_name); 180 #ifdef COLORLS 181 if (f_color && color_printed) 182 endcolor(0); 183 #endif 184 if (f_type) 185 printtype(sp->st_mode); 186 if (S_ISLNK(sp->st_mode)) 187 printlink(p); 188 putchar('\n'); 189 } 190 } 191 192 void 193 printstream(const DISPLAY *dp) 194 { 195 FTSENT *p; 196 int chcnt; 197 198 for (p = dp->list, chcnt = 0; p; p = p->fts_link) { 199 if (p->fts_number == NO_PRINT) 200 continue; 201 /* XXX strlen does not take octal escapes into account. */ 202 if (strlen(p->fts_name) + chcnt + 203 (p->fts_link ? 2 : 0) >= (unsigned)termwidth) { 204 putchar('\n'); 205 chcnt = 0; 206 } 207 chcnt += printaname(p, dp->s_inode, dp->s_block); 208 if (p->fts_link) { 209 printf(", "); 210 chcnt += 2; 211 } 212 } 213 if (chcnt) 214 putchar('\n'); 215 } 216 217 void 218 printcol(const DISPLAY *dp) 219 { 220 static FTSENT **array; 221 static int lastentries = -1; 222 FTSENT *p; 223 FTSENT **narray; 224 int base; 225 int chcnt; 226 int cnt; 227 int col; 228 int colwidth; 229 int endcol; 230 int num; 231 int numcols; 232 int numrows; 233 int row; 234 int tabwidth; 235 236 if (f_notabs) 237 tabwidth = 1; 238 else 239 tabwidth = 8; 240 241 /* 242 * Have to do random access in the linked list -- build a table 243 * of pointers. 244 */ 245 if (dp->entries > lastentries) { 246 lastentries = dp->entries; 247 if ((narray = 248 realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) { 249 warn(NULL); 250 printscol(dp); 251 return; 252 } 253 lastentries = dp->entries; 254 array = narray; 255 } 256 for (p = dp->list, num = 0; p; p = p->fts_link) 257 if (p->fts_number != NO_PRINT) 258 array[num++] = p; 259 260 colwidth = dp->maxlen; 261 if (f_inode) 262 colwidth += dp->s_inode + 1; 263 if (f_size) 264 colwidth += dp->s_block + 1; 265 if (f_type) 266 colwidth += 1; 267 268 colwidth = (colwidth + tabwidth) & ~(tabwidth - 1); 269 if (termwidth < 2 * colwidth) { 270 printscol(dp); 271 return; 272 } 273 numcols = termwidth / colwidth; 274 numrows = num / numcols; 275 if (num % numcols) 276 ++numrows; 277 278 if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) && 279 (f_longform || f_size)) { 280 printf("total %lu\n", howmany(dp->btotal, blocksize)); 281 } 282 283 /* counter if f_sortacross, else case-by-case */ 284 base = 0; 285 286 for (row = 0; row < numrows; ++row) { 287 endcol = colwidth; 288 if (!f_sortacross) 289 base = row; 290 for (col = 0, chcnt = 0; col < numcols; ++col) { 291 chcnt += printaname(array[base], dp->s_inode, 292 dp->s_block); 293 if (f_sortacross) 294 base++; 295 else 296 base += numrows; 297 if (base >= num) 298 break; 299 while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1))) 300 <= endcol) { 301 if (f_sortacross && col + 1 >= numcols) 302 break; 303 putchar(f_notabs ? ' ' : '\t'); 304 chcnt = cnt; 305 } 306 endcol += colwidth; 307 } 308 putchar('\n'); 309 } 310 } 311 312 /* 313 * print [inode] [size] name 314 * return # of characters printed, no trailing characters. 315 */ 316 static int 317 printaname(const FTSENT *p, u_long inodefield, u_long sizefield) 318 { 319 struct stat *sp; 320 int chcnt; 321 #ifdef COLORLS 322 int color_printed = 0; 323 #endif 324 325 sp = p->fts_statp; 326 chcnt = 0; 327 if (f_inode) { 328 chcnt += printf("%*ju ", (int)inodefield, 329 (uintmax_t)sp->st_ino); 330 } 331 if (f_size) 332 chcnt += printf("%*jd ", 333 (int)sizefield, howmany(sp->st_blocks, blocksize)); 334 #ifdef COLORLS 335 if (f_color) 336 color_printed = colortype(sp->st_mode); 337 #endif 338 chcnt += printname(p->fts_name); 339 #ifdef COLORLS 340 if (f_color && color_printed) 341 endcolor(0); 342 #endif 343 if (f_type) 344 chcnt += printtype(sp->st_mode); 345 return (chcnt); 346 } 347 348 static void 349 printtime(const struct timespec stt) 350 { 351 char longstring[80]; 352 static time_t now; 353 const struct tm *ptime; 354 const char *format; 355 static char *lc_time; 356 static int posix_time; 357 358 #define SIXMONTHS ((365 / 2) * 86400) 359 360 if (now == 0) 361 now = time(NULL); 362 363 if (lc_time == NULL) { 364 lc_time = setlocale(LC_TIME, NULL); 365 posix_time = (strcmp(lc_time, "C") == 0); 366 } 367 368 if (f_timeformat) { 369 /* 370 * User specified format 371 */ 372 format = f_timeformat; 373 } else if (f_sectime) { 374 /* 375 * POSIX: Not covered by standard. If C/POSIX locale used 376 * the old convention of mmm dd hh:mm:ss yyyy is kept. 377 * Named locales use the extended ISO 8601 format without T: 378 * YYYY-DD-MM hh:mm:ss 379 */ 380 format = posix_time ? "%b %e %T %Y" : "%F %T"; 381 } else if (posix_time) { 382 /* 383 * POSIX: Future or older than 6 months returns equivalent of 384 * date "+%b %e %Y" 385 * If file was modified within the past 6 months, 386 * return equivalent of: date "+%b %e %H:%M" 387 */ 388 if ((stt.tv_sec > now) || (stt.tv_sec < now - SIXMONTHS)) 389 format = "%b %e %Y"; 390 else 391 format = "%b %e %R"; 392 } else { 393 /* 394 * Named locales use format DD-MMM-YYYY hh:mm 395 * Regardless of locale, English is used for month field 396 */ 397 format = "%d-%b-%Y %H:%M"; 398 } 399 400 /* 401 * If printing the nanotime field, always use GMT time 402 */ 403 if (f_nanotime) 404 ptime = gmtime(&(stt.tv_sec)); 405 else 406 ptime = localtime(&(stt.tv_sec)); 407 408 strftime_l(longstring, sizeof(longstring), format, ptime, NULL); 409 fputs(longstring, stdout); 410 if (f_nanotime) 411 printf(".%09ju", stt.tv_nsec); 412 } 413 414 static int 415 printtype(u_int mode) 416 { 417 418 if (f_slash) { 419 if ((mode & S_IFMT) == S_IFDIR) { 420 putchar('/'); 421 return (1); 422 } 423 return (0); 424 } 425 426 switch (mode & S_IFMT) { 427 case S_IFDIR: 428 putchar('/'); 429 return (1); 430 case S_IFIFO: 431 putchar('|'); 432 return (1); 433 case S_IFLNK: 434 putchar('@'); 435 return (1); 436 case S_IFSOCK: 437 putchar('='); 438 return (1); 439 case S_IFWHT: 440 putchar('%'); 441 return (1); 442 default: 443 break; 444 } 445 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) { 446 putchar('*'); 447 return (1); 448 } 449 return (0); 450 } 451 452 #ifdef COLORLS 453 static int 454 putch(int c) 455 { 456 putchar(c); 457 return 0; 458 } 459 460 static int 461 writech(int c) 462 { 463 char tmp = (char)c; 464 465 write(STDOUT_FILENO, &tmp, 1); 466 return 0; 467 } 468 469 static void 470 printcolor(Colors c) 471 { 472 char *ansiseq; 473 474 if (colors[c].bold) 475 tputs(enter_bold, 1, putch); 476 477 if (colors[c].num[0] != -1) { 478 ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]); 479 if (ansiseq) 480 tputs(ansiseq, 1, putch); 481 } 482 if (colors[c].num[1] != -1) { 483 ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]); 484 if (ansiseq) 485 tputs(ansiseq, 1, putch); 486 } 487 } 488 489 static void 490 endcolor(int sig) 491 { 492 tputs(ansi_coloff, 1, sig ? writech : putch); 493 tputs(attrs_off, 1, sig ? writech : putch); 494 } 495 496 static int 497 colortype(mode_t mode) 498 { 499 switch (mode & S_IFMT) { 500 case S_IFDIR: 501 if (mode & S_IWOTH) 502 if (mode & S_ISTXT) 503 printcolor(C_WSDIR); 504 else 505 printcolor(C_WDIR); 506 else 507 printcolor(C_DIR); 508 return (1); 509 case S_IFLNK: 510 printcolor(C_LNK); 511 return (1); 512 case S_IFSOCK: 513 printcolor(C_SOCK); 514 return (1); 515 case S_IFIFO: 516 printcolor(C_FIFO); 517 return (1); 518 case S_IFBLK: 519 printcolor(C_BLK); 520 return (1); 521 case S_IFCHR: 522 printcolor(C_CHR); 523 return (1); 524 default:; 525 } 526 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) { 527 if (mode & S_ISUID) 528 printcolor(C_SUID); 529 else if (mode & S_ISGID) 530 printcolor(C_SGID); 531 else 532 printcolor(C_EXEC); 533 return (1); 534 } 535 return (0); 536 } 537 538 void 539 parsecolors(const char *cs) 540 { 541 int i; 542 int j; 543 size_t len; 544 char c[2]; 545 short legacy_warn = 0; 546 547 if (cs == NULL) 548 cs = ""; /* LSCOLORS not set */ 549 len = strlen(cs); 550 for (i = 0; i < C_NUMCOLORS; i++) { 551 colors[i].bold = 0; 552 553 if (len <= 2 * (size_t)i) { 554 c[0] = defcolors[2 * i]; 555 c[1] = defcolors[2 * i + 1]; 556 } else { 557 c[0] = cs[2 * i]; 558 c[1] = cs[2 * i + 1]; 559 } 560 for (j = 0; j < 2; j++) { 561 /* Legacy colours used 0-7 */ 562 if (c[j] >= '0' && c[j] <= '7') { 563 colors[i].num[j] = c[j] - '0'; 564 if (!legacy_warn) { 565 warnx("LSCOLORS should use " 566 "characters a-h instead of 0-9 (" 567 "see the manual page)\n"); 568 } 569 legacy_warn = 1; 570 } else if (c[j] >= 'a' && c[j] <= 'h') 571 colors[i].num[j] = c[j] - 'a'; 572 else if (c[j] >= 'A' && c[j] <= 'H') { 573 colors[i].num[j] = c[j] - 'A'; 574 colors[i].bold = 1; 575 } else if (tolower((unsigned char)c[j] == 'x')) 576 colors[i].num[j] = -1; 577 else { 578 warnx("invalid character '%c' in LSCOLORS" 579 " env var\n", c[j]); 580 colors[i].num[j] = -1; 581 } 582 } 583 } 584 } 585 586 void 587 colorquit(int sig) 588 { 589 endcolor(sig); 590 591 signal(sig, SIG_DFL); 592 kill(getpid(), sig); 593 } 594 595 #endif /* COLORLS */ 596 597 static void 598 printlink(const FTSENT *p) 599 { 600 int lnklen; 601 char name[MAXPATHLEN + 1]; 602 char path[MAXPATHLEN + 1]; 603 604 if (p->fts_level == FTS_ROOTLEVEL) 605 snprintf(name, sizeof(name), "%s", p->fts_name); 606 else 607 snprintf(name, sizeof(name), 608 "%s/%s", p->fts_parent->fts_accpath, p->fts_name); 609 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) { 610 fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno)); 611 return; 612 } 613 path[lnklen] = '\0'; 614 printf(" -> "); 615 printname(path); 616 } 617 618 static void 619 printsize(size_t width, off_t bytes) 620 { 621 if (f_humanval) { 622 char buf[5]; 623 624 humanize_number(buf, sizeof(buf), (int64_t)bytes, "", 625 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 626 printf("%5s ", buf); 627 } else 628 printf("%*jd ", (int)width, (uintmax_t)bytes); 629 } 630