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_statustime)
194 printtime(sp->st_ctime);
196 printtime(sp->st_mtime);
199 color_printed = colortype(sp->st_mode);
201 (void)printname(p->fts_name);
203 if (f_color && color_printed)
207 (void)printtype(sp->st_mode);
208 if (S_ISLNK(sp->st_mode))
215 printstream(const DISPLAY *dp)
220 for (p = dp->list, chcnt = 0; p; p = p->fts_link) {
221 if (p->fts_number == NO_PRINT)
223 /* XXX strlen does not take octal escapes into account. */
224 if (strlen(p->fts_name) + chcnt +
225 (p->fts_link ? 2 : 0) >= (unsigned)termwidth) {
229 chcnt += printaname(p, dp->s_inode, dp->s_block);
240 printcol(const DISPLAY *dp)
242 static FTSENT **array;
243 static int lastentries = -1;
264 * Have to do random access in the linked list -- build a table
267 if (dp->entries > lastentries) {
269 realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
274 lastentries = dp->entries;
277 for (p = dp->list, num = 0; p; p = p->fts_link)
278 if (p->fts_number != NO_PRINT)
281 colwidth = dp->maxlen;
283 colwidth += dp->s_inode + 1;
285 colwidth += dp->s_block + 1;
289 colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
290 if (termwidth < 2 * colwidth) {
294 numcols = termwidth / colwidth;
295 numrows = num / numcols;
299 if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
300 (f_longform || f_size)) {
301 (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
305 for (row = 0; row < numrows; ++row) {
309 for (col = 0, chcnt = 0; col < numcols; ++col) {
310 chcnt += printaname(array[base], dp->s_inode,
318 while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
320 if (f_sortacross && col + 1 >= numcols)
322 (void)putchar(f_notabs ? ' ' : '\t');
332 * print [inode] [size] name
333 * return # of characters printed, no trailing characters.
336 printaname(const FTSENT *p, u_long inodefield, u_long sizefield)
341 int color_printed = 0;
347 chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino);
349 chcnt += printf("%*jd ",
350 (int)sizefield, howmany(sp->st_blocks, blocksize));
353 color_printed = colortype(sp->st_mode);
355 chcnt += printname(p->fts_name);
357 if (f_color && color_printed)
361 chcnt += printtype(sp->st_mode);
366 printtime(time_t ftime)
369 static time_t now = 0;
371 static int d_first = -1;
374 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
378 #define SIXMONTHS ((365 / 2) * 86400)
380 /* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */
381 format = d_first ? "%e %b %T %Y " : "%b %e %T %Y ";
382 else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
383 /* mmm dd hh:mm || dd mmm hh:mm */
384 format = d_first ? "%e %b %R " : "%b %e %R ";
386 /* mmm dd yyyy || dd mmm yyyy */
387 format = d_first ? "%e %b %Y " : "%b %e %Y ";
388 strftime(longstring, sizeof(longstring), format, localtime(&ftime));
389 fputs(longstring, stdout);
393 printtype(u_int mode)
397 if ((mode & S_IFMT) == S_IFDIR) {
404 switch (mode & S_IFMT) {
423 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
443 (void)write(STDOUT_FILENO, &tmp, 1);
453 tputs(enter_bold, 1, putch);
455 if (colors[c].num[0] != -1) {
456 ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]);
458 tputs(ansiseq, 1, putch);
460 if (colors[c].num[1] != -1) {
461 ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]);
463 tputs(ansiseq, 1, putch);
470 tputs(ansi_coloff, 1, sig ? writech : putch);
471 tputs(attrs_off, 1, sig ? writech : putch);
475 colortype(mode_t mode)
477 switch (mode & S_IFMT) {
504 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
507 else if (mode & S_ISGID)
517 parsecolors(const char *cs)
523 short legacy_warn = 0;
526 cs = ""; /* LSCOLORS not set */
528 for (i = 0; i < (int)C_NUMCOLORS; i++) {
531 if (len <= 2 * (size_t)i) {
532 c[0] = defcolors[2 * i];
533 c[1] = defcolors[2 * i + 1];
536 c[1] = cs[2 * i + 1];
538 for (j = 0; j < 2; j++) {
539 /* Legacy colours used 0-7 */
540 if (c[j] >= '0' && c[j] <= '7') {
541 colors[i].num[j] = c[j] - '0';
543 warnx("LSCOLORS should use "
544 "characters a-h instead of 0-9 ("
545 "see the manual page)");
548 } else if (c[j] >= 'a' && c[j] <= 'h')
549 colors[i].num[j] = c[j] - 'a';
550 else if (c[j] >= 'A' && c[j] <= 'H') {
551 colors[i].num[j] = c[j] - 'A';
553 } else if (tolower((unsigned char)c[j]) == 'x')
554 colors[i].num[j] = -1;
556 warnx("invalid character '%c' in LSCOLORS"
558 colors[i].num[j] = -1;
569 (void)signal(sig, SIG_DFL);
570 (void)kill(getpid(), sig);
576 printlink(const FTSENT *p)
579 char name[MAXPATHLEN + 1];
580 char path[MAXPATHLEN + 1];
582 if (p->fts_level == FTS_ROOTLEVEL)
583 (void)snprintf(name, sizeof(name), "%s", p->fts_name);
585 (void)snprintf(name, sizeof(name),
586 "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
587 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
588 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
592 (void)printf(" -> ");
593 (void)printname(path);
597 printsize(size_t width, off_t bytes)
603 humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
604 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
605 (void)printf("%5s ", buf);
607 (void)printf("%*jd ", (u_int)width, bytes);
611 aclmode(char *buf, const FTSENT *p, int *haveacls)
613 char name[MAXPATHLEN + 1];
619 * Add a + after the standard rwxrwxrwx mode if the file has an
620 * extended ACL. strmode() reserves space at the end of the string.
622 if (p->fts_level == FTS_ROOTLEVEL)
623 snprintf(name, sizeof(name), "%s", p->fts_name);
625 snprintf(name, sizeof(name), "%s/%s",
626 p->fts_parent->fts_accpath, p->fts_name);
628 * We have no way to tell whether a symbolic link has an ACL since
629 * pathconf() and acl_get_file() both follow them.
631 if (S_ISLNK(p->fts_statp->st_mode)) {
635 if ((ret = pathconf(name, _PC_ACL_EXTENDED)) <= 0) {
636 if (ret < 0 && errno != EINVAL)
643 if ((facl = acl_get_file(name, ACL_TYPE_ACCESS)) != NULL) {
644 if (acl_get_entry(facl, ACL_FIRST_ENTRY, &ae) == 1) {
646 while (acl_get_entry(facl, ACL_NEXT_ENTRY, &ae) == 1)
650 * POSIX.1e requires that ACLs of type ACL_TYPE_ACCESS
651 * must have at least three entries (owner, group,
652 * and other). So anything with more than 3 ACLs looks