2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
39 static char sccsid
[] = "@(#)print.c 8.4 (Berkeley) 4/17/94";
42 #include <sys/cdefs.h>
43 __RCSID("$FreeBSD: src/bin/ls/print.c,v 1.57 2002/08/29 14:29:09 keramida Exp $");
45 #include <sys/param.h>
49 #include <sys/xattr.h>
50 #include <sys/types.h>
53 #if !TARGET_OS_EMBEDDED
54 #include <membership.h>
55 #include <membershipPriv.h>
57 #include <uuid/uuid.h>
76 #include <stdint.h> /* intmax_t */
78 #include <get_compat.h>
80 #define COMPAT_MODE(a,b) (1)
81 #endif /* __APPLE__ */
86 static int printaname(FTSENT
*, u_long
, u_long
);
87 static void printlink(FTSENT
*);
88 static void printtime(time_t);
89 static int printtype(u_int
);
90 static void printsize(size_t, off_t
);
92 static void endcolor(int);
93 static int colortype(mode_t
);
96 #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
99 /* Most of these are taken from <sys/stat.h> */
100 typedef enum Colors
{
101 C_DIR
, /* directory */
102 C_LNK
, /* symbolic link */
105 C_EXEC
, /* executable */
106 C_BLK
, /* block special */
107 C_CHR
, /* character special */
108 C_SUID
, /* setuid executable */
109 C_SGID
, /* setgid executable */
110 C_WSDIR
, /* directory writeble to others, with sticky
112 C_WDIR
, /* directory writeble to others, without
114 C_NUMCOLORS
/* just a place-holder */
117 static const char *defcolors
= "exfxcxdxbxegedabagacad";
119 /* colors for file types */
123 } colors
[C_NUMCOLORS
];
127 printscol(DISPLAY
*dp
)
131 if (COMPAT_MODE("bin/ls", "Unix2003")) {
132 if (dp
->list
->fts_level
!= FTS_ROOTLEVEL
&& (f_longform
|| f_size
))
133 (void)printf("total %qu\n", (u_int64_t
)howmany(dp
->btotal
, blocksize
));
136 for (p
= dp
->list
; p
; p
= p
->fts_link
) {
139 (void)printaname(p
, dp
->s_inode
, dp
->s_block
);
145 * print name in current style
148 printname(const char *name
)
150 if (f_octal
|| f_octal_escape
)
151 return prn_octal(name
);
153 return prn_printable(name
);
155 return prn_normal(name
);
159 * print access control list
165 #define ACL_PERM_DIR (1<<0)
166 #define ACL_PERM_FILE (1<<1)
168 {ACL_READ_DATA
, "read", ACL_PERM_FILE
},
169 {ACL_LIST_DIRECTORY
, "list", ACL_PERM_DIR
},
170 {ACL_WRITE_DATA
, "write", ACL_PERM_FILE
},
171 {ACL_ADD_FILE
, "add_file", ACL_PERM_DIR
},
172 {ACL_EXECUTE
, "execute", ACL_PERM_FILE
},
173 {ACL_SEARCH
, "search", ACL_PERM_DIR
},
174 {ACL_DELETE
, "delete", ACL_PERM_FILE
| ACL_PERM_DIR
},
175 {ACL_APPEND_DATA
, "append", ACL_PERM_FILE
},
176 {ACL_ADD_SUBDIRECTORY
, "add_subdirectory", ACL_PERM_DIR
},
177 {ACL_DELETE_CHILD
, "delete_child", ACL_PERM_DIR
},
178 {ACL_READ_ATTRIBUTES
, "readattr", ACL_PERM_FILE
| ACL_PERM_DIR
},
179 {ACL_WRITE_ATTRIBUTES
, "writeattr", ACL_PERM_FILE
| ACL_PERM_DIR
},
180 {ACL_READ_EXTATTRIBUTES
, "readextattr", ACL_PERM_FILE
| ACL_PERM_DIR
},
181 {ACL_WRITE_EXTATTRIBUTES
, "writeextattr", ACL_PERM_FILE
| ACL_PERM_DIR
},
182 {ACL_READ_SECURITY
, "readsecurity", ACL_PERM_FILE
| ACL_PERM_DIR
},
183 {ACL_WRITE_SECURITY
, "writesecurity", ACL_PERM_FILE
| ACL_PERM_DIR
},
184 {ACL_CHANGE_OWNER
, "chown", ACL_PERM_FILE
| ACL_PERM_DIR
},
193 {ACL_ENTRY_FILE_INHERIT
, "file_inherit", ACL_PERM_DIR
},
194 {ACL_ENTRY_DIRECTORY_INHERIT
, "directory_inherit", ACL_PERM_DIR
},
195 {ACL_ENTRY_LIMIT_INHERIT
, "limit_inherit", ACL_PERM_FILE
| ACL_PERM_DIR
},
196 {ACL_ENTRY_ONLY_INHERIT
, "only_inherit", ACL_PERM_DIR
},
201 uuid_to_name(uuid_t
*uu
)
203 #if TARGET_OS_EMBEDDED
204 return strdup("<UNKNOWN>");
205 #else /* !TARGET_OS_EMBEDDED */
207 struct group
*tgrp
= NULL
;
208 struct passwd
*tpass
= NULL
;
213 #define MAXNAMETAG (MAXLOGNAME + 6) /* + strlen("group:") */
214 name
= (char *) malloc(MAXNAMETAG
);
219 if (!f_numericonly
) {
220 if (0 != mbr_uuid_to_id(*uu
, &id
, &is_gid
))
226 tpass
= getpwuid(id
);
230 snprintf(name
, MAXNAMETAG
, "%s:%s", "user", tpass
->pw_name
);
233 tgrp
= getgrgid((gid_t
) id
);
237 snprintf(name
, MAXNAMETAG
, "%s:%s", "group", tgrp
->gr_name
);
244 if (0 != mbr_uuid_to_string(*uu
, name
)) {
245 fprintf(stderr
, "Unable to translate qualifier on ACL\n");
246 strcpy(name
, "<UNKNOWN>");
249 #endif /* !TARGET_OS_EMBEDDED */
253 printxattr(DISPLAY
*dp
, char *filename
, ssize_t xattr
)
255 int flags
= XATTR_NOFOLLOW
;
256 char *buf
= malloc(xattr
);
260 if (listxattr(filename
, buf
, xattr
, flags
) > 0) {
262 for (name
= buf
; name
< buf
+xattr
; name
+= strlen(name
) + 1) {
263 ssize_t size
= getxattr(filename
, name
, 0, 0, 0, flags
);
267 printsize(dp
->s_size
, size
);
275 printacl(acl_t acl
, int isdir
)
277 acl_entry_t entry
= NULL
;
289 acl_get_entry(acl
, entry
== NULL
? ACL_FIRST_ENTRY
: ACL_NEXT_ENTRY
, &entry
) == 0;
291 if (acl_get_tag_type(entry
, &tag
) != 0)
293 if (acl_get_flagset_np(entry
, &flags
) != 0)
295 if (acl_get_permset(entry
, &perms
) != 0)
297 if ((applicable
= (uuid_t
*) acl_get_qualifier(entry
)) == NULL
)
299 name
= uuid_to_name(applicable
);
300 acl_free(applicable
);
302 case ACL_EXTENDED_ALLOW
:
305 case ACL_EXTENDED_DENY
:
312 (void)printf(" %d: %s%s %s ",
315 acl_get_flag_np(flags
, ACL_ENTRY_INHERITED
) ? " inherited" : "",
321 for (i
= 0, first
= 0; acl_perms
[i
].name
!= NULL
; i
++) {
322 if (acl_get_perm_np(perms
, acl_perms
[i
].perm
) == 0)
324 if (!(acl_perms
[i
].flags
& (isdir
? ACL_PERM_DIR
: ACL_PERM_FILE
)))
326 (void)printf("%s%s", first
++ ? "," : "", acl_perms
[i
].name
);
328 for (i
= 0; acl_flags
[i
].name
!= NULL
; i
++) {
329 if (acl_get_flag_np(flags
, acl_flags
[i
].flag
) == 0)
331 if (!(acl_flags
[i
].flags
& (isdir
? ACL_PERM_DIR
: ACL_PERM_FILE
)))
333 (void)printf("%s%s", first
++ ? "," : "", acl_flags
[i
].name
);
342 printlong(DISPLAY
*dp
)
351 char full_path
[MAXPATHLEN
];
357 int color_printed
= 0;
360 if (dp
->list
->fts_level
!= FTS_ROOTLEVEL
&& (f_longform
|| f_size
))
361 (void)printf("total %qu\n", (u_int64_t
)howmany(dp
->btotal
, blocksize
));
363 for (p
= dp
->list
; p
; p
= p
->fts_link
) {
368 #if _DARWIN_FEATURE_64_BIT_INODE
369 (void)printf("%*llu ", dp
->s_inode
, (u_quad_t
)sp
->st_ino
);
371 (void)printf("%*lu ", dp
->s_inode
, (u_long
)sp
->st_ino
);
374 (void)printf("%*qu ",
375 dp
->s_block
, (u_int64_t
)howmany(sp
->st_blocks
, blocksize
));
376 strmode(sp
->st_mode
, buf
);
379 buf
[10] = '\0'; /* make +/@ abut the mode */
380 filename
= p
->fts_name
;
381 if (p
->fts_level
!= FTS_ROOTLEVEL
)
383 snprintf(full_path
, sizeof full_path
, "%s/%s",
384 p
->fts_parent
->fts_accpath
, p
->fts_name
);
385 filename
= full_path
;
387 /* symlinks can not have ACLs */
388 acl
= acl_get_link_np(filename
, ACL_TYPE_EXTENDED
);
389 if (acl
&& acl_get_entry(acl
, ACL_FIRST_ENTRY
, &dummy
) == -1) {
393 xattr
= listxattr(filename
, NULL
, 0, XATTR_NOFOLLOW
);
399 else if (acl
!= NULL
)
403 #endif /* __APPLE__ */
404 if (f_group
&& f_owner
) { /* means print neither */
406 (void)printf("%s%s %*u ", buf
, str
, dp
->s_nlink
,
408 #else /* ! __APPLE__ */
409 (void)printf("%s %*u ", buf
, dp
->s_nlink
,
411 #endif /* __APPLE__ */
415 (void)printf("%s%s %*u %-*s ", buf
, str
, dp
->s_nlink
,
416 sp
->st_nlink
, dp
->s_group
, np
->group
);
417 #else /* ! __APPLE__ */
418 (void)printf("%s %*u %-*s ", buf
, dp
->s_nlink
,
419 sp
->st_nlink
, dp
->s_group
, np
->group
);
420 #endif /* __APPLE__ */
424 (void)printf("%s%s %*u %-*s ", buf
, str
, dp
->s_nlink
,
425 sp
->st_nlink
, dp
->s_user
, np
->user
);
426 #else /* ! __APPLE__ */
427 (void)printf("%s %*u %-*s ", buf
, dp
->s_nlink
,
428 sp
->st_nlink
, dp
->s_user
, np
->user
);
429 #endif /* __APPLE__ */
433 (void)printf("%s%s %*u %-*s %-*s ", buf
, str
, dp
->s_nlink
,
434 sp
->st_nlink
, dp
->s_user
, np
->user
, dp
->s_group
,
436 #else /* ! __APPLE__ */
437 (void)printf("%s %*u %-*s %-*s ", buf
, dp
->s_nlink
,
438 sp
->st_nlink
, dp
->s_user
, np
->user
, dp
->s_group
,
440 #endif /* ! __APPLE__ */
443 (void)printf("%-*s ", dp
->s_flags
, np
->flags
);
444 if (S_ISCHR(sp
->st_mode
) || S_ISBLK(sp
->st_mode
))
445 if (minor(sp
->st_rdev
) > 255 || minor(sp
->st_rdev
) < 0)
446 (void)printf("%3d, 0x%08x ",
448 (u_int
)minor(sp
->st_rdev
));
450 (void)printf("%3d, %3d ",
451 major(sp
->st_rdev
), minor(sp
->st_rdev
));
453 (void)printf("%*s%*qu ",
454 8 - dp
->s_size
, "", dp
->s_size
, (u_int64_t
)sp
->st_size
);
456 printsize(dp
->s_size
, sp
->st_size
);
458 printtime(sp
->st_atime
);
459 else if (f_statustime
)
460 printtime(sp
->st_ctime
);
461 else if (f_birthtime
)
462 printtime(sp
->st_birthtime
);
464 printtime(sp
->st_mtime
);
467 color_printed
= colortype(sp
->st_mode
);
469 (void)printname(p
->fts_name
);
471 if (f_color
&& color_printed
)
475 (void)printtype(sp
->st_mode
);
476 if (S_ISLNK(sp
->st_mode
))
480 if (f_xattr
&& xattr
) {
481 printxattr(dp
, filename
, xattr
);
485 printacl(acl
, S_ISDIR(sp
->st_mode
));
489 #endif /* __APPLE__ */
494 printstream(DISPLAY
*dp
)
497 extern int termwidth
;
500 for (p
= dp
->list
, chcnt
= 0; p
; p
= p
->fts_link
) {
501 if (p
->fts_number
== NO_PRINT
)
503 if (strlen(p
->fts_name
) + chcnt
+
504 (p
->fts_link
? 2 : 0) >= (unsigned)termwidth
) {
508 chcnt
+= printaname(p
, dp
->s_inode
, dp
->s_block
);
519 printcol(DISPLAY
*dp
)
521 extern int termwidth
;
522 static FTSENT
**array
;
523 static int lastentries
= -1;
543 * Have to do random access in the linked list -- build a table
546 if (dp
->entries
> lastentries
) {
547 lastentries
= dp
->entries
;
549 realloc(array
, dp
->entries
* sizeof(FTSENT
*))) == NULL
) {
554 for (p
= dp
->list
, num
= 0; p
; p
= p
->fts_link
)
555 if (p
->fts_number
!= NO_PRINT
)
558 colwidth
= dp
->maxlen
;
560 colwidth
+= dp
->s_inode
+ 1;
562 colwidth
+= dp
->s_block
+ 1;
566 colwidth
= (colwidth
+ tabwidth
) & ~(tabwidth
- 1);
567 if (termwidth
< 2 * colwidth
) {
571 numcols
= termwidth
/ colwidth
;
572 numrows
= num
/ numcols
;
576 if (dp
->list
->fts_level
!= FTS_ROOTLEVEL
&& (f_longform
|| f_size
))
577 (void)printf("total %qu\n", (u_int64_t
)howmany(dp
->btotal
, blocksize
));
580 for (row
= 0; row
< numrows
; ++row
) {
584 for (col
= 0, chcnt
= 0; col
< numcols
; ++col
) {
585 chcnt
+= printaname(array
[base
], dp
->s_inode
,
593 while ((cnt
= ((chcnt
+ tabwidth
) & ~(tabwidth
- 1)))
595 if (f_sortacross
&& col
+ 1 >= numcols
)
597 (void)putchar(f_notabs
? ' ' : '\t');
607 * print [inode] [size] name
608 * return # of characters printed, no trailing characters.
611 printaname(FTSENT
*p
, u_long inodefield
, u_long sizefield
)
616 int color_printed
= 0;
622 #if _DARWIN_FEATURE_64_BIT_INODE
623 chcnt
+= printf("%*llu ", (int)inodefield
, (u_quad_t
)sp
->st_ino
);
625 chcnt
+= printf("%*lu ", (int)inodefield
, (u_long
)sp
->st_ino
);
628 chcnt
+= printf("%*qu ",
629 (int)sizefield
, (u_int64_t
)howmany(sp
->st_blocks
, blocksize
));
632 color_printed
= colortype(sp
->st_mode
);
634 chcnt
+= printname(p
->fts_name
);
636 if (f_color
&& color_printed
)
640 chcnt
+= printtype(sp
->st_mode
);
645 printtime(time_t ftime
)
650 static int d_first
= -1;
653 d_first
= (*nl_langinfo(D_MD_ORDER
) == 'd');
657 #define SIXMONTHS ((365 / 2) * 86400)
659 /* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */
660 format
= d_first
? "%e %b %T %Y " : "%b %e %T %Y ";
661 else if (COMPAT_MODE("bin/ls", "Unix2003")) {
662 if (ftime
+ SIXMONTHS
> now
&& ftime
<= now
)
663 /* mmm dd hh:mm || dd mmm hh:mm */
664 format
= d_first
? "%e %b %R " : "%b %e %R ";
666 /* mmm dd yyyy || dd mmm yyyy */
667 format
= d_first
? "%e %b %Y " : "%b %e %Y ";
669 else if (ftime
+ SIXMONTHS
> now
&& ftime
< now
+ SIXMONTHS
)
670 /* mmm dd hh:mm || dd mmm hh:mm */
671 format
= d_first
? "%e %b %R " : "%b %e %R ";
673 /* mmm dd yyyy || dd mmm yyyy */
674 format
= d_first
? "%e %b %Y " : "%b %e %Y ";
675 strftime(longstring
, sizeof(longstring
), format
, localtime(&ftime
));
676 fputs(longstring
, stdout
);
680 printtype(u_int mode
)
684 if ((mode
& S_IFMT
) == S_IFDIR
) {
691 switch (mode
& S_IFMT
) {
710 if (mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) {
730 (void)write(STDOUT_FILENO
, &tmp
, 1);
740 tputs(enter_bold
, 1, putch
);
742 if (colors
[c
].num
[0] != -1) {
743 ansiseq
= tgoto(ansi_fgcol
, 0, colors
[c
].num
[0]);
745 tputs(ansiseq
, 1, putch
);
747 if (colors
[c
].num
[1] != -1) {
748 ansiseq
= tgoto(ansi_bgcol
, 0, colors
[c
].num
[1]);
750 tputs(ansiseq
, 1, putch
);
757 tputs(ansi_coloff
, 1, sig
? writech
: putch
);
758 tputs(attrs_off
, 1, sig
? writech
: putch
);
762 colortype(mode_t mode
)
764 switch (mode
& S_IFMT
) {
790 if (mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) {
793 else if (mode
& S_ISGID
)
803 parsecolors(const char *cs
)
809 short legacy_warn
= 0;
812 cs
= ""; /* LSCOLORS not set */
814 for (i
= 0; i
< C_NUMCOLORS
; i
++) {
818 c
[0] = defcolors
[2 * i
];
819 c
[1] = defcolors
[2 * i
+ 1];
822 c
[1] = cs
[2 * i
+ 1];
824 for (j
= 0; j
< 2; j
++) {
825 /* Legacy colours used 0-7 */
826 if (c
[j
] >= '0' && c
[j
] <= '7') {
827 colors
[i
].num
[j
] = c
[j
] - '0';
830 "warn: LSCOLORS should use "
831 "characters a-h instead of 0-9 ("
832 "see the manual page)\n");
835 } else if (c
[j
] >= 'a' && c
[j
] <= 'h')
836 colors
[i
].num
[j
] = c
[j
] - 'a';
837 else if (c
[j
] >= 'A' && c
[j
] <= 'H') {
838 colors
[i
].num
[j
] = c
[j
] - 'A';
840 } else if (tolower((unsigned char)c
[j
] == 'x'))
841 colors
[i
].num
[j
] = -1;
844 "error: invalid character '%c' in LSCOLORS"
846 colors
[i
].num
[j
] = -1;
857 (void)signal(sig
, SIG_DFL
);
858 (void)kill(getpid(), sig
);
867 char name
[MAXPATHLEN
+ 1];
868 char path
[MAXPATHLEN
+ 1];
870 if (p
->fts_level
== FTS_ROOTLEVEL
)
871 (void)snprintf(name
, sizeof(name
), "%s", p
->fts_name
);
873 (void)snprintf(name
, sizeof(name
),
874 "%s/%s", p
->fts_parent
->fts_accpath
, p
->fts_name
);
875 if ((lnklen
= readlink(name
, path
, sizeof(path
) - 1)) == -1) {
876 (void)fprintf(stderr
, "\nls: %s: %s\n", name
, strerror(errno
));
880 (void)printf(" -> ");
881 (void)printname(path
);
885 printsize(size_t width
, off_t bytes
)
891 humanize_number(buf
, sizeof(buf
), (int64_t)bytes
, "",
892 HN_AUTOSCALE
, HN_B
| HN_NOSPACE
| HN_DECIMAL
);
893 (void)printf("%5s ", buf
);
895 (void)printf("%*jd ", (u_int
)width
, (intmax_t)bytes
);