1 /* $NetBSD: ls.c,v 1.69 2011/08/29 14:44:21 joerg Exp $ */
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
35 #include <sys/cdefs.h>
37 __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
38 The Regents of the University of California. All rights reserved.");
43 static char sccsid
[] = "@(#)ls.c 8.7 (Berkeley) 8/5/94";
45 __RCSID("$NetBSD: ls.c,v 1.69 2011/08/29 14:44:21 joerg Exp $");
49 #include <sys/param.h>
50 #include <sys/types.h>
52 #include <sys/ioctl.h>
71 static void display(FTSENT
*, FTSENT
*);
72 static int mastercmp(const FTSENT
**, const FTSENT
**);
73 static void traverse(int, char **, int);
75 static void (*printfcn
)(DISPLAY
*);
76 static int (*sortfcn
)(const FTSENT
*, const FTSENT
*);
82 long blocksize
; /* block size units */
83 int termwidth
= 80; /* default terminal width */
84 int sortkey
= BY_NAME
;
85 int rval
= EXIT_SUCCESS
; /* exit value - set if error encountered */
88 int f_accesstime
; /* use time of last access */
89 int f_column
; /* columnated format */
90 int f_columnacross
; /* columnated format, sorted across */
91 int f_flags
; /* show flags associated with a file */
92 int f_grouponly
; /* long listing without owner */
93 int f_humanize
; /* humanize the size field */
94 int f_commas
; /* separate size field with comma */
95 int f_inode
; /* print inode */
96 int f_listdir
; /* list actual directory, not contents */
97 int f_listdot
; /* list files beginning with . */
98 int f_longform
; /* long listing format */
99 int f_nonprint
; /* show unprintables as ? */
100 int f_nosort
; /* don't sort output */
101 int f_numericonly
; /* don't convert uid/gid to name */
102 int f_octal
; /* print octal escapes for nongraphic characters */
103 int f_octal_escape
; /* like f_octal but use C escapes if possible */
104 int f_recursive
; /* ls subdirectories also */
105 int f_reversesort
; /* reverse whatever sort is used */
106 int f_sectime
; /* print the real time for all files */
107 int f_singlecol
; /* use single column output */
108 int f_size
; /* list size in short listing */
109 int f_statustime
; /* use time of last mode change */
110 int f_stream
; /* stream format */
111 int f_type
; /* add type character for non-regular files */
112 int f_typedir
; /* add type character for directories */
113 int f_whiteout
; /* show whiteout entries */
119 (void)fprintf(stderr
,
120 "usage: %s [-AaBbCcdFfghikLlMmnopqRrSsTtuWwx1] [file ...]\n",
127 ls_main(int argc
, char *argv
[])
129 static char dot
[] = ".", *dotav
[] = { dot
, NULL
};
135 setprogname(argv
[0]);
136 (void)setlocale(LC_ALL
, "");
138 /* Terminal defaults to -Cq, non-terminal defaults to -1. */
139 if (isatty(STDOUT_FILENO
)) {
140 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &win
) == 0 &&
142 termwidth
= win
.ws_col
;
143 f_column
= f_nonprint
= 1;
147 /* Root is -A automatically. */
151 fts_options
= FTS_PHYSICAL
;
152 while ((ch
= getopt(argc
, argv
, "1ABCFLMRSTWabcdfghiklmnopqrstuwx")) != -1) {
155 * The -1, -C, -l, -m and -x options all override each other so
156 * shell aliasing works correctly.
160 f_column
= f_columnacross
= f_longform
= f_stream
= 0;
164 f_columnacross
= f_longform
= f_singlecol
= f_stream
=
168 if (f_grouponly
!= -1)
171 f_column
= f_columnacross
= f_singlecol
= f_stream
= 0;
175 f_column
= f_columnacross
= f_singlecol
= f_stream
= 0;
176 /* Never let -g take precedence over -l. */
181 f_column
= f_columnacross
= f_longform
= f_singlecol
=
186 f_column
= f_longform
= f_singlecol
= f_stream
= 0;
188 /* The -c and -u options override each other. */
201 fts_options
&= ~FTS_PHYSICAL
;
202 fts_options
|= FTS_LOGICAL
;
208 fts_options
|= FTS_SEEDOT
;
213 /* The -B option turns off the -b, -q and -w options. */
219 /* The -b option turns off the -B, -q and -w options. */
225 /* The -d option turns off the -R option. */
241 /* The -h option forces all sizes to be measured in bytes. */
254 f_column
= f_columnacross
= f_singlecol
= f_stream
= 0;
262 /* The -q option turns off the -B, -b and -w options. */
286 /* The -w option turns off the -B, -b and -q options. */
300 if (f_column
|| f_columnacross
|| f_stream
) {
301 if ((p
= getenv("COLUMNS")) != NULL
)
306 * If both -g and -l options, let -l take precedence.
308 if (f_grouponly
== -1)
312 * If not -F, -i, -l, -p, -S, -s or -t options, don't require stat
315 if (!f_inode
&& !f_longform
&& !f_size
&& !f_type
&& !f_typedir
&&
317 fts_options
|= FTS_NOSTAT
;
320 * If not -F, -d or -l options, follow any symbolic links listed on
323 if (!f_longform
&& !f_listdir
&& !f_type
)
324 fts_options
|= FTS_COMFOLLOW
;
327 * If -W, show whiteout entries
331 fts_options
|= FTS_WHITEOUT
;
334 /* If -l or -s, figure out block size. */
335 if (f_inode
|| f_longform
|| f_size
) {
337 (void)getbsize(NULL
, &blocksize
);
341 /* Select a sort function. */
345 sortfcn
= revnamecmp
;
348 sortfcn
= revsizecmp
;
353 else if (f_statustime
)
354 sortfcn
= revstatcmp
;
355 else /* Use modification time. */
370 else if (f_statustime
)
372 else /* Use modification time. */
378 /* Select a print function. */
380 printfcn
= printscol
;
381 else if (f_columnacross
)
382 printfcn
= printacol
;
384 printfcn
= printlong
;
386 printfcn
= printstream
;
391 traverse(argc
, argv
, fts_options
);
393 traverse(1, dotav
, fts_options
);
398 static int output
; /* If anything output. */
401 * Traverse() walks the logical directory structure specified by the argv list
402 * in the order specified by the mastercmp() comparison function. During the
403 * traversal it passes linked lists of structures to display() which represent
404 * a superset (may be exact set) of the files to be displayed.
407 traverse(int argc
, char *argv
[], int options
)
411 int ch_options
, error
;
414 fts_open(argv
, options
, f_nosort
? NULL
: mastercmp
)) == NULL
)
415 err(EXIT_FAILURE
, NULL
);
417 display(NULL
, fts_children(ftsp
, 0));
419 (void)fts_close(ftsp
);
424 * If not recursing down this tree and don't need stat info, just get
427 ch_options
= !f_recursive
&& options
& FTS_NOSTAT
? FTS_NAMEONLY
: 0;
429 while ((p
= fts_read(ftsp
)) != NULL
)
430 switch (p
->fts_info
) {
432 warnx("%s: directory causes a cycle", p
->fts_name
);
436 warnx("%s: %s", p
->fts_name
, strerror(p
->fts_errno
));
440 if (p
->fts_level
!= FTS_ROOTLEVEL
&&
441 p
->fts_name
[0] == '.' && !f_listdot
)
445 * If already output something, put out a newline as
446 * a separator. If multiple arguments, precede each
447 * directory with its name.
450 (void)printf("\n%s:\n", p
->fts_path
);
452 (void)printf("%s:\n", p
->fts_path
);
456 chp
= fts_children(ftsp
, ch_options
);
459 if (!f_recursive
&& chp
!= NULL
)
460 (void)fts_set(ftsp
, p
, FTS_SKIP
);
464 (void)fts_close(ftsp
);
467 err(EXIT_FAILURE
, "fts_read");
471 * Display() takes a linked list of FTSENT structures and passes the list
472 * along with any other necessary information to the print function. P
473 * points to the parent directory of the display list.
476 display(FTSENT
*p
, FTSENT
*list
)
482 u_int64_t btotal
, stotal
;
486 int maxmajor
, maxminor
;
488 int bcfile
, entries
, flen
, glen
, ulen
, maxflags
, maxgroup
;
490 int maxuser
, needstats
;
491 const char *user
, *group
;
492 char buf
[21]; /* 64 bits == 20 digits, +1 for NUL */
493 char nuser
[12], ngroup
[12];
497 * If list is NULL there are two possibilities: that the parent
498 * directory p has no children, or that fts_children() returned an
499 * error. We ignore the error case since it will be replicated
500 * on the next call to fts_read() on the post-order visit to the
501 * directory p, and will be signalled in traverse().
506 needstats
= f_inode
|| f_longform
|| f_size
;
508 maxinode
= maxnlink
= 0;
510 maxuser
= maxgroup
= maxflags
= maxlen
= 0;
511 btotal
= stotal
= maxblock
= maxsize
= 0;
512 maxmajor
= maxminor
= 0;
513 for (cur
= list
, entries
= 0; cur
; cur
= cur
->fts_link
) {
514 if (cur
->fts_info
== FTS_ERR
|| cur
->fts_info
== FTS_NS
) {
516 cur
->fts_name
, strerror(cur
->fts_errno
));
517 cur
->fts_number
= NO_PRINT
;
523 * P is NULL if list is the argv list, to which different rules
527 /* Directories will be displayed later. */
528 if (cur
->fts_info
== FTS_D
&& !f_listdir
) {
529 cur
->fts_number
= NO_PRINT
;
533 /* Only display dot file if -a/-A set. */
534 if (cur
->fts_name
[0] == '.' && !f_listdot
) {
535 cur
->fts_number
= NO_PRINT
;
539 if (cur
->fts_namelen
> maxlen
)
540 maxlen
= cur
->fts_namelen
;
543 if (sp
->st_blocks
> maxblock
)
544 maxblock
= sp
->st_blocks
;
545 if (sp
->st_ino
> maxinode
)
546 maxinode
= sp
->st_ino
;
547 if (sp
->st_nlink
> maxnlink
)
548 maxnlink
= sp
->st_nlink
;
549 if (sp
->st_size
> maxsize
)
550 maxsize
= sp
->st_size
;
551 if (S_ISCHR(sp
->st_mode
) || S_ISBLK(sp
->st_mode
)) {
553 if (major(sp
->st_rdev
) > maxmajor
)
554 maxmajor
= major(sp
->st_rdev
);
555 if (minor(sp
->st_rdev
) > maxminor
)
556 maxminor
= minor(sp
->st_rdev
);
559 btotal
+= sp
->st_blocks
;
560 stotal
+= sp
->st_size
;
563 (user
= user_from_uid(sp
->st_uid
, 0)) ==
565 (void)snprintf(nuser
, sizeof(nuser
),
570 (group
= group_from_gid(sp
->st_gid
, 0)) ==
572 (void)snprintf(ngroup
, sizeof(ngroup
),
576 if ((ulen
= strlen(user
)) > maxuser
)
578 if ((glen
= strlen(group
)) > maxgroup
)
582 flags_to_string((u_long
)sp
->st_flags
, "-");
583 if ((flen
= strlen(flags
)) > maxflags
)
588 if ((np
= malloc(sizeof(NAMES
) +
589 ulen
+ glen
+ flen
+ 2)) == NULL
)
590 err(EXIT_FAILURE
, NULL
);
592 np
->user
= &np
->data
[0];
593 (void)strcpy(np
->user
, user
);
594 np
->group
= &np
->data
[ulen
+ 1];
595 (void)strcpy(np
->group
, group
);
598 np
->flags
= &np
->data
[ulen
+ glen
+ 2];
599 (void)strcpy(np
->flags
, flags
);
602 cur
->fts_pointer
= np
;
618 d
.s_block
= 4; /* min buf length for humanize_number */
620 (void)snprintf(buf
, sizeof(buf
), "%llu",
621 (long long)howmany(maxblock
, blocksize
));
622 d
.s_block
= strlen(buf
);
623 if (f_commas
) /* allow for commas before every third digit */
624 d
.s_block
+= (d
.s_block
- 1) / 3;
626 d
.s_flags
= maxflags
;
627 d
.s_group
= maxgroup
;
628 (void)snprintf(buf
, sizeof(buf
), "%llu",
629 (unsigned long long)maxinode
);
630 d
.s_inode
= strlen(buf
);
631 (void)snprintf(buf
, sizeof(buf
), "%u", maxnlink
);
632 d
.s_nlink
= strlen(buf
);
634 d
.s_size
= 4; /* min buf length for humanize_number */
636 (void)snprintf(buf
, sizeof(buf
), "%llu",
638 d
.s_size
= strlen(buf
);
639 if (f_commas
) /* allow for commas before every third digit */
640 d
.s_size
+= (d
.s_size
- 1) / 3;
644 (void)snprintf(buf
, sizeof(buf
), "%u", maxmajor
);
645 d
.s_major
= strlen(buf
);
646 (void)snprintf(buf
, sizeof(buf
), "%u", maxminor
);
647 d
.s_minor
= strlen(buf
);
648 if (d
.s_major
+ d
.s_minor
+ 2 > d
.s_size
)
649 d
.s_size
= d
.s_major
+ d
.s_minor
+ 2;
650 else if (d
.s_size
- d
.s_minor
- 2 > d
.s_major
)
651 d
.s_major
= d
.s_size
- d
.s_minor
- 2;
662 for (cur
= list
; cur
; cur
= cur
->fts_link
)
663 free(cur
->fts_pointer
);
667 * Ordering for mastercmp:
668 * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories
669 * as larger than directories. Within either group, use the sort function.
670 * All other levels use the sort function. Error entries remain unsorted.
673 mastercmp(const FTSENT
**a
, const FTSENT
**b
)
677 a_info
= (*a
)->fts_info
;
678 if (a_info
== FTS_ERR
)
680 b_info
= (*b
)->fts_info
;
681 if (b_info
== FTS_ERR
)
684 if (a_info
== FTS_NS
|| b_info
== FTS_NS
) {
685 if (b_info
!= FTS_NS
)
687 else if (a_info
!= FTS_NS
)
690 return (namecmp(*a
, *b
));
693 if (a_info
!= b_info
&& !f_listdir
&&
694 (*a
)->fts_level
== FTS_ROOTLEVEL
) {
697 else if (b_info
== FTS_D
)
700 return (sortfcn(*a
, *b
));