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. 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 $
36 #include <sys/param.h>
58 static int printaname(const FTSENT *, u_long, u_long);
59 static void printlink(const FTSENT *);
60 static void printtime(time_t);
61 static int printtype(u_int);
62 static void printsize(size_t, off_t);
64 static void endcolor(int);
65 static int colortype(mode_t);
68 #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
71 /* Most of these are taken from <sys/stat.h> */
73 C_DIR, /* directory */
74 C_LNK, /* symbolic link */
77 C_EXEC, /* executable */
78 C_BLK, /* block special */
79 C_CHR, /* character special */
80 C_SUID, /* setuid executable */
81 C_SGID, /* setgid executable */
82 C_WSDIR, /* directory writeble to others, with sticky
84 C_WDIR, /* directory writeble to others, without
86 C_NUMCOLORS /* just a place-holder */
89 static const char *defcolors = "exfxcxdxbxegedabagacad";
91 /* colors for file types */
95 } colors[C_NUMCOLORS];
99 printscol(const DISPLAY *dp)
103 for (p = dp->list; p; p = p->fts_link) {
106 printaname(p, dp->s_inode, dp->s_block);
112 * print name in current style
115 printname(const char *name)
117 if (f_octal || f_octal_escape)
118 return prn_octal(name);
120 return prn_printable(name);
122 return prn_normal(name);
126 printlong(const DISPLAY *dp)
133 int color_printed = 0;
136 if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
137 (f_longform || f_size)) {
138 printf("total %lu\n", howmany(dp->btotal, blocksize));
141 for (p = dp->list; p; p = p->fts_link) {
146 printf("%*ju ", dp->s_inode, (uintmax_t)sp->st_ino);
149 dp->s_block, howmany(sp->st_blocks, blocksize));
150 strmode(sp->st_mode, buf);
152 printf("%s %*u %-*s %-*s ", buf, dp->s_nlink,
153 sp->st_nlink, dp->s_user, np->user, dp->s_group,
156 printf("%s ", np->fsmid);
158 printf("%-*s ", dp->s_flags, np->flags);
159 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
160 printf("%3d, 0x%08x ",
161 major(sp->st_rdev), (u_int)minor(sp->st_rdev));
164 8 - dp->s_size, "", dp->s_size, (intmax_t)sp->st_size);
166 printsize(dp->s_size, sp->st_size);
168 printtime(sp->st_atime);
169 else if (f_statustime)
170 printtime(sp->st_ctime);
172 printtime(sp->st_mtime);
175 color_printed = colortype(sp->st_mode);
177 printname(p->fts_name);
179 if (f_color && color_printed)
183 printtype(sp->st_mode);
184 if (S_ISLNK(sp->st_mode))
191 printstream(const DISPLAY *dp)
196 for (p = dp->list, chcnt = 0; p; p = p->fts_link) {
197 if (p->fts_number == NO_PRINT)
199 /* XXX strlen does not take octal escapes into account. */
200 if (strlen(p->fts_name) + chcnt +
201 (p->fts_link ? 2 : 0) >= (unsigned)termwidth) {
205 chcnt += printaname(p, dp->s_inode, dp->s_block);
216 printcol(const DISPLAY *dp)
218 static FTSENT **array;
219 static int lastentries = -1;
240 * Have to do random access in the linked list -- build a table
243 if (dp->entries > lastentries) {
244 lastentries = dp->entries;
246 realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
251 lastentries = dp->entries;
254 for (p = dp->list, num = 0; p; p = p->fts_link)
255 if (p->fts_number != NO_PRINT)
258 colwidth = dp->maxlen;
260 colwidth += dp->s_inode + 1;
262 colwidth += dp->s_block + 1;
266 colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
267 if (termwidth < 2 * colwidth) {
271 numcols = termwidth / colwidth;
272 numrows = num / numcols;
276 if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
277 (f_longform || f_size)) {
278 printf("total %lu\n", howmany(dp->btotal, blocksize));
281 /* counter if f_sortacross, else case-by-case */
284 for (row = 0; row < numrows; ++row) {
288 for (col = 0, chcnt = 0; col < numcols; ++col) {
289 chcnt += printaname(array[base], dp->s_inode,
297 while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
299 if (f_sortacross && col + 1 >= numcols)
301 putchar(f_notabs ? ' ' : '\t');
311 * print [inode] [size] name
312 * return # of characters printed, no trailing characters.
315 printaname(const FTSENT *p, u_long inodefield, u_long sizefield)
320 int color_printed = 0;
326 chcnt += printf("%*ju ", (int)inodefield,
327 (uintmax_t)sp->st_ino);
330 chcnt += printf("%*jd ",
331 (int)sizefield, howmany(sp->st_blocks, blocksize));
334 color_printed = colortype(sp->st_mode);
336 chcnt += printname(p->fts_name);
338 if (f_color && color_printed)
342 chcnt += printtype(sp->st_mode);
347 printtime(time_t ftime)
357 /* ISO 8601 (extended without T): YYYY-DD-MM hh:mm:ss */
360 /* ISO 8601 (basic with T): YYYYDDMMThh */
361 format = "%Y%m%dT%H ";
362 strftime(longstring, sizeof(longstring), format, localtime(&ftime));
363 fputs(longstring, stdout);
367 printtype(u_int mode)
371 if ((mode & S_IFMT) == S_IFDIR) {
378 switch (mode & S_IFMT) {
397 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
417 write(STDOUT_FILENO, &tmp, 1);
427 tputs(enter_bold, 1, putch);
429 if (colors[c].num[0] != -1) {
430 ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]);
432 tputs(ansiseq, 1, putch);
434 if (colors[c].num[1] != -1) {
435 ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]);
437 tputs(ansiseq, 1, putch);
444 tputs(ansi_coloff, 1, sig ? writech : putch);
445 tputs(attrs_off, 1, sig ? writech : putch);
449 colortype(mode_t mode)
451 switch (mode & S_IFMT) {
478 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
481 else if (mode & S_ISGID)
491 parsecolors(const char *cs)
497 short legacy_warn = 0;
500 cs = ""; /* LSCOLORS not set */
502 for (i = 0; i < C_NUMCOLORS; i++) {
505 if (len <= 2 * (size_t)i) {
506 c[0] = defcolors[2 * i];
507 c[1] = defcolors[2 * i + 1];
510 c[1] = cs[2 * i + 1];
512 for (j = 0; j < 2; j++) {
513 /* Legacy colours used 0-7 */
514 if (c[j] >= '0' && c[j] <= '7') {
515 colors[i].num[j] = c[j] - '0';
517 warnx("LSCOLORS should use "
518 "characters a-h instead of 0-9 ("
519 "see the manual page)\n");
522 } else if (c[j] >= 'a' && c[j] <= 'h')
523 colors[i].num[j] = c[j] - 'a';
524 else if (c[j] >= 'A' && c[j] <= 'H') {
525 colors[i].num[j] = c[j] - 'A';
527 } else if (tolower((unsigned char)c[j] == 'x'))
528 colors[i].num[j] = -1;
530 warnx("invalid character '%c' in LSCOLORS"
532 colors[i].num[j] = -1;
543 signal(sig, SIG_DFL);
550 printlink(const FTSENT *p)
553 char name[MAXPATHLEN + 1];
554 char path[MAXPATHLEN + 1];
556 if (p->fts_level == FTS_ROOTLEVEL)
557 snprintf(name, sizeof(name), "%s", p->fts_name);
559 snprintf(name, sizeof(name),
560 "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
561 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
562 fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
571 printsize(size_t width, off_t bytes)
576 humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
577 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
580 printf("%*jd ", (int)width, (uintmax_t)bytes);