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("%*lu ", dp->s_inode, (u_long)sp->st_ino);
157 (void)printf("%*jd ",
158 dp->s_block, howmany(sp->st_blocks, blocksize));
159 strmode(sp->st_mode, buf);
162 (void)printf("%s %*u %-*s %-*s ", buf, dp->s_nlink,
163 sp->st_nlink, dp->s_user, np->user, dp->s_group,
166 (void)printf("%-*s ", dp->s_flags, np->flags);
168 (void)printf("%-*s ", dp->s_label, np->label);
169 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
170 printdev(dp->s_size, sp->st_rdev);
172 printsize(dp->s_size, sp->st_size);
174 printtime(sp->st_atime);
175 else if (f_birthtime)
176 printtime(sp->st_birthtime);
177 else if (f_statustime)
178 printtime(sp->st_ctime);
180 printtime(sp->st_mtime);
183 color_printed = colortype(sp->st_mode);
185 (void)printname(p->fts_name);
187 if (f_color && color_printed)
191 (void)printtype(sp->st_mode);
192 if (S_ISLNK(sp->st_mode))
199 printstream(const DISPLAY *dp)
204 for (p = dp->list, chcnt = 0; p; p = p->fts_link) {
205 if (p->fts_number == NO_PRINT)
207 /* XXX strlen does not take octal escapes into account. */
208 if (strlen(p->fts_name) + chcnt +
209 (p->fts_link ? 2 : 0) >= (unsigned)termwidth) {
213 chcnt += printaname(p, dp->s_inode, dp->s_block);
224 printcol(const DISPLAY *dp)
226 static FTSENT **array;
227 static int lastentries = -1;
248 * Have to do random access in the linked list -- build a table
251 if (dp->entries > lastentries) {
253 realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
258 lastentries = dp->entries;
261 for (p = dp->list, num = 0; p; p = p->fts_link)
262 if (p->fts_number != NO_PRINT)
265 colwidth = dp->maxlen;
267 colwidth += dp->s_inode + 1;
269 colwidth += dp->s_block + 1;
273 colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
274 if (termwidth < 2 * colwidth) {
278 numcols = termwidth / colwidth;
279 numrows = num / numcols;
283 if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
284 (f_longform || f_size)) {
285 (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
289 for (row = 0; row < numrows; ++row) {
293 for (col = 0, chcnt = 0; col < numcols; ++col) {
294 chcnt += printaname(array[base], dp->s_inode,
302 while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
304 if (f_sortacross && col + 1 >= numcols)
306 (void)putchar(f_notabs ? ' ' : '\t');
316 * print [inode] [size] name
317 * return # of characters printed, no trailing characters.
320 printaname(const FTSENT *p, u_long inodefield, u_long sizefield)
325 int color_printed = 0;
331 chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino);
333 chcnt += printf("%*jd ",
334 (int)sizefield, howmany(sp->st_blocks, blocksize));
337 color_printed = colortype(sp->st_mode);
339 chcnt += printname(p->fts_name);
341 if (f_color && color_printed)
345 chcnt += printtype(sp->st_mode);
350 * Print device special file major and minor numbers.
353 printdev(size_t width, dev_t dev)
356 (void)printf("%#*jx ", (u_int)width, (uintmax_t)dev);
360 printtime(time_t ftime)
363 static time_t now = 0;
365 static int d_first = -1;
368 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
372 #define SIXMONTHS ((365 / 2) * 86400)
373 if (f_timeformat) /* user specified format */
374 format = f_timeformat;
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";
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);
390 printtype(u_int mode)
394 if ((mode & S_IFMT) == S_IFDIR) {
401 switch (mode & S_IFMT) {
420 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
440 (void)write(STDOUT_FILENO, &tmp, 1);
450 tputs(enter_bold, 1, putch);
452 if (colors[c].num[0] != -1) {
453 ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]);
455 tputs(ansiseq, 1, putch);
457 if (colors[c].num[1] != -1) {
458 ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]);
460 tputs(ansiseq, 1, putch);
467 tputs(ansi_coloff, 1, sig ? writech : putch);
468 tputs(attrs_off, 1, sig ? writech : putch);
472 colortype(mode_t mode)
474 switch (mode & S_IFMT) {
501 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
504 else if (mode & S_ISGID)
514 parsecolors(const char *cs)
520 short legacy_warn = 0;
523 cs = ""; /* LSCOLORS not set */
525 for (i = 0; i < (int)C_NUMCOLORS; i++) {
528 if (len <= 2 * (size_t)i) {
529 c[0] = defcolors[2 * i];
530 c[1] = defcolors[2 * i + 1];
533 c[1] = cs[2 * i + 1];
535 for (j = 0; j < 2; j++) {
536 /* Legacy colours used 0-7 */
537 if (c[j] >= '0' && c[j] <= '7') {
538 colors[i].num[j] = c[j] - '0';
540 warnx("LSCOLORS should use "
541 "characters a-h instead of 0-9 ("
542 "see the manual page)");
545 } else if (c[j] >= 'a' && c[j] <= 'h')
546 colors[i].num[j] = c[j] - 'a';
547 else if (c[j] >= 'A' && c[j] <= 'H') {
548 colors[i].num[j] = c[j] - 'A';
550 } else if (tolower((unsigned char)c[j]) == 'x')
551 colors[i].num[j] = -1;
553 warnx("invalid character '%c' in LSCOLORS"
555 colors[i].num[j] = -1;
566 (void)signal(sig, SIG_DFL);
567 (void)kill(getpid(), sig);
573 printlink(const FTSENT *p)
576 char name[MAXPATHLEN + 1];
577 char path[MAXPATHLEN + 1];
579 if (p->fts_level == FTS_ROOTLEVEL)
580 (void)snprintf(name, sizeof(name), "%s", p->fts_name);
582 (void)snprintf(name, sizeof(name),
583 "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
584 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
585 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
589 (void)printf(" -> ");
590 (void)printname(path);
594 printsize(size_t width, off_t bytes)
599 * Reserve one space before the size and allocate room for
602 char buf[HUMANVALSTR_LEN - 1 + 1];
604 humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
605 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
606 (void)printf("%*s ", (u_int)width, buf);
608 (void)printf("%*jd ", (u_int)width, bytes);
612 * Add a + after the standard rwxrwxrwx mode if the file has an
613 * ACL. strmode() reserves space at the end of the string.
616 aclmode(char *buf, const FTSENT *p)
618 char name[MAXPATHLEN + 1];
620 static dev_t previous_dev = NODEV;
621 static int supports_acls = -1;
622 static int type = ACL_TYPE_ACCESS;
626 * XXX: ACLs are not supported on whiteouts and device files
629 if (S_ISCHR(p->fts_statp->st_mode) || S_ISBLK(p->fts_statp->st_mode) ||
630 S_ISWHT(p->fts_statp->st_mode))
633 if (previous_dev == p->fts_statp->st_dev && supports_acls == 0)
636 if (p->fts_level == FTS_ROOTLEVEL)
637 snprintf(name, sizeof(name), "%s", p->fts_name);
639 snprintf(name, sizeof(name), "%s/%s",
640 p->fts_parent->fts_accpath, p->fts_name);
642 if (previous_dev != p->fts_statp->st_dev) {
643 previous_dev = p->fts_statp->st_dev;
646 ret = lpathconf(name, _PC_ACL_NFS4);
648 type = ACL_TYPE_NFS4;
650 } else if (ret < 0 && errno != EINVAL) {
654 if (supports_acls == 0) {
655 ret = lpathconf(name, _PC_ACL_EXTENDED);
657 type = ACL_TYPE_ACCESS;
659 } else if (ret < 0 && errno != EINVAL) {
665 if (supports_acls == 0)
667 facl = acl_get_link_np(name, type);
672 if (acl_is_trivial_np(facl, &trivial)) {