2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static char sccsid[] = "@(#)print.c 8.4 (Berkeley) 4/17/94";
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
69 static int printaname(const FTSENT *, u_long, u_long);
70 static void printdev(size_t, dev_t);
71 static void printlink(const FTSENT *);
72 static void printtime(time_t);
73 static int printtype(u_int);
74 static void printsize(size_t, off_t);
76 static void endcolor_termcap(int);
77 static void endcolor_ansi(void);
78 static void endcolor(int);
79 static int colortype(mode_t);
81 static void aclmode(char *, const FTSENT *);
83 #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
86 /* Most of these are taken from <sys/stat.h> */
88 C_DIR, /* directory */
89 C_LNK, /* symbolic link */
92 C_EXEC, /* executable */
93 C_BLK, /* block special */
94 C_CHR, /* character special */
95 C_SUID, /* setuid executable */
96 C_SGID, /* setgid executable */
97 C_WSDIR, /* directory writeble to others, with sticky
99 C_WDIR, /* directory writeble to others, without
101 C_NUMCOLORS /* just a place-holder */
104 static const char *defcolors = "exfxcxdxbxegedabagacad";
106 /* colors for file types */
111 } colors[C_NUMCOLORS];
114 static size_t padding_for_month[12];
115 static size_t month_max_size = 0;
118 printscol(const DISPLAY *dp)
122 for (p = dp->list; p; p = p->fts_link) {
125 (void)printaname(p, dp->s_inode, dp->s_block);
131 * print name in current style
134 printname(const char *name)
136 if (f_octal || f_octal_escape)
137 return prn_octal(name);
139 return prn_printable(name);
141 return prn_normal(name);
149 case 0: return (nl_langinfo(ABMON_1));
150 case 1: return (nl_langinfo(ABMON_2));
151 case 2: return (nl_langinfo(ABMON_3));
152 case 3: return (nl_langinfo(ABMON_4));
153 case 4: return (nl_langinfo(ABMON_5));
154 case 5: return (nl_langinfo(ABMON_6));
155 case 6: return (nl_langinfo(ABMON_7));
156 case 7: return (nl_langinfo(ABMON_8));
157 case 8: return (nl_langinfo(ABMON_9));
158 case 9: return (nl_langinfo(ABMON_10));
159 case 10: return (nl_langinfo(ABMON_11));
160 case 11: return (nl_langinfo(ABMON_12));
163 /* should never happen */
168 mbswidth(const char *month)
171 size_t width, donelen, clen, w;
174 while ((clen = mbrtowc(&wc, month + donelen, MB_LEN_MAX, NULL)) != 0) {
175 if (clen == (size_t)-1 || clen == (size_t)-2)
178 if ((w = wcwidth(wc)) == (size_t)-1)
187 compute_abbreviated_month_size(void)
191 size_t months_width[12];
193 for (i = 0; i < 12; i++) {
194 width = mbswidth(get_abmon(i));
195 if (width == (size_t)-1) {
199 months_width[i] = width;
200 if (width > month_max_size)
201 month_max_size = width;
204 for (i = 0; i < 12; i++)
205 padding_for_month[i] = month_max_size - months_width[i];
209 printlong(const DISPLAY *dp)
216 int color_printed = 0;
219 if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
220 (f_longform || f_size)) {
221 (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
224 for (p = dp->list; p; p = p->fts_link) {
229 (void)printf("%*ju ",
230 dp->s_inode, (uintmax_t)sp->st_ino);
232 (void)printf("%*jd ",
233 dp->s_block, howmany(sp->st_blocks, blocksize));
234 strmode(sp->st_mode, buf);
237 (void)printf("%s %*ju ", buf, dp->s_nlink,
238 (uintmax_t)sp->st_nlink);
240 (void)printf("%-*s ", dp->s_user, np->user);
241 (void)printf("%-*s ", dp->s_group, np->group);
243 (void)printf("%-*s ", dp->s_flags, np->flags);
245 (void)printf("%-*s ", dp->s_label, np->label);
246 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
247 printdev(dp->s_size, sp->st_rdev);
249 printsize(dp->s_size, sp->st_size);
251 printtime(sp->st_atime);
252 else if (f_birthtime)
253 printtime(sp->st_birthtime);
254 else if (f_statustime)
255 printtime(sp->st_ctime);
257 printtime(sp->st_mtime);
260 color_printed = colortype(sp->st_mode);
262 (void)printname(p->fts_name);
264 if (f_color && color_printed)
268 (void)printtype(sp->st_mode);
269 if (S_ISLNK(sp->st_mode))
276 printstream(const DISPLAY *dp)
281 for (p = dp->list, chcnt = 0; p; p = p->fts_link) {
282 if (p->fts_number == NO_PRINT)
284 /* XXX strlen does not take octal escapes into account. */
285 if (strlen(p->fts_name) + chcnt +
286 (p->fts_link ? 2 : 0) >= (unsigned)termwidth) {
290 chcnt += printaname(p, dp->s_inode, dp->s_block);
301 printcol(const DISPLAY *dp)
303 static FTSENT **array;
304 static int lastentries = -1;
325 * Have to do random access in the linked list -- build a table
328 if (dp->entries > lastentries) {
330 realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
335 lastentries = dp->entries;
338 for (p = dp->list, num = 0; p; p = p->fts_link)
339 if (p->fts_number != NO_PRINT)
342 colwidth = dp->maxlen;
344 colwidth += dp->s_inode + 1;
346 colwidth += dp->s_block + 1;
350 colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
351 if (termwidth < 2 * colwidth) {
355 numcols = termwidth / colwidth;
356 numrows = num / numcols;
360 if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
361 (f_longform || f_size)) {
362 (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
366 for (row = 0; row < numrows; ++row) {
370 for (col = 0, chcnt = 0; col < numcols; ++col) {
371 chcnt += printaname(array[base], dp->s_inode,
379 while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
381 if (f_sortacross && col + 1 >= numcols)
383 (void)putchar(f_notabs ? ' ' : '\t');
393 * print [inode] [size] name
394 * return # of characters printed, no trailing characters.
397 printaname(const FTSENT *p, u_long inodefield, u_long sizefield)
402 int color_printed = 0;
408 chcnt += printf("%*ju ",
409 (int)inodefield, (uintmax_t)sp->st_ino);
411 chcnt += printf("%*jd ",
412 (int)sizefield, howmany(sp->st_blocks, blocksize));
415 color_printed = colortype(sp->st_mode);
417 chcnt += printname(p->fts_name);
419 if (f_color && color_printed)
423 chcnt += printtype(sp->st_mode);
428 * Print device special file major and minor numbers.
431 printdev(size_t width, dev_t dev)
434 (void)printf("%#*jx ", (u_int)width, (uintmax_t)dev);
438 ls_strftime(char *str, size_t len, const char *fmt, const struct tm *tm)
440 char *posb, nfmt[BUFSIZ];
441 const char *format = fmt;
443 if ((posb = strstr(fmt, "%b")) != NULL) {
444 if (month_max_size == 0) {
445 compute_abbreviated_month_size();
447 if (month_max_size > 0 && tm != NULL) {
448 snprintf(nfmt, sizeof(nfmt), "%.*s%s%*s%s",
449 (int)(posb - fmt), fmt,
450 get_abmon(tm->tm_mon),
451 (int)padding_for_month[tm->tm_mon],
458 strftime(str, len, format, tm);
460 strlcpy(str, "bad date val", len);
464 printtime(time_t ftime)
467 static time_t now = 0;
469 static int d_first = -1;
472 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
476 #define SIXMONTHS ((365 / 2) * 86400)
477 if (f_timeformat) /* user specified format */
478 format = f_timeformat;
480 /* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */
481 format = d_first ? "%e %b %T %Y" : "%b %e %T %Y";
482 else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
483 /* mmm dd hh:mm || dd mmm hh:mm */
484 format = d_first ? "%e %b %R" : "%b %e %R";
486 /* mmm dd yyyy || dd mmm yyyy */
487 format = d_first ? "%e %b %Y" : "%b %e %Y";
488 ls_strftime(longstring, sizeof(longstring), format, localtime(&ftime));
489 fputs(longstring, stdout);
494 printtype(u_int mode)
498 if ((mode & S_IFMT) == S_IFDIR) {
505 switch (mode & S_IFMT) {
524 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
544 (void)write(STDOUT_FILENO, &tmp, 1);
549 printcolor_termcap(Colors c)
554 tputs(enter_bold, 1, putch);
555 if (colors[c].underline)
556 tputs(enter_underline, 1, putch);
558 if (colors[c].num[0] != -1) {
559 ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]);
561 tputs(ansiseq, 1, putch);
563 if (colors[c].num[1] != -1) {
564 ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]);
566 tputs(ansiseq, 1, putch);
571 printcolor_ansi(Colors c)
578 if (colors[c].underline)
580 if (colors[c].num[0] != -1)
581 printf(";3%d", colors[c].num[0]);
582 if (colors[c].num[1] != -1)
583 printf(";4%d", colors[c].num[1]);
594 printcolor_termcap(c);
598 endcolor_termcap(int sig)
601 tputs(ansi_coloff, 1, sig ? writech : putch);
602 tputs(attrs_off, 1, sig ? writech : putch);
619 endcolor_termcap(sig);
623 colortype(mode_t mode)
625 switch (mode & S_IFMT) {
652 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
655 else if (mode & S_ISGID)
665 parsecolors(const char *cs)
671 short legacy_warn = 0;
674 cs = ""; /* LSCOLORS not set */
676 for (i = 0; i < (int)C_NUMCOLORS; i++) {
677 colors[i].bold = false;
678 colors[i].underline = false;
680 if (len <= 2 * (size_t)i) {
681 c[0] = defcolors[2 * i];
682 c[1] = defcolors[2 * i + 1];
685 c[1] = cs[2 * i + 1];
687 for (j = 0; j < 2; j++) {
688 /* Legacy colours used 0-7 */
689 if (c[j] >= '0' && c[j] <= '7') {
690 colors[i].num[j] = c[j] - '0';
692 warnx("LSCOLORS should use "
693 "characters a-h instead of 0-9 ("
694 "see the manual page)");
697 } else if (c[j] >= 'a' && c[j] <= 'h')
698 colors[i].num[j] = c[j] - 'a';
699 else if (c[j] >= 'A' && c[j] <= 'H') {
700 colors[i].num[j] = c[j] - 'A';
702 colors[i].underline = true;
704 colors[i].bold = true;
705 } else if (tolower((unsigned char)c[j]) == 'x') {
706 if (j == 1 && c[j] == 'X')
707 colors[i].underline = true;
708 colors[i].num[j] = -1;
710 warnx("invalid character '%c' in LSCOLORS"
712 colors[i].num[j] = -1;
723 (void)signal(sig, SIG_DFL);
724 (void)kill(getpid(), sig);
730 printlink(const FTSENT *p)
733 char name[MAXPATHLEN + 1];
734 char path[MAXPATHLEN + 1];
736 if (p->fts_level == FTS_ROOTLEVEL)
737 (void)snprintf(name, sizeof(name), "%s", p->fts_name);
739 (void)snprintf(name, sizeof(name),
740 "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
741 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
742 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
746 (void)printf(" -> ");
747 (void)printname(path);
751 printsize(size_t width, off_t bytes)
756 * Reserve one space before the size and allocate room for
759 char buf[HUMANVALSTR_LEN - 1 + 1];
761 humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
762 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
763 (void)printf("%*s ", (u_int)width, buf);
764 } else if (f_thousands) { /* with commas */
765 /* This format assignment needed to work round gcc bug. */
766 const char *format = "%*j'd ";
767 (void)printf(format, (u_int)width, bytes);
769 (void)printf("%*jd ", (u_int)width, bytes);
773 * Add a + after the standard rwxrwxrwx mode if the file has an
774 * ACL. strmode() reserves space at the end of the string.
777 aclmode(char *buf, const FTSENT *p)
779 char name[MAXPATHLEN + 1];
781 static dev_t previous_dev = NODEV;
782 static int supports_acls = -1;
783 static int type = ACL_TYPE_ACCESS;
787 * XXX: ACLs are not supported on whiteouts and device files
790 if (S_ISCHR(p->fts_statp->st_mode) || S_ISBLK(p->fts_statp->st_mode) ||
791 S_ISWHT(p->fts_statp->st_mode))
794 if (previous_dev == p->fts_statp->st_dev && supports_acls == 0)
797 if (p->fts_level == FTS_ROOTLEVEL)
798 snprintf(name, sizeof(name), "%s", p->fts_name);
800 snprintf(name, sizeof(name), "%s/%s",
801 p->fts_parent->fts_accpath, p->fts_name);
803 if (previous_dev != p->fts_statp->st_dev) {
804 previous_dev = p->fts_statp->st_dev;
807 ret = lpathconf(name, _PC_ACL_NFS4);
809 type = ACL_TYPE_NFS4;
811 } else if (ret < 0 && errno != EINVAL) {
815 if (supports_acls == 0) {
816 ret = lpathconf(name, _PC_ACL_EXTENDED);
818 type = ACL_TYPE_ACCESS;
820 } else if (ret < 0 && errno != EINVAL) {
826 if (supports_acls == 0)
828 facl = acl_get_link_np(name, type);
833 if (acl_is_trivial_np(facl, &trivial)) {