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 *, int *); 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 int haveacls; 143 dev_t prevdev; 144 145 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) 146 (void)printf("total %lu\n", howmany(dp->btotal, blocksize)); 147 148 haveacls = 1; 149 prevdev = (dev_t)-1; 150 for (p = dp->list; p; p = p->fts_link) { 151 if (IS_NOPRINT(p)) 152 continue; 153 sp = p->fts_statp; 154 if (f_inode) 155 (void)printf("%*lu ", dp->s_inode, (u_long)sp->st_ino); 156 if (f_size) 157 (void)printf("%*jd ", 158 dp->s_block, howmany(sp->st_blocks, blocksize)); 159 strmode(sp->st_mode, buf); 160 /* 161 * Cache whether or not the filesystem supports ACL's to 162 * avoid expensive syscalls. Try again when we change devices. 163 */ 164 if (haveacls || sp->st_dev != prevdev) { 165 aclmode(buf, p, &haveacls); 166 prevdev = sp->st_dev; 167 } 168 np = p->fts_pointer; 169 (void)printf("%s %*u %-*s %-*s ", buf, dp->s_nlink, 170 sp->st_nlink, dp->s_user, np->user, dp->s_group, 171 np->group); 172 if (f_flags) 173 (void)printf("%-*s ", dp->s_flags, np->flags); 174 if (f_label) 175 (void)printf("%-*s ", dp->s_label, np->label); 176 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode)) 177 if (minor(sp->st_rdev) > 255 || minor(sp->st_rdev) < 0) 178 (void)printf("%3d, 0x%08x ", 179 major(sp->st_rdev), 180 (u_int)minor(sp->st_rdev)); 181 else 182 (void)printf("%3d, %3d ", 183 major(sp->st_rdev), minor(sp->st_rdev)); 184 else if (dp->bcfile) 185 (void)printf("%*s%*jd ", 186 8 - dp->s_size, "", dp->s_size, sp->st_size); 187 else 188 printsize(dp->s_size, sp->st_size); 189 if (f_accesstime) 190 printtime(sp->st_atime); 191 else if (f_statustime) 192 printtime(sp->st_ctime); 193 else 194 printtime(sp->st_mtime); 195 #ifdef COLORLS 196 if (f_color) 197 color_printed = colortype(sp->st_mode); 198 #endif 199 (void)printname(p->fts_name); 200 #ifdef COLORLS 201 if (f_color && color_printed) 202 endcolor(0); 203 #endif 204 if (f_type) 205 (void)printtype(sp->st_mode); 206 if (S_ISLNK(sp->st_mode)) 207 printlink(p); 208 (void)putchar('\n'); 209 } 210 } 211 212 void 213 printstream(const DISPLAY *dp) 214 { 215 FTSENT *p; 216 int chcnt; 217 218 for (p = dp->list, chcnt = 0; p; p = p->fts_link) { 219 if (p->fts_number == NO_PRINT) 220 continue; 221 /* XXX strlen does not take octal escapes into account. */ 222 if (strlen(p->fts_name) + chcnt + 223 (p->fts_link ? 2 : 0) >= (unsigned)termwidth) { 224 putchar('\n'); 225 chcnt = 0; 226 } 227 chcnt += printaname(p, dp->s_inode, dp->s_block); 228 if (p->fts_link) { 229 printf(", "); 230 chcnt += 2; 231 } 232 } 233 if (chcnt) 234 putchar('\n'); 235 } 236 237 void 238 printcol(const DISPLAY *dp) 239 { 240 static FTSENT **array; 241 static int lastentries = -1; 242 FTSENT *p; 243 FTSENT **narray; 244 int base; 245 int chcnt; 246 int cnt; 247 int col; 248 int colwidth; 249 int endcol; 250 int num; 251 int numcols; 252 int numrows; 253 int row; 254 int tabwidth; 255 256 if (f_notabs) 257 tabwidth = 1; 258 else 259 tabwidth = 8; 260 261 /* 262 * Have to do random access in the linked list -- build a table 263 * of pointers. 264 */ 265 if (dp->entries > lastentries) { 266 if ((narray = 267 realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) { 268 warn(NULL); 269 printscol(dp); 270 return; 271 } 272 lastentries = dp->entries; 273 array = narray; 274 } 275 for (p = dp->list, num = 0; p; p = p->fts_link) 276 if (p->fts_number != NO_PRINT) 277 array[num++] = p; 278 279 colwidth = dp->maxlen; 280 if (f_inode) 281 colwidth += dp->s_inode + 1; 282 if (f_size) 283 colwidth += dp->s_block + 1; 284 if (f_type) 285 colwidth += 1; 286 287 colwidth = (colwidth + tabwidth) & ~(tabwidth - 1); 288 if (termwidth < 2 * colwidth) { 289 printscol(dp); 290 return; 291 } 292 numcols = termwidth / colwidth; 293 numrows = num / numcols; 294 if (num % numcols) 295 ++numrows; 296 297 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) 298 (void)printf("total %lu\n", howmany(dp->btotal, blocksize)); 299 300 base = 0; 301 for (row = 0; row < numrows; ++row) { 302 endcol = colwidth; 303 if (!f_sortacross) 304 base = row; 305 for (col = 0, chcnt = 0; col < numcols; ++col) { 306 chcnt += printaname(array[base], dp->s_inode, 307 dp->s_block); 308 if (f_sortacross) 309 base++; 310 else 311 base += numrows; 312 if (base >= num) 313 break; 314 while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1))) 315 <= endcol) { 316 if (f_sortacross && col + 1 >= numcols) 317 break; 318 (void)putchar(f_notabs ? ' ' : '\t'); 319 chcnt = cnt; 320 } 321 endcol += colwidth; 322 } 323 (void)putchar('\n'); 324 } 325 } 326 327 /* 328 * print [inode] [size] name 329 * return # of characters printed, no trailing characters. 330 */ 331 static int 332 printaname(const FTSENT *p, u_long inodefield, u_long sizefield) 333 { 334 struct stat *sp; 335 int chcnt; 336 #ifdef COLORLS 337 int color_printed = 0; 338 #endif 339 340 sp = p->fts_statp; 341 chcnt = 0; 342 if (f_inode) 343 chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino); 344 if (f_size) 345 chcnt += printf("%*jd ", 346 (int)sizefield, howmany(sp->st_blocks, blocksize)); 347 #ifdef COLORLS 348 if (f_color) 349 color_printed = colortype(sp->st_mode); 350 #endif 351 chcnt += printname(p->fts_name); 352 #ifdef COLORLS 353 if (f_color && color_printed) 354 endcolor(0); 355 #endif 356 if (f_type) 357 chcnt += printtype(sp->st_mode); 358 return (chcnt); 359 } 360 361 static void 362 printtime(time_t ftime) 363 { 364 char longstring[80]; 365 static time_t now = 0; 366 const char *format; 367 static int d_first = -1; 368 369 if (d_first < 0) 370 d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 371 if (now == 0) 372 now = time(NULL); 373 374 #define SIXMONTHS ((365 / 2) * 86400) 375 if (f_sectime) 376 /* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */ 377 format = d_first ? "%e %b %T %Y " : "%b %e %T %Y "; 378 else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS) 379 /* mmm dd hh:mm || dd mmm hh:mm */ 380 format = d_first ? "%e %b %R " : "%b %e %R "; 381 else 382 /* mmm dd yyyy || dd mmm yyyy */ 383 format = d_first ? "%e %b %Y " : "%b %e %Y "; 384 strftime(longstring, sizeof(longstring), format, localtime(&ftime)); 385 fputs(longstring, stdout); 386 } 387 388 static int 389 printtype(u_int mode) 390 { 391 392 if (f_slash) { 393 if ((mode & S_IFMT) == S_IFDIR) { 394 (void)putchar('/'); 395 return (1); 396 } 397 return (0); 398 } 399 400 switch (mode & S_IFMT) { 401 case S_IFDIR: 402 (void)putchar('/'); 403 return (1); 404 case S_IFIFO: 405 (void)putchar('|'); 406 return (1); 407 case S_IFLNK: 408 (void)putchar('@'); 409 return (1); 410 case S_IFSOCK: 411 (void)putchar('='); 412 return (1); 413 case S_IFWHT: 414 (void)putchar('%'); 415 return (1); 416 default: 417 break; 418 } 419 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) { 420 (void)putchar('*'); 421 return (1); 422 } 423 return (0); 424 } 425 426 #ifdef COLORLS 427 static int 428 putch(int c) 429 { 430 (void)putchar(c); 431 return 0; 432 } 433 434 static int 435 writech(int c) 436 { 437 char tmp = (char)c; 438 439 (void)write(STDOUT_FILENO, &tmp, 1); 440 return 0; 441 } 442 443 static void 444 printcolor(Colors c) 445 { 446 char *ansiseq; 447 448 if (colors[c].bold) 449 tputs(enter_bold, 1, putch); 450 451 if (colors[c].num[0] != -1) { 452 ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]); 453 if (ansiseq) 454 tputs(ansiseq, 1, putch); 455 } 456 if (colors[c].num[1] != -1) { 457 ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]); 458 if (ansiseq) 459 tputs(ansiseq, 1, putch); 460 } 461 } 462 463 static void 464 endcolor(int sig) 465 { 466 tputs(ansi_coloff, 1, sig ? writech : putch); 467 tputs(attrs_off, 1, sig ? writech : putch); 468 } 469 470 static int 471 colortype(mode_t mode) 472 { 473 switch (mode & S_IFMT) { 474 case S_IFDIR: 475 if (mode & S_IWOTH) 476 if (mode & S_ISTXT) 477 printcolor(C_WSDIR); 478 else 479 printcolor(C_WDIR); 480 else 481 printcolor(C_DIR); 482 return (1); 483 case S_IFLNK: 484 printcolor(C_LNK); 485 return (1); 486 case S_IFSOCK: 487 printcolor(C_SOCK); 488 return (1); 489 case S_IFIFO: 490 printcolor(C_FIFO); 491 return (1); 492 case S_IFBLK: 493 printcolor(C_BLK); 494 return (1); 495 case S_IFCHR: 496 printcolor(C_CHR); 497 return (1); 498 default:; 499 } 500 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) { 501 if (mode & S_ISUID) 502 printcolor(C_SUID); 503 else if (mode & S_ISGID) 504 printcolor(C_SGID); 505 else 506 printcolor(C_EXEC); 507 return (1); 508 } 509 return (0); 510 } 511 512 void 513 parsecolors(const char *cs) 514 { 515 int i; 516 int j; 517 size_t len; 518 char c[2]; 519 short legacy_warn = 0; 520 521 if (cs == NULL) 522 cs = ""; /* LSCOLORS not set */ 523 len = strlen(cs); 524 for (i = 0; i < (int)C_NUMCOLORS; i++) { 525 colors[i].bold = 0; 526 527 if (len <= 2 * (size_t)i) { 528 c[0] = defcolors[2 * i]; 529 c[1] = defcolors[2 * i + 1]; 530 } else { 531 c[0] = cs[2 * i]; 532 c[1] = cs[2 * i + 1]; 533 } 534 for (j = 0; j < 2; j++) { 535 /* Legacy colours used 0-7 */ 536 if (c[j] >= '0' && c[j] <= '7') { 537 colors[i].num[j] = c[j] - '0'; 538 if (!legacy_warn) { 539 warnx("LSCOLORS should use " 540 "characters a-h instead of 0-9 (" 541 "see the manual page)"); 542 } 543 legacy_warn = 1; 544 } else if (c[j] >= 'a' && c[j] <= 'h') 545 colors[i].num[j] = c[j] - 'a'; 546 else if (c[j] >= 'A' && c[j] <= 'H') { 547 colors[i].num[j] = c[j] - 'A'; 548 colors[i].bold = 1; 549 } else if (tolower((unsigned char)c[j]) == 'x') 550 colors[i].num[j] = -1; 551 else { 552 warnx("invalid character '%c' in LSCOLORS" 553 " env var", c[j]); 554 colors[i].num[j] = -1; 555 } 556 } 557 } 558 } 559 560 void 561 colorquit(int sig) 562 { 563 endcolor(sig); 564 565 (void)signal(sig, SIG_DFL); 566 (void)kill(getpid(), sig); 567 } 568 569 #endif /* COLORLS */ 570 571 static void 572 printlink(const FTSENT *p) 573 { 574 int lnklen; 575 char name[MAXPATHLEN + 1]; 576 char path[MAXPATHLEN + 1]; 577 578 if (p->fts_level == FTS_ROOTLEVEL) 579 (void)snprintf(name, sizeof(name), "%s", p->fts_name); 580 else 581 (void)snprintf(name, sizeof(name), 582 "%s/%s", p->fts_parent->fts_accpath, p->fts_name); 583 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) { 584 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno)); 585 return; 586 } 587 path[lnklen] = '\0'; 588 (void)printf(" -> "); 589 (void)printname(path); 590 } 591 592 static void 593 printsize(size_t width, off_t bytes) 594 { 595 596 if (f_humanval) { 597 char buf[5]; 598 599 humanize_number(buf, sizeof(buf), (int64_t)bytes, "", 600 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 601 (void)printf("%5s ", buf); 602 } else 603 (void)printf("%*jd ", (u_int)width, bytes); 604 } 605 606 static void 607 aclmode(char *buf, const FTSENT *p, int *haveacls) 608 { 609 char name[MAXPATHLEN + 1]; 610 int entries, ret; 611 acl_t facl; 612 acl_entry_t ae; 613 614 /* 615 * Add a + after the standard rwxrwxrwx mode if the file has an 616 * extended ACL. strmode() reserves space at the end of the string. 617 */ 618 if (p->fts_level == FTS_ROOTLEVEL) 619 snprintf(name, sizeof(name), "%s", p->fts_name); 620 else 621 snprintf(name, sizeof(name), "%s/%s", 622 p->fts_parent->fts_accpath, p->fts_name); 623 /* 624 * We have no way to tell whether a symbolic link has an ACL since 625 * pathconf() and acl_get_file() both follow them. 626 */ 627 if (S_ISLNK(p->fts_statp->st_mode)) { 628 *haveacls = 1; 629 return; 630 } 631 if ((ret = pathconf(name, _PC_ACL_EXTENDED)) <= 0) { 632 if (ret < 0 && errno != EINVAL) 633 warn("%s", name); 634 else 635 *haveacls = 0; 636 return; 637 } 638 *haveacls = 1; 639 if ((facl = acl_get_file(name, ACL_TYPE_ACCESS)) != NULL) { 640 if (acl_get_entry(facl, ACL_FIRST_ENTRY, &ae) == 1) { 641 entries = 1; 642 while (acl_get_entry(facl, ACL_NEXT_ENTRY, &ae) == 1) 643 if (++entries > 3) 644 break; 645 /* 646 * POSIX.1e requires that ACLs of type ACL_TYPE_ACCESS 647 * must have at least three entries (owner, group, 648 * and other). So anything with more than 3 ACLs looks 649 * interesting to us. 650 */ 651 if (entries > 3) 652 buf[10] = '+'; 653 } 654 acl_free(facl); 655 } else 656 warn("%s", name); 657 } 658