xref: /freebsd/bin/ls/print.c (revision ba3c1f5972d7b90feb6e6da47905ff2757e0fe57)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Michael Fischbein.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #if 0
36 #ifndef lint
37 static char sccsid[] = "@(#)print.c	8.4 (Berkeley) 4/17/94";
38 #endif /* not lint */
39 #endif
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42 
43 #include <sys/param.h>
44 #include <sys/stat.h>
45 #include <sys/acl.h>
46 
47 #include <err.h>
48 #include <errno.h>
49 #include <fts.h>
50 #include <langinfo.h>
51 #include <libutil.h>
52 #include <limits.h>
53 #include <stdio.h>
54 #include <stdint.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <time.h>
58 #include <unistd.h>
59 #include <wchar.h>
60 #ifdef COLORLS
61 #include <ctype.h>
62 #include <termcap.h>
63 #include <signal.h>
64 #endif
65 
66 #include "ls.h"
67 #include "extern.h"
68 
69 static int	printaname(const FTSENT *, u_long, u_long);
70 static void	printdev(size_t, dev_t);
71 static void	printlink(const FTSENT *);
72 static void	printtime(time_t);
73 static int	printtype(u_int);
74 static void	printsize(size_t, off_t);
75 #ifdef COLORLS
76 static void	endcolor_termcap(int);
77 static void	endcolor_ansi(void);
78 static void	endcolor(int);
79 static int	colortype(mode_t);
80 #endif
81 static void	aclmode(char *, const FTSENT *);
82 
83 #define	IS_NOPRINT(p)	((p)->fts_number == NO_PRINT)
84 
85 #ifdef COLORLS
86 /* Most of these are taken from <sys/stat.h> */
87 typedef enum Colors {
88 	C_DIR,			/* directory */
89 	C_LNK,			/* symbolic link */
90 	C_SOCK,			/* socket */
91 	C_FIFO,			/* pipe */
92 	C_EXEC,			/* executable */
93 	C_BLK,			/* block special */
94 	C_CHR,			/* character special */
95 	C_SUID,			/* setuid executable */
96 	C_SGID,			/* setgid executable */
97 	C_WSDIR,		/* directory writeble to others, with sticky
98 				 * bit */
99 	C_WDIR,			/* directory writeble to others, without
100 				 * sticky bit */
101 	C_NUMCOLORS		/* just a place-holder */
102 } Colors;
103 
104 static const char *defcolors = "exfxcxdxbxegedabagacad";
105 
106 /* colors for file types */
107 static struct {
108 	int	num[2];
109 	bool	bold;
110 	bool	underline;
111 } colors[C_NUMCOLORS];
112 #endif
113 
114 static size_t padding_for_month[12];
115 static size_t month_max_size = 0;
116 
117 void
118 printscol(const DISPLAY *dp)
119 {
120 	FTSENT *p;
121 
122 	for (p = dp->list; p; p = p->fts_link) {
123 		if (IS_NOPRINT(p))
124 			continue;
125 		(void)printaname(p, dp->s_inode, dp->s_block);
126 		(void)putchar('\n');
127 	}
128 }
129 
130 /*
131  * print name in current style
132  */
133 int
134 printname(const char *name)
135 {
136 	if (f_octal || f_octal_escape)
137 		return prn_octal(name);
138 	else if (f_nonprint)
139 		return prn_printable(name);
140 	else
141 		return prn_normal(name);
142 }
143 
144 static const char *
145 get_abmon(int mon)
146 {
147 
148 	switch (mon) {
149 	case 0: return (nl_langinfo(ABMON_1));
150 	case 1: return (nl_langinfo(ABMON_2));
151 	case 2: return (nl_langinfo(ABMON_3));
152 	case 3: return (nl_langinfo(ABMON_4));
153 	case 4: return (nl_langinfo(ABMON_5));
154 	case 5: return (nl_langinfo(ABMON_6));
155 	case 6: return (nl_langinfo(ABMON_7));
156 	case 7: return (nl_langinfo(ABMON_8));
157 	case 8: return (nl_langinfo(ABMON_9));
158 	case 9: return (nl_langinfo(ABMON_10));
159 	case 10: return (nl_langinfo(ABMON_11));
160 	case 11: return (nl_langinfo(ABMON_12));
161 	}
162 
163 	/* should never happen */
164 	abort();
165 }
166 
167 static size_t
168 mbswidth(const char *month)
169 {
170 	wchar_t wc;
171 	size_t width, donelen, clen, w;
172 
173 	width = donelen = 0;
174 	while ((clen = mbrtowc(&wc, month + donelen, MB_LEN_MAX, NULL)) != 0) {
175 		if (clen == (size_t)-1 || clen == (size_t)-2)
176 			return (-1);
177 		donelen += clen;
178 		if ((w = wcwidth(wc)) == (size_t)-1)
179 			return (-1);
180 		width += w;
181 	}
182 
183 	return (width);
184 }
185 
186 static void
187 compute_abbreviated_month_size(void)
188 {
189 	int i;
190 	size_t width;
191 	size_t months_width[12];
192 
193 	for (i = 0; i < 12; i++) {
194 		width = mbswidth(get_abmon(i));
195 		if (width == (size_t)-1) {
196 			month_max_size = -1;
197 			return;
198 		}
199 		months_width[i] = width;
200 		if (width > month_max_size)
201 			month_max_size = width;
202 	}
203 
204 	for (i = 0; i < 12; i++)
205 		padding_for_month[i] = month_max_size - months_width[i];
206 }
207 
208 void
209 printlong(const DISPLAY *dp)
210 {
211 	struct stat *sp;
212 	FTSENT *p;
213 	NAMES *np;
214 	char buf[20];
215 #ifdef COLORLS
216 	int color_printed = 0;
217 #endif
218 
219 	if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
220 	    (f_longform || f_size)) {
221 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
222 	}
223 
224 	for (p = dp->list; p; p = p->fts_link) {
225 		if (IS_NOPRINT(p))
226 			continue;
227 		sp = p->fts_statp;
228 		if (f_inode)
229 			(void)printf("%*ju ",
230 			    dp->s_inode, (uintmax_t)sp->st_ino);
231 		if (f_size)
232 			(void)printf("%*jd ",
233 			    dp->s_block, howmany(sp->st_blocks, blocksize));
234 		strmode(sp->st_mode, buf);
235 		aclmode(buf, p);
236 		np = p->fts_pointer;
237 		(void)printf("%s %*ju ", buf, dp->s_nlink,
238 		    (uintmax_t)sp->st_nlink);
239 		if (!f_sowner)
240 			(void)printf("%-*s ", dp->s_user, np->user);
241 		(void)printf("%-*s ", dp->s_group, np->group);
242 		if (f_flags)
243 			(void)printf("%-*s ", dp->s_flags, np->flags);
244 		if (f_label)
245 			(void)printf("%-*s ", dp->s_label, np->label);
246 		if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
247 			printdev(dp->s_size, sp->st_rdev);
248 		else
249 			printsize(dp->s_size, sp->st_size);
250 		if (f_accesstime)
251 			printtime(sp->st_atime);
252 		else if (f_birthtime)
253 			printtime(sp->st_birthtime);
254 		else if (f_statustime)
255 			printtime(sp->st_ctime);
256 		else
257 			printtime(sp->st_mtime);
258 #ifdef COLORLS
259 		if (f_color)
260 			color_printed = colortype(sp->st_mode);
261 #endif
262 		(void)printname(p->fts_name);
263 #ifdef COLORLS
264 		if (f_color && color_printed)
265 			endcolor(0);
266 #endif
267 		if (f_type)
268 			(void)printtype(sp->st_mode);
269 		if (S_ISLNK(sp->st_mode))
270 			printlink(p);
271 		(void)putchar('\n');
272 	}
273 }
274 
275 void
276 printstream(const DISPLAY *dp)
277 {
278 	FTSENT *p;
279 	int chcnt;
280 
281 	for (p = dp->list, chcnt = 0; p; p = p->fts_link) {
282 		if (p->fts_number == NO_PRINT)
283 			continue;
284 		/* XXX strlen does not take octal escapes into account. */
285 		if (strlen(p->fts_name) + chcnt +
286 		    (p->fts_link ? 2 : 0) >= (unsigned)termwidth) {
287 			putchar('\n');
288 			chcnt = 0;
289 		}
290 		chcnt += printaname(p, dp->s_inode, dp->s_block);
291 		if (p->fts_link) {
292 			printf(", ");
293 			chcnt += 2;
294 		}
295 	}
296 	if (chcnt)
297 		putchar('\n');
298 }
299 
300 void
301 printcol(const DISPLAY *dp)
302 {
303 	static FTSENT **array;
304 	static int lastentries = -1;
305 	FTSENT *p;
306 	FTSENT **narray;
307 	int base;
308 	int chcnt;
309 	int cnt;
310 	int col;
311 	int colwidth;
312 	int endcol;
313 	int num;
314 	int numcols;
315 	int numrows;
316 	int row;
317 	int tabwidth;
318 
319 	if (f_notabs)
320 		tabwidth = 1;
321 	else
322 		tabwidth = 8;
323 
324 	/*
325 	 * Have to do random access in the linked list -- build a table
326 	 * of pointers.
327 	 */
328 	if (dp->entries > lastentries) {
329 		if ((narray =
330 		    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
331 			warn(NULL);
332 			printscol(dp);
333 			return;
334 		}
335 		lastentries = dp->entries;
336 		array = narray;
337 	}
338 	for (p = dp->list, num = 0; p; p = p->fts_link)
339 		if (p->fts_number != NO_PRINT)
340 			array[num++] = p;
341 
342 	colwidth = dp->maxlen;
343 	if (f_inode)
344 		colwidth += dp->s_inode + 1;
345 	if (f_size)
346 		colwidth += dp->s_block + 1;
347 	if (f_type)
348 		colwidth += 1;
349 
350 	colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
351 	if (termwidth < 2 * colwidth) {
352 		printscol(dp);
353 		return;
354 	}
355 	numcols = termwidth / colwidth;
356 	numrows = num / numcols;
357 	if (num % numcols)
358 		++numrows;
359 
360 	if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
361 	    (f_longform || f_size)) {
362 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
363 	}
364 
365 	base = 0;
366 	for (row = 0; row < numrows; ++row) {
367 		endcol = colwidth;
368 		if (!f_sortacross)
369 			base = row;
370 		for (col = 0, chcnt = 0; col < numcols; ++col) {
371 			chcnt += printaname(array[base], dp->s_inode,
372 			    dp->s_block);
373 			if (f_sortacross)
374 				base++;
375 			else
376 				base += numrows;
377 			if (base >= num)
378 				break;
379 			while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
380 			    <= endcol) {
381 				if (f_sortacross && col + 1 >= numcols)
382 					break;
383 				(void)putchar(f_notabs ? ' ' : '\t');
384 				chcnt = cnt;
385 			}
386 			endcol += colwidth;
387 		}
388 		(void)putchar('\n');
389 	}
390 }
391 
392 /*
393  * print [inode] [size] name
394  * return # of characters printed, no trailing characters.
395  */
396 static int
397 printaname(const FTSENT *p, u_long inodefield, u_long sizefield)
398 {
399 	struct stat *sp;
400 	int chcnt;
401 #ifdef COLORLS
402 	int color_printed = 0;
403 #endif
404 
405 	sp = p->fts_statp;
406 	chcnt = 0;
407 	if (f_inode)
408 		chcnt += printf("%*ju ",
409 		    (int)inodefield, (uintmax_t)sp->st_ino);
410 	if (f_size)
411 		chcnt += printf("%*jd ",
412 		    (int)sizefield, howmany(sp->st_blocks, blocksize));
413 #ifdef COLORLS
414 	if (f_color)
415 		color_printed = colortype(sp->st_mode);
416 #endif
417 	chcnt += printname(p->fts_name);
418 #ifdef COLORLS
419 	if (f_color && color_printed)
420 		endcolor(0);
421 #endif
422 	if (f_type)
423 		chcnt += printtype(sp->st_mode);
424 	return (chcnt);
425 }
426 
427 /*
428  * Print device special file major and minor numbers.
429  */
430 static void
431 printdev(size_t width, dev_t dev)
432 {
433 
434 	(void)printf("%#*jx ", (u_int)width, (uintmax_t)dev);
435 }
436 
437 static void
438 ls_strftime(char *str, size_t len, const char *fmt, const struct tm *tm)
439 {
440 	char *posb, nfmt[BUFSIZ];
441 	const char *format = fmt;
442 
443 	if ((posb = strstr(fmt, "%b")) != NULL) {
444 		if (month_max_size == 0) {
445 			compute_abbreviated_month_size();
446 		}
447 		if (month_max_size > 0 && tm != NULL) {
448 			snprintf(nfmt, sizeof(nfmt),  "%.*s%s%*s%s",
449 			    (int)(posb - fmt), fmt,
450 			    get_abmon(tm->tm_mon),
451 			    (int)padding_for_month[tm->tm_mon],
452 			    "",
453 			    posb + 2);
454 			format = nfmt;
455 		}
456 	}
457 	if (tm != NULL)
458 		strftime(str, len, format, tm);
459 	else
460 		strlcpy(str, "bad date val", len);
461 }
462 
463 static void
464 printtime(time_t ftime)
465 {
466 	char longstring[80];
467 	static time_t now = 0;
468 	const char *format;
469 	static int d_first = -1;
470 
471 	if (d_first < 0)
472 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
473 	if (now == 0)
474 		now = time(NULL);
475 
476 #define	SIXMONTHS	((365 / 2) * 86400)
477 	if (f_timeformat)  /* user specified format */
478 		format = f_timeformat;
479 	else if (f_sectime)
480 		/* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */
481 		format = d_first ? "%e %b %T %Y" : "%b %e %T %Y";
482 	else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
483 		/* mmm dd hh:mm || dd mmm hh:mm */
484 		format = d_first ? "%e %b %R" : "%b %e %R";
485 	else
486 		/* mmm dd  yyyy || dd mmm  yyyy */
487 		format = d_first ? "%e %b  %Y" : "%b %e  %Y";
488 	ls_strftime(longstring, sizeof(longstring), format, localtime(&ftime));
489 	fputs(longstring, stdout);
490 	fputc(' ', stdout);
491 }
492 
493 static int
494 printtype(u_int mode)
495 {
496 
497 	if (f_slash) {
498 		if ((mode & S_IFMT) == S_IFDIR) {
499 			(void)putchar('/');
500 			return (1);
501 		}
502 		return (0);
503 	}
504 
505 	switch (mode & S_IFMT) {
506 	case S_IFDIR:
507 		(void)putchar('/');
508 		return (1);
509 	case S_IFIFO:
510 		(void)putchar('|');
511 		return (1);
512 	case S_IFLNK:
513 		(void)putchar('@');
514 		return (1);
515 	case S_IFSOCK:
516 		(void)putchar('=');
517 		return (1);
518 	case S_IFWHT:
519 		(void)putchar('%');
520 		return (1);
521 	default:
522 		break;
523 	}
524 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
525 		(void)putchar('*');
526 		return (1);
527 	}
528 	return (0);
529 }
530 
531 #ifdef COLORLS
532 static int
533 putch(int c)
534 {
535 	(void)putchar(c);
536 	return 0;
537 }
538 
539 static int
540 writech(int c)
541 {
542 	char tmp = (char)c;
543 
544 	(void)write(STDOUT_FILENO, &tmp, 1);
545 	return 0;
546 }
547 
548 static void
549 printcolor_termcap(Colors c)
550 {
551 	char *ansiseq;
552 
553 	if (colors[c].bold)
554 		tputs(enter_bold, 1, putch);
555 	if (colors[c].underline)
556 		tputs(enter_underline, 1, putch);
557 
558 	if (colors[c].num[0] != -1) {
559 		ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]);
560 		if (ansiseq)
561 			tputs(ansiseq, 1, putch);
562 	}
563 	if (colors[c].num[1] != -1) {
564 		ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]);
565 		if (ansiseq)
566 			tputs(ansiseq, 1, putch);
567 	}
568 }
569 
570 static void
571 printcolor_ansi(Colors c)
572 {
573 
574 	printf("\033[");
575 
576 	if (colors[c].bold)
577 		printf("1");
578 	if (colors[c].underline)
579 		printf(";4");
580 	if (colors[c].num[0] != -1)
581 		printf(";3%d", colors[c].num[0]);
582 	if (colors[c].num[1] != -1)
583 		printf(";4%d", colors[c].num[1]);
584 	printf("m");
585 }
586 
587 static void
588 printcolor(Colors c)
589 {
590 
591 	if (explicitansi)
592 		printcolor_ansi(c);
593 	else
594 		printcolor_termcap(c);
595 }
596 
597 static void
598 endcolor_termcap(int sig)
599 {
600 
601 	tputs(ansi_coloff, 1, sig ? writech : putch);
602 	tputs(attrs_off, 1, sig ? writech : putch);
603 }
604 
605 static void
606 endcolor_ansi(void)
607 {
608 
609 	printf("\33[m");
610 }
611 
612 static void
613 endcolor(int sig)
614 {
615 
616 	if (explicitansi)
617 		endcolor_ansi();
618 	else
619 		endcolor_termcap(sig);
620 }
621 
622 static int
623 colortype(mode_t mode)
624 {
625 	switch (mode & S_IFMT) {
626 	case S_IFDIR:
627 		if (mode & S_IWOTH)
628 			if (mode & S_ISTXT)
629 				printcolor(C_WSDIR);
630 			else
631 				printcolor(C_WDIR);
632 		else
633 			printcolor(C_DIR);
634 		return (1);
635 	case S_IFLNK:
636 		printcolor(C_LNK);
637 		return (1);
638 	case S_IFSOCK:
639 		printcolor(C_SOCK);
640 		return (1);
641 	case S_IFIFO:
642 		printcolor(C_FIFO);
643 		return (1);
644 	case S_IFBLK:
645 		printcolor(C_BLK);
646 		return (1);
647 	case S_IFCHR:
648 		printcolor(C_CHR);
649 		return (1);
650 	default:;
651 	}
652 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
653 		if (mode & S_ISUID)
654 			printcolor(C_SUID);
655 		else if (mode & S_ISGID)
656 			printcolor(C_SGID);
657 		else
658 			printcolor(C_EXEC);
659 		return (1);
660 	}
661 	return (0);
662 }
663 
664 void
665 parsecolors(const char *cs)
666 {
667 	int i;
668 	int j;
669 	size_t len;
670 	char c[2];
671 	short legacy_warn = 0;
672 
673 	if (cs == NULL)
674 		cs = "";	/* LSCOLORS not set */
675 	len = strlen(cs);
676 	for (i = 0; i < (int)C_NUMCOLORS; i++) {
677 		colors[i].bold = false;
678 		colors[i].underline = false;
679 
680 		if (len <= 2 * (size_t)i) {
681 			c[0] = defcolors[2 * i];
682 			c[1] = defcolors[2 * i + 1];
683 		} else {
684 			c[0] = cs[2 * i];
685 			c[1] = cs[2 * i + 1];
686 		}
687 		for (j = 0; j < 2; j++) {
688 			/* Legacy colours used 0-7 */
689 			if (c[j] >= '0' && c[j] <= '7') {
690 				colors[i].num[j] = c[j] - '0';
691 				if (!legacy_warn) {
692 					warnx("LSCOLORS should use "
693 					    "characters a-h instead of 0-9 ("
694 					    "see the manual page)");
695 				}
696 				legacy_warn = 1;
697 			} else if (c[j] >= 'a' && c[j] <= 'h')
698 				colors[i].num[j] = c[j] - 'a';
699 			else if (c[j] >= 'A' && c[j] <= 'H') {
700 				colors[i].num[j] = c[j] - 'A';
701 				if (j == 1)
702 					colors[i].underline = true;
703 				else
704 					colors[i].bold = true;
705 			} else if (tolower((unsigned char)c[j]) == 'x') {
706 				if (j == 1 && c[j] == 'X')
707 					colors[i].underline = true;
708 				colors[i].num[j] = -1;
709 			} else {
710 				warnx("invalid character '%c' in LSCOLORS"
711 				    " env var", c[j]);
712 				colors[i].num[j] = -1;
713 			}
714 		}
715 	}
716 }
717 
718 void
719 colorquit(int sig)
720 {
721 	endcolor(sig);
722 
723 	(void)signal(sig, SIG_DFL);
724 	(void)kill(getpid(), sig);
725 }
726 
727 #endif /* COLORLS */
728 
729 static void
730 printlink(const FTSENT *p)
731 {
732 	int lnklen;
733 	char name[MAXPATHLEN + 1];
734 	char path[MAXPATHLEN + 1];
735 
736 	if (p->fts_level == FTS_ROOTLEVEL)
737 		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
738 	else
739 		(void)snprintf(name, sizeof(name),
740 		    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
741 	if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
742 		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
743 		return;
744 	}
745 	path[lnklen] = '\0';
746 	(void)printf(" -> ");
747 	(void)printname(path);
748 }
749 
750 static void
751 printsize(size_t width, off_t bytes)
752 {
753 
754 	if (f_humanval) {
755 		/*
756 		 * Reserve one space before the size and allocate room for
757 		 * the trailing '\0'.
758 		 */
759 		char buf[HUMANVALSTR_LEN - 1 + 1];
760 
761 		humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
762 		    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
763 		(void)printf("%*s ", (u_int)width, buf);
764 	} else if (f_thousands) {		/* with commas */
765 		/* This format assignment needed to work round gcc bug. */
766 		const char *format = "%*j'd ";
767 		(void)printf(format, (u_int)width, bytes);
768 	} else
769 		(void)printf("%*jd ", (u_int)width, bytes);
770 }
771 
772 /*
773  * Add a + after the standard rwxrwxrwx mode if the file has an
774  * ACL. strmode() reserves space at the end of the string.
775  */
776 static void
777 aclmode(char *buf, const FTSENT *p)
778 {
779 	char name[MAXPATHLEN + 1];
780 	int ret, trivial;
781 	static dev_t previous_dev = NODEV;
782 	static int supports_acls = -1;
783 	static int type = ACL_TYPE_ACCESS;
784 	acl_t facl;
785 
786 	/*
787 	 * XXX: ACLs are not supported on whiteouts and device files
788 	 * residing on UFS.
789 	 */
790 	if (S_ISCHR(p->fts_statp->st_mode) || S_ISBLK(p->fts_statp->st_mode) ||
791 	    S_ISWHT(p->fts_statp->st_mode))
792 		return;
793 
794 	if (previous_dev == p->fts_statp->st_dev && supports_acls == 0)
795 		return;
796 
797 	if (p->fts_level == FTS_ROOTLEVEL)
798 		snprintf(name, sizeof(name), "%s", p->fts_name);
799 	else
800 		snprintf(name, sizeof(name), "%s/%s",
801 		    p->fts_parent->fts_accpath, p->fts_name);
802 
803 	if (previous_dev != p->fts_statp->st_dev) {
804 		previous_dev = p->fts_statp->st_dev;
805 		supports_acls = 0;
806 
807 		ret = lpathconf(name, _PC_ACL_NFS4);
808 		if (ret > 0) {
809 			type = ACL_TYPE_NFS4;
810 			supports_acls = 1;
811 		} else if (ret < 0 && errno != EINVAL) {
812 			warn("%s", name);
813 			return;
814 		}
815 		if (supports_acls == 0) {
816 			ret = lpathconf(name, _PC_ACL_EXTENDED);
817 			if (ret > 0) {
818 				type = ACL_TYPE_ACCESS;
819 				supports_acls = 1;
820 			} else if (ret < 0 && errno != EINVAL) {
821 				warn("%s", name);
822 				return;
823 			}
824 		}
825 	}
826 	if (supports_acls == 0)
827 		return;
828 	facl = acl_get_link_np(name, type);
829 	if (facl == NULL) {
830 		warn("%s", name);
831 		return;
832 	}
833 	if (acl_is_trivial_np(facl, &trivial)) {
834 		acl_free(facl);
835 		warn("%s", name);
836 		return;
837 	}
838 	if (!trivial)
839 		buf[10] = '+';
840 	acl_free(facl);
841 }
842