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/types.h>
52 #include <membership.h>
53 #include <membershipPriv.h>
54 #include <uuid/uuid.h>
76 static int printaname(FTSENT
*, u_long
, u_long
);
77 static void printlink(FTSENT
*);
78 static void printtime(time_t);
79 static int printtype(u_int
);
80 static void printsize(size_t, off_t
);
82 static void endcolor(int);
83 static int colortype(mode_t
);
86 #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
88 #define KILO_SZ(n) (n)
89 #define MEGA_SZ(n) ((n) * (n))
90 #define GIGA_SZ(n) ((n) * (n) * (n))
91 #define TERA_SZ(n) ((n) * (n) * (n) * (n))
92 #define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n))
94 #define KILO_2_SZ (KILO_SZ(1024ULL))
95 #define MEGA_2_SZ (MEGA_SZ(1024ULL))
96 #define GIGA_2_SZ (GIGA_SZ(1024ULL))
97 #define TERA_2_SZ (TERA_SZ(1024ULL))
98 #define PETA_2_SZ (PETA_SZ(1024ULL))
100 static u_int64_t vals_base2
[] = {1, KILO_2_SZ
, MEGA_2_SZ
, GIGA_2_SZ
, TERA_2_SZ
, PETA_2_SZ
};
103 NONE
, KILO
, MEGA
, GIGA
, TERA
, PETA
, UNIT_MAX
105 static unit_t
unit_adjust(off_t
*);
107 static int unitp
[] = {NONE
, KILO
, MEGA
, GIGA
, TERA
, PETA
};
110 /* Most of these are taken from <sys/stat.h> */
111 typedef enum Colors
{
112 C_DIR
, /* directory */
113 C_LNK
, /* symbolic link */
116 C_EXEC
, /* executable */
117 C_BLK
, /* block special */
118 C_CHR
, /* character special */
119 C_SUID
, /* setuid executable */
120 C_SGID
, /* setgid executable */
121 C_WSDIR
, /* directory writeble to others, with sticky
123 C_WDIR
, /* directory writeble to others, without
125 C_NUMCOLORS
/* just a place-holder */
128 static const char *defcolors
= "exfxcxdxbxegedabagacad";
130 /* colors for file types */
134 } colors
[C_NUMCOLORS
];
138 printscol(DISPLAY
*dp
)
142 for (p
= dp
->list
; p
; p
= p
->fts_link
) {
145 (void)printaname(p
, dp
->s_inode
, dp
->s_block
);
151 * print name in current style
154 printname(const char *name
)
156 if (f_octal
|| f_octal_escape
)
157 return prn_octal(name
);
159 return prn_printable(name
);
161 return printf("%s", name
);
165 * print access control list
171 #define ACL_PERM_DIR (1<<0)
172 #define ACL_PERM_FILE (1<<1)
174 {ACL_READ_DATA
, "read", ACL_PERM_FILE
},
175 {ACL_LIST_DIRECTORY
, "list", ACL_PERM_DIR
},
176 {ACL_WRITE_DATA
, "write", ACL_PERM_FILE
},
177 {ACL_ADD_FILE
, "add_file", ACL_PERM_DIR
},
178 {ACL_EXECUTE
, "execute", ACL_PERM_FILE
},
179 {ACL_SEARCH
, "search", ACL_PERM_DIR
},
180 {ACL_DELETE
, "delete", ACL_PERM_FILE
| ACL_PERM_DIR
},
181 {ACL_APPEND_DATA
, "append", ACL_PERM_FILE
},
182 {ACL_ADD_SUBDIRECTORY
, "add_subdirectory", ACL_PERM_DIR
},
183 {ACL_DELETE_CHILD
, "delete_child", ACL_PERM_DIR
},
184 {ACL_READ_ATTRIBUTES
, "readattr", ACL_PERM_FILE
| ACL_PERM_DIR
},
185 {ACL_WRITE_ATTRIBUTES
, "writeattr", ACL_PERM_FILE
| ACL_PERM_DIR
},
186 {ACL_READ_EXTATTRIBUTES
, "readextattr", ACL_PERM_FILE
| ACL_PERM_DIR
},
187 {ACL_WRITE_EXTATTRIBUTES
, "writeextattr", ACL_PERM_FILE
| ACL_PERM_DIR
},
188 {ACL_READ_SECURITY
, "readsecurity", ACL_PERM_FILE
| ACL_PERM_DIR
},
189 {ACL_WRITE_SECURITY
, "writesecurity", ACL_PERM_FILE
| ACL_PERM_DIR
},
190 {ACL_CHANGE_OWNER
, "chown", ACL_PERM_FILE
| ACL_PERM_DIR
},
199 {ACL_ENTRY_FILE_INHERIT
, "file_inherit", ACL_PERM_DIR
},
200 {ACL_ENTRY_DIRECTORY_INHERIT
, "directory_inherit", ACL_PERM_DIR
},
201 {ACL_ENTRY_LIMIT_INHERIT
, "limit_inherit", ACL_PERM_FILE
| ACL_PERM_DIR
},
202 {ACL_ENTRY_ONLY_INHERIT
, "only_inherit", ACL_PERM_DIR
},
207 uuid_to_name(uuid_t
*uu
)
210 struct group
*tgrp
= NULL
;
211 struct passwd
*tpass
= NULL
;
216 #define MAXNAMETAG (MAXLOGNAME + 6) /* + strlen("group:") */
217 name
= (char *) malloc(MAXNAMETAG
);
222 if (0 != mbr_uuid_to_id(uu
, &id
, &is_gid
))
227 tpass
= getpwuid(id
);
231 snprintf(name
, MAXNAMETAG
, "%s:%s", "user", tpass
->pw_name
);
234 tgrp
= getgrgid((gid_t
) id
);
238 snprintf(name
, MAXNAMETAG
, "%s:%s", "group", tgrp
->gr_name
);
241 if (0 != mbr_uuid_to_string(uu
, name
))
246 fprintf(stderr
, "Unable to translate qualifier on ACL\n");
247 strcpy(name
, "<UNKNOWN>");
252 printacl(acl_t acl
, int isdir
)
266 acl_get_entry(acl
, entry
== NULL ? ACL_FIRST_ENTRY
: ACL_NEXT_ENTRY
, &entry
) == 0;
268 if ((applicable
= (uuid_t
*) acl_get_qualifier(entry
)) == NULL
)
270 if (acl_get_tag_type(entry
, &tag
) != 0)
272 if (acl_get_flagset_np(entry
, &flags
) != 0)
274 if (acl_get_permset(entry
, &perms
) != 0)
277 name
= uuid_to_name(applicable
);
278 acl_free(applicable
);
281 case ACL_EXTENDED_ALLOW
:
284 case ACL_EXTENDED_DENY
:
291 (void)printf(" %d: %s%s %s ",
294 acl_get_flag_np(flags
, ACL_ENTRY_INHERITED
) ?
" inherited" : "",
300 for (i
= 0, first
= 0; acl_perms
[i
].name
!= NULL
; i
++) {
301 if (acl_get_perm_np(perms
, acl_perms
[i
].perm
) == 0)
303 if (!(acl_perms
[i
].flags
& (isdir ? ACL_PERM_DIR
: ACL_PERM_FILE
)))
305 (void)printf("%s%s", first
++ ?
"," : "", acl_perms
[i
].name
);
307 for (i
= 0; acl_flags
[i
].name
!= NULL
; i
++) {
308 if (acl_get_flag_np(flags
, acl_flags
[i
].flag
) == 0)
310 if (!(acl_flags
[i
].flags
& (isdir ? ACL_PERM_DIR
: ACL_PERM_FILE
)))
312 (void)printf("%s%s", first
++ ?
"," : "", acl_flags
[i
].name
);
321 printlong(DISPLAY
*dp
)
329 char full_path
[MAXPATHLEN
];
332 int color_printed
= 0;
335 if (dp
->list
->fts_level
!= FTS_ROOTLEVEL
&& (f_longform
|| f_size
))
336 (void)printf("total %qu\n", (u_int64_t
)howmany(dp
->btotal
, blocksize
));
338 for (p
= dp
->list
; p
; p
= p
->fts_link
) {
343 (void)printf("%*lu ", dp
->s_inode
, (u_long
)sp
->st_ino
);
345 (void)printf("%*qu ",
346 dp
->s_block
, (u_int64_t
)howmany(sp
->st_blocks
, blocksize
));
347 strmode(sp
->st_mode
, buf
);
350 if (p
->fts_parent
->fts_name
&& *p
->fts_parent
->fts_name
)
352 snprintf(full_path
, sizeof full_path
, "%s/%s",
353 p
->fts_parent
->fts_accpath
, p
->fts_accpath
);
354 acl
= acl_get_file(full_path
, ACL_TYPE_EXTENDED
);
356 acl
= acl_get_file(p
->fts_accpath
, ACL_TYPE_EXTENDED
);
357 #endif /* __APPLE__ */
360 (void)printf("%s%s %*u %-*s ", buf
, acl
== NULL ?
" " : "+", dp
->s_nlink
,
361 sp
->st_nlink
, dp
->s_group
, np
->group
);
362 #else /* ! __APPLE__ */
363 (void)printf("%s %*u %-*s ", buf
, dp
->s_nlink
,
364 sp
->st_nlink
, dp
->s_group
, np
->group
);
365 #endif /* __APPLE__ */
369 (void)printf("%s%s %*u %-*s %-*s ", buf
, acl
== NULL ?
" " : "+", dp
->s_nlink
,
370 sp
->st_nlink
, dp
->s_user
, np
->user
, dp
->s_group
,
372 #else /* ! __APPLE__ */
373 (void)printf("%s %*u %-*s %-*s ", buf
, dp
->s_nlink
,
374 sp
->st_nlink
, dp
->s_user
, np
->user
, dp
->s_group
,
376 #endif /* ! __APPLE__ */
379 (void)printf("%-*s ", dp
->s_flags
, np
->flags
);
380 if (S_ISCHR(sp
->st_mode
) || S_ISBLK(sp
->st_mode
))
381 if (minor(sp
->st_rdev
) > 255 || minor(sp
->st_rdev
) < 0)
382 (void)printf("%3d, 0x%08x ",
384 (u_int
)minor(sp
->st_rdev
));
386 (void)printf("%3d, %3d ",
387 major(sp
->st_rdev
), minor(sp
->st_rdev
));
389 (void)printf("%*s%*qu ",
390 8 - dp
->s_size
, "", dp
->s_size
, (u_int64_t
)sp
->st_size
);
392 printsize(dp
->s_size
, sp
->st_size
);
394 printtime(sp
->st_atime
);
395 else if (f_statustime
)
396 printtime(sp
->st_ctime
);
398 printtime(sp
->st_mtime
);
401 color_printed
= colortype(sp
->st_mode
);
403 (void)printname(p
->fts_name
);
405 if (f_color
&& color_printed
)
409 (void)printtype(sp
->st_mode
);
410 if (S_ISLNK(sp
->st_mode
))
414 if (f_acl
&& (acl
!= NULL
))
415 printacl(acl
, S_ISDIR(sp
->st_mode
));
417 #endif /* __APPLE__ */
422 printstream(DISPLAY
*dp
)
425 extern int termwidth
;
428 for (p
= dp
->list
, chcnt
= 0; p
; p
= p
->fts_link
) {
429 if (p
->fts_number
== NO_PRINT
)
431 if (strlen(p
->fts_name
) + chcnt
+
432 (p
->fts_link ?
2 : 0) >= (unsigned)termwidth
) {
436 chcnt
+= printaname(p
, dp
->s_inode
, dp
->s_block
);
447 printcol(DISPLAY
*dp
)
449 extern int termwidth
;
450 static FTSENT
**array
;
451 static int lastentries
= -1;
471 * Have to do random access in the linked list -- build a table
474 if (dp
->entries
> lastentries
) {
475 lastentries
= dp
->entries
;
477 realloc(array
, dp
->entries
* sizeof(FTSENT
*))) == NULL
) {
482 for (p
= dp
->list
, num
= 0; p
; p
= p
->fts_link
)
483 if (p
->fts_number
!= NO_PRINT
)
486 colwidth
= dp
->maxlen
;
488 colwidth
+= dp
->s_inode
+ 1;
490 colwidth
+= dp
->s_block
+ 1;
494 colwidth
= (colwidth
+ tabwidth
) & ~(tabwidth
- 1);
495 if (termwidth
< 2 * colwidth
) {
499 numcols
= termwidth
/ colwidth
;
500 numrows
= num
/ numcols
;
504 if (dp
->list
->fts_level
!= FTS_ROOTLEVEL
&& (f_longform
|| f_size
))
505 (void)printf("total %qu\n", (u_int64_t
)howmany(dp
->btotal
, blocksize
));
508 for (row
= 0; row
< numrows
; ++row
) {
512 for (col
= 0, chcnt
= 0; col
< numcols
; ++col
) {
513 chcnt
+= printaname(array
[base
], dp
->s_inode
,
521 while ((cnt
= ((chcnt
+ tabwidth
) & ~(tabwidth
- 1)))
523 if (f_sortacross
&& col
+ 1 >= numcols
)
525 (void)putchar(f_notabs ?
' ' : '\t');
535 * print [inode] [size] name
536 * return # of characters printed, no trailing characters.
539 printaname(FTSENT
*p
, u_long inodefield
, u_long sizefield
)
544 int color_printed
= 0;
550 chcnt
+= printf("%*lu ", (int)inodefield
, (u_long
)sp
->st_ino
);
552 chcnt
+= printf("%*qu ",
553 (int)sizefield
, (u_int64_t
)howmany(sp
->st_blocks
, blocksize
));
556 color_printed
= colortype(sp
->st_mode
);
558 chcnt
+= printname(p
->fts_name
);
560 if (f_color
&& color_printed
)
564 chcnt
+= printtype(sp
->st_mode
);
569 printtime(time_t ftime
)
574 static int d_first
= -1;
577 d_first
= (*nl_langinfo(D_MD_ORDER
) == 'd');
581 #define SIXMONTHS ((365 / 2) * 86400)
583 /* mmm dd hh:mm:ss yyyy || dd mmm hh:mm:ss yyyy */
584 format
= d_first ?
"%e %b %T %Y " : "%b %e %T %Y ";
585 else if (ftime
+ SIXMONTHS
> now
&& ftime
< now
+ SIXMONTHS
)
586 /* mmm dd hh:mm || dd mmm hh:mm */
587 format
= d_first ?
"%e %b %R " : "%b %e %R ";
589 /* mmm dd yyyy || dd mmm yyyy */
590 format
= d_first ?
"%e %b %Y " : "%b %e %Y ";
591 strftime(longstring
, sizeof(longstring
), format
, localtime(&ftime
));
592 fputs(longstring
, stdout
);
596 printtype(u_int mode
)
600 if ((mode
& S_IFMT
) == S_IFDIR
) {
607 switch (mode
& S_IFMT
) {
626 if (mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) {
646 (void)write(STDOUT_FILENO
, &tmp
, 1);
656 tputs(enter_bold
, 1, putch
);
658 if (colors
[c
].num
[0] != -1) {
659 ansiseq
= tgoto(ansi_fgcol
, 0, colors
[c
].num
[0]);
661 tputs(ansiseq
, 1, putch
);
663 if (colors
[c
].num
[1] != -1) {
664 ansiseq
= tgoto(ansi_bgcol
, 0, colors
[c
].num
[1]);
666 tputs(ansiseq
, 1, putch
);
673 tputs(ansi_coloff
, 1, sig ? writech
: putch
);
674 tputs(attrs_off
, 1, sig ? writech
: putch
);
678 colortype(mode_t mode
)
680 switch (mode
& S_IFMT
) {
706 if (mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) {
709 else if (mode
& S_ISGID
)
719 parsecolors(const char *cs
)
725 short legacy_warn
= 0;
728 cs
= ""; /* LSCOLORS not set */
730 for (i
= 0; i
< C_NUMCOLORS
; i
++) {
734 c
[0] = defcolors
[2 * i
];
735 c
[1] = defcolors
[2 * i
+ 1];
738 c
[1] = cs
[2 * i
+ 1];
740 for (j
= 0; j
< 2; j
++) {
741 /* Legacy colours used 0-7 */
742 if (c
[j
] >= '0' && c
[j
] <= '7') {
743 colors
[i
].num
[j
] = c
[j
] - '0';
746 "warn: LSCOLORS should use "
747 "characters a-h instead of 0-9 ("
748 "see the manual page)\n");
751 } else if (c
[j
] >= 'a' && c
[j
] <= 'h')
752 colors
[i
].num
[j
] = c
[j
] - 'a';
753 else if (c
[j
] >= 'A' && c
[j
] <= 'H') {
754 colors
[i
].num
[j
] = c
[j
] - 'A';
756 } else if (tolower((unsigned char)c
[j
] == 'x'))
757 colors
[i
].num
[j
] = -1;
760 "error: invalid character '%c' in LSCOLORS"
762 colors
[i
].num
[j
] = -1;
773 (void)signal(sig
, SIG_DFL
);
774 (void)kill(getpid(), sig
);
783 char name
[MAXPATHLEN
+ 1];
784 char path
[MAXPATHLEN
+ 1];
786 if (p
->fts_level
== FTS_ROOTLEVEL
)
787 (void)snprintf(name
, sizeof(name
), "%s", p
->fts_name
);
789 (void)snprintf(name
, sizeof(name
),
790 "%s/%s", p
->fts_parent
->fts_accpath
, p
->fts_name
);
791 if ((lnklen
= readlink(name
, path
, sizeof(path
) - 1)) == -1) {
792 (void)fprintf(stderr
, "\nls: %s: %s\n", name
, strerror(errno
));
796 (void)printf(" -> ");
797 (void)printname(path
);
801 printsize(size_t width
, off_t bytes
)
806 unit
= unit_adjust(&bytes
);
809 (void)printf("%*s ", (int)width
, "0B");
811 (void)printf("%*lld%c ", (int)width
- 1, bytes
,
814 (void)printf("%*lld ", (int)width
, bytes
);
818 * Output in "human-readable" format. Uses 3 digits max and puts
819 * unit suffixes at the end. Makes output compact and easy to read,
820 * especially on huge disks.
824 unit_adjust(off_t
*val
)
828 unsigned int unit_sz
;
830 abval
= fabs((double)*val
);
832 unit_sz
= abval ?
ilogb(abval
) / 10 : 0;
834 if (unit_sz
>= UNIT_MAX
) {
837 unit
= unitp
[unit_sz
];
838 *val
/= (double)vals_base2
[unit_sz
];