print.c revision 1.56 1 1.56 christos /* $NetBSD: print.c,v 1.56 2020/05/16 18:31:45 christos Exp $ */
2 1.13 cgd
3 1.1 cgd /*
4 1.11 mycroft * Copyright (c) 1989, 1993, 1994
5 1.11 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Michael Fischbein.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.35 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.16 christos #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.13 cgd #if 0
38 1.14 jtc static char sccsid[] = "@(#)print.c 8.5 (Berkeley) 7/28/94";
39 1.13 cgd #else
40 1.56 christos __RCSID("$NetBSD: print.c,v 1.56 2020/05/16 18:31:45 christos Exp $");
41 1.13 cgd #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd #include <sys/param.h>
45 1.1 cgd #include <sys/stat.h>
46 1.56 christos #include <sys/acl.h>
47 1.11 mycroft
48 1.11 mycroft #include <err.h>
49 1.11 mycroft #include <errno.h>
50 1.55 martin #include <inttypes.h>
51 1.5 mycroft #include <fts.h>
52 1.1 cgd #include <grp.h>
53 1.1 cgd #include <pwd.h>
54 1.11 mycroft #include <stdio.h>
55 1.5 mycroft #include <stdlib.h>
56 1.5 mycroft #include <string.h>
57 1.11 mycroft #include <time.h>
58 1.11 mycroft #include <tzfile.h>
59 1.11 mycroft #include <unistd.h>
60 1.38 grant #include <util.h>
61 1.11 mycroft
62 1.1 cgd #include "ls.h"
63 1.5 mycroft #include "extern.h"
64 1.1 cgd
65 1.31 christos extern int termwidth;
66 1.31 christos
67 1.30 lukem static int printaname(FTSENT *, int, int);
68 1.30 lukem static void printlink(FTSENT *);
69 1.30 lukem static void printtime(time_t);
70 1.43 ahoka static void printtotal(DISPLAY *dp);
71 1.30 lukem static int printtype(u_int);
72 1.56 christos static void aclmode(char *, const FTSENT *);
73 1.5 mycroft
74 1.20 mycroft static time_t now;
75 1.20 mycroft
76 1.5 mycroft #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
77 1.5 mycroft
78 1.53 christos static int
79 1.53 christos safe_printpath(const FTSENT *p) {
80 1.53 christos int chcnt;
81 1.53 christos
82 1.53 christos if (f_fullpath) {
83 1.53 christos chcnt = safe_print(p->fts_path);
84 1.53 christos chcnt += safe_print("/");
85 1.53 christos } else
86 1.53 christos chcnt = 0;
87 1.53 christos return chcnt + safe_print(p->fts_name);
88 1.53 christos }
89 1.53 christos
90 1.53 christos static int
91 1.53 christos printescapedpath(const FTSENT *p) {
92 1.53 christos int chcnt;
93 1.53 christos
94 1.53 christos if (f_fullpath) {
95 1.53 christos chcnt = printescaped(p->fts_path);
96 1.53 christos chcnt += printescaped("/");
97 1.53 christos } else
98 1.53 christos chcnt = 0;
99 1.53 christos
100 1.53 christos return chcnt + printescaped(p->fts_name);
101 1.53 christos }
102 1.53 christos
103 1.53 christos static int
104 1.53 christos printpath(const FTSENT *p) {
105 1.53 christos if (f_fullpath)
106 1.53 christos return printf("%s/%s", p->fts_path, p->fts_name);
107 1.53 christos else
108 1.54 mlelstv return printf("%s", p->fts_name);
109 1.53 christos }
110 1.53 christos
111 1.5 mycroft void
112 1.30 lukem printscol(DISPLAY *dp)
113 1.1 cgd {
114 1.11 mycroft FTSENT *p;
115 1.5 mycroft
116 1.5 mycroft for (p = dp->list; p; p = p->fts_link) {
117 1.5 mycroft if (IS_NOPRINT(p))
118 1.5 mycroft continue;
119 1.5 mycroft (void)printaname(p, dp->s_inode, dp->s_block);
120 1.1 cgd (void)putchar('\n');
121 1.1 cgd }
122 1.1 cgd }
123 1.1 cgd
124 1.5 mycroft void
125 1.30 lukem printlong(DISPLAY *dp)
126 1.5 mycroft {
127 1.11 mycroft struct stat *sp;
128 1.11 mycroft FTSENT *p;
129 1.5 mycroft NAMES *np;
130 1.38 grant char buf[20], szbuf[5];
131 1.5 mycroft
132 1.22 mycroft now = time(NULL);
133 1.20 mycroft
134 1.53 christos if (!f_leafonly)
135 1.53 christos printtotal(dp); /* "total: %u\n" */
136 1.43 ahoka
137 1.5 mycroft for (p = dp->list; p; p = p->fts_link) {
138 1.5 mycroft if (IS_NOPRINT(p))
139 1.5 mycroft continue;
140 1.5 mycroft sp = p->fts_statp;
141 1.1 cgd if (f_inode)
142 1.55 martin (void)printf("%*"PRIu64" ", dp->s_inode, sp->st_ino);
143 1.41 jschauma if (f_size) {
144 1.41 jschauma if (f_humanize) {
145 1.41 jschauma if ((humanize_number(szbuf, sizeof(szbuf),
146 1.48 enami sp->st_blocks * S_BLKSIZE,
147 1.48 enami "", HN_AUTOSCALE,
148 1.48 enami (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
149 1.48 enami err(1, "humanize_number");
150 1.48 enami (void)printf("%*s ", dp->s_block, szbuf);
151 1.41 jschauma } else {
152 1.50 christos (void)printf(f_commas ? "%'*llu " : "%*llu ",
153 1.50 christos dp->s_block,
154 1.50 christos (unsigned long long)howmany(sp->st_blocks,
155 1.48 enami blocksize));
156 1.41 jschauma }
157 1.38 grant }
158 1.5 mycroft (void)strmode(sp->st_mode, buf);
159 1.56 christos aclmode(buf, p);
160 1.5 mycroft np = p->fts_pointer;
161 1.34 grant (void)printf("%s %*lu ", buf, dp->s_nlink,
162 1.34 grant (unsigned long)sp->st_nlink);
163 1.34 grant if (!f_grouponly)
164 1.34 grant (void)printf("%-*s ", dp->s_user, np->user);
165 1.34 grant (void)printf("%-*s ", dp->s_group, np->group);
166 1.5 mycroft if (f_flags)
167 1.5 mycroft (void)printf("%-*s ", dp->s_flags, np->flags);
168 1.5 mycroft if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
169 1.44 christos (void)printf("%*lld, %*lld ",
170 1.44 christos dp->s_major, (long long)major(sp->st_rdev),
171 1.44 christos dp->s_minor, (long long)minor(sp->st_rdev));
172 1.1 cgd else
173 1.38 grant if (f_humanize) {
174 1.38 grant if ((humanize_number(szbuf, sizeof(szbuf),
175 1.38 grant sp->st_size, "", HN_AUTOSCALE,
176 1.38 grant (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
177 1.38 grant err(1, "humanize_number");
178 1.38 grant (void)printf("%*s ", dp->s_size, szbuf);
179 1.38 grant } else {
180 1.50 christos (void)printf(f_commas ? "%'*llu " : "%*llu ",
181 1.50 christos dp->s_size, (unsigned long long)
182 1.50 christos sp->st_size);
183 1.38 grant }
184 1.1 cgd if (f_accesstime)
185 1.5 mycroft printtime(sp->st_atime);
186 1.1 cgd else if (f_statustime)
187 1.5 mycroft printtime(sp->st_ctime);
188 1.1 cgd else
189 1.5 mycroft printtime(sp->st_mtime);
190 1.37 jschauma if (f_octal || f_octal_escape)
191 1.53 christos (void)safe_printpath(p);
192 1.36 jschauma else if (f_nonprint)
193 1.53 christos (void)printescapedpath(p);
194 1.28 assar else
195 1.53 christos (void)printpath(p);
196 1.28 assar
197 1.26 kleink if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
198 1.5 mycroft (void)printtype(sp->st_mode);
199 1.5 mycroft if (S_ISLNK(sp->st_mode))
200 1.5 mycroft printlink(p);
201 1.1 cgd (void)putchar('\n');
202 1.1 cgd }
203 1.1 cgd }
204 1.1 cgd
205 1.5 mycroft void
206 1.30 lukem printcol(DISPLAY *dp)
207 1.1 cgd {
208 1.5 mycroft static FTSENT **array;
209 1.5 mycroft static int lastentries = -1;
210 1.11 mycroft FTSENT *p;
211 1.16 christos int base, chcnt, col, colwidth, num;
212 1.15 thorpej int numcols, numrows, row;
213 1.1 cgd
214 1.19 lukem colwidth = dp->maxlen;
215 1.19 lukem if (f_inode)
216 1.19 lukem colwidth += dp->s_inode + 1;
217 1.38 grant if (f_size) {
218 1.38 grant if (f_humanize)
219 1.38 grant colwidth += dp->s_size + 1;
220 1.38 grant else
221 1.38 grant colwidth += dp->s_block + 1;
222 1.38 grant }
223 1.26 kleink if (f_type || f_typedir)
224 1.19 lukem colwidth += 1;
225 1.19 lukem
226 1.19 lukem colwidth += 1;
227 1.19 lukem
228 1.19 lukem if (termwidth < 2 * colwidth) {
229 1.19 lukem printscol(dp);
230 1.19 lukem return;
231 1.19 lukem }
232 1.19 lukem
233 1.5 mycroft /*
234 1.5 mycroft * Have to do random access in the linked list -- build a table
235 1.5 mycroft * of pointers.
236 1.5 mycroft */
237 1.5 mycroft if (dp->entries > lastentries) {
238 1.51 yamt FTSENT **newarray;
239 1.51 yamt
240 1.51 yamt newarray = realloc(array, dp->entries * sizeof(FTSENT *));
241 1.51 yamt if (newarray == NULL) {
242 1.27 drochner warn(NULL);
243 1.5 mycroft printscol(dp);
244 1.51 yamt return;
245 1.5 mycroft }
246 1.51 yamt lastentries = dp->entries;
247 1.51 yamt array = newarray;
248 1.5 mycroft }
249 1.5 mycroft for (p = dp->list, num = 0; p; p = p->fts_link)
250 1.5 mycroft if (p->fts_number != NO_PRINT)
251 1.5 mycroft array[num++] = p;
252 1.5 mycroft
253 1.19 lukem numcols = termwidth / colwidth;
254 1.19 lukem colwidth = termwidth / numcols; /* spread out if possible */
255 1.19 lukem numrows = num / numcols;
256 1.19 lukem if (num % numcols)
257 1.19 lukem ++numrows;
258 1.19 lukem
259 1.43 ahoka printtotal(dp); /* "total: %u\n" */
260 1.43 ahoka
261 1.19 lukem for (row = 0; row < numrows; ++row) {
262 1.19 lukem for (base = row, chcnt = col = 0; col < numcols; ++col) {
263 1.19 lukem chcnt = printaname(array[base], dp->s_inode,
264 1.38 grant f_humanize ? dp->s_size : dp->s_block);
265 1.19 lukem if ((base += numrows) >= num)
266 1.19 lukem break;
267 1.19 lukem while (chcnt++ < colwidth)
268 1.22 mycroft (void)putchar(' ');
269 1.19 lukem }
270 1.19 lukem (void)putchar('\n');
271 1.19 lukem }
272 1.19 lukem }
273 1.19 lukem
274 1.19 lukem void
275 1.30 lukem printacol(DISPLAY *dp)
276 1.19 lukem {
277 1.19 lukem FTSENT *p;
278 1.19 lukem int chcnt, col, colwidth;
279 1.19 lukem int numcols;
280 1.19 lukem
281 1.5 mycroft colwidth = dp->maxlen;
282 1.1 cgd if (f_inode)
283 1.5 mycroft colwidth += dp->s_inode + 1;
284 1.38 grant if (f_size) {
285 1.38 grant if (f_humanize)
286 1.38 grant colwidth += dp->s_size + 1;
287 1.38 grant else
288 1.38 grant colwidth += dp->s_block + 1;
289 1.38 grant }
290 1.26 kleink if (f_type || f_typedir)
291 1.1 cgd colwidth += 1;
292 1.1 cgd
293 1.15 thorpej colwidth += 1;
294 1.15 thorpej
295 1.1 cgd if (termwidth < 2 * colwidth) {
296 1.5 mycroft printscol(dp);
297 1.1 cgd return;
298 1.1 cgd }
299 1.1 cgd
300 1.1 cgd numcols = termwidth / colwidth;
301 1.15 thorpej colwidth = termwidth / numcols; /* spread out if possible */
302 1.1 cgd
303 1.43 ahoka printtotal(dp); /* "total: %u\n" */
304 1.43 ahoka
305 1.19 lukem chcnt = col = 0;
306 1.19 lukem for (p = dp->list; p; p = p->fts_link) {
307 1.19 lukem if (IS_NOPRINT(p))
308 1.19 lukem continue;
309 1.19 lukem if (col >= numcols) {
310 1.19 lukem chcnt = col = 0;
311 1.22 mycroft (void)putchar('\n');
312 1.1 cgd }
313 1.38 grant chcnt = printaname(p, dp->s_inode,
314 1.38 grant f_humanize ? dp->s_size : dp->s_block);
315 1.19 lukem while (chcnt++ < colwidth)
316 1.22 mycroft (void)putchar(' ');
317 1.19 lukem col++;
318 1.25 kleink }
319 1.25 kleink (void)putchar('\n');
320 1.25 kleink }
321 1.25 kleink
322 1.25 kleink void
323 1.30 lukem printstream(DISPLAY *dp)
324 1.25 kleink {
325 1.25 kleink FTSENT *p;
326 1.25 kleink int col;
327 1.25 kleink int extwidth;
328 1.25 kleink
329 1.25 kleink extwidth = 0;
330 1.25 kleink if (f_inode)
331 1.25 kleink extwidth += dp->s_inode + 1;
332 1.38 grant if (f_size) {
333 1.38 grant if (f_humanize)
334 1.38 grant extwidth += dp->s_size + 1;
335 1.38 grant else
336 1.38 grant extwidth += dp->s_block + 1;
337 1.38 grant }
338 1.25 kleink if (f_type)
339 1.25 kleink extwidth += 1;
340 1.25 kleink
341 1.25 kleink for (col = 0, p = dp->list; p != NULL; p = p->fts_link) {
342 1.25 kleink if (IS_NOPRINT(p))
343 1.25 kleink continue;
344 1.25 kleink if (col > 0) {
345 1.25 kleink (void)putchar(','), col++;
346 1.45 lukem if (col + 1 + extwidth + (int)p->fts_namelen >= termwidth)
347 1.25 kleink (void)putchar('\n'), col = 0;
348 1.25 kleink else
349 1.25 kleink (void)putchar(' '), col++;
350 1.25 kleink }
351 1.38 grant col += printaname(p, dp->s_inode,
352 1.38 grant f_humanize ? dp->s_size : dp->s_block);
353 1.1 cgd }
354 1.22 mycroft (void)putchar('\n');
355 1.1 cgd }
356 1.1 cgd
357 1.1 cgd /*
358 1.1 cgd * print [inode] [size] name
359 1.5 mycroft * return # of characters printed, no trailing characters.
360 1.1 cgd */
361 1.5 mycroft static int
362 1.30 lukem printaname(FTSENT *p, int inodefield, int sizefield)
363 1.1 cgd {
364 1.5 mycroft struct stat *sp;
365 1.1 cgd int chcnt;
366 1.38 grant char szbuf[5];
367 1.1 cgd
368 1.5 mycroft sp = p->fts_statp;
369 1.1 cgd chcnt = 0;
370 1.1 cgd if (f_inode)
371 1.55 martin chcnt += printf("%*"PRIu64" ", inodefield, sp->st_ino);
372 1.38 grant if (f_size) {
373 1.38 grant if (f_humanize) {
374 1.38 grant if ((humanize_number(szbuf, sizeof(szbuf), sp->st_size,
375 1.38 grant "", HN_AUTOSCALE,
376 1.38 grant (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
377 1.38 grant err(1, "humanize_number");
378 1.38 grant chcnt += printf("%*s ", sizefield, szbuf);
379 1.38 grant } else {
380 1.50 christos chcnt += printf(f_commas ? "%'*llu " : "%*llu ",
381 1.50 christos sizefield, (unsigned long long)
382 1.50 christos howmany(sp->st_blocks, blocksize));
383 1.38 grant }
384 1.38 grant }
385 1.37 jschauma if (f_octal || f_octal_escape)
386 1.53 christos chcnt += safe_printpath(p);
387 1.36 jschauma else if (f_nonprint)
388 1.53 christos chcnt += printescapedpath(p);
389 1.29 assar else
390 1.53 christos chcnt += printpath(p);
391 1.26 kleink if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
392 1.5 mycroft chcnt += printtype(sp->st_mode);
393 1.5 mycroft return (chcnt);
394 1.1 cgd }
395 1.1 cgd
396 1.5 mycroft static void
397 1.30 lukem printtime(time_t ftime)
398 1.1 cgd {
399 1.1 cgd int i;
400 1.46 christos const char *longstring;
401 1.1 cgd
402 1.47 christos if ((longstring = ctime(&ftime)) == NULL) {
403 1.46 christos /* 012345678901234567890123 */
404 1.46 christos longstring = "????????????????????????";
405 1.46 christos }
406 1.1 cgd for (i = 4; i < 11; ++i)
407 1.1 cgd (void)putchar(longstring[i]);
408 1.1 cgd
409 1.1 cgd #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
410 1.1 cgd if (f_sectime)
411 1.1 cgd for (i = 11; i < 24; i++)
412 1.1 cgd (void)putchar(longstring[i]);
413 1.40 mycroft else if (ftime + SIXMONTHS > now && ftime - SIXMONTHS < now)
414 1.1 cgd for (i = 11; i < 16; ++i)
415 1.1 cgd (void)putchar(longstring[i]);
416 1.1 cgd else {
417 1.1 cgd (void)putchar(' ');
418 1.1 cgd for (i = 20; i < 24; ++i)
419 1.1 cgd (void)putchar(longstring[i]);
420 1.1 cgd }
421 1.1 cgd (void)putchar(' ');
422 1.1 cgd }
423 1.1 cgd
424 1.43 ahoka /*
425 1.43 ahoka * Display total used disk space in the form "total: %u\n".
426 1.43 ahoka * Note: POSIX (IEEE Std 1003.1-2001) says this should be always in 512 blocks,
427 1.49 erh * but we humanise it with -h, or separate it with commas with -M, and use 1024
428 1.49 erh * with -k.
429 1.43 ahoka */
430 1.43 ahoka static void
431 1.43 ahoka printtotal(DISPLAY *dp)
432 1.43 ahoka {
433 1.43 ahoka char szbuf[5];
434 1.43 ahoka
435 1.43 ahoka if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) {
436 1.43 ahoka if (f_humanize) {
437 1.43 ahoka if ((humanize_number(szbuf, sizeof(szbuf), (int64_t)dp->stotal,
438 1.43 ahoka "", HN_AUTOSCALE,
439 1.43 ahoka (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
440 1.43 ahoka err(1, "humanize_number");
441 1.43 ahoka (void)printf("total %s\n", szbuf);
442 1.43 ahoka } else {
443 1.50 christos (void)printf(f_commas ? "total %'llu\n" :
444 1.50 christos "total %llu\n", (unsigned long long)
445 1.50 christos howmany(dp->btotal, blocksize));
446 1.43 ahoka }
447 1.43 ahoka }
448 1.43 ahoka }
449 1.43 ahoka
450 1.5 mycroft static int
451 1.30 lukem printtype(u_int mode)
452 1.1 cgd {
453 1.11 mycroft switch (mode & S_IFMT) {
454 1.1 cgd case S_IFDIR:
455 1.1 cgd (void)putchar('/');
456 1.5 mycroft return (1);
457 1.11 mycroft case S_IFIFO:
458 1.11 mycroft (void)putchar('|');
459 1.11 mycroft return (1);
460 1.1 cgd case S_IFLNK:
461 1.1 cgd (void)putchar('@');
462 1.5 mycroft return (1);
463 1.1 cgd case S_IFSOCK:
464 1.1 cgd (void)putchar('=');
465 1.12 mycroft return (1);
466 1.12 mycroft case S_IFWHT:
467 1.12 mycroft (void)putchar('%');
468 1.6 jtc return (1);
469 1.1 cgd }
470 1.1 cgd if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
471 1.1 cgd (void)putchar('*');
472 1.5 mycroft return (1);
473 1.1 cgd }
474 1.5 mycroft return (0);
475 1.1 cgd }
476 1.1 cgd
477 1.5 mycroft static void
478 1.30 lukem printlink(FTSENT *p)
479 1.1 cgd {
480 1.1 cgd int lnklen;
481 1.5 mycroft char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
482 1.5 mycroft
483 1.5 mycroft if (p->fts_level == FTS_ROOTLEVEL)
484 1.5 mycroft (void)snprintf(name, sizeof(name), "%s", p->fts_name);
485 1.33 enami else
486 1.11 mycroft (void)snprintf(name, sizeof(name),
487 1.11 mycroft "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
488 1.11 mycroft if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
489 1.1 cgd (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
490 1.1 cgd return;
491 1.1 cgd }
492 1.1 cgd path[lnklen] = '\0';
493 1.28 assar (void)printf(" -> ");
494 1.37 jschauma if (f_octal || f_octal_escape)
495 1.36 jschauma (void)safe_print(path);
496 1.36 jschauma else if (f_nonprint)
497 1.36 jschauma (void)printescaped(path);
498 1.28 assar else
499 1.28 assar (void)printf("%s", path);
500 1.1 cgd }
501 1.56 christos
502 1.56 christos /*
503 1.56 christos * Add a + after the standard rwxrwxrwx mode if the file has an
504 1.56 christos * ACL. strmode() reserves space at the end of the string.
505 1.56 christos */
506 1.56 christos static void
507 1.56 christos aclmode(char *buf, const FTSENT *p)
508 1.56 christos {
509 1.56 christos char name[MAXPATHLEN + 1];
510 1.56 christos int ret, trivial;
511 1.56 christos static dev_t previous_dev = NODEV;
512 1.56 christos static int supports_acls = -1;
513 1.56 christos static int type = ACL_TYPE_ACCESS;
514 1.56 christos acl_t facl;
515 1.56 christos
516 1.56 christos /*
517 1.56 christos * XXX: ACLs are not supported on whiteouts and device files
518 1.56 christos * residing on UFS.
519 1.56 christos */
520 1.56 christos if (S_ISCHR(p->fts_statp->st_mode) || S_ISBLK(p->fts_statp->st_mode) ||
521 1.56 christos S_ISWHT(p->fts_statp->st_mode))
522 1.56 christos return;
523 1.56 christos
524 1.56 christos if (previous_dev == p->fts_statp->st_dev && supports_acls == 0)
525 1.56 christos return;
526 1.56 christos
527 1.56 christos if (p->fts_level == FTS_ROOTLEVEL)
528 1.56 christos snprintf(name, sizeof(name), "%s", p->fts_name);
529 1.56 christos else
530 1.56 christos snprintf(name, sizeof(name), "%s/%s",
531 1.56 christos p->fts_parent->fts_accpath, p->fts_name);
532 1.56 christos
533 1.56 christos if (supports_acls == -1 || previous_dev != p->fts_statp->st_dev) {
534 1.56 christos previous_dev = p->fts_statp->st_dev;
535 1.56 christos supports_acls = 0;
536 1.56 christos
537 1.56 christos ret = lpathconf(name, _PC_ACL_NFS4);
538 1.56 christos if (ret > 0) {
539 1.56 christos type = ACL_TYPE_NFS4;
540 1.56 christos supports_acls = 1;
541 1.56 christos } else if (ret < 0 && errno != EINVAL) {
542 1.56 christos warn("%s", name);
543 1.56 christos return;
544 1.56 christos }
545 1.56 christos if (supports_acls == 0) {
546 1.56 christos ret = lpathconf(name, _PC_ACL_EXTENDED);
547 1.56 christos if (ret > 0) {
548 1.56 christos type = ACL_TYPE_ACCESS;
549 1.56 christos supports_acls = 1;
550 1.56 christos } else if (ret < 0 && errno != EINVAL) {
551 1.56 christos warn("%s", name);
552 1.56 christos return;
553 1.56 christos }
554 1.56 christos }
555 1.56 christos }
556 1.56 christos if (supports_acls == 0)
557 1.56 christos return;
558 1.56 christos facl = acl_get_link_np(name, type);
559 1.56 christos if (facl == NULL) {
560 1.56 christos warn("%s", name);
561 1.56 christos return;
562 1.56 christos }
563 1.56 christos if (acl_is_trivial_np(facl, &trivial)) {
564 1.56 christos acl_free(facl);
565 1.56 christos warn("%s", name);
566 1.56 christos return;
567 1.56 christos }
568 1.56 christos if (!trivial)
569 1.56 christos buf[10] = '+';
570 1.56 christos acl_free(facl);
571 1.56 christos }
572