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