xref: /freebsd/bin/ls/print.c (revision 84823cc70824c8d842f503d8c2e6d7b0c2d95b61)
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 %-*s  %-*s  ", buf, dp->s_nlink,
238 		    (uintmax_t)sp->st_nlink, dp->s_user, np->user, dp->s_group,
239 		    np->group);
240 		if (f_flags)
241 			(void)printf("%-*s ", dp->s_flags, np->flags);
242 		if (f_label)
243 			(void)printf("%-*s ", dp->s_label, np->label);
244 		if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
245 			printdev(dp->s_size, sp->st_rdev);
246 		else
247 			printsize(dp->s_size, sp->st_size);
248 		if (f_accesstime)
249 			printtime(sp->st_atime);
250 		else if (f_birthtime)
251 			printtime(sp->st_birthtime);
252 		else if (f_statustime)
253 			printtime(sp->st_ctime);
254 		else
255 			printtime(sp->st_mtime);
256 #ifdef COLORLS
257 		if (f_color)
258 			color_printed = colortype(sp->st_mode);
259 #endif
260 		(void)printname(p->fts_name);
261 #ifdef COLORLS
262 		if (f_color && color_printed)
263 			endcolor(0);
264 #endif
265 		if (f_type)
266 			(void)printtype(sp->st_mode);
267 		if (S_ISLNK(sp->st_mode))
268 			printlink(p);
269 		(void)putchar('\n');
270 	}
271 }
272 
273 void
274 printstream(const DISPLAY *dp)
275 {
276 	FTSENT *p;
277 	int chcnt;
278 
279 	for (p = dp->list, chcnt = 0; p; p = p->fts_link) {
280 		if (p->fts_number == NO_PRINT)
281 			continue;
282 		/* XXX strlen does not take octal escapes into account. */
283 		if (strlen(p->fts_name) + chcnt +
284 		    (p->fts_link ? 2 : 0) >= (unsigned)termwidth) {
285 			putchar('\n');
286 			chcnt = 0;
287 		}
288 		chcnt += printaname(p, dp->s_inode, dp->s_block);
289 		if (p->fts_link) {
290 			printf(", ");
291 			chcnt += 2;
292 		}
293 	}
294 	if (chcnt)
295 		putchar('\n');
296 }
297 
298 void
299 printcol(const DISPLAY *dp)
300 {
301 	static FTSENT **array;
302 	static int lastentries = -1;
303 	FTSENT *p;
304 	FTSENT **narray;
305 	int base;
306 	int chcnt;
307 	int cnt;
308 	int col;
309 	int colwidth;
310 	int endcol;
311 	int num;
312 	int numcols;
313 	int numrows;
314 	int row;
315 	int tabwidth;
316 
317 	if (f_notabs)
318 		tabwidth = 1;
319 	else
320 		tabwidth = 8;
321 
322 	/*
323 	 * Have to do random access in the linked list -- build a table
324 	 * of pointers.
325 	 */
326 	if (dp->entries > lastentries) {
327 		if ((narray =
328 		    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
329 			warn(NULL);
330 			printscol(dp);
331 			return;
332 		}
333 		lastentries = dp->entries;
334 		array = narray;
335 	}
336 	for (p = dp->list, num = 0; p; p = p->fts_link)
337 		if (p->fts_number != NO_PRINT)
338 			array[num++] = p;
339 
340 	colwidth = dp->maxlen;
341 	if (f_inode)
342 		colwidth += dp->s_inode + 1;
343 	if (f_size)
344 		colwidth += dp->s_block + 1;
345 	if (f_type)
346 		colwidth += 1;
347 
348 	colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
349 	if (termwidth < 2 * colwidth) {
350 		printscol(dp);
351 		return;
352 	}
353 	numcols = termwidth / colwidth;
354 	numrows = num / numcols;
355 	if (num % numcols)
356 		++numrows;
357 
358 	if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
359 	    (f_longform || f_size)) {
360 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
361 	}
362 
363 	base = 0;
364 	for (row = 0; row < numrows; ++row) {
365 		endcol = colwidth;
366 		if (!f_sortacross)
367 			base = row;
368 		for (col = 0, chcnt = 0; col < numcols; ++col) {
369 			chcnt += printaname(array[base], dp->s_inode,
370 			    dp->s_block);
371 			if (f_sortacross)
372 				base++;
373 			else
374 				base += numrows;
375 			if (base >= num)
376 				break;
377 			while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
378 			    <= endcol) {
379 				if (f_sortacross && col + 1 >= numcols)
380 					break;
381 				(void)putchar(f_notabs ? ' ' : '\t');
382 				chcnt = cnt;
383 			}
384 			endcol += colwidth;
385 		}
386 		(void)putchar('\n');
387 	}
388 }
389 
390 /*
391  * print [inode] [size] name
392  * return # of characters printed, no trailing characters.
393  */
394 static int
395 printaname(const FTSENT *p, u_long inodefield, u_long sizefield)
396 {
397 	struct stat *sp;
398 	int chcnt;
399 #ifdef COLORLS
400 	int color_printed = 0;
401 #endif
402 
403 	sp = p->fts_statp;
404 	chcnt = 0;
405 	if (f_inode)
406 		chcnt += printf("%*ju ",
407 		    (int)inodefield, (uintmax_t)sp->st_ino);
408 	if (f_size)
409 		chcnt += printf("%*jd ",
410 		    (int)sizefield, howmany(sp->st_blocks, blocksize));
411 #ifdef COLORLS
412 	if (f_color)
413 		color_printed = colortype(sp->st_mode);
414 #endif
415 	chcnt += printname(p->fts_name);
416 #ifdef COLORLS
417 	if (f_color && color_printed)
418 		endcolor(0);
419 #endif
420 	if (f_type)
421 		chcnt += printtype(sp->st_mode);
422 	return (chcnt);
423 }
424 
425 /*
426  * Print device special file major and minor numbers.
427  */
428 static void
429 printdev(size_t width, dev_t dev)
430 {
431 
432 	(void)printf("%#*jx ", (u_int)width, (uintmax_t)dev);
433 }
434 
435 static void
436 ls_strftime(char *str, size_t len, const char *fmt, const struct tm *tm)
437 {
438 	char *posb, nfmt[BUFSIZ];
439 	const char *format = fmt;
440 
441 	if ((posb = strstr(fmt, "%b")) != NULL) {
442 		if (month_max_size == 0) {
443 			compute_abbreviated_month_size();
444 		}
445 		if (month_max_size > 0 && tm != NULL) {
446 			snprintf(nfmt, sizeof(nfmt),  "%.*s%s%*s%s",
447 			    (int)(posb - fmt), fmt,
448 			    get_abmon(tm->tm_mon),
449 			    (int)padding_for_month[tm->tm_mon],
450 			    "",
451 			    posb + 2);
452 			format = nfmt;
453 		}
454 	}
455 	if (tm != NULL)
456 		strftime(str, len, format, tm);
457 	else
458 		strlcpy(str, "bad date val", len);
459 }
460 
461 static void
462 printtime(time_t ftime)
463 {
464 	char longstring[80];
465 	static time_t now = 0;
466 	const char *format;
467 	static int d_first = -1;
468 
469 	if (d_first < 0)
470 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
471 	if (now == 0)
472 		now = time(NULL);
473 
474 #define	SIXMONTHS	((365 / 2) * 86400)
475 	if (f_timeformat)  /* user specified format */
476 		format = f_timeformat;
477 	else if (f_sectime)
478 		/* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */
479 		format = d_first ? "%e %b %T %Y" : "%b %e %T %Y";
480 	else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
481 		/* mmm dd hh:mm || dd mmm hh:mm */
482 		format = d_first ? "%e %b %R" : "%b %e %R";
483 	else
484 		/* mmm dd  yyyy || dd mmm  yyyy */
485 		format = d_first ? "%e %b  %Y" : "%b %e  %Y";
486 	ls_strftime(longstring, sizeof(longstring), format, localtime(&ftime));
487 	fputs(longstring, stdout);
488 	fputc(' ', stdout);
489 }
490 
491 static int
492 printtype(u_int mode)
493 {
494 
495 	if (f_slash) {
496 		if ((mode & S_IFMT) == S_IFDIR) {
497 			(void)putchar('/');
498 			return (1);
499 		}
500 		return (0);
501 	}
502 
503 	switch (mode & S_IFMT) {
504 	case S_IFDIR:
505 		(void)putchar('/');
506 		return (1);
507 	case S_IFIFO:
508 		(void)putchar('|');
509 		return (1);
510 	case S_IFLNK:
511 		(void)putchar('@');
512 		return (1);
513 	case S_IFSOCK:
514 		(void)putchar('=');
515 		return (1);
516 	case S_IFWHT:
517 		(void)putchar('%');
518 		return (1);
519 	default:
520 		break;
521 	}
522 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
523 		(void)putchar('*');
524 		return (1);
525 	}
526 	return (0);
527 }
528 
529 #ifdef COLORLS
530 static int
531 putch(int c)
532 {
533 	(void)putchar(c);
534 	return 0;
535 }
536 
537 static int
538 writech(int c)
539 {
540 	char tmp = (char)c;
541 
542 	(void)write(STDOUT_FILENO, &tmp, 1);
543 	return 0;
544 }
545 
546 static void
547 printcolor_termcap(Colors c)
548 {
549 	char *ansiseq;
550 
551 	if (colors[c].bold)
552 		tputs(enter_bold, 1, putch);
553 	if (colors[c].underline)
554 		tputs(enter_underline, 1, putch);
555 
556 	if (colors[c].num[0] != -1) {
557 		ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]);
558 		if (ansiseq)
559 			tputs(ansiseq, 1, putch);
560 	}
561 	if (colors[c].num[1] != -1) {
562 		ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]);
563 		if (ansiseq)
564 			tputs(ansiseq, 1, putch);
565 	}
566 }
567 
568 static void
569 printcolor_ansi(Colors c)
570 {
571 
572 	printf("\033[");
573 
574 	if (colors[c].bold)
575 		printf("1");
576 	if (colors[c].underline)
577 		printf(";4");
578 	if (colors[c].num[0] != -1)
579 		printf(";3%d", colors[c].num[0]);
580 	if (colors[c].num[1] != -1)
581 		printf(";4%d", colors[c].num[1]);
582 	printf("m");
583 }
584 
585 static void
586 printcolor(Colors c)
587 {
588 
589 	if (explicitansi)
590 		printcolor_ansi(c);
591 	else
592 		printcolor_termcap(c);
593 }
594 
595 static void
596 endcolor_termcap(int sig)
597 {
598 
599 	tputs(ansi_coloff, 1, sig ? writech : putch);
600 	tputs(attrs_off, 1, sig ? writech : putch);
601 }
602 
603 static void
604 endcolor_ansi(void)
605 {
606 
607 	printf("\33[m");
608 }
609 
610 static void
611 endcolor(int sig)
612 {
613 
614 	if (explicitansi)
615 		endcolor_ansi();
616 	else
617 		endcolor_termcap(sig);
618 }
619 
620 static int
621 colortype(mode_t mode)
622 {
623 	switch (mode & S_IFMT) {
624 	case S_IFDIR:
625 		if (mode & S_IWOTH)
626 			if (mode & S_ISTXT)
627 				printcolor(C_WSDIR);
628 			else
629 				printcolor(C_WDIR);
630 		else
631 			printcolor(C_DIR);
632 		return (1);
633 	case S_IFLNK:
634 		printcolor(C_LNK);
635 		return (1);
636 	case S_IFSOCK:
637 		printcolor(C_SOCK);
638 		return (1);
639 	case S_IFIFO:
640 		printcolor(C_FIFO);
641 		return (1);
642 	case S_IFBLK:
643 		printcolor(C_BLK);
644 		return (1);
645 	case S_IFCHR:
646 		printcolor(C_CHR);
647 		return (1);
648 	default:;
649 	}
650 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
651 		if (mode & S_ISUID)
652 			printcolor(C_SUID);
653 		else if (mode & S_ISGID)
654 			printcolor(C_SGID);
655 		else
656 			printcolor(C_EXEC);
657 		return (1);
658 	}
659 	return (0);
660 }
661 
662 void
663 parsecolors(const char *cs)
664 {
665 	int i;
666 	int j;
667 	size_t len;
668 	char c[2];
669 	short legacy_warn = 0;
670 
671 	if (cs == NULL)
672 		cs = "";	/* LSCOLORS not set */
673 	len = strlen(cs);
674 	for (i = 0; i < (int)C_NUMCOLORS; i++) {
675 		colors[i].bold = false;
676 		colors[i].underline = false;
677 
678 		if (len <= 2 * (size_t)i) {
679 			c[0] = defcolors[2 * i];
680 			c[1] = defcolors[2 * i + 1];
681 		} else {
682 			c[0] = cs[2 * i];
683 			c[1] = cs[2 * i + 1];
684 		}
685 		for (j = 0; j < 2; j++) {
686 			/* Legacy colours used 0-7 */
687 			if (c[j] >= '0' && c[j] <= '7') {
688 				colors[i].num[j] = c[j] - '0';
689 				if (!legacy_warn) {
690 					warnx("LSCOLORS should use "
691 					    "characters a-h instead of 0-9 ("
692 					    "see the manual page)");
693 				}
694 				legacy_warn = 1;
695 			} else if (c[j] >= 'a' && c[j] <= 'h')
696 				colors[i].num[j] = c[j] - 'a';
697 			else if (c[j] >= 'A' && c[j] <= 'H') {
698 				colors[i].num[j] = c[j] - 'A';
699 				if (j == 1)
700 					colors[i].underline = true;
701 				else
702 					colors[i].bold = true;
703 			} else if (tolower((unsigned char)c[j]) == 'x') {
704 				if (j == 1 && c[j] == 'X')
705 					colors[i].underline = true;
706 				colors[i].num[j] = -1;
707 			} else {
708 				warnx("invalid character '%c' in LSCOLORS"
709 				    " env var", c[j]);
710 				colors[i].num[j] = -1;
711 			}
712 		}
713 	}
714 }
715 
716 void
717 colorquit(int sig)
718 {
719 	endcolor(sig);
720 
721 	(void)signal(sig, SIG_DFL);
722 	(void)kill(getpid(), sig);
723 }
724 
725 #endif /* COLORLS */
726 
727 static void
728 printlink(const FTSENT *p)
729 {
730 	int lnklen;
731 	char name[MAXPATHLEN + 1];
732 	char path[MAXPATHLEN + 1];
733 
734 	if (p->fts_level == FTS_ROOTLEVEL)
735 		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
736 	else
737 		(void)snprintf(name, sizeof(name),
738 		    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
739 	if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
740 		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
741 		return;
742 	}
743 	path[lnklen] = '\0';
744 	(void)printf(" -> ");
745 	(void)printname(path);
746 }
747 
748 static void
749 printsize(size_t width, off_t bytes)
750 {
751 
752 	if (f_humanval) {
753 		/*
754 		 * Reserve one space before the size and allocate room for
755 		 * the trailing '\0'.
756 		 */
757 		char buf[HUMANVALSTR_LEN - 1 + 1];
758 
759 		humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
760 		    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
761 		(void)printf("%*s ", (u_int)width, buf);
762 	} else if (f_thousands) {		/* with commas */
763 		/* This format assignment needed to work round gcc bug. */
764 		const char *format = "%*j'd ";
765 		(void)printf(format, (u_int)width, bytes);
766 	} else
767 		(void)printf("%*jd ", (u_int)width, bytes);
768 }
769 
770 /*
771  * Add a + after the standard rwxrwxrwx mode if the file has an
772  * ACL. strmode() reserves space at the end of the string.
773  */
774 static void
775 aclmode(char *buf, const FTSENT *p)
776 {
777 	char name[MAXPATHLEN + 1];
778 	int ret, trivial;
779 	static dev_t previous_dev = NODEV;
780 	static int supports_acls = -1;
781 	static int type = ACL_TYPE_ACCESS;
782 	acl_t facl;
783 
784 	/*
785 	 * XXX: ACLs are not supported on whiteouts and device files
786 	 * residing on UFS.
787 	 */
788 	if (S_ISCHR(p->fts_statp->st_mode) || S_ISBLK(p->fts_statp->st_mode) ||
789 	    S_ISWHT(p->fts_statp->st_mode))
790 		return;
791 
792 	if (previous_dev == p->fts_statp->st_dev && supports_acls == 0)
793 		return;
794 
795 	if (p->fts_level == FTS_ROOTLEVEL)
796 		snprintf(name, sizeof(name), "%s", p->fts_name);
797 	else
798 		snprintf(name, sizeof(name), "%s/%s",
799 		    p->fts_parent->fts_accpath, p->fts_name);
800 
801 	if (previous_dev != p->fts_statp->st_dev) {
802 		previous_dev = p->fts_statp->st_dev;
803 		supports_acls = 0;
804 
805 		ret = lpathconf(name, _PC_ACL_NFS4);
806 		if (ret > 0) {
807 			type = ACL_TYPE_NFS4;
808 			supports_acls = 1;
809 		} else if (ret < 0 && errno != EINVAL) {
810 			warn("%s", name);
811 			return;
812 		}
813 		if (supports_acls == 0) {
814 			ret = lpathconf(name, _PC_ACL_EXTENDED);
815 			if (ret > 0) {
816 				type = ACL_TYPE_ACCESS;
817 				supports_acls = 1;
818 			} else if (ret < 0 && errno != EINVAL) {
819 				warn("%s", name);
820 				return;
821 			}
822 		}
823 	}
824 	if (supports_acls == 0)
825 		return;
826 	facl = acl_get_link_np(name, type);
827 	if (facl == NULL) {
828 		warn("%s", name);
829 		return;
830 	}
831 	if (acl_is_trivial_np(facl, &trivial)) {
832 		acl_free(facl);
833 		warn("%s", name);
834 		return;
835 	}
836 	if (!trivial)
837 		buf[10] = '+';
838 	acl_free(facl);
839 }
840