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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)print.c 8.4 (Berkeley) 4/17/94
37 * $FreeBSD: src/bin/ls/print.c,v 1.19.2.7 2002/11/17 10:27:34 tjr Exp $
38 * $DragonFly: src/bin/ls/print.c,v 1.5 2004/07/22 11:39:02 asmodai Exp $
41 #include <sys/param.h>
63 static int printaname(FTSENT *, u_long, u_long);
64 static void printlink(FTSENT *);
65 static void printtime(time_t);
66 static int printtype(u_int);
67 static void printsize(size_t, off_t);
69 static void endcolor(int);
70 static int colortype(mode_t);
73 #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
75 #define KILO_SZ(n) (n)
76 #define MEGA_SZ(n) ((n) * (n))
77 #define GIGA_SZ(n) ((n) * (n) * (n))
78 #define TERA_SZ(n) ((n) * (n) * (n) * (n))
79 #define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n))
81 #define KILO_2_SZ (KILO_SZ(1024ULL))
82 #define MEGA_2_SZ (MEGA_SZ(1024ULL))
83 #define GIGA_2_SZ (GIGA_SZ(1024ULL))
84 #define TERA_2_SZ (TERA_SZ(1024ULL))
85 #define PETA_2_SZ (PETA_SZ(1024ULL))
87 static unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TERA_2_SZ, PETA_2_SZ};
90 NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX
92 static unit_t unit_adjust(off_t *);
94 static int unitp[] = {NONE, KILO, MEGA, GIGA, TERA, PETA};
97 /* Most of these are taken from <sys/stat.h> */
99 C_DIR, /* directory */
100 C_LNK, /* symbolic link */
103 C_EXEC, /* executable */
104 C_BLK, /* block special */
105 C_CHR, /* character special */
106 C_SUID, /* setuid executable */
107 C_SGID, /* setgid executable */
108 C_WSDIR, /* directory writeble to others, with sticky
110 C_WDIR, /* directory writeble to others, without
112 C_NUMCOLORS /* just a place-holder */
115 static const char *defcolors = "exfxcxdxbxegedabagacad";
117 /* colors for file types */
121 } colors[C_NUMCOLORS];
125 printscol(DISPLAY *dp)
129 for (p = dp->list; p; p = p->fts_link) {
132 (void)printaname(p, dp->s_inode, dp->s_block);
138 * print name in current style
141 printname(const char *name)
143 if (f_octal || f_octal_escape)
144 return prn_octal(name);
146 return prn_printable(name);
148 return printf("%s", name);
152 printlong(DISPLAY *dp)
159 int color_printed = 0;
162 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
163 (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
165 for (p = dp->list; p; p = p->fts_link) {
170 (void)printf("%*lu ", dp->s_inode, (u_long)sp->st_ino);
172 (void)printf("%*lld ",
173 dp->s_block, howmany(sp->st_blocks, blocksize));
174 strmode(sp->st_mode, buf);
176 (void)printf("%s %*u %-*s %-*s ", buf, dp->s_nlink,
177 sp->st_nlink, dp->s_user, np->user, dp->s_group,
180 (void)printf("%-*s ", dp->s_flags, np->flags);
181 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
182 if (minor(sp->st_rdev) > 255 || minor(sp->st_rdev) < 0)
183 (void)printf("%3d, 0x%08x ",
185 (u_int)minor(sp->st_rdev));
187 (void)printf("%3d, %3d ",
188 major(sp->st_rdev), minor(sp->st_rdev));
190 (void)printf("%*s%*lld ",
191 8 - dp->s_size, "", dp->s_size, sp->st_size);
193 printsize(dp->s_size, sp->st_size);
195 printtime(sp->st_atime);
196 else if (f_statustime)
197 printtime(sp->st_ctime);
199 printtime(sp->st_mtime);
202 color_printed = colortype(sp->st_mode);
204 (void)printname(p->fts_name);
206 if (f_color && color_printed)
210 (void)printtype(sp->st_mode);
211 if (S_ISLNK(sp->st_mode))
218 printstream(DISPLAY *dp)
221 extern int termwidth;
224 for (p = dp->list, chcnt = 0; p; p = p->fts_link) {
225 if (p->fts_number == NO_PRINT)
227 if (strlen(p->fts_name) + chcnt +
228 (p->fts_link ? 2 : 0) >= (unsigned)termwidth) {
232 chcnt += printaname(p, dp->s_inode, dp->s_block);
243 printcol(DISPLAY *dp)
245 extern int termwidth;
246 static FTSENT **array;
247 static int lastentries = -1;
268 * Have to do random access in the linked list -- build a table
271 if (dp->entries > lastentries) {
272 lastentries = dp->entries;
274 realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
279 lastentries = dp->entries;
282 for (p = dp->list, num = 0; p; p = p->fts_link)
283 if (p->fts_number != NO_PRINT)
286 colwidth = dp->maxlen;
288 colwidth += dp->s_inode + 1;
290 colwidth += dp->s_block + 1;
294 colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
295 if (termwidth < 2 * colwidth) {
299 numcols = termwidth / colwidth;
300 numrows = num / numcols;
304 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
305 (void)printf("total %lu\n", howmany(dp->btotal, blocksize));
307 /* counter if f_sortacross, else case-by-case */
310 for (row = 0; row < numrows; ++row) {
314 for (col = 0, chcnt = 0; col < numcols; ++col) {
315 chcnt += printaname(array[base], dp->s_inode,
323 while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
325 if (f_sortacross && col + 1 >= numcols)
327 (void)putchar(f_notabs ? ' ' : '\t');
337 * print [inode] [size] name
338 * return # of characters printed, no trailing characters.
341 printaname(FTSENT *p, u_long inodefield, u_long sizefield)
346 int color_printed = 0;
352 chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino);
354 chcnt += printf("%*lld ",
355 (int)sizefield, howmany(sp->st_blocks, blocksize));
358 color_printed = colortype(sp->st_mode);
360 chcnt += printname(p->fts_name);
362 if (f_color && color_printed)
366 chcnt += printtype(sp->st_mode);
371 printtime(time_t ftime)
376 static int d_first = -1;
379 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
383 #define SIXMONTHS ((365 / 2) * 86400)
385 /* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */
386 format = d_first ? "%e %b %T %Y " : "%b %e %T %Y ";
387 else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
388 /* mmm dd hh:mm || dd mmm hh:mm */
389 format = d_first ? "%e %b %R " : "%b %e %R ";
391 /* mmm dd yyyy || dd mmm yyyy */
392 format = d_first ? "%e %b %Y " : "%b %e %Y ";
393 strftime(longstring, sizeof(longstring), format, localtime(&ftime));
394 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) {
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 < C_NUMCOLORS; 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';
548 "warn: LSCOLORS should use "
549 "characters a-h instead of 0-9 ("
550 "see the manual page)\n");
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;
562 "error: invalid character '%c' in LSCOLORS"
564 colors[i].num[j] = -1;
575 (void)signal(sig, SIG_DFL);
576 (void)kill(getpid(), sig);
585 char name[MAXPATHLEN + 1];
586 char path[MAXPATHLEN + 1];
588 if (p->fts_level == FTS_ROOTLEVEL)
589 (void)snprintf(name, sizeof(name), "%s", p->fts_name);
591 (void)snprintf(name, sizeof(name),
592 "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
593 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
594 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
598 (void)printf(" -> ");
599 (void)printname(path);
603 printsize(size_t width, off_t bytes)
608 unit = unit_adjust(&bytes);
611 (void)printf("%*s ", width, "0B");
613 (void)printf("%*lld%c ", width - 1, bytes,
616 (void)printf("%*lld ", width, bytes);
620 * Output in "human-readable" format. Uses 3 digits max and puts
621 * unit suffixes at the end. Makes output compact and easy to read,
622 * especially on huge disks.
626 unit_adjust(off_t *val)
630 unsigned int unit_sz;
632 abval = fabs((double)*val);
634 unit_sz = abval ? ilogb(abval) / 10 : 0;
636 if (unit_sz >= UNIT_MAX) {
639 unit = unitp[unit_sz];
640 *val /= (double)vals_base2[unit_sz];