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)
381 if (f_timeformat) /* user specified format */
382 format = f_timeformat;
384 /* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */
385 format = d_first ? "%e %b %T %Y" : "%b %e %T %Y";
386 else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
387 /* mmm dd hh:mm || dd mmm hh:mm */
388 format = d_first ? "%e %b %R" : "%b %e %R";
390 /* mmm dd yyyy || dd mmm yyyy */
391 format = d_first ? "%e %b %Y" : "%b %e %Y";
392 strftime(longstring, sizeof(longstring), format, localtime(&ftime));
393 fputs(longstring, stdout);
398 printtype(u_int mode)
402 if ((mode & S_IFMT) == S_IFDIR) {
409 switch (mode & S_IFMT) {
428 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
448 (void)write(STDOUT_FILENO, &tmp, 1);
458 tputs(enter_bold, 1, putch);
460 if (colors[c].num[0] != -1) {
461 ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]);
463 tputs(ansiseq, 1, putch);
465 if (colors[c].num[1] != -1) {
466 ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]);
468 tputs(ansiseq, 1, putch);
475 tputs(ansi_coloff, 1, sig ? writech : putch);
476 tputs(attrs_off, 1, sig ? writech : putch);
480 colortype(mode_t mode)
482 switch (mode & S_IFMT) {
509 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
512 else if (mode & S_ISGID)
522 parsecolors(const char *cs)
528 short legacy_warn = 0;
531 cs = ""; /* LSCOLORS not set */
533 for (i = 0; i < (int)C_NUMCOLORS; i++) {
536 if (len <= 2 * (size_t)i) {
537 c[0] = defcolors[2 * i];
538 c[1] = defcolors[2 * i + 1];
541 c[1] = cs[2 * i + 1];
543 for (j = 0; j < 2; j++) {
544 /* Legacy colours used 0-7 */
545 if (c[j] >= '0' && c[j] <= '7') {
546 colors[i].num[j] = c[j] - '0';
548 warnx("LSCOLORS should use "
549 "characters a-h instead of 0-9 ("
550 "see the manual page)");
553 } else if (c[j] >= 'a' && c[j] <= 'h')
554 colors[i].num[j] = c[j] - 'a';
555 else if (c[j] >= 'A' && c[j] <= 'H') {
556 colors[i].num[j] = c[j] - 'A';
558 } else if (tolower((unsigned char)c[j]) == 'x')
559 colors[i].num[j] = -1;
561 warnx("invalid character '%c' in LSCOLORS"
563 colors[i].num[j] = -1;
574 (void)signal(sig, SIG_DFL);
575 (void)kill(getpid(), sig);
581 printlink(const FTSENT *p)
584 char name[MAXPATHLEN + 1];
585 char path[MAXPATHLEN + 1];
587 if (p->fts_level == FTS_ROOTLEVEL)
588 (void)snprintf(name, sizeof(name), "%s", p->fts_name);
590 (void)snprintf(name, sizeof(name),
591 "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
592 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
593 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
597 (void)printf(" -> ");
598 (void)printname(path);
602 printsize(size_t width, off_t bytes)
608 humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
609 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
610 (void)printf("%5s ", buf);
612 (void)printf("%*jd ", (u_int)width, bytes);
616 aclmode(char *buf, const FTSENT *p, int *haveacls)
618 char name[MAXPATHLEN + 1];
624 * Add a + after the standard rwxrwxrwx mode if the file has an
625 * extended ACL. strmode() reserves space at the end of the string.
627 if (p->fts_level == FTS_ROOTLEVEL)
628 snprintf(name, sizeof(name), "%s", p->fts_name);
630 snprintf(name, sizeof(name), "%s/%s",
631 p->fts_parent->fts_accpath, p->fts_name);
633 * We have no way to tell whether a symbolic link has an ACL since
634 * pathconf() and acl_get_file() both follow them. They also don't
637 if (S_ISLNK(p->fts_statp->st_mode) || S_ISWHT(p->fts_statp->st_mode)) {
641 if ((ret = pathconf(name, _PC_ACL_EXTENDED)) <= 0) {
642 if (ret < 0 && errno != EINVAL)
649 if ((facl = acl_get_file(name, ACL_TYPE_ACCESS)) != NULL) {
650 if (acl_get_entry(facl, ACL_FIRST_ENTRY, &ae) == 1) {
652 while (acl_get_entry(facl, ACL_NEXT_ENTRY, &ae) == 1)
656 * POSIX.1e requires that ACLs of type ACL_TYPE_ACCESS
657 * must have at least three entries (owner, group,
658 * and other). So anything with more than 3 ACLs looks