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 ", buf, dp->s_nlink,
236 (uintmax_t)sp->st_nlink);
237 if (!f_sowner)
238 (void)printf("%-*s ", dp->s_user, np->user);
239 (void)printf("%-*s ", dp->s_group, 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
printstream(const DISPLAY * dp)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
printcol(const DISPLAY * dp)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
printaname(const FTSENT * p,u_long inodefield,u_long sizefield)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(f_thousands ? "%'*jd " : "%*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
printdev(size_t width,dev_t dev)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
ls_strftime(char * str,size_t len,const char * fmt,const struct tm * tm)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
printtime(time_t ftime)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
printtype(u_int mode)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
putch(int c)531 putch(int c)
532 {
533 (void)putchar(c);
534 return 0;
535 }
536
537 static int
writech(int c)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
printcolor_termcap(Colors c)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
printcolor_ansi(Colors c)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
printcolor(Colors c)586 printcolor(Colors c)
587 {
588
589 if (explicitansi)
590 printcolor_ansi(c);
591 else
592 printcolor_termcap(c);
593 }
594
595 static void
endcolor_termcap(int sig)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
endcolor_ansi(void)604 endcolor_ansi(void)
605 {
606
607 printf("\33[m");
608 }
609
610 static void
endcolor(int sig)611 endcolor(int sig)
612 {
613
614 if (explicitansi)
615 endcolor_ansi();
616 else
617 endcolor_termcap(sig);
618 }
619
620 static int
colortype(mode_t mode)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
parsecolors(const char * cs)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
colorquit(int sig)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
printlink(const FTSENT * p)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
printsize(size_t width,off_t bytes)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 {
763 (void)printf(f_thousands ? "%'*jd " : "%*jd ",
764 (u_int)width, bytes);
765 }
766 }
767
768 /*
769 * Add a + after the standard rwxrwxrwx mode if the file has an
770 * ACL. strmode() reserves space at the end of the string.
771 */
772 static void
aclmode(char * buf,const FTSENT * p)773 aclmode(char *buf, const FTSENT *p)
774 {
775 char name[MAXPATHLEN + 1];
776 int ret, trivial;
777 static dev_t previous_dev = NODEV;
778 static int supports_acls = -1;
779 static int type = ACL_TYPE_ACCESS;
780 acl_t facl;
781
782 /*
783 * XXX: ACLs are not supported on whiteouts and device files
784 * residing on UFS.
785 */
786 if (S_ISCHR(p->fts_statp->st_mode) || S_ISBLK(p->fts_statp->st_mode) ||
787 S_ISWHT(p->fts_statp->st_mode))
788 return;
789
790 if (previous_dev == p->fts_statp->st_dev && supports_acls == 0)
791 return;
792
793 if (p->fts_level == FTS_ROOTLEVEL)
794 snprintf(name, sizeof(name), "%s", p->fts_name);
795 else
796 snprintf(name, sizeof(name), "%s/%s",
797 p->fts_parent->fts_accpath, p->fts_name);
798
799 if (previous_dev != p->fts_statp->st_dev) {
800 previous_dev = p->fts_statp->st_dev;
801 supports_acls = 0;
802
803 ret = lpathconf(name, _PC_ACL_NFS4);
804 if (ret > 0) {
805 type = ACL_TYPE_NFS4;
806 supports_acls = 1;
807 } else if (ret < 0 && errno != EINVAL) {
808 warn("%s", name);
809 return;
810 }
811 if (supports_acls == 0) {
812 ret = lpathconf(name, _PC_ACL_EXTENDED);
813 if (ret > 0) {
814 type = ACL_TYPE_ACCESS;
815 supports_acls = 1;
816 } else if (ret < 0 && errno != EINVAL) {
817 warn("%s", name);
818 return;
819 }
820 }
821 }
822 if (supports_acls == 0)
823 return;
824 facl = acl_get_link_np(name, type);
825 if (facl == NULL) {
826 warn("%s", name);
827 return;
828 }
829 if (acl_is_trivial_np(facl, &trivial)) {
830 acl_free(facl);
831 warn("%s", name);
832 return;
833 }
834 if (!trivial)
835 buf[10] = '+';
836 acl_free(facl);
837 }
838