2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
35 static char sccsid[] = "@(#)print.c 8.4 (Berkeley) 4/17/94";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
65 static int printaname(const FTSENT *, u_long, u_long);
66 static void printdev(size_t, dev_t);
67 static void printlink(const FTSENT *);
68 static void printtime(time_t);
69 static int printtype(u_int);
70 static void printsize(size_t, off_t);
72 static void endcolor(int);
73 static int colortype(mode_t);
75 static void aclmode(char *, const FTSENT *);
77 #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
80 /* Most of these are taken from <sys/stat.h> */
82 C_DIR, /* directory */
83 C_LNK, /* symbolic link */
86 C_EXEC, /* executable */
87 C_BLK, /* block special */
88 C_CHR, /* character special */
89 C_SUID, /* setuid executable */
90 C_SGID, /* setgid executable */
91 C_WSDIR, /* directory writeble to others, with sticky
93 C_WDIR, /* directory writeble to others, without
95 C_NUMCOLORS /* just a place-holder */
98 static const char *defcolors = "exfxcxdxbxegedabagacad";
100 /* colors for file types */
104 } colors[C_NUMCOLORS];
108 printscol(const DISPLAY *dp)
112 for (p = dp->list; p; p = p->fts_link) {
115 (void)printaname(p, dp->s_inode, dp->s_block);
121 * print name in current style
124 printname(const char *name)
126 if (f_octal || f_octal_escape)
127 return prn_octal(name);
129 return prn_printable(name);
131 return prn_normal(name);
135 printlong(const DISPLAY *dp)
142 int color_printed = 0;
145 if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
146 (f_longform || f_size)) {
147 (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
150 for (p = dp->list; p; p = p->fts_link) {
155 (void)printf("%*ju ",
156 dp->s_inode, (uintmax_t)sp->st_ino);
158 (void)printf("%*jd ",
159 dp->s_block, howmany(sp->st_blocks, blocksize));
160 strmode(sp->st_mode, buf);
163 (void)printf("%s %*u %-*s %-*s ", buf, dp->s_nlink,
164 sp->st_nlink, dp->s_user, np->user, dp->s_group,
167 (void)printf("%-*s ", dp->s_flags, np->flags);
169 (void)printf("%-*s ", dp->s_label, np->label);
170 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
171 printdev(dp->s_size, sp->st_rdev);
173 printsize(dp->s_size, sp->st_size);
175 printtime(sp->st_atime);
176 else if (f_birthtime)
177 printtime(sp->st_birthtime);
178 else if (f_statustime)
179 printtime(sp->st_ctime);
181 printtime(sp->st_mtime);
184 color_printed = colortype(sp->st_mode);
186 (void)printname(p->fts_name);
188 if (f_color && color_printed)
192 (void)printtype(sp->st_mode);
193 if (S_ISLNK(sp->st_mode))
200 printstream(const DISPLAY *dp)
205 for (p = dp->list, chcnt = 0; p; p = p->fts_link) {
206 if (p->fts_number == NO_PRINT)
208 /* XXX strlen does not take octal escapes into account. */
209 if (strlen(p->fts_name) + chcnt +
210 (p->fts_link ? 2 : 0) >= (unsigned)termwidth) {
214 chcnt += printaname(p, dp->s_inode, dp->s_block);
225 printcol(const DISPLAY *dp)
227 static FTSENT **array;
228 static int lastentries = -1;
249 * Have to do random access in the linked list -- build a table
252 if (dp->entries > lastentries) {
254 realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
259 lastentries = dp->entries;
262 for (p = dp->list, num = 0; p; p = p->fts_link)
263 if (p->fts_number != NO_PRINT)
266 colwidth = dp->maxlen;
268 colwidth += dp->s_inode + 1;
270 colwidth += dp->s_block + 1;
274 colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
275 if (termwidth < 2 * colwidth) {
279 numcols = termwidth / colwidth;
280 numrows = num / numcols;
284 if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
285 (f_longform || f_size)) {
286 (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
290 for (row = 0; row < numrows; ++row) {
294 for (col = 0, chcnt = 0; col < numcols; ++col) {
295 chcnt += printaname(array[base], dp->s_inode,
303 while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
305 if (f_sortacross && col + 1 >= numcols)
307 (void)putchar(f_notabs ? ' ' : '\t');
317 * print [inode] [size] name
318 * return # of characters printed, no trailing characters.
321 printaname(const FTSENT *p, u_long inodefield, u_long sizefield)
326 int color_printed = 0;
332 chcnt += printf("%*ju ",
333 (int)inodefield, (uintmax_t)sp->st_ino);
335 chcnt += printf("%*jd ",
336 (int)sizefield, howmany(sp->st_blocks, blocksize));
339 color_printed = colortype(sp->st_mode);
341 chcnt += printname(p->fts_name);
343 if (f_color && color_printed)
347 chcnt += printtype(sp->st_mode);
352 * Print device special file major and minor numbers.
355 printdev(size_t width, dev_t dev)
358 (void)printf("%#*jx ", (u_int)width, (uintmax_t)dev);
362 printtime(time_t ftime)
365 static time_t now = 0;
367 static int d_first = -1;
370 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
374 #define SIXMONTHS ((365 / 2) * 86400)
375 if (f_timeformat) /* user specified format */
376 format = f_timeformat;
378 /* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */
379 format = d_first ? "%e %b %T %Y" : "%b %e %T %Y";
380 else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
381 /* mmm dd hh:mm || dd mmm hh:mm */
382 format = d_first ? "%e %b %R" : "%b %e %R";
384 /* mmm dd yyyy || dd mmm yyyy */
385 format = d_first ? "%e %b %Y" : "%b %e %Y";
386 strftime(longstring, sizeof(longstring), format, localtime(&ftime));
387 fputs(longstring, stdout);
392 printtype(u_int mode)
396 if ((mode & S_IFMT) == S_IFDIR) {
403 switch (mode & S_IFMT) {
422 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
442 (void)write(STDOUT_FILENO, &tmp, 1);
452 tputs(enter_bold, 1, putch);
454 if (colors[c].num[0] != -1) {
455 ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]);
457 tputs(ansiseq, 1, putch);
459 if (colors[c].num[1] != -1) {
460 ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]);
462 tputs(ansiseq, 1, putch);
469 tputs(ansi_coloff, 1, sig ? writech : putch);
470 tputs(attrs_off, 1, sig ? writech : putch);
474 colortype(mode_t mode)
476 switch (mode & S_IFMT) {
503 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
506 else if (mode & S_ISGID)
516 parsecolors(const char *cs)
522 short legacy_warn = 0;
525 cs = ""; /* LSCOLORS not set */
527 for (i = 0; i < (int)C_NUMCOLORS; i++) {
530 if (len <= 2 * (size_t)i) {
531 c[0] = defcolors[2 * i];
532 c[1] = defcolors[2 * i + 1];
535 c[1] = cs[2 * i + 1];
537 for (j = 0; j < 2; j++) {
538 /* Legacy colours used 0-7 */
539 if (c[j] >= '0' && c[j] <= '7') {
540 colors[i].num[j] = c[j] - '0';
542 warnx("LSCOLORS should use "
543 "characters a-h instead of 0-9 ("
544 "see the manual page)");
547 } else if (c[j] >= 'a' && c[j] <= 'h')
548 colors[i].num[j] = c[j] - 'a';
549 else if (c[j] >= 'A' && c[j] <= 'H') {
550 colors[i].num[j] = c[j] - 'A';
552 } else if (tolower((unsigned char)c[j]) == 'x')
553 colors[i].num[j] = -1;
555 warnx("invalid character '%c' in LSCOLORS"
557 colors[i].num[j] = -1;
568 (void)signal(sig, SIG_DFL);
569 (void)kill(getpid(), sig);
575 printlink(const FTSENT *p)
578 char name[MAXPATHLEN + 1];
579 char path[MAXPATHLEN + 1];
581 if (p->fts_level == FTS_ROOTLEVEL)
582 (void)snprintf(name, sizeof(name), "%s", p->fts_name);
584 (void)snprintf(name, sizeof(name),
585 "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
586 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
587 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
591 (void)printf(" -> ");
592 (void)printname(path);
596 printsize(size_t width, off_t bytes)
601 * Reserve one space before the size and allocate room for
604 char buf[HUMANVALSTR_LEN - 1 + 1];
606 humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
607 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
608 (void)printf("%*s ", (u_int)width, buf);
609 } else if (f_thousands) { /* with commas */
610 /* This format assignment needed to work round gcc bug. */
611 const char *format = "%*j'd ";
612 (void)printf(format, (u_int)width, bytes);
614 (void)printf("%*jd ", (u_int)width, bytes);
618 * Add a + after the standard rwxrwxrwx mode if the file has an
619 * ACL. strmode() reserves space at the end of the string.
622 aclmode(char *buf, const FTSENT *p)
624 char name[MAXPATHLEN + 1];
626 static dev_t previous_dev = NODEV;
627 static int supports_acls = -1;
628 static int type = ACL_TYPE_ACCESS;
632 * XXX: ACLs are not supported on whiteouts and device files
635 if (S_ISCHR(p->fts_statp->st_mode) || S_ISBLK(p->fts_statp->st_mode) ||
636 S_ISWHT(p->fts_statp->st_mode))
639 if (previous_dev == p->fts_statp->st_dev && supports_acls == 0)
642 if (p->fts_level == FTS_ROOTLEVEL)
643 snprintf(name, sizeof(name), "%s", p->fts_name);
645 snprintf(name, sizeof(name), "%s/%s",
646 p->fts_parent->fts_accpath, p->fts_name);
648 if (previous_dev != p->fts_statp->st_dev) {
649 previous_dev = p->fts_statp->st_dev;
652 ret = lpathconf(name, _PC_ACL_NFS4);
654 type = ACL_TYPE_NFS4;
656 } else if (ret < 0 && errno != EINVAL) {
660 if (supports_acls == 0) {
661 ret = lpathconf(name, _PC_ACL_EXTENDED);
663 type = ACL_TYPE_ACCESS;
665 } else if (ret < 0 && errno != EINVAL) {
671 if (supports_acls == 0)
673 facl = acl_get_link_np(name, type);
678 if (acl_is_trivial_np(facl, &trivial)) {