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 printlink(const FTSENT *);
66 static void printtime(time_t);
67 static int printtype(u_int);
68 static void printsize(size_t, off_t);
70 static void endcolor(int);
71 static int colortype(mode_t);
73 static void aclmode(char *, const FTSENT *, int *);
75 #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
78 /* Most of these are taken from <sys/stat.h> */
80 C_DIR, /* directory */
81 C_LNK, /* symbolic link */
84 C_EXEC, /* executable */
85 C_BLK, /* block special */
86 C_CHR, /* character special */
87 C_SUID, /* setuid executable */
88 C_SGID, /* setgid executable */
89 C_WSDIR, /* directory writeble to others, with sticky
91 C_WDIR, /* directory writeble to others, without
93 C_NUMCOLORS /* just a place-holder */
96 static const char *defcolors = "exfxcxdxbxegedabagacad";
98 /* colors for file types */
102 } colors[C_NUMCOLORS];
106 printscol(const DISPLAY *dp)
110 for (p = dp->list; p; p = p->fts_link) {
113 (void)printaname(p, dp->s_inode, dp->s_block);
119 * print name in current style
122 printname(const char *name)
124 if (f_octal || f_octal_escape)
125 return prn_octal(name);
127 return prn_printable(name);
129 return prn_normal(name);
133 printlong(const DISPLAY *dp)
140 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));
152 for (p = dp->list; p; p = p->fts_link) {
157 (void)printf("%*lu ", dp->s_inode, (u_long)sp->st_ino);
159 (void)printf("%*jd ",
160 dp->s_block, howmany(sp->st_blocks, blocksize));
161 strmode(sp->st_mode, buf);
163 * Cache whether or not the filesystem supports ACL's to
164 * avoid expensive syscalls. Try again when we change devices.
166 if (haveacls || sp->st_dev != prevdev) {
167 aclmode(buf, p, &haveacls);
168 prevdev = sp->st_dev;
171 (void)printf("%s %*u %-*s %-*s ", buf, dp->s_nlink,
172 sp->st_nlink, dp->s_user, np->user, dp->s_group,
175 (void)printf("%-*s ", dp->s_flags, np->flags);
177 (void)printf("%-*s ", dp->s_label, np->label);
178 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
179 if (minor(sp->st_rdev) > 255 || minor(sp->st_rdev) < 0)
180 (void)printf("%3d, 0x%08x ",
182 (u_int)minor(sp->st_rdev));
184 (void)printf("%3d, %3d ",
185 major(sp->st_rdev), minor(sp->st_rdev));
187 (void)printf("%*s%*jd ",
188 8 - dp->s_size, "", dp->s_size, sp->st_size);
190 printsize(dp->s_size, sp->st_size);
192 printtime(sp->st_atime);
193 else if (f_birthtime)
194 printtime(sp->st_birthtime);
195 else if (f_statustime)
196 printtime(sp->st_ctime);
198 printtime(sp->st_mtime);
201 color_printed = colortype(sp->st_mode);
203 (void)printname(p->fts_name);
205 if (f_color && color_printed)
209 (void)printtype(sp->st_mode);
210 if (S_ISLNK(sp->st_mode))
217 printstream(const DISPLAY *dp)
222 for (p = dp->list, chcnt = 0; p; p = p->fts_link) {
223 if (p->fts_number == NO_PRINT)
225 /* XXX strlen does not take octal escapes into account. */
226 if (strlen(p->fts_name) + chcnt +
227 (p->fts_link ? 2 : 0) >= (unsigned)termwidth) {
231 chcnt += printaname(p, dp->s_inode, dp->s_block);
242 printcol(const DISPLAY *dp)
244 static FTSENT **array;
245 static int lastentries = -1;
266 * Have to do random access in the linked list -- build a table
269 if (dp->entries > lastentries) {
271 realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
276 lastentries = dp->entries;
279 for (p = dp->list, num = 0; p; p = p->fts_link)
280 if (p->fts_number != NO_PRINT)
283 colwidth = dp->maxlen;
285 colwidth += dp->s_inode + 1;
287 colwidth += dp->s_block + 1;
291 colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
292 if (termwidth < 2 * colwidth) {
296 numcols = termwidth / colwidth;
297 numrows = num / numcols;
301 if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
302 (f_longform || f_size)) {
303 (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
307 for (row = 0; row < numrows; ++row) {
311 for (col = 0, chcnt = 0; col < numcols; ++col) {
312 chcnt += printaname(array[base], dp->s_inode,
320 while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
322 if (f_sortacross && col + 1 >= numcols)
324 (void)putchar(f_notabs ? ' ' : '\t');
334 * print [inode] [size] name
335 * return # of characters printed, no trailing characters.
338 printaname(const FTSENT *p, u_long inodefield, u_long sizefield)
343 int color_printed = 0;
349 chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino);
351 chcnt += printf("%*jd ",
352 (int)sizefield, howmany(sp->st_blocks, blocksize));
355 color_printed = colortype(sp->st_mode);
357 chcnt += printname(p->fts_name);
359 if (f_color && color_printed)
363 chcnt += printtype(sp->st_mode);
368 printtime(time_t ftime)
371 static time_t now = 0;
373 static int d_first = -1;
376 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
380 #define SIXMONTHS ((365 / 2) * 86400)
382 /* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */
383 format = d_first ? "%e %b %T %Y " : "%b %e %T %Y ";
384 else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
385 /* mmm dd hh:mm || dd mmm hh:mm */
386 format = d_first ? "%e %b %R " : "%b %e %R ";
388 /* mmm dd yyyy || dd mmm yyyy */
389 format = d_first ? "%e %b %Y " : "%b %e %Y ";
390 strftime(longstring, sizeof(longstring), format, localtime(&ftime));
391 fputs(longstring, stdout);
395 printtype(u_int mode)
399 if ((mode & S_IFMT) == S_IFDIR) {
406 switch (mode & S_IFMT) {
425 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
445 (void)write(STDOUT_FILENO, &tmp, 1);
455 tputs(enter_bold, 1, putch);
457 if (colors[c].num[0] != -1) {
458 ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]);
460 tputs(ansiseq, 1, putch);
462 if (colors[c].num[1] != -1) {
463 ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]);
465 tputs(ansiseq, 1, putch);
472 tputs(ansi_coloff, 1, sig ? writech : putch);
473 tputs(attrs_off, 1, sig ? writech : putch);
477 colortype(mode_t mode)
479 switch (mode & S_IFMT) {
506 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
509 else if (mode & S_ISGID)
519 parsecolors(const char *cs)
525 short legacy_warn = 0;
528 cs = ""; /* LSCOLORS not set */
530 for (i = 0; i < (int)C_NUMCOLORS; i++) {
533 if (len <= 2 * (size_t)i) {
534 c[0] = defcolors[2 * i];
535 c[1] = defcolors[2 * i + 1];
538 c[1] = cs[2 * i + 1];
540 for (j = 0; j < 2; j++) {
541 /* Legacy colours used 0-7 */
542 if (c[j] >= '0' && c[j] <= '7') {
543 colors[i].num[j] = c[j] - '0';
545 warnx("LSCOLORS should use "
546 "characters a-h instead of 0-9 ("
547 "see the manual page)");
550 } else if (c[j] >= 'a' && c[j] <= 'h')
551 colors[i].num[j] = c[j] - 'a';
552 else if (c[j] >= 'A' && c[j] <= 'H') {
553 colors[i].num[j] = c[j] - 'A';
555 } else if (tolower((unsigned char)c[j]) == 'x')
556 colors[i].num[j] = -1;
558 warnx("invalid character '%c' in LSCOLORS"
560 colors[i].num[j] = -1;
571 (void)signal(sig, SIG_DFL);
572 (void)kill(getpid(), sig);
578 printlink(const FTSENT *p)
581 char name[MAXPATHLEN + 1];
582 char path[MAXPATHLEN + 1];
584 if (p->fts_level == FTS_ROOTLEVEL)
585 (void)snprintf(name, sizeof(name), "%s", p->fts_name);
587 (void)snprintf(name, sizeof(name),
588 "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
589 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
590 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
594 (void)printf(" -> ");
595 (void)printname(path);
599 printsize(size_t width, off_t bytes)
605 humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
606 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
607 (void)printf("%5s ", buf);
609 (void)printf("%*jd ", (u_int)width, bytes);
613 aclmode(char *buf, const FTSENT *p, int *haveacls)
615 char name[MAXPATHLEN + 1];
621 * Add a + after the standard rwxrwxrwx mode if the file has an
622 * extended ACL. strmode() reserves space at the end of the string.
624 if (p->fts_level == FTS_ROOTLEVEL)
625 snprintf(name, sizeof(name), "%s", p->fts_name);
627 snprintf(name, sizeof(name), "%s/%s",
628 p->fts_parent->fts_accpath, p->fts_name);
630 * We have no way to tell whether a symbolic link has an ACL since
631 * pathconf() and acl_get_file() both follow them. They also don't
634 if (S_ISLNK(p->fts_statp->st_mode) || S_ISWHT(p->fts_statp->st_mode)) {
638 if ((ret = pathconf(name, _PC_ACL_EXTENDED)) <= 0) {
639 if (ret < 0 && errno != EINVAL)
646 if ((facl = acl_get_file(name, ACL_TYPE_ACCESS)) != NULL) {
647 if (acl_get_entry(facl, ACL_FIRST_ENTRY, &ae) == 1) {
649 while (acl_get_entry(facl, ACL_NEXT_ENTRY, &ae) == 1)
653 * POSIX.1e requires that ACLs of type ACL_TYPE_ACCESS
654 * must have at least three entries (owner, group,
655 * and other). So anything with more than 3 ACLs looks