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 #include <sys/param.h>
67 static int printaname(const FTSENT *, u_long, u_long);
68 static void printdev(size_t, dev_t);
69 static void printlink(const FTSENT *);
70 static void printtime(time_t);
71 static int printtype(u_int);
72 static void printsize(size_t, off_t);
74 static void endcolor_termcap(int);
75 static void endcolor_ansi(void);
76 static void endcolor(int);
77 static int colortype(mode_t);
79 static void aclmode(char *, const FTSENT *);
81 #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
84 /* Most of these are taken from <sys/stat.h> */
86 C_DIR, /* directory */
87 C_LNK, /* symbolic link */
90 C_EXEC, /* executable */
91 C_BLK, /* block special */
92 C_CHR, /* character special */
93 C_SUID, /* setuid executable */
94 C_SGID, /* setgid executable */
95 C_WSDIR, /* directory writeble to others, with sticky
97 C_WDIR, /* directory writeble to others, without
99 C_NUMCOLORS /* just a place-holder */
102 static const char *defcolors = "exfxcxdxbxegedabagacad";
104 /* colors for file types */
109 } colors[C_NUMCOLORS];
112 static size_t padding_for_month[12];
113 static size_t month_max_size = 0;
116 printscol(const DISPLAY *dp)
120 for (p = dp->list; p; p = p->fts_link) {
123 (void)printaname(p, dp->s_inode, dp->s_block);
129 * print name in current style
132 printname(const char *name)
134 if (f_octal || f_octal_escape)
135 return prn_octal(name);
137 return prn_printable(name);
139 return prn_normal(name);
147 case 0: return (nl_langinfo(ABMON_1));
148 case 1: return (nl_langinfo(ABMON_2));
149 case 2: return (nl_langinfo(ABMON_3));
150 case 3: return (nl_langinfo(ABMON_4));
151 case 4: return (nl_langinfo(ABMON_5));
152 case 5: return (nl_langinfo(ABMON_6));
153 case 6: return (nl_langinfo(ABMON_7));
154 case 7: return (nl_langinfo(ABMON_8));
155 case 8: return (nl_langinfo(ABMON_9));
156 case 9: return (nl_langinfo(ABMON_10));
157 case 10: return (nl_langinfo(ABMON_11));
158 case 11: return (nl_langinfo(ABMON_12));
161 /* should never happen */
166 mbswidth(const char *month)
169 size_t width, donelen, clen, w;
172 while ((clen = mbrtowc(&wc, month + donelen, MB_LEN_MAX, NULL)) != 0) {
173 if (clen == (size_t)-1 || clen == (size_t)-2)
176 if ((w = wcwidth(wc)) == (size_t)-1)
185 compute_abbreviated_month_size(void)
189 size_t months_width[12];
191 for (i = 0; i < 12; i++) {
192 width = mbswidth(get_abmon(i));
193 if (width == (size_t)-1) {
197 months_width[i] = width;
198 if (width > month_max_size)
199 month_max_size = width;
202 for (i = 0; i < 12; i++)
203 padding_for_month[i] = month_max_size - months_width[i];
207 printlong(const DISPLAY *dp)
214 int color_printed = 0;
217 if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
218 (f_longform || f_size)) {
219 (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
222 for (p = dp->list; p; p = p->fts_link) {
227 (void)printf("%*ju ",
228 dp->s_inode, (uintmax_t)sp->st_ino);
230 (void)printf("%*jd ",
231 dp->s_block, howmany(sp->st_blocks, blocksize));
232 strmode(sp->st_mode, buf);
235 (void)printf("%s %*ju ", buf, dp->s_nlink,
236 (uintmax_t)sp->st_nlink);
238 (void)printf("%-*s ", dp->s_user, np->user);
239 (void)printf("%-*s ", dp->s_group, np->group);
241 (void)printf("%-*s ", dp->s_flags, np->flags);
243 (void)printf("%-*s ", dp->s_label, np->label);
244 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
245 printdev(dp->s_size, sp->st_rdev);
247 printsize(dp->s_size, sp->st_size);
249 printtime(sp->st_atime);
250 else if (f_birthtime)
251 printtime(sp->st_birthtime);
252 else if (f_statustime)
253 printtime(sp->st_ctime);
255 printtime(sp->st_mtime);
258 color_printed = colortype(sp->st_mode);
260 (void)printname(p->fts_name);
262 if (f_color && color_printed)
266 (void)printtype(sp->st_mode);
267 if (S_ISLNK(sp->st_mode))
274 printstream(const DISPLAY *dp)
279 for (p = dp->list, chcnt = 0; p; p = p->fts_link) {
280 if (p->fts_number == NO_PRINT)
282 /* XXX strlen does not take octal escapes into account. */
283 if (strlen(p->fts_name) + chcnt +
284 (p->fts_link ? 2 : 0) >= (unsigned)termwidth) {
288 chcnt += printaname(p, dp->s_inode, dp->s_block);
299 printcol(const DISPLAY *dp)
301 static FTSENT **array;
302 static int lastentries = -1;
323 * Have to do random access in the linked list -- build a table
326 if (dp->entries > lastentries) {
328 realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
333 lastentries = dp->entries;
336 for (p = dp->list, num = 0; p; p = p->fts_link)
337 if (p->fts_number != NO_PRINT)
340 colwidth = dp->maxlen;
342 colwidth += dp->s_inode + 1;
344 colwidth += dp->s_block + 1;
348 colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
349 if (termwidth < 2 * colwidth) {
353 numcols = termwidth / colwidth;
354 numrows = num / numcols;
358 if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
359 (f_longform || f_size)) {
360 (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
364 for (row = 0; row < numrows; ++row) {
368 for (col = 0, chcnt = 0; col < numcols; ++col) {
369 chcnt += printaname(array[base], dp->s_inode,
377 while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
379 if (f_sortacross && col + 1 >= numcols)
381 (void)putchar(f_notabs ? ' ' : '\t');
391 * print [inode] [size] name
392 * return # of characters printed, no trailing characters.
395 printaname(const FTSENT *p, u_long inodefield, u_long sizefield)
400 int color_printed = 0;
406 chcnt += printf("%*ju ",
407 (int)inodefield, (uintmax_t)sp->st_ino);
409 chcnt += printf("%*jd ",
410 (int)sizefield, howmany(sp->st_blocks, blocksize));
413 color_printed = colortype(sp->st_mode);
415 chcnt += printname(p->fts_name);
417 if (f_color && color_printed)
421 chcnt += printtype(sp->st_mode);
426 * Print device special file major and minor numbers.
429 printdev(size_t width, dev_t dev)
432 (void)printf("%#*jx ", (u_int)width, (uintmax_t)dev);
436 ls_strftime(char *str, size_t len, const char *fmt, const struct tm *tm)
438 char *posb, nfmt[BUFSIZ];
439 const char *format = fmt;
441 if ((posb = strstr(fmt, "%b")) != NULL) {
442 if (month_max_size == 0) {
443 compute_abbreviated_month_size();
445 if (month_max_size > 0 && tm != NULL) {
446 snprintf(nfmt, sizeof(nfmt), "%.*s%s%*s%s",
447 (int)(posb - fmt), fmt,
448 get_abmon(tm->tm_mon),
449 (int)padding_for_month[tm->tm_mon],
456 strftime(str, len, format, tm);
458 strlcpy(str, "bad date val", len);
462 printtime(time_t ftime)
465 static time_t now = 0;
467 static int d_first = -1;
470 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
474 #define SIXMONTHS ((365 / 2) * 86400)
475 if (f_timeformat) /* user specified format */
476 format = f_timeformat;
478 /* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */
479 format = d_first ? "%e %b %T %Y" : "%b %e %T %Y";
480 else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
481 /* mmm dd hh:mm || dd mmm hh:mm */
482 format = d_first ? "%e %b %R" : "%b %e %R";
484 /* mmm dd yyyy || dd mmm yyyy */
485 format = d_first ? "%e %b %Y" : "%b %e %Y";
486 ls_strftime(longstring, sizeof(longstring), format, localtime(&ftime));
487 fputs(longstring, stdout);
492 printtype(u_int mode)
496 if ((mode & S_IFMT) == S_IFDIR) {
503 switch (mode & S_IFMT) {
522 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
542 (void)write(STDOUT_FILENO, &tmp, 1);
547 printcolor_termcap(Colors c)
552 tputs(enter_bold, 1, putch);
553 if (colors[c].underline)
554 tputs(enter_underline, 1, putch);
556 if (colors[c].num[0] != -1) {
557 ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]);
559 tputs(ansiseq, 1, putch);
561 if (colors[c].num[1] != -1) {
562 ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]);
564 tputs(ansiseq, 1, putch);
569 printcolor_ansi(Colors c)
576 if (colors[c].underline)
578 if (colors[c].num[0] != -1)
579 printf(";3%d", colors[c].num[0]);
580 if (colors[c].num[1] != -1)
581 printf(";4%d", colors[c].num[1]);
592 printcolor_termcap(c);
596 endcolor_termcap(int sig)
599 tputs(ansi_coloff, 1, sig ? writech : putch);
600 tputs(attrs_off, 1, sig ? writech : putch);
617 endcolor_termcap(sig);
621 colortype(mode_t mode)
623 switch (mode & S_IFMT) {
650 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
653 else if (mode & S_ISGID)
663 parsecolors(const char *cs)
669 short legacy_warn = 0;
672 cs = ""; /* LSCOLORS not set */
674 for (i = 0; i < (int)C_NUMCOLORS; i++) {
675 colors[i].bold = false;
676 colors[i].underline = false;
678 if (len <= 2 * (size_t)i) {
679 c[0] = defcolors[2 * i];
680 c[1] = defcolors[2 * i + 1];
683 c[1] = cs[2 * i + 1];
685 for (j = 0; j < 2; j++) {
686 /* Legacy colours used 0-7 */
687 if (c[j] >= '0' && c[j] <= '7') {
688 colors[i].num[j] = c[j] - '0';
690 warnx("LSCOLORS should use "
691 "characters a-h instead of 0-9 ("
692 "see the manual page)");
695 } else if (c[j] >= 'a' && c[j] <= 'h')
696 colors[i].num[j] = c[j] - 'a';
697 else if (c[j] >= 'A' && c[j] <= 'H') {
698 colors[i].num[j] = c[j] - 'A';
700 colors[i].underline = true;
702 colors[i].bold = true;
703 } else if (tolower((unsigned char)c[j]) == 'x') {
704 if (j == 1 && c[j] == 'X')
705 colors[i].underline = true;
706 colors[i].num[j] = -1;
708 warnx("invalid character '%c' in LSCOLORS"
710 colors[i].num[j] = -1;
721 (void)signal(sig, SIG_DFL);
722 (void)kill(getpid(), sig);
728 printlink(const FTSENT *p)
731 char name[MAXPATHLEN + 1];
732 char path[MAXPATHLEN + 1];
734 if (p->fts_level == FTS_ROOTLEVEL)
735 (void)snprintf(name, sizeof(name), "%s", p->fts_name);
737 (void)snprintf(name, sizeof(name),
738 "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
739 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
740 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
744 (void)printf(" -> ");
745 (void)printname(path);
749 printsize(size_t width, off_t bytes)
754 * Reserve one space before the size and allocate room for
757 char buf[HUMANVALSTR_LEN - 1 + 1];
759 humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
760 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
761 (void)printf("%*s ", (u_int)width, buf);
762 } else if (f_thousands) { /* with commas */
763 /* This format assignment needed to work round gcc bug. */
764 const char *format = "%*j'd ";
765 (void)printf(format, (u_int)width, bytes);
767 (void)printf("%*jd ", (u_int)width, bytes);
771 * Add a + after the standard rwxrwxrwx mode if the file has an
772 * ACL. strmode() reserves space at the end of the string.
775 aclmode(char *buf, const FTSENT *p)
777 char name[MAXPATHLEN + 1];
779 static dev_t previous_dev = NODEV;
780 static int supports_acls = -1;
781 static int type = ACL_TYPE_ACCESS;
785 * XXX: ACLs are not supported on whiteouts and device files
788 if (S_ISCHR(p->fts_statp->st_mode) || S_ISBLK(p->fts_statp->st_mode) ||
789 S_ISWHT(p->fts_statp->st_mode))
792 if (previous_dev == p->fts_statp->st_dev && supports_acls == 0)
795 if (p->fts_level == FTS_ROOTLEVEL)
796 snprintf(name, sizeof(name), "%s", p->fts_name);
798 snprintf(name, sizeof(name), "%s/%s",
799 p->fts_parent->fts_accpath, p->fts_name);
801 if (previous_dev != p->fts_statp->st_dev) {
802 previous_dev = p->fts_statp->st_dev;
805 ret = lpathconf(name, _PC_ACL_NFS4);
807 type = ACL_TYPE_NFS4;
809 } else if (ret < 0 && errno != EINVAL) {
813 if (supports_acls == 0) {
814 ret = lpathconf(name, _PC_ACL_EXTENDED);
816 type = ACL_TYPE_ACCESS;
818 } else if (ret < 0 && errno != EINVAL) {
824 if (supports_acls == 0)
826 facl = acl_get_link_np(name, type);
831 if (acl_is_trivial_np(facl, &trivial)) {