file_cmds-251.tar.gz
[apple/file_cmds.git] / ls / print.c
1 /*
2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Michael Fischbein.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #if 0
38 #ifndef lint
39 static char sccsid[] = "@(#)print.c 8.4 (Berkeley) 4/17/94";
40 #endif /* not lint */
41 #endif
42 #include <sys/cdefs.h>
43 __RCSID("$FreeBSD: src/bin/ls/print.c,v 1.57 2002/08/29 14:29:09 keramida Exp $");
44
45 #include <sys/param.h>
46 #include <sys/stat.h>
47 #ifdef __APPLE__
48 #include <sys/acl.h>
49 #include <sys/xattr.h>
50 #include <sys/types.h>
51 #include <grp.h>
52 #include <pwd.h>
53 #include <TargetConditionals.h>
54 #if !TARGET_OS_EMBEDDED
55 #include <membership.h>
56 #include <membershipPriv.h>
57 #endif
58 #include <uuid/uuid.h>
59 #endif
60
61 #include <err.h>
62 #include <errno.h>
63 #include <fts.h>
64 #include <math.h>
65 #include <langinfo.h>
66 #include <libutil.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <time.h>
71 #include <unistd.h>
72 #ifdef COLORLS
73 #include <ctype.h>
74 #include <termcap.h>
75 #include <signal.h>
76 #endif
77 #include <stdint.h> /* intmax_t */
78 #include <assert.h>
79 #ifdef __APPLE__
80 #include <get_compat.h>
81 #else
82 #define COMPAT_MODE(a,b) (1)
83 #endif /* __APPLE__ */
84
85 #include "ls.h"
86 #include "extern.h"
87
88 static int printaname(FTSENT *, u_long, u_long);
89 static void printlink(FTSENT *);
90 static void printtime(time_t);
91 static int printtype(u_int);
92 static void printsize(size_t, off_t);
93 #ifdef COLORLS
94 static void endcolor(int);
95 static int colortype(mode_t);
96 #endif
97
98 #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
99
100 #ifdef COLORLS
101 /* Most of these are taken from <sys/stat.h> */
102 typedef enum Colors {
103 C_DIR, /* directory */
104 C_LNK, /* symbolic link */
105 C_SOCK, /* socket */
106 C_FIFO, /* pipe */
107 C_EXEC, /* executable */
108 C_BLK, /* block special */
109 C_CHR, /* character special */
110 C_SUID, /* setuid executable */
111 C_SGID, /* setgid executable */
112 C_WSDIR, /* directory writeble to others, with sticky
113 * bit */
114 C_WDIR, /* directory writeble to others, without
115 * sticky bit */
116 C_NUMCOLORS /* just a place-holder */
117 } Colors;
118
119 static const char *defcolors = "exfxcxdxbxegedabagacad";
120
121 /* colors for file types */
122 static struct {
123 int num[2];
124 int bold;
125 } colors[C_NUMCOLORS];
126 #endif
127
128 void
129 printscol(DISPLAY *dp)
130 {
131 FTSENT *p;
132
133 assert(dp);
134 if (COMPAT_MODE("bin/ls", "Unix2003") && (dp->list != NULL)) {
135 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
136 (void)printf("total %qu\n", (u_int64_t)howmany(dp->btotal, blocksize));
137 }
138
139 for (p = dp->list; p; p = p->fts_link) {
140 if (IS_NOPRINT(p))
141 continue;
142 (void)printaname(p, dp->s_inode, dp->s_block);
143 (void)putchar('\n');
144 }
145 }
146
147 /*
148 * print name in current style
149 */
150 static int
151 printname(const char *name)
152 {
153 if (f_octal || f_octal_escape)
154 return prn_octal(name);
155 else if (f_nonprint)
156 return prn_printable(name);
157 else
158 return prn_normal(name);
159 }
160
161 /*
162 * print access control list
163 */
164 static struct {
165 acl_perm_t perm;
166 char *name;
167 int flags;
168 #define ACL_PERM_DIR (1<<0)
169 #define ACL_PERM_FILE (1<<1)
170 } acl_perms[] = {
171 {ACL_READ_DATA, "read", ACL_PERM_FILE},
172 {ACL_LIST_DIRECTORY, "list", ACL_PERM_DIR},
173 {ACL_WRITE_DATA, "write", ACL_PERM_FILE},
174 {ACL_ADD_FILE, "add_file", ACL_PERM_DIR},
175 {ACL_EXECUTE, "execute", ACL_PERM_FILE},
176 {ACL_SEARCH, "search", ACL_PERM_DIR},
177 {ACL_DELETE, "delete", ACL_PERM_FILE | ACL_PERM_DIR},
178 {ACL_APPEND_DATA, "append", ACL_PERM_FILE},
179 {ACL_ADD_SUBDIRECTORY, "add_subdirectory", ACL_PERM_DIR},
180 {ACL_DELETE_CHILD, "delete_child", ACL_PERM_DIR},
181 {ACL_READ_ATTRIBUTES, "readattr", ACL_PERM_FILE | ACL_PERM_DIR},
182 {ACL_WRITE_ATTRIBUTES, "writeattr", ACL_PERM_FILE | ACL_PERM_DIR},
183 {ACL_READ_EXTATTRIBUTES, "readextattr", ACL_PERM_FILE | ACL_PERM_DIR},
184 {ACL_WRITE_EXTATTRIBUTES, "writeextattr", ACL_PERM_FILE | ACL_PERM_DIR},
185 {ACL_READ_SECURITY, "readsecurity", ACL_PERM_FILE | ACL_PERM_DIR},
186 {ACL_WRITE_SECURITY, "writesecurity", ACL_PERM_FILE | ACL_PERM_DIR},
187 {ACL_CHANGE_OWNER, "chown", ACL_PERM_FILE | ACL_PERM_DIR},
188 {0, NULL, 0}
189 };
190
191 static struct {
192 acl_flag_t flag;
193 char *name;
194 int flags;
195 } acl_flags[] = {
196 {ACL_ENTRY_FILE_INHERIT, "file_inherit", ACL_PERM_DIR},
197 {ACL_ENTRY_DIRECTORY_INHERIT, "directory_inherit", ACL_PERM_DIR},
198 {ACL_ENTRY_LIMIT_INHERIT, "limit_inherit", ACL_PERM_FILE | ACL_PERM_DIR},
199 {ACL_ENTRY_ONLY_INHERIT, "only_inherit", ACL_PERM_DIR},
200 {0, NULL, 0}
201 };
202
203 static char *
204 uuid_to_name(uuid_t *uu)
205 {
206 #if TARGET_OS_EMBEDDED
207 return strdup("<UNKNOWN>");
208 #else /* !TARGET_OS_EMBEDDED */
209 int type;
210 char *name = NULL;
211 char *recname = NULL;
212
213 #define MAXNAMETAG (MAXLOGNAME + 6) /* + strlen("group:") */
214 name = (char *) malloc(MAXNAMETAG);
215
216 if (NULL == name) {
217 err(1, "malloc");
218 }
219
220 if (f_numericonly) {
221 goto errout;
222 }
223
224 if (mbr_identifier_translate(ID_TYPE_UUID, *uu, sizeof(*uu), ID_TYPE_NAME, (void **) &recname, &type)) {
225 goto errout;
226 }
227
228 snprintf(name, MAXNAMETAG, "%s:%s", (type == MBR_REC_TYPE_USER ? "user" : "group"), recname);
229 free(recname);
230
231 return name;
232 errout:
233 uuid_unparse_upper(*uu, name);
234
235 return name;
236 #endif /* !TARGET_OS_EMBEDDED */
237 }
238
239 static void
240 printxattr(DISPLAY *dp, int count, char *buf, int sizes[])
241 {
242 for (int i = 0; i < count; i++) {
243 putchar('\t');
244 printname(buf);
245 putchar('\t');
246 printsize(dp->s_size, sizes[i]);
247 putchar('\n');
248 buf += strlen(buf) + 1;
249 }
250 }
251
252 static void
253 printacl(acl_t acl, int isdir)
254 {
255 acl_entry_t entry = NULL;
256 int index;
257 uuid_t *applicable;
258 char *name = NULL;
259 acl_tag_t tag;
260 acl_flagset_t flags;
261 acl_permset_t perms;
262 char *type;
263 int i, first;
264
265
266 for (index = 0;
267 acl_get_entry(acl, entry == NULL ? ACL_FIRST_ENTRY : ACL_NEXT_ENTRY, &entry) == 0;
268 index++) {
269 if (acl_get_tag_type(entry, &tag) != 0)
270 continue;
271 if (acl_get_flagset_np(entry, &flags) != 0)
272 continue;
273 if (acl_get_permset(entry, &perms) != 0)
274 continue;
275 if ((applicable = (uuid_t *) acl_get_qualifier(entry)) == NULL)
276 continue;
277 name = uuid_to_name(applicable);
278 acl_free(applicable);
279 switch(tag) {
280 case ACL_EXTENDED_ALLOW:
281 type = "allow";
282 break;
283 case ACL_EXTENDED_DENY:
284 type = "deny";
285 break;
286 default:
287 type = "unknown";
288 }
289
290 (void)printf(" %d: %s%s %s ",
291 index,
292 name,
293 acl_get_flag_np(flags, ACL_ENTRY_INHERITED) ? " inherited" : "",
294 type);
295
296 if (name)
297 free(name);
298
299 for (i = 0, first = 0; acl_perms[i].name != NULL; i++) {
300 if (acl_get_perm_np(perms, acl_perms[i].perm) == 0)
301 continue;
302 if (!(acl_perms[i].flags & (isdir ? ACL_PERM_DIR : ACL_PERM_FILE)))
303 continue;
304 (void)printf("%s%s", first++ ? "," : "", acl_perms[i].name);
305 }
306 for (i = 0; acl_flags[i].name != NULL; i++) {
307 if (acl_get_flag_np(flags, acl_flags[i].flag) == 0)
308 continue;
309 if (!(acl_flags[i].flags & (isdir ? ACL_PERM_DIR : ACL_PERM_FILE)))
310 continue;
311 (void)printf("%s%s", first++ ? "," : "", acl_flags[i].name);
312 }
313
314 (void)putchar('\n');
315 }
316
317 }
318
319 void
320 printlong(DISPLAY *dp)
321 {
322 struct stat *sp;
323 FTSENT *p;
324 NAMES *np;
325 char buf[20];
326 #ifdef COLORLS
327 int color_printed = 0;
328 #endif
329
330 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
331 (void)printf("total %qu\n", (u_int64_t)howmany(dp->btotal, blocksize));
332
333 for (p = dp->list; p; p = p->fts_link) {
334 if (IS_NOPRINT(p))
335 continue;
336 sp = p->fts_statp;
337 if (f_inode)
338 #if _DARWIN_FEATURE_64_BIT_INODE
339 (void)printf("%*llu ", dp->s_inode, (u_quad_t)sp->st_ino);
340 #else
341 (void)printf("%*lu ", dp->s_inode, (u_long)sp->st_ino);
342 #endif
343 if (f_size)
344 (void)printf("%*qu ",
345 dp->s_block, (u_int64_t)howmany(sp->st_blocks, blocksize));
346 strmode(sp->st_mode, buf);
347 np = p->fts_pointer;
348 #ifdef __APPLE__
349 buf[10] = '\0'; /* make +/@ abut the mode */
350 char str[2] = { np->mode_suffix, '\0' };
351 #endif /* __APPLE__ */
352 if (f_group && f_owner) { /* means print neither */
353 #ifdef __APPLE__
354 (void)printf("%s%s %*u ", buf, str, dp->s_nlink,
355 sp->st_nlink);
356 #else /* ! __APPLE__ */
357 (void)printf("%s %*u ", buf, dp->s_nlink,
358 sp->st_nlink);
359 #endif /* __APPLE__ */
360 }
361 else if (f_group) {
362 #ifdef __APPLE__
363 (void)printf("%s%s %*u %-*s ", buf, str, dp->s_nlink,
364 sp->st_nlink, dp->s_group, np->group);
365 #else /* ! __APPLE__ */
366 (void)printf("%s %*u %-*s ", buf, dp->s_nlink,
367 sp->st_nlink, dp->s_group, np->group);
368 #endif /* __APPLE__ */
369 }
370 else if (f_owner) {
371 #ifdef __APPLE__
372 (void)printf("%s%s %*u %-*s ", buf, str, dp->s_nlink,
373 sp->st_nlink, dp->s_user, np->user);
374 #else /* ! __APPLE__ */
375 (void)printf("%s %*u %-*s ", buf, dp->s_nlink,
376 sp->st_nlink, dp->s_user, np->user);
377 #endif /* __APPLE__ */
378 }
379 else {
380 #ifdef __APPLE__
381 (void)printf("%s%s %*u %-*s %-*s ", buf, str, dp->s_nlink,
382 sp->st_nlink, dp->s_user, np->user, dp->s_group,
383 np->group);
384 #else /* ! __APPLE__ */
385 (void)printf("%s %*u %-*s %-*s ", buf, dp->s_nlink,
386 sp->st_nlink, dp->s_user, np->user, dp->s_group,
387 np->group);
388 #endif /* ! __APPLE__ */
389 }
390 if (f_flags)
391 (void)printf("%-*s ", dp->s_flags, np->flags);
392 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
393 if (minor(sp->st_rdev) > 255 || minor(sp->st_rdev) < 0)
394 (void)printf("%3d, 0x%08x ",
395 major(sp->st_rdev),
396 (u_int)minor(sp->st_rdev));
397 else
398 (void)printf("%3d, %3d ",
399 major(sp->st_rdev), minor(sp->st_rdev));
400 else if (dp->bcfile)
401 (void)printf("%*s%*qu ",
402 8 - dp->s_size, "", dp->s_size, (u_int64_t)sp->st_size);
403 else
404 printsize(dp->s_size, sp->st_size);
405 if (f_accesstime)
406 printtime(sp->st_atime);
407 else if (f_statustime)
408 printtime(sp->st_ctime);
409 else if (f_birthtime)
410 printtime(sp->st_birthtime);
411 else
412 printtime(sp->st_mtime);
413 #ifdef COLORLS
414 if (f_color)
415 color_printed = colortype(sp->st_mode);
416 #endif
417 (void)printname(p->fts_name);
418 #ifdef COLORLS
419 if (f_color && color_printed)
420 endcolor(0);
421 #endif
422 if (f_type)
423 (void)printtype(sp->st_mode);
424 if (S_ISLNK(sp->st_mode))
425 printlink(p);
426 (void)putchar('\n');
427 #ifdef __APPLE__
428 if (np->xattr_count && f_xattr) {
429 printxattr(dp, np->xattr_count, np->xattr_names, np->xattr_sizes);
430 }
431 if (np->acl != NULL && f_acl) {
432 printacl(np->acl, S_ISDIR(sp->st_mode));
433 }
434 #endif /* __APPLE__ */
435 }
436 }
437
438 void
439 printstream(DISPLAY *dp)
440 {
441 FTSENT *p;
442 extern int termwidth;
443 int chcnt;
444
445 for (p = dp->list, chcnt = 0; p; p = p->fts_link) {
446 if (p->fts_number == NO_PRINT)
447 continue;
448 if (strlen(p->fts_name) + chcnt +
449 (p->fts_link ? 2 : 0) >= (unsigned)termwidth) {
450 putchar('\n');
451 chcnt = 0;
452 }
453 chcnt += printaname(p, dp->s_inode, dp->s_block);
454 if (p->fts_link) {
455 printf(", ");
456 chcnt += 2;
457 }
458 }
459 if (chcnt)
460 putchar('\n');
461 }
462
463 void
464 printcol(DISPLAY *dp)
465 {
466 extern int termwidth;
467 static FTSENT **array;
468 static int lastentries = -1;
469 FTSENT *p;
470 int base;
471 int chcnt;
472 int cnt;
473 int col;
474 int colwidth;
475 int endcol;
476 int num;
477 int numcols;
478 int numrows;
479 int row;
480 int tabwidth;
481
482 if (f_notabs)
483 tabwidth = 1;
484 else
485 tabwidth = 8;
486
487 /*
488 * Have to do random access in the linked list -- build a table
489 * of pointers.
490 */
491 if ((lastentries == -1) || (dp->entries > lastentries)) {
492 lastentries = dp->entries;
493 if ((array = realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
494 warn(NULL);
495 printscol(dp);
496 return;
497 }
498 }
499 memset(array, 0, dp->entries * sizeof(FTSENT *));
500 for (p = dp->list, num = 0; p; p = p->fts_link)
501 if (p->fts_number != NO_PRINT)
502 array[num++] = p;
503
504 colwidth = dp->maxlen;
505 if (f_inode)
506 colwidth += dp->s_inode + 1;
507 if (f_size)
508 colwidth += dp->s_block + 1;
509 if (f_type)
510 colwidth += 1;
511
512 colwidth = (colwidth + tabwidth) & ~(tabwidth - 1);
513 if (termwidth < 2 * colwidth) {
514 printscol(dp);
515 return;
516 }
517 numcols = termwidth / colwidth;
518 numrows = num / numcols;
519 if (num % numcols)
520 ++numrows;
521
522 assert(dp->list);
523 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
524 (void)printf("total %qu\n", (u_int64_t)howmany(dp->btotal, blocksize));
525
526 base = 0;
527 for (row = 0; row < numrows; ++row) {
528 endcol = colwidth;
529 if (!f_sortacross)
530 base = row;
531 for (col = 0, chcnt = 0; col < numcols; ++col) {
532 assert(base < dp->entries);
533 chcnt += printaname(array[base], dp->s_inode, dp->s_block);
534 if (f_sortacross)
535 base++;
536 else
537 base += numrows;
538 if (base >= num)
539 break;
540 while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
541 <= endcol) {
542 if (f_sortacross && col + 1 >= numcols)
543 break;
544 (void)putchar(f_notabs ? ' ' : '\t');
545 chcnt = cnt;
546 }
547 endcol += colwidth;
548 }
549 (void)putchar('\n');
550 }
551 }
552
553 /*
554 * print [inode] [size] name
555 * return # of characters printed, no trailing characters.
556 */
557 static int
558 printaname(FTSENT *p, u_long inodefield, u_long sizefield)
559 {
560 struct stat *sp;
561 int chcnt;
562 #ifdef COLORLS
563 int color_printed = 0;
564 #endif
565
566 sp = p->fts_statp;
567 chcnt = 0;
568 if (f_inode)
569 #if _DARWIN_FEATURE_64_BIT_INODE
570 chcnt += printf("%*llu ", (int)inodefield, (u_quad_t)sp->st_ino);
571 #else
572 chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino);
573 #endif
574 if (f_size)
575 chcnt += printf("%*qu ",
576 (int)sizefield, (u_int64_t)howmany(sp->st_blocks, blocksize));
577 #ifdef COLORLS
578 if (f_color)
579 color_printed = colortype(sp->st_mode);
580 #endif
581 chcnt += printname(p->fts_name);
582 #ifdef COLORLS
583 if (f_color && color_printed)
584 endcolor(0);
585 #endif
586 if (f_type)
587 chcnt += printtype(sp->st_mode);
588 return (chcnt);
589 }
590
591 static void
592 printtime(time_t ftime)
593 {
594 char longstring[80];
595 static time_t now;
596 const char *format;
597 static int d_first = -1;
598
599 if (d_first < 0)
600 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
601 if (now == 0)
602 now = time(NULL);
603
604 #define SIXMONTHS ((365 / 2) * 86400)
605 if (f_sectime)
606 /* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */
607 format = d_first ? "%e %b %T %Y " : "%b %e %T %Y ";
608 else if (COMPAT_MODE("bin/ls", "Unix2003")) {
609 if (ftime + SIXMONTHS > now && ftime <= now)
610 /* mmm dd hh:mm || dd mmm hh:mm */
611 format = d_first ? "%e %b %R " : "%b %e %R ";
612 else
613 /* mmm dd yyyy || dd mmm yyyy */
614 format = d_first ? "%e %b %Y " : "%b %e %Y ";
615 }
616 else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
617 /* mmm dd hh:mm || dd mmm hh:mm */
618 format = d_first ? "%e %b %R " : "%b %e %R ";
619 else
620 /* mmm dd yyyy || dd mmm yyyy */
621 format = d_first ? "%e %b %Y " : "%b %e %Y ";
622 strftime(longstring, sizeof(longstring), format, localtime(&ftime));
623 fputs(longstring, stdout);
624 }
625
626 static int
627 printtype(u_int mode)
628 {
629
630 if (f_slash) {
631 if ((mode & S_IFMT) == S_IFDIR) {
632 (void)putchar('/');
633 return (1);
634 }
635 return (0);
636 }
637
638 switch (mode & S_IFMT) {
639 case S_IFDIR:
640 (void)putchar('/');
641 return (1);
642 case S_IFIFO:
643 (void)putchar('|');
644 return (1);
645 case S_IFLNK:
646 (void)putchar('@');
647 return (1);
648 case S_IFSOCK:
649 (void)putchar('=');
650 return (1);
651 case S_IFWHT:
652 (void)putchar('%');
653 return (1);
654 default:
655 break;
656 }
657 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
658 (void)putchar('*');
659 return (1);
660 }
661 return (0);
662 }
663
664 #ifdef COLORLS
665 static int
666 putch(int c)
667 {
668 (void)putchar(c);
669 return 0;
670 }
671
672 static int
673 writech(int c)
674 {
675 char tmp = c;
676
677 (void)write(STDOUT_FILENO, &tmp, 1);
678 return 0;
679 }
680
681 static void
682 printcolor(Colors c)
683 {
684 char *ansiseq;
685
686 if (colors[c].bold)
687 tputs(enter_bold, 1, putch);
688
689 if (colors[c].num[0] != -1) {
690 ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]);
691 if (ansiseq)
692 tputs(ansiseq, 1, putch);
693 }
694 if (colors[c].num[1] != -1) {
695 ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]);
696 if (ansiseq)
697 tputs(ansiseq, 1, putch);
698 }
699 }
700
701 static void
702 endcolor(int sig)
703 {
704 tputs(ansi_coloff, 1, sig ? writech : putch);
705 tputs(attrs_off, 1, sig ? writech : putch);
706 }
707
708 static int
709 colortype(mode_t mode)
710 {
711 switch (mode & S_IFMT) {
712 case S_IFDIR:
713 if (mode & S_IWOTH)
714 if (mode & S_ISTXT)
715 printcolor(C_WSDIR);
716 else
717 printcolor(C_WDIR);
718 else
719 printcolor(C_DIR);
720 return (1);
721 case S_IFLNK:
722 printcolor(C_LNK);
723 return (1);
724 case S_IFSOCK:
725 printcolor(C_SOCK);
726 return (1);
727 case S_IFIFO:
728 printcolor(C_FIFO);
729 return (1);
730 case S_IFBLK:
731 printcolor(C_BLK);
732 return (1);
733 case S_IFCHR:
734 printcolor(C_CHR);
735 return (1);
736 }
737 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
738 if (mode & S_ISUID)
739 printcolor(C_SUID);
740 else if (mode & S_ISGID)
741 printcolor(C_SGID);
742 else
743 printcolor(C_EXEC);
744 return (1);
745 }
746 return (0);
747 }
748
749 void
750 parsecolors(const char *cs)
751 {
752 int i;
753 int j;
754 int len;
755 char c[2];
756 short legacy_warn = 0;
757
758 if (cs == NULL)
759 cs = ""; /* LSCOLORS not set */
760 len = strlen(cs);
761 for (i = 0; i < C_NUMCOLORS; i++) {
762 colors[i].bold = 0;
763
764 if (len <= 2 * i) {
765 c[0] = defcolors[2 * i];
766 c[1] = defcolors[2 * i + 1];
767 } else {
768 c[0] = cs[2 * i];
769 c[1] = cs[2 * i + 1];
770 }
771 for (j = 0; j < 2; j++) {
772 /* Legacy colours used 0-7 */
773 if (c[j] >= '0' && c[j] <= '7') {
774 colors[i].num[j] = c[j] - '0';
775 if (!legacy_warn) {
776 fprintf(stderr,
777 "warn: LSCOLORS should use "
778 "characters a-h instead of 0-9 ("
779 "see the manual page)\n");
780 }
781 legacy_warn = 1;
782 } else if (c[j] >= 'a' && c[j] <= 'h')
783 colors[i].num[j] = c[j] - 'a';
784 else if (c[j] >= 'A' && c[j] <= 'H') {
785 colors[i].num[j] = c[j] - 'A';
786 colors[i].bold = 1;
787 } else if (tolower((unsigned char)c[j] == 'x'))
788 colors[i].num[j] = -1;
789 else {
790 fprintf(stderr,
791 "error: invalid character '%c' in LSCOLORS"
792 " env var\n", c[j]);
793 colors[i].num[j] = -1;
794 }
795 }
796 }
797 }
798
799 void
800 colorquit(int sig)
801 {
802 endcolor(sig);
803
804 (void)signal(sig, SIG_DFL);
805 (void)kill(getpid(), sig);
806 }
807
808 #endif /* COLORLS */
809
810 static void
811 printlink(FTSENT *p)
812 {
813 int lnklen;
814 char name[MAXPATHLEN + 1];
815 char path[MAXPATHLEN + 1];
816
817 if (p->fts_level == FTS_ROOTLEVEL)
818 (void)snprintf(name, sizeof(name), "%s", p->fts_name);
819 else
820 (void)snprintf(name, sizeof(name),
821 "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
822 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
823 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
824 return;
825 }
826 path[lnklen] = '\0';
827 (void)printf(" -> ");
828 (void)printname(path);
829 }
830
831 static void
832 printsize(size_t width, off_t bytes)
833 {
834
835 if (f_humanval) {
836 char buf[5];
837
838 humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
839 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
840 (void)printf("%5s ", buf);
841 } else
842 (void)printf("%*jd ", (u_int)width, (intmax_t)bytes);
843 }