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