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
32 * @(#)print.c 8.4 (Berkeley) 4/17/94
33 * $FreeBSD: src/bin/ls/print.c,v 1.73 2004/06/08 09:27:42 das Exp $
34 * $DragonFly: src/bin/ls/print.c,v 1.20 2008/06/24 21:55:48 y0netan1 Exp $
37 #include <sys/param.h>
59 static int printaname(const FTSENT *, u_long, u_long);
60 static void printlink(const FTSENT *);
61 static void printtime(time_t);
62 static int printtype(u_int);
63 static void printsize(size_t, off_t);
65 static void endcolor(int);
66 static int colortype(mode_t);
69 #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
72 /* Most of these are taken from <sys/stat.h> */
74 C_DIR, /* directory */
75 C_LNK, /* symbolic link */
78 C_EXEC, /* executable */
79 C_BLK, /* block special */
80 C_CHR, /* character special */
81 C_SUID, /* setuid executable */
82 C_SGID, /* setgid executable */
83 C_WSDIR, /* directory writeble to others, with sticky
85 C_WDIR, /* directory writeble to others, without
87 C_NUMCOLORS /* just a place-holder */
90 static const char *defcolors = "exfxcxdxbxegedabagacad";
92 /* colors for file types */
96 } colors[C_NUMCOLORS];
100 printscol(const DISPLAY *dp)
104 for (p = dp->list; p; p = p->fts_link) {
107 printaname(p, dp->s_inode, dp->s_block);
113 * print name in current style
116 printname(const char *name)
118 if (f_octal || f_octal_escape)
119 return prn_octal(name);
121 return prn_printable(name);
123 return prn_normal(name);
127 printlong(const DISPLAY *dp)
134 int color_printed = 0;
137 if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
138 (f_longform || f_size)) {
139 printf("total %lu\n", howmany(dp->btotal, blocksize));
142 for (p = dp->list; p; p = p->fts_link) {
147 printf("%*llu ", dp->s_inode, (unsigned long long)sp->st_ino);
150 dp->s_block, howmany(sp->st_blocks, blocksize));
151 strmode(sp->st_mode, buf);
153 printf("%s %*u %-*s %-*s ", buf, dp->s_nlink,
154 sp->st_nlink, dp->s_user, np->user, dp->s_group,
157 printf("%s ", np->fsmid);
159 printf("%-*s ", dp->s_flags, np->flags);
160 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
161 printf("%3d, 0x%08x ",
162 major(sp->st_rdev), (u_int)minor(sp->st_rdev));
165 8 - dp->s_size, "", dp->s_size, sp->st_size);
167 printsize(dp->s_size, sp->st_size);
169 printtime(sp->st_atime);
170 else if (f_statustime)
171 printtime(sp->st_ctime);
173 printtime(sp->st_mtime);
176 color_printed = colortype(sp->st_mode);
178 printname(p->fts_name);
180 if (f_color && color_printed)
184 printtype(sp->st_mode);
185 if (S_ISLNK(sp->st_mode))
192 printstream(const DISPLAY *dp)
197 for (p = dp->list, chcnt = 0; p; p = p->fts_link) {
198 if (p->fts_number == NO_PRINT)
200 /* XXX strlen does not take octal escapes into account. */
201 if (strlen(p->fts_name) + chcnt +
202 (p->fts_link ? 2 : 0) >= (unsigned)termwidth) {
206 chcnt += printaname(p, dp->s_inode, dp->s_block);
217 printcol(const DISPLAY *dp)
219 static FTSENT **array;
220 static int lastentries = -1;
241 * Have to do random access in the linked list -- build a table
244 if (dp->entries > lastentries) {
245 lastentries = dp->entries;
247 realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
252 lastentries = dp->entries;
255 for (p = dp->list, num = 0; p; p = p->fts_link)
256 if (p->fts_number != NO_PRINT)
259 colwidth = dp->maxlen;
261 colwidth += dp->s_inode + 1;
263 colwidth += dp->s_block + 1;
267 colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
268 if (termwidth < 2 * colwidth) {
272 numcols = termwidth / colwidth;
273 numrows = num / numcols;
277 if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
278 (f_longform || f_size)) {
279 printf("total %lu\n", howmany(dp->btotal, blocksize));
282 /* counter if f_sortacross, else case-by-case */
285 for (row = 0; row < numrows; ++row) {
289 for (col = 0, chcnt = 0; col < numcols; ++col) {
290 chcnt += printaname(array[base], dp->s_inode,
298 while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
300 if (f_sortacross && col + 1 >= numcols)
302 putchar(f_notabs ? ' ' : '\t');
312 * print [inode] [size] name
313 * return # of characters printed, no trailing characters.
316 printaname(const FTSENT *p, u_long inodefield, u_long sizefield)
321 int color_printed = 0;
327 chcnt += printf("%*llu ", (int)inodefield,
328 (unsigned long long)sp->st_ino);
331 chcnt += printf("%*lld ",
332 (int)sizefield, howmany(sp->st_blocks, blocksize));
335 color_printed = colortype(sp->st_mode);
337 chcnt += printname(p->fts_name);
339 if (f_color && color_printed)
343 chcnt += printtype(sp->st_mode);
348 printtime(time_t ftime)
353 static int d_first = -1;
356 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
360 #define SIXMONTHS ((365 / 2) * 86400)
362 /* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */
363 format = d_first ? "%e %b %T %Y " : "%b %e %T %Y ";
364 else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
365 /* mmm dd hh:mm || dd mmm hh:mm */
366 format = d_first ? "%e %b %R " : "%b %e %R ";
368 /* mmm dd yyyy || dd mmm yyyy */
369 format = d_first ? "%e %b %Y " : "%b %e %Y ";
370 strftime(longstring, sizeof(longstring), format, localtime(&ftime));
371 fputs(longstring, stdout);
375 printtype(u_int mode)
379 if ((mode & S_IFMT) == S_IFDIR) {
386 switch (mode & S_IFMT) {
405 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
425 write(STDOUT_FILENO, &tmp, 1);
435 tputs(enter_bold, 1, putch);
437 if (colors[c].num[0] != -1) {
438 ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]);
440 tputs(ansiseq, 1, putch);
442 if (colors[c].num[1] != -1) {
443 ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]);
445 tputs(ansiseq, 1, putch);
452 tputs(ansi_coloff, 1, sig ? writech : putch);
453 tputs(attrs_off, 1, sig ? writech : putch);
457 colortype(mode_t mode)
459 switch (mode & S_IFMT) {
486 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
489 else if (mode & S_ISGID)
499 parsecolors(const char *cs)
505 short legacy_warn = 0;
508 cs = ""; /* LSCOLORS not set */
510 for (i = 0; i < C_NUMCOLORS; i++) {
513 if (len <= 2 * (size_t)i) {
514 c[0] = defcolors[2 * i];
515 c[1] = defcolors[2 * i + 1];
518 c[1] = cs[2 * i + 1];
520 for (j = 0; j < 2; j++) {
521 /* Legacy colours used 0-7 */
522 if (c[j] >= '0' && c[j] <= '7') {
523 colors[i].num[j] = c[j] - '0';
525 warnx("LSCOLORS should use "
526 "characters a-h instead of 0-9 ("
527 "see the manual page)\n");
530 } else if (c[j] >= 'a' && c[j] <= 'h')
531 colors[i].num[j] = c[j] - 'a';
532 else if (c[j] >= 'A' && c[j] <= 'H') {
533 colors[i].num[j] = c[j] - 'A';
535 } else if (tolower((unsigned char)c[j] == 'x'))
536 colors[i].num[j] = -1;
538 warnx("invalid character '%c' in LSCOLORS"
540 colors[i].num[j] = -1;
551 signal(sig, SIG_DFL);
558 printlink(const FTSENT *p)
561 char name[MAXPATHLEN + 1];
562 char path[MAXPATHLEN + 1];
564 if (p->fts_level == FTS_ROOTLEVEL)
565 snprintf(name, sizeof(name), "%s", p->fts_name);
567 snprintf(name, sizeof(name),
568 "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
569 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
570 fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
579 printsize(size_t width, off_t bytes)
584 humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
585 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
588 printf("%*lld ", width, bytes);