Change bwillwrite() to smooth out performance under heavy loads. Blocking
[dragonfly.git] / bin / ls / print.c
blobd9c4ad2721fcd15fb525e588130edc0db192f9cf
1 /*
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
6 * Michael Fischbein.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
30 * SUCH DAMAGE.
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.19 2008/05/04 02:28:28 dillon Exp $
37 #include <sys/param.h>
38 #include <sys/stat.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <fts.h>
43 #include <langinfo.h>
44 #include <libutil.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <time.h>
49 #include <unistd.h>
50 #ifdef COLORLS
51 #include <ctype.h>
52 #include <termcap.h>
53 #include <signal.h>
54 #endif
56 #include "ls.h"
57 #include "extern.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);
64 #ifdef COLORLS
65 static void endcolor(int);
66 static int colortype(mode_t);
67 #endif
69 #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
71 #ifdef COLORLS
72 /* Most of these are taken from <sys/stat.h> */
73 typedef enum Colors {
74 C_DIR, /* directory */
75 C_LNK, /* symbolic link */
76 C_SOCK, /* socket */
77 C_FIFO, /* pipe */
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
84 * bit */
85 C_WDIR, /* directory writeble to others, without
86 * sticky bit */
87 C_NUMCOLORS /* just a place-holder */
88 } Colors;
90 static const char *defcolors = "exfxcxdxbxegedabagacad";
92 /* colors for file types */
93 static struct {
94 int num[2];
95 int bold;
96 } colors[C_NUMCOLORS];
97 #endif
99 void
100 printscol(const DISPLAY *dp)
102 FTSENT *p;
104 for (p = dp->list; p; p = p->fts_link) {
105 if (IS_NOPRINT(p))
106 continue;
107 printaname(p, dp->s_inode, dp->s_block);
108 putchar('\n');
113 * print name in current style
116 printname(const char *name)
118 if (f_octal || f_octal_escape)
119 return prn_octal(name);
120 else if (f_nonprint)
121 return prn_printable(name);
122 else
123 return prn_normal(name);
126 void
127 printlong(const DISPLAY *dp)
129 struct stat *sp;
130 FTSENT *p;
131 NAMES *np;
132 char buf[20];
133 #ifdef COLORLS
134 int color_printed = 0;
135 #endif
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) {
143 if (IS_NOPRINT(p))
144 continue;
145 sp = p->fts_statp;
146 if (f_inode)
147 printf("%*llu ", dp->s_inode, (unsigned long long)sp->st_ino);
148 if (f_size)
149 printf("%*lld ",
150 dp->s_block, howmany(sp->st_blocks, blocksize));
151 strmode(sp->st_mode, buf);
152 np = p->fts_pointer;
153 printf("%s %*u %-*s %-*s ", buf, dp->s_nlink,
154 sp->st_nlink, dp->s_user, np->user, dp->s_group,
155 np->group);
156 if (f_fsmid)
157 printf("%s ", np->fsmid);
158 if (f_flags)
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));
163 else if (dp->bcfile)
164 printf("%*s%*lld ",
165 8 - dp->s_size, "", dp->s_size, sp->st_size);
166 else
167 printsize(dp->s_size, sp->st_size);
168 if (f_accesstime)
169 printtime(sp->st_atime);
170 else if (f_statustime)
171 printtime(sp->st_ctime);
172 else
173 printtime(sp->st_mtime);
174 #ifdef COLORLS
175 if (f_color)
176 color_printed = colortype(sp->st_mode);
177 #endif
178 printname(p->fts_name);
179 #ifdef COLORLS
180 if (f_color && color_printed)
181 endcolor(0);
182 #endif
183 if (f_type)
184 printtype(sp->st_mode);
185 if (S_ISLNK(sp->st_mode))
186 printlink(p);
187 putchar('\n');
191 void
192 printstream(const DISPLAY *dp)
194 FTSENT *p;
195 int chcnt;
197 for (p = dp->list, chcnt = 0; p; p = p->fts_link) {
198 if (p->fts_number == NO_PRINT)
199 continue;
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) {
203 putchar('\n');
204 chcnt = 0;
206 chcnt += printaname(p, dp->s_inode, dp->s_block);
207 if (p->fts_link) {
208 printf(", ");
209 chcnt += 2;
212 if (chcnt)
213 putchar('\n');
216 void
217 printcol(const DISPLAY *dp)
219 static FTSENT **array;
220 static int lastentries = -1;
221 FTSENT *p;
222 FTSENT **narray;
223 int base;
224 int chcnt;
225 int cnt;
226 int col;
227 int colwidth;
228 int endcol;
229 int num;
230 int numcols;
231 int numrows;
232 int row;
233 int tabwidth;
235 if (f_notabs)
236 tabwidth = 1;
237 else
238 tabwidth = 8;
241 * Have to do random access in the linked list -- build a table
242 * of pointers.
244 if (dp->entries > lastentries) {
245 lastentries = dp->entries;
246 if ((narray =
247 realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
248 warn(NULL);
249 printscol(dp);
250 return;
252 lastentries = dp->entries;
253 array = narray;
255 for (p = dp->list, num = 0; p; p = p->fts_link)
256 if (p->fts_number != NO_PRINT)
257 array[num++] = p;
259 colwidth = dp->maxlen;
260 if (f_inode)
261 colwidth += dp->s_inode + 1;
262 if (f_size)
263 colwidth += dp->s_block + 1;
264 if (f_type)
265 colwidth += 1;
267 colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
268 if (termwidth < 2 * colwidth) {
269 printscol(dp);
270 return;
272 numcols = termwidth / colwidth;
273 numrows = num / numcols;
274 if (num % numcols)
275 ++numrows;
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 */
283 base = 0;
285 for (row = 0; row < numrows; ++row) {
286 endcol = colwidth;
287 if (!f_sortacross)
288 base = row;
289 for (col = 0, chcnt = 0; col < numcols; ++col) {
290 chcnt += printaname(array[base], dp->s_inode,
291 dp->s_block);
292 if (f_sortacross)
293 base++;
294 else
295 base += numrows;
296 if (base >= num)
297 break;
298 while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
299 <= endcol) {
300 if (f_sortacross && col + 1 >= numcols)
301 break;
302 putchar(f_notabs ? ' ' : '\t');
303 chcnt = cnt;
305 endcol += colwidth;
307 putchar('\n');
312 * print [inode] [size] name
313 * return # of characters printed, no trailing characters.
315 static int
316 printaname(const FTSENT *p, u_long inodefield, u_long sizefield)
318 struct stat *sp;
319 int chcnt;
320 #ifdef COLORLS
321 int color_printed = 0;
322 #endif
324 sp = p->fts_statp;
325 chcnt = 0;
326 if (f_inode)
327 chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino);
328 if (f_size)
329 chcnt += printf("%*lld ",
330 (int)sizefield, howmany(sp->st_blocks, blocksize));
331 #ifdef COLORLS
332 if (f_color)
333 color_printed = colortype(sp->st_mode);
334 #endif
335 chcnt += printname(p->fts_name);
336 #ifdef COLORLS
337 if (f_color && color_printed)
338 endcolor(0);
339 #endif
340 if (f_type)
341 chcnt += printtype(sp->st_mode);
342 return (chcnt);
345 static void
346 printtime(time_t ftime)
348 char longstring[80];
349 static time_t now;
350 const char *format;
351 static int d_first = -1;
353 if (d_first < 0)
354 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
355 if (now == 0)
356 now = time(NULL);
358 #define SIXMONTHS ((365 / 2) * 86400)
359 if (f_sectime)
360 /* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */
361 format = d_first ? "%e %b %T %Y " : "%b %e %T %Y ";
362 else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
363 /* mmm dd hh:mm || dd mmm hh:mm */
364 format = d_first ? "%e %b %R " : "%b %e %R ";
365 else
366 /* mmm dd yyyy || dd mmm yyyy */
367 format = d_first ? "%e %b %Y " : "%b %e %Y ";
368 strftime(longstring, sizeof(longstring), format, localtime(&ftime));
369 fputs(longstring, stdout);
372 static int
373 printtype(u_int mode)
376 if (f_slash) {
377 if ((mode & S_IFMT) == S_IFDIR) {
378 putchar('/');
379 return (1);
381 return (0);
384 switch (mode & S_IFMT) {
385 case S_IFDIR:
386 putchar('/');
387 return (1);
388 case S_IFIFO:
389 putchar('|');
390 return (1);
391 case S_IFLNK:
392 putchar('@');
393 return (1);
394 case S_IFSOCK:
395 putchar('=');
396 return (1);
397 case S_IFWHT:
398 putchar('%');
399 return (1);
400 default:
401 break;
403 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
404 putchar('*');
405 return (1);
407 return (0);
410 #ifdef COLORLS
411 static int
412 putch(int c)
414 putchar(c);
415 return 0;
418 static int
419 writech(int c)
421 char tmp = (char)c;
423 write(STDOUT_FILENO, &tmp, 1);
424 return 0;
427 static void
428 printcolor(Colors c)
430 char *ansiseq;
432 if (colors[c].bold)
433 tputs(enter_bold, 1, putch);
435 if (colors[c].num[0] != -1) {
436 ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]);
437 if (ansiseq)
438 tputs(ansiseq, 1, putch);
440 if (colors[c].num[1] != -1) {
441 ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]);
442 if (ansiseq)
443 tputs(ansiseq, 1, putch);
447 static void
448 endcolor(int sig)
450 tputs(ansi_coloff, 1, sig ? writech : putch);
451 tputs(attrs_off, 1, sig ? writech : putch);
454 static int
455 colortype(mode_t mode)
457 switch (mode & S_IFMT) {
458 case S_IFDIR:
459 if (mode & S_IWOTH)
460 if (mode & S_ISTXT)
461 printcolor(C_WSDIR);
462 else
463 printcolor(C_WDIR);
464 else
465 printcolor(C_DIR);
466 return (1);
467 case S_IFLNK:
468 printcolor(C_LNK);
469 return (1);
470 case S_IFSOCK:
471 printcolor(C_SOCK);
472 return (1);
473 case S_IFIFO:
474 printcolor(C_FIFO);
475 return (1);
476 case S_IFBLK:
477 printcolor(C_BLK);
478 return (1);
479 case S_IFCHR:
480 printcolor(C_CHR);
481 return (1);
482 default:;
484 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
485 if (mode & S_ISUID)
486 printcolor(C_SUID);
487 else if (mode & S_ISGID)
488 printcolor(C_SGID);
489 else
490 printcolor(C_EXEC);
491 return (1);
493 return (0);
496 void
497 parsecolors(const char *cs)
499 int i;
500 int j;
501 size_t len;
502 char c[2];
503 short legacy_warn = 0;
505 if (cs == NULL)
506 cs = ""; /* LSCOLORS not set */
507 len = strlen(cs);
508 for (i = 0; i < C_NUMCOLORS; i++) {
509 colors[i].bold = 0;
511 if (len <= 2 * (size_t)i) {
512 c[0] = defcolors[2 * i];
513 c[1] = defcolors[2 * i + 1];
514 } else {
515 c[0] = cs[2 * i];
516 c[1] = cs[2 * i + 1];
518 for (j = 0; j < 2; j++) {
519 /* Legacy colours used 0-7 */
520 if (c[j] >= '0' && c[j] <= '7') {
521 colors[i].num[j] = c[j] - '0';
522 if (!legacy_warn) {
523 warnx("LSCOLORS should use "
524 "characters a-h instead of 0-9 ("
525 "see the manual page)\n");
527 legacy_warn = 1;
528 } else if (c[j] >= 'a' && c[j] <= 'h')
529 colors[i].num[j] = c[j] - 'a';
530 else if (c[j] >= 'A' && c[j] <= 'H') {
531 colors[i].num[j] = c[j] - 'A';
532 colors[i].bold = 1;
533 } else if (tolower((unsigned char)c[j] == 'x'))
534 colors[i].num[j] = -1;
535 else {
536 warnx("invalid character '%c' in LSCOLORS"
537 " env var\n", c[j]);
538 colors[i].num[j] = -1;
544 void
545 colorquit(int sig)
547 endcolor(sig);
549 signal(sig, SIG_DFL);
550 kill(getpid(), sig);
553 #endif /* COLORLS */
555 static void
556 printlink(const FTSENT *p)
558 int lnklen;
559 char name[MAXPATHLEN + 1];
560 char path[MAXPATHLEN + 1];
562 if (p->fts_level == FTS_ROOTLEVEL)
563 snprintf(name, sizeof(name), "%s", p->fts_name);
564 else
565 snprintf(name, sizeof(name),
566 "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
567 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
568 fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
569 return;
571 path[lnklen] = '\0';
572 printf(" -> ");
573 printname(path);
576 static void
577 printsize(size_t width, off_t bytes)
579 if (f_humanval) {
580 char buf[5];
582 humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
583 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
584 printf("%5s ", buf);
585 } else
586 printf("%*lld ", width, bytes);