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