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>
64 static int printaname(const FTSENT *, u_long, u_long);
65 static void printdev(size_t, dev_t);
66 static void printlink(const FTSENT *);
67 static void printtime(time_t);
68 static int printtype(u_int);
69 static void printsize(size_t, off_t);
71 static void endcolor(int);
72 static int colortype(mode_t);
74 static void aclmode(char *, const FTSENT *);
76 #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
79 /* Most of these are taken from <sys/stat.h> */
81 C_DIR, /* directory */
82 C_LNK, /* symbolic link */
85 C_EXEC, /* executable */
86 C_BLK, /* block special */
87 C_CHR, /* character special */
88 C_SUID, /* setuid executable */
89 C_SGID, /* setgid executable */
90 C_WSDIR, /* directory writeble to others, with sticky
92 C_WDIR, /* directory writeble to others, without
94 C_NUMCOLORS /* just a place-holder */
97 static const char *defcolors = "exfxcxdxbxegedabagacad";
99 /* colors for file types */
103 } colors[C_NUMCOLORS];
107 printscol(const DISPLAY *dp)
111 for (p = dp->list; p; p = p->fts_link) {
114 (void)printaname(p, dp->s_inode, dp->s_block);
120 * print name in current style
123 printname(const char *name)
125 if (f_octal || f_octal_escape)
126 return prn_octal(name);
128 return prn_printable(name);
130 return prn_normal(name);
134 printlong(const DISPLAY *dp)
141 int color_printed = 0;
144 if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
145 (f_longform || f_size)) {
146 (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
149 for (p = dp->list; p; p = p->fts_link) {
154 (void)printf("%*lu ", dp->s_inode, (u_long)sp->st_ino);
156 (void)printf("%*jd ",
157 dp->s_block, howmany(sp->st_blocks, blocksize));
158 strmode(sp->st_mode, buf);
161 (void)printf("%s %*u %-*s %-*s ", buf, dp->s_nlink,
162 sp->st_nlink, dp->s_user, np->user, dp->s_group,
165 (void)printf("%-*s ", dp->s_flags, np->flags);
167 (void)printf("%-*s ", dp->s_label, np->label);
168 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
169 printdev(dp->s_size, sp->st_rdev);
171 printsize(dp->s_size, sp->st_size);
173 printtime(sp->st_atime);
174 else if (f_birthtime)
175 printtime(sp->st_birthtime);
176 else if (f_statustime)
177 printtime(sp->st_ctime);
179 printtime(sp->st_mtime);
182 color_printed = colortype(sp->st_mode);
184 (void)printname(p->fts_name);
186 if (f_color && color_printed)
190 (void)printtype(sp->st_mode);
191 if (S_ISLNK(sp->st_mode))
198 printstream(const DISPLAY *dp)
203 for (p = dp->list, chcnt = 0; p; p = p->fts_link) {
204 if (p->fts_number == NO_PRINT)
206 /* XXX strlen does not take octal escapes into account. */
207 if (strlen(p->fts_name) + chcnt +
208 (p->fts_link ? 2 : 0) >= (unsigned)termwidth) {
212 chcnt += printaname(p, dp->s_inode, dp->s_block);
223 printcol(const DISPLAY *dp)
225 static FTSENT **array;
226 static int lastentries = -1;
247 * Have to do random access in the linked list -- build a table
250 if (dp->entries > lastentries) {
252 realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
257 lastentries = dp->entries;
260 for (p = dp->list, num = 0; p; p = p->fts_link)
261 if (p->fts_number != NO_PRINT)
264 colwidth = dp->maxlen;
266 colwidth += dp->s_inode + 1;
268 colwidth += dp->s_block + 1;
272 colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
273 if (termwidth < 2 * colwidth) {
277 numcols = termwidth / colwidth;
278 numrows = num / numcols;
282 if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
283 (f_longform || f_size)) {
284 (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
288 for (row = 0; row < numrows; ++row) {
292 for (col = 0, chcnt = 0; col < numcols; ++col) {
293 chcnt += printaname(array[base], dp->s_inode,
301 while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
303 if (f_sortacross && col + 1 >= numcols)
305 (void)putchar(f_notabs ? ' ' : '\t');
315 * print [inode] [size] name
316 * return # of characters printed, no trailing characters.
319 printaname(const FTSENT *p, u_long inodefield, u_long sizefield)
324 int color_printed = 0;
330 chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino);
332 chcnt += printf("%*jd ",
333 (int)sizefield, howmany(sp->st_blocks, blocksize));
336 color_printed = colortype(sp->st_mode);
338 chcnt += printname(p->fts_name);
340 if (f_color && color_printed)
344 chcnt += printtype(sp->st_mode);
349 * Print device special file major and minor numbers.
352 printdev(size_t width, dev_t dev)
354 char buf[DEVSTR_HEX_LEN + 1];
356 if (minor(dev) > 255 || minor(dev) < 0)
357 (void)snprintf(buf, sizeof(buf), "%3d, 0x%08x",
358 major(dev), (u_int)minor(dev));
360 (void)snprintf(buf, sizeof(buf), "%3d, %3d",
361 major(dev), minor(dev));
363 (void)printf("%*s ", (u_int)width, buf);
367 printtime(time_t ftime)
370 static time_t now = 0;
372 static int d_first = -1;
375 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
379 #define SIXMONTHS ((365 / 2) * 86400)
380 if (f_timeformat) /* user specified format */
381 format = f_timeformat;
383 /* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */
384 format = d_first ? "%e %b %T %Y" : "%b %e %T %Y";
385 else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
386 /* mmm dd hh:mm || dd mmm hh:mm */
387 format = d_first ? "%e %b %R" : "%b %e %R";
389 /* mmm dd yyyy || dd mmm yyyy */
390 format = d_first ? "%e %b %Y" : "%b %e %Y";
391 strftime(longstring, sizeof(longstring), format, localtime(&ftime));
392 fputs(longstring, stdout);
397 printtype(u_int mode)
401 if ((mode & S_IFMT) == S_IFDIR) {
408 switch (mode & S_IFMT) {
427 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
447 (void)write(STDOUT_FILENO, &tmp, 1);
457 tputs(enter_bold, 1, putch);
459 if (colors[c].num[0] != -1) {
460 ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]);
462 tputs(ansiseq, 1, putch);
464 if (colors[c].num[1] != -1) {
465 ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]);
467 tputs(ansiseq, 1, putch);
474 tputs(ansi_coloff, 1, sig ? writech : putch);
475 tputs(attrs_off, 1, sig ? writech : putch);
479 colortype(mode_t mode)
481 switch (mode & S_IFMT) {
508 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
511 else if (mode & S_ISGID)
521 parsecolors(const char *cs)
527 short legacy_warn = 0;
530 cs = ""; /* LSCOLORS not set */
532 for (i = 0; i < (int)C_NUMCOLORS; i++) {
535 if (len <= 2 * (size_t)i) {
536 c[0] = defcolors[2 * i];
537 c[1] = defcolors[2 * i + 1];
540 c[1] = cs[2 * i + 1];
542 for (j = 0; j < 2; j++) {
543 /* Legacy colours used 0-7 */
544 if (c[j] >= '0' && c[j] <= '7') {
545 colors[i].num[j] = c[j] - '0';
547 warnx("LSCOLORS should use "
548 "characters a-h instead of 0-9 ("
549 "see the manual page)");
552 } else if (c[j] >= 'a' && c[j] <= 'h')
553 colors[i].num[j] = c[j] - 'a';
554 else if (c[j] >= 'A' && c[j] <= 'H') {
555 colors[i].num[j] = c[j] - 'A';
557 } else if (tolower((unsigned char)c[j]) == 'x')
558 colors[i].num[j] = -1;
560 warnx("invalid character '%c' in LSCOLORS"
562 colors[i].num[j] = -1;
573 (void)signal(sig, SIG_DFL);
574 (void)kill(getpid(), sig);
580 printlink(const FTSENT *p)
583 char name[MAXPATHLEN + 1];
584 char path[MAXPATHLEN + 1];
586 if (p->fts_level == FTS_ROOTLEVEL)
587 (void)snprintf(name, sizeof(name), "%s", p->fts_name);
589 (void)snprintf(name, sizeof(name),
590 "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
591 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
592 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
596 (void)printf(" -> ");
597 (void)printname(path);
601 printsize(size_t width, off_t bytes)
606 * Reserve one space before the size and allocate room for
609 char buf[HUMANVALSTR_LEN - 1 + 1];
611 humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
612 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
613 (void)printf("%*s ", (u_int)width, buf);
615 (void)printf("%*jd ", (u_int)width, bytes);
619 * Add a + after the standard rwxrwxrwx mode if the file has an
620 * ACL. strmode() reserves space at the end of the string.
623 aclmode(char *buf, const FTSENT *p)
625 char name[MAXPATHLEN + 1];
627 static dev_t previous_dev = NODEV;
628 static int supports_acls = -1;
629 static int type = ACL_TYPE_ACCESS;
633 * XXX: ACLs are not supported on whiteouts and device files
636 if (S_ISCHR(p->fts_statp->st_mode) || S_ISBLK(p->fts_statp->st_mode) ||
637 S_ISWHT(p->fts_statp->st_mode))
640 if (previous_dev == p->fts_statp->st_dev && supports_acls == 0)
643 if (p->fts_level == FTS_ROOTLEVEL)
644 snprintf(name, sizeof(name), "%s", p->fts_name);
646 snprintf(name, sizeof(name), "%s/%s",
647 p->fts_parent->fts_accpath, p->fts_name);
649 if (previous_dev != p->fts_statp->st_dev) {
650 previous_dev = p->fts_statp->st_dev;
653 ret = lpathconf(name, _PC_ACL_NFS4);
655 type = ACL_TYPE_NFS4;
657 } else if (ret < 0 && errno != EINVAL) {
661 if (supports_acls == 0) {
662 ret = lpathconf(name, _PC_ACL_EXTENDED);
664 type = ACL_TYPE_ACCESS;
666 } else if (ret < 0 && errno != EINVAL) {
672 if (supports_acls == 0)
674 facl = acl_get_link_np(name, type);
679 if (acl_is_trivial_np(facl, &trivial)) {