]>
git.saurik.com Git - apple/file_cmds.git/blob - ls/ls.c
749af9860cf684a9db4ff925acbb72a88799e7ab
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
38 static const char copyright
[] =
39 "@(#) Copyright (c) 1989, 1993, 1994\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid
[] = "@(#)ls.c 8.5 (Berkeley) 4/2/94";
48 #include <sys/cdefs.h>
49 __RCSID("$FreeBSD: src/bin/ls/ls.c,v 1.66 2002/09/21 01:28:36 wollman Exp $");
51 #include <sys/types.h>
53 #include <sys/ioctl.h>
72 #include <get_compat.h>
74 #define COMPAT_MODE(a,b) (1)
75 #endif /* __APPLE__ */
80 * Upward approximation of the maximum number of characters needed to
81 * represent a value of integral type t as a string, excluding the
82 * NUL terminator, with provision for a sign.
84 #define STRBUF_SIZEOF(t) (1 + CHAR_BIT * sizeof(t) / 3 + 1)
86 static void display(FTSENT
*, FTSENT
*);
87 static u_quad_t
makenines(u_quad_t
);
88 static int mastercmp(const FTSENT
**, const FTSENT
**);
89 static void traverse(int, char **, int);
91 static void (*printfcn
)(DISPLAY
*);
92 static int (*sortfcn
)(const FTSENT
*, const FTSENT
*);
94 long blocksize
; /* block size units */
95 int termwidth
= 80; /* default terminal width */
98 int f_accesstime
; /* use time of last access */
99 int f_flags
; /* show flags associated with a file */
100 int f_humanval
; /* show human-readable file sizes */
101 int f_inode
; /* print inode */
102 static int f_kblocks
; /* print size in kilobytes */
103 static int f_listdir
; /* list actual directory, not contents */
104 static int f_listdot
; /* list files beginning with . */
105 int f_longform
; /* long listing format */
106 int f_nonprint
; /* show unprintables as ? */
107 static int f_nosort
; /* don't sort output */
108 int f_notabs
; /* don't use tab-separated multi-col output */
109 int f_numericonly
; /* don't convert uid/gid to name */
110 int f_octal
; /* show unprintables as \xxx */
111 int f_octal_escape
; /* like f_octal but use C escapes if possible */
112 static int f_recursive
; /* ls subdirectories also */
113 static int f_reversesort
; /* reverse whatever sort is used */
114 int f_sectime
; /* print the real time for all files */
115 static int f_singlecol
; /* use single column output */
116 int f_size
; /* list size in short listing */
117 int f_slash
; /* similar to f_type, but only for dirs */
118 int f_sortacross
; /* sort across rows, not down columns */
119 int f_statustime
; /* use time of last mode change */
120 int f_stream
; /* stream the output, separate with commas */
121 static int f_timesort
; /* sort by time vice name */
122 static int f_sizesort
; /* sort by size */
123 int f_type
; /* add type character for non-regular files */
124 static int f_whiteout
; /* show whiteout entries */
125 int f_acl
; /* show ACLs in long listing */
126 int f_xattr
; /* show extended attributes in long listing */
127 int f_group
; /* show group */
128 int f_owner
; /* show owner */
130 int f_color
; /* add type in color for non-regular files */
132 char *ansi_bgcol
; /* ANSI sequence to set background colour */
133 char *ansi_fgcol
; /* ANSI sequence to set foreground colour */
134 char *ansi_coloff
; /* ANSI sequence to reset colours */
135 char *attrs_off
; /* ANSI sequence to turn off attributes */
136 char *enter_bold
; /* ANSI sequence to set color to bold mode */
142 main(int argc
, char *argv
[])
144 static char dot
[] = ".", *dotav
[] = {dot
, NULL
};
146 int ch
, fts_options
, notused
;
149 char termcapbuf
[1024]; /* termcap definition buffer */
150 char tcapbuf
[512]; /* capability buffer */
156 (void)setlocale(LC_ALL
, "");
158 /* Terminal defaults to -Cq, non-terminal defaults to -1. */
159 if (isatty(STDOUT_FILENO
)) {
161 if ((p
= getenv("COLUMNS")) != NULL
&& *p
!= '\0')
163 else if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &win
) != -1 &&
165 termwidth
= win
.ws_col
;
169 /* retrieve environment variable, in case of explicit -C */
170 p
= getenv("COLUMNS");
175 /* Root is -A automatically. */
179 fts_options
= FTS_PHYSICAL
;
180 while ((ch
= getopt(argc
, argv
, "1@ABCFGHLOPRSTWabcdefghiklmnopqrstuvwx"))
184 * The -1, -C, -x and -l options all override each other so
185 * shell aliasing works right.
198 f_sortacross
= f_longform
= f_singlecol
= 0;
210 /* The -c and -u options override each other. */
224 if (COMPAT_MODE("bin/ls", "Unix2003")) {
225 fts_options
&= ~FTS_LOGICAL
;
226 fts_options
|= FTS_PHYSICAL
;
227 fts_options
|= FTS_COMFOLLOWDIR
;
229 fts_options
|= FTS_COMFOLLOW
;
232 setenv("CLICOLOR", "", 1);
235 fts_options
&= ~FTS_PHYSICAL
;
236 fts_options
|= FTS_LOGICAL
;
237 if (COMPAT_MODE("bin/ls", "Unix2003")) {
238 fts_options
&= ~(FTS_COMFOLLOW
|FTS_COMFOLLOWDIR
);
242 fts_options
&= ~(FTS_COMFOLLOW
|FTS_COMFOLLOWDIR
);
243 fts_options
&= ~FTS_LOGICAL
;
244 fts_options
|= FTS_PHYSICAL
;
250 fts_options
|= FTS_SEEDOT
;
255 /* The -d option turns off the -R option. */
262 if (COMPAT_MODE("bin/ls", "Unix2003")) {
263 fts_options
|= FTS_SEEDOT
;
267 case 'g': /* Compatibility with Unix03 */
268 if (COMPAT_MODE("bin/ls", "Unix2003")) {
291 if (COMPAT_MODE("bin/ls", "Unix2003")) {
298 if (COMPAT_MODE("bin/ls", "Unix2003")) {
320 /* Darwin 1.4.1 compatibility */
336 /* Darwin 1.4.1 compatibility */
366 /* Enabling of colours is conditional on the environment. */
367 if (getenv("CLICOLOR") &&
368 (isatty(STDOUT_FILENO
) || getenv("CLICOLOR_FORCE")))
370 if (tgetent(termcapbuf
, getenv("TERM")) == 1) {
371 ansi_fgcol
= tgetstr("AF", &bp
);
372 ansi_bgcol
= tgetstr("AB", &bp
);
373 attrs_off
= tgetstr("me", &bp
);
374 enter_bold
= tgetstr("md", &bp
);
376 /* To switch colours off use 'op' if
377 * available, otherwise use 'oc', or
378 * don't do colours at all. */
379 ansi_coloff
= tgetstr("op", &bp
);
381 ansi_coloff
= tgetstr("oc", &bp
);
382 if (ansi_fgcol
&& ansi_bgcol
&& ansi_coloff
)
386 (void)fprintf(stderr
, "Color support not compiled in.\n");
392 * We can't put tabs and color sequences together:
393 * column number will be incremented incorrectly
394 * for "stty oxtabs" mode.
397 (void)signal(SIGINT
, colorquit
);
398 (void)signal(SIGQUIT
, colorquit
);
399 parsecolors(getenv("LSCOLORS"));
404 * If not -F, -i, -l, -s or -t options, don't require stat
405 * information, unless in color mode in which case we do
406 * need this to determine which colors to display.
408 if (!f_inode
&& !f_longform
&& !f_size
&& !f_timesort
&& !f_type
&& !f_sizesort
413 fts_options
|= FTS_NOSTAT
;
416 * If not -F, -d or -l options, follow any symbolic links listed on
419 if (!f_longform
&& !f_listdir
&& !f_type
&& !f_inode
)
420 fts_options
|= FTS_COMFOLLOW
;
423 * If -W, show whiteout entries
427 fts_options
|= FTS_WHITEOUT
;
430 /* If -l or -s, figure out block size. */
431 if (f_longform
|| f_size
) {
435 (void)getbsize(¬used
, &blocksize
);
439 /* Select a sort function. */
442 sortfcn
= revsizecmp
;
443 else if (!f_timesort
)
444 sortfcn
= revnamecmp
;
445 else if (f_accesstime
)
447 else if (f_statustime
)
448 sortfcn
= revstatcmp
;
449 else /* Use modification time. */
454 else if (!f_timesort
)
456 else if (f_accesstime
)
458 else if (f_statustime
)
460 else /* Use modification time. */
464 /* Select a print function. */
466 printfcn
= printscol
;
468 printfcn
= printlong
;
470 printfcn
= printstream
;
475 traverse(argc
, argv
, fts_options
);
477 traverse(1, dotav
, fts_options
);
481 static int output
; /* If anything output. */
484 * Traverse() walks the logical directory structure specified by the argv list
485 * in the order specified by the mastercmp() comparison function. During the
486 * traversal it passes linked lists of structures to display() which represent
487 * a superset (may be exact set) of the files to be displayed.
490 traverse(int argc
, char *argv
[], int options
)
494 int ch_options
, error
;
497 fts_open(argv
, options
, f_nosort
? NULL
: mastercmp
)) == NULL
)
500 display(NULL
, fts_children(ftsp
, 0));
507 * If not recursing down this tree and don't need stat info, just get
510 ch_options
= !f_recursive
&& options
& FTS_NOSTAT
? FTS_NAMEONLY
: 0;
512 while ((p
= fts_read(ftsp
)) != NULL
)
513 switch (p
->fts_info
) {
515 warnx("%s: directory causes a cycle", p
->fts_name
);
516 if (COMPAT_MODE("bin/ls", "Unix2003")) {
522 warnx("%s: %s", p
->fts_name
, strerror(p
->fts_errno
));
526 if (p
->fts_level
!= FTS_ROOTLEVEL
&&
527 p
->fts_name
[0] == '.' && !f_listdot
)
531 * If already output something, put out a newline as
532 * a separator. If multiple arguments, precede each
533 * directory with its name.
536 (void)printf("\n%s:\n", p
->fts_path
);
538 (void)printf("%s:\n", p
->fts_path
);
541 chp
= fts_children(ftsp
, ch_options
);
544 if (!f_recursive
&& chp
!= NULL
)
545 (void)fts_set(ftsp
, p
, FTS_SKIP
);
547 case FTS_SLNONE
: /* Same as default unless Unix conformance */
548 if (COMPAT_MODE("bin/ls", "Unix2003")) {
549 if ((options
& FTS_LOGICAL
)!=0) { /* -L was specified */
551 warnx("%s: %s", p
->fts_name
, strerror(p
->fts_errno
));
569 * Display() takes a linked list of FTSENT structures and passes the list
570 * along with any other necessary information to the print function. P
571 * points to the parent directory of the display list.
574 display(FTSENT
*p
, FTSENT
*list
)
581 u_int64_t btotal
, maxblock
;
582 u_long lattrlen
, maxlen
, maxnlink
, maxlattr
;
584 int bcfile
, maxflags
;
587 size_t flen
, ulen
, glen
;
589 int entries
, needstats
;
590 const char *user
, *group
;
591 char *flags
, *lattr
= NULL
;
592 char buf
[STRBUF_SIZEOF(u_quad_t
) + 1];
593 char ngroup
[STRBUF_SIZEOF(uid_t
) + 1];
594 char nuser
[STRBUF_SIZEOF(gid_t
) + 1];
597 * If list is NULL there are two possibilities: that the parent
598 * directory p has no children, or that fts_children() returned an
599 * error. We ignore the error case since it will be replicated
600 * on the next call to fts_read() on the post-order visit to the
601 * directory p, and will be signaled in traverse().
606 needstats
= f_inode
|| f_longform
|| f_size
;
609 initmax
= getenv("LS_COLWIDTHS");
610 /* Fields match -lios order. New ones should be added at the end. */
611 maxlattr
= maxblock
= maxinode
= maxlen
= maxnlink
=
612 maxuser
= maxgroup
= maxflags
= maxsize
= 0;
613 if (initmax
!= NULL
&& *initmax
!= '\0') {
614 char *initmax2
, *jinitmax
;
617 /* Fill-in "::" as "0:0:0" for the sake of scanf. */
618 jinitmax
= initmax2
= malloc(strlen(initmax
) * 2 + 2);
619 if (jinitmax
== NULL
)
622 strcpy(initmax2
, "0:"), initmax2
+= 2;
624 *initmax2
++ = *initmax
, *initmax2
= '\0';
625 for (initmax
++; *initmax
!= '\0'; initmax
++) {
626 if (initmax
[-1] == ':' && initmax
[0] == ':') {
628 *initmax2
++ = initmax
[0];
631 *initmax2
++ = initmax
[0];
635 if (initmax2
[-1] == ':')
636 strcpy(initmax2
, "0");
638 ninitmax
= sscanf(jinitmax
,
639 #if _DARWIN_FEATURE_64_BIT_INODE
640 " %llu : %qu : %lu : %i : %i : %i : %qu : %lu : %lu ",
642 " %lu : %qu : %lu : %i : %i : %i : %qu : %lu : %lu ",
644 &maxinode
, &maxblock
, &maxnlink
, &maxuser
,
645 &maxgroup
, &maxflags
, &maxsize
, &maxlen
, &maxlattr
);
683 maxinode
= makenines(maxinode
);
684 maxblock
= makenines(maxblock
);
685 maxnlink
= makenines(maxnlink
);
686 maxsize
= makenines(maxsize
);
690 for (cur
= list
, entries
= 0; cur
; cur
= cur
->fts_link
) {
691 if (cur
->fts_info
== FTS_ERR
|| cur
->fts_info
== FTS_NS
) {
693 cur
->fts_name
, strerror(cur
->fts_errno
));
694 cur
->fts_number
= NO_PRINT
;
699 * P is NULL if list is the argv list, to which different rules
703 /* Directories will be displayed later. */
704 if (cur
->fts_info
== FTS_D
&& !f_listdir
) {
705 cur
->fts_number
= NO_PRINT
;
709 /* Only display dot file if -a/-A set. */
710 if (cur
->fts_name
[0] == '.' && !f_listdot
) {
711 cur
->fts_number
= NO_PRINT
;
715 if (cur
->fts_namelen
> maxlen
)
716 maxlen
= cur
->fts_namelen
;
717 if (f_octal
|| f_octal_escape
) {
718 u_long t
= len_octal(cur
->fts_name
, cur
->fts_namelen
);
725 if (sp
->st_blocks
> maxblock
)
726 maxblock
= sp
->st_blocks
;
727 if (sp
->st_ino
> maxinode
)
728 maxinode
= sp
->st_ino
;
729 if (sp
->st_nlink
> maxnlink
)
730 maxnlink
= sp
->st_nlink
;
731 if (sp
->st_size
> maxsize
)
732 maxsize
= sp
->st_size
;
734 btotal
+= sp
->st_blocks
;
737 (void)snprintf(nuser
, sizeof(nuser
),
739 (void)snprintf(ngroup
, sizeof(ngroup
),
744 user
= user_from_uid(sp
->st_uid
, 0);
745 group
= group_from_gid(sp
->st_gid
, 0);
747 if ((ulen
= strlen(user
)) > maxuser
)
749 if ((glen
= strlen(group
)) > maxgroup
)
752 flags
= fflagstostr(sp
->st_flags
);
753 if (flags
!= NULL
&& *flags
== '\0') {
758 err(1, "fflagstostr");
759 flen
= strlen(flags
);
760 if (flen
> (size_t)maxflags
)
767 if ((np
= malloc(sizeof(NAMES
) + lattrlen
+
768 ulen
+ glen
+ flen
+ 4)) == NULL
)
771 np
->user
= &np
->data
[0];
772 (void)strcpy(np
->user
, user
);
773 np
->group
= &np
->data
[ulen
+ 1];
774 (void)strcpy(np
->group
, group
);
776 if (S_ISCHR(sp
->st_mode
) ||
777 S_ISBLK(sp
->st_mode
))
781 np
->flags
= &np
->data
[ulen
+ glen
+ 2];
782 (void)strcpy(np
->flags
, flags
);
785 cur
->fts_pointer
= np
;
800 (void)snprintf(buf
, sizeof(buf
), "%qu", (u_int64_t
)maxblock
);
801 d
.s_block
= strlen(buf
);
802 d
.s_flags
= maxflags
;
803 d
.s_lattr
= maxlattr
;
804 d
.s_group
= maxgroup
;
805 #if _DARWIN_FEATURE_64_BIT_INODE
806 (void)snprintf(buf
, sizeof(buf
), "%llu", maxinode
);
808 (void)snprintf(buf
, sizeof(buf
), "%lu", maxinode
);
810 d
.s_inode
= strlen(buf
);
811 (void)snprintf(buf
, sizeof(buf
), "%lu", maxnlink
);
812 d
.s_nlink
= strlen(buf
);
813 (void)snprintf(buf
, sizeof(buf
), "%qu", (u_int64_t
)maxsize
);
814 d
.s_size
= strlen(buf
);
821 for (cur
= list
; cur
; cur
= cur
->fts_link
)
822 free(cur
->fts_pointer
);
826 * Ordering for mastercmp:
827 * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories
828 * as larger than directories. Within either group, use the sort function.
829 * All other levels use the sort function. Error entries remain unsorted.
832 mastercmp(const FTSENT
**a
, const FTSENT
**b
)
836 a_info
= (*a
)->fts_info
;
837 if (a_info
== FTS_ERR
)
839 b_info
= (*b
)->fts_info
;
840 if (b_info
== FTS_ERR
)
843 if (a_info
== FTS_NS
|| b_info
== FTS_NS
)
844 return (namecmp(*a
, *b
));
846 if (a_info
!= b_info
&&
847 (*a
)->fts_level
== FTS_ROOTLEVEL
&& !f_listdir
) {
853 return (sortfcn(*a
, *b
));
857 * Makenines() returns (10**n)-1. This is useful for converting a width
858 * into a number that wide in decimal.
861 makenines(u_quad_t n
)
867 /* Use a loop instead of pow(), since all values of n are small. */
868 for (i
= 0; i
< n
; i
++)