xref: /freebsd/bin/ls/print.c (revision e87ec409fa9b21abf79895837fe375ab3d7e408a)
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 size_t
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 	size_t ret;
441 
442 	if ((posb = strstr(fmt, "%b")) != NULL) {
443 		if (month_max_size == 0) {
444 			compute_abbreviated_month_size();
445 		}
446 		if (month_max_size > 0) {
447 			snprintf(nfmt, sizeof(nfmt),  "%.*s%s%*s%s",
448 			    (int)(posb - fmt), fmt,
449 			    get_abmon(tm->tm_mon),
450 			    (int)padding_for_month[tm->tm_mon],
451 			    "",
452 			    posb + 2);
453 			format = nfmt;
454 		}
455 	}
456 	ret = strftime(str, len, format, tm);
457 	return (ret);
458 }
459 
460 static void
461 printtime(time_t ftime)
462 {
463 	char longstring[80];
464 	static time_t now = 0;
465 	const char *format;
466 	static int d_first = -1;
467 
468 	if (d_first < 0)
469 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
470 	if (now == 0)
471 		now = time(NULL);
472 
473 #define	SIXMONTHS	((365 / 2) * 86400)
474 	if (f_timeformat)  /* user specified format */
475 		format = f_timeformat;
476 	else if (f_sectime)
477 		/* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */
478 		format = d_first ? "%e %b %T %Y" : "%b %e %T %Y";
479 	else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
480 		/* mmm dd hh:mm || dd mmm hh:mm */
481 		format = d_first ? "%e %b %R" : "%b %e %R";
482 	else
483 		/* mmm dd  yyyy || dd mmm  yyyy */
484 		format = d_first ? "%e %b  %Y" : "%b %e  %Y";
485 	ls_strftime(longstring, sizeof(longstring), format, localtime(&ftime));
486 	fputs(longstring, stdout);
487 	fputc(' ', stdout);
488 }
489 
490 static int
491 printtype(u_int mode)
492 {
493 
494 	if (f_slash) {
495 		if ((mode & S_IFMT) == S_IFDIR) {
496 			(void)putchar('/');
497 			return (1);
498 		}
499 		return (0);
500 	}
501 
502 	switch (mode & S_IFMT) {
503 	case S_IFDIR:
504 		(void)putchar('/');
505 		return (1);
506 	case S_IFIFO:
507 		(void)putchar('|');
508 		return (1);
509 	case S_IFLNK:
510 		(void)putchar('@');
511 		return (1);
512 	case S_IFSOCK:
513 		(void)putchar('=');
514 		return (1);
515 	case S_IFWHT:
516 		(void)putchar('%');
517 		return (1);
518 	default:
519 		break;
520 	}
521 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
522 		(void)putchar('*');
523 		return (1);
524 	}
525 	return (0);
526 }
527 
528 #ifdef COLORLS
529 static int
530 putch(int c)
531 {
532 	(void)putchar(c);
533 	return 0;
534 }
535 
536 static int
537 writech(int c)
538 {
539 	char tmp = (char)c;
540 
541 	(void)write(STDOUT_FILENO, &tmp, 1);
542 	return 0;
543 }
544 
545 static void
546 printcolor_termcap(Colors c)
547 {
548 	char *ansiseq;
549 
550 	if (colors[c].bold)
551 		tputs(enter_bold, 1, putch);
552 	if (colors[c].underline)
553 		tputs(enter_underline, 1, putch);
554 
555 	if (colors[c].num[0] != -1) {
556 		ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]);
557 		if (ansiseq)
558 			tputs(ansiseq, 1, putch);
559 	}
560 	if (colors[c].num[1] != -1) {
561 		ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]);
562 		if (ansiseq)
563 			tputs(ansiseq, 1, putch);
564 	}
565 }
566 
567 static void
568 printcolor_ansi(Colors c)
569 {
570 
571 	printf("\033[");
572 
573 	if (colors[c].bold)
574 		printf("1");
575 	if (colors[c].underline)
576 		printf(";4");
577 	if (colors[c].num[0] != -1)
578 		printf(";3%d", colors[c].num[0]);
579 	if (colors[c].num[1] != -1)
580 		printf(";4%d", colors[c].num[1]);
581 	printf("m");
582 }
583 
584 static void
585 printcolor(Colors c)
586 {
587 
588 	if (explicitansi)
589 		printcolor_ansi(c);
590 	else
591 		printcolor_termcap(c);
592 }
593 
594 static void
595 endcolor_termcap(int sig)
596 {
597 
598 	tputs(ansi_coloff, 1, sig ? writech : putch);
599 	tputs(attrs_off, 1, sig ? writech : putch);
600 }
601 
602 static void
603 endcolor_ansi(void)
604 {
605 
606 	printf("\33[m");
607 }
608 
609 static void
610 endcolor(int sig)
611 {
612 
613 	if (explicitansi)
614 		endcolor_ansi();
615 	else
616 		endcolor_termcap(sig);
617 }
618 
619 static int
620 colortype(mode_t mode)
621 {
622 	switch (mode & S_IFMT) {
623 	case S_IFDIR:
624 		if (mode & S_IWOTH)
625 			if (mode & S_ISTXT)
626 				printcolor(C_WSDIR);
627 			else
628 				printcolor(C_WDIR);
629 		else
630 			printcolor(C_DIR);
631 		return (1);
632 	case S_IFLNK:
633 		printcolor(C_LNK);
634 		return (1);
635 	case S_IFSOCK:
636 		printcolor(C_SOCK);
637 		return (1);
638 	case S_IFIFO:
639 		printcolor(C_FIFO);
640 		return (1);
641 	case S_IFBLK:
642 		printcolor(C_BLK);
643 		return (1);
644 	case S_IFCHR:
645 		printcolor(C_CHR);
646 		return (1);
647 	default:;
648 	}
649 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
650 		if (mode & S_ISUID)
651 			printcolor(C_SUID);
652 		else if (mode & S_ISGID)
653 			printcolor(C_SGID);
654 		else
655 			printcolor(C_EXEC);
656 		return (1);
657 	}
658 	return (0);
659 }
660 
661 void
662 parsecolors(const char *cs)
663 {
664 	int i;
665 	int j;
666 	size_t len;
667 	char c[2];
668 	short legacy_warn = 0;
669 
670 	if (cs == NULL)
671 		cs = "";	/* LSCOLORS not set */
672 	len = strlen(cs);
673 	for (i = 0; i < (int)C_NUMCOLORS; i++) {
674 		colors[i].bold = false;
675 		colors[i].underline = false;
676 
677 		if (len <= 2 * (size_t)i) {
678 			c[0] = defcolors[2 * i];
679 			c[1] = defcolors[2 * i + 1];
680 		} else {
681 			c[0] = cs[2 * i];
682 			c[1] = cs[2 * i + 1];
683 		}
684 		for (j = 0; j < 2; j++) {
685 			/* Legacy colours used 0-7 */
686 			if (c[j] >= '0' && c[j] <= '7') {
687 				colors[i].num[j] = c[j] - '0';
688 				if (!legacy_warn) {
689 					warnx("LSCOLORS should use "
690 					    "characters a-h instead of 0-9 ("
691 					    "see the manual page)");
692 				}
693 				legacy_warn = 1;
694 			} else if (c[j] >= 'a' && c[j] <= 'h')
695 				colors[i].num[j] = c[j] - 'a';
696 			else if (c[j] >= 'A' && c[j] <= 'H') {
697 				colors[i].num[j] = c[j] - 'A';
698 				if (j == 1)
699 					colors[i].underline = true;
700 				else
701 					colors[i].bold = true;
702 			} else if (tolower((unsigned char)c[j]) == 'x') {
703 				if (j == 1 && c[j] == 'X')
704 					colors[i].underline = true;
705 				colors[i].num[j] = -1;
706 			} else {
707 				warnx("invalid character '%c' in LSCOLORS"
708 				    " env var", c[j]);
709 				colors[i].num[j] = -1;
710 			}
711 		}
712 	}
713 }
714 
715 void
716 colorquit(int sig)
717 {
718 	endcolor(sig);
719 
720 	(void)signal(sig, SIG_DFL);
721 	(void)kill(getpid(), sig);
722 }
723 
724 #endif /* COLORLS */
725 
726 static void
727 printlink(const FTSENT *p)
728 {
729 	int lnklen;
730 	char name[MAXPATHLEN + 1];
731 	char path[MAXPATHLEN + 1];
732 
733 	if (p->fts_level == FTS_ROOTLEVEL)
734 		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
735 	else
736 		(void)snprintf(name, sizeof(name),
737 		    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
738 	if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
739 		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
740 		return;
741 	}
742 	path[lnklen] = '\0';
743 	(void)printf(" -> ");
744 	(void)printname(path);
745 }
746 
747 static void
748 printsize(size_t width, off_t bytes)
749 {
750 
751 	if (f_humanval) {
752 		/*
753 		 * Reserve one space before the size and allocate room for
754 		 * the trailing '\0'.
755 		 */
756 		char buf[HUMANVALSTR_LEN - 1 + 1];
757 
758 		humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
759 		    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
760 		(void)printf("%*s ", (u_int)width, buf);
761 	} else if (f_thousands) {		/* with commas */
762 		/* This format assignment needed to work round gcc bug. */
763 		const char *format = "%*j'd ";
764 		(void)printf(format, (u_int)width, bytes);
765 	} else
766 		(void)printf("%*jd ", (u_int)width, bytes);
767 }
768 
769 /*
770  * Add a + after the standard rwxrwxrwx mode if the file has an
771  * ACL. strmode() reserves space at the end of the string.
772  */
773 static void
774 aclmode(char *buf, const FTSENT *p)
775 {
776 	char name[MAXPATHLEN + 1];
777 	int ret, trivial;
778 	static dev_t previous_dev = NODEV;
779 	static int supports_acls = -1;
780 	static int type = ACL_TYPE_ACCESS;
781 	acl_t facl;
782 
783 	/*
784 	 * XXX: ACLs are not supported on whiteouts and device files
785 	 * residing on UFS.
786 	 */
787 	if (S_ISCHR(p->fts_statp->st_mode) || S_ISBLK(p->fts_statp->st_mode) ||
788 	    S_ISWHT(p->fts_statp->st_mode))
789 		return;
790 
791 	if (previous_dev == p->fts_statp->st_dev && supports_acls == 0)
792 		return;
793 
794 	if (p->fts_level == FTS_ROOTLEVEL)
795 		snprintf(name, sizeof(name), "%s", p->fts_name);
796 	else
797 		snprintf(name, sizeof(name), "%s/%s",
798 		    p->fts_parent->fts_accpath, p->fts_name);
799 
800 	if (previous_dev != p->fts_statp->st_dev) {
801 		previous_dev = p->fts_statp->st_dev;
802 		supports_acls = 0;
803 
804 		ret = lpathconf(name, _PC_ACL_NFS4);
805 		if (ret > 0) {
806 			type = ACL_TYPE_NFS4;
807 			supports_acls = 1;
808 		} else if (ret < 0 && errno != EINVAL) {
809 			warn("%s", name);
810 			return;
811 		}
812 		if (supports_acls == 0) {
813 			ret = lpathconf(name, _PC_ACL_EXTENDED);
814 			if (ret > 0) {
815 				type = ACL_TYPE_ACCESS;
816 				supports_acls = 1;
817 			} else if (ret < 0 && errno != EINVAL) {
818 				warn("%s", name);
819 				return;
820 			}
821 		}
822 	}
823 	if (supports_acls == 0)
824 		return;
825 	facl = acl_get_link_np(name, type);
826 	if (facl == NULL) {
827 		warn("%s", name);
828 		return;
829 	}
830 	if (acl_is_trivial_np(facl, &trivial)) {
831 		acl_free(facl);
832 		warn("%s", name);
833 		return;
834 	}
835 	if (!trivial)
836 		buf[10] = '+';
837 	acl_free(facl);
838 }
839