1 /* $NetBSD: ls.c,v 1.65 2008/09/25 23:44:05 lukem 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.65 2008/09/25 23:44:05 lukem Exp $");
49 #include <sys/types.h>
51 #include <sys/ioctl.h>
70 static void display(FTSENT
*, FTSENT
*);
71 static int mastercmp(const FTSENT
**, const FTSENT
**);
72 static void traverse(int, char **, int);
74 static void (*printfcn
)(DISPLAY
*);
75 static int (*sortfcn
)(const FTSENT
*, const FTSENT
*);
81 long blocksize
; /* block size units */
82 int termwidth
= 80; /* default terminal width */
83 int sortkey
= BY_NAME
;
84 int rval
= EXIT_SUCCESS
; /* exit value - set if error encountered */
87 int f_accesstime
; /* use time of last access */
88 int f_column
; /* columnated format */
89 int f_columnacross
; /* columnated format, sorted across */
90 int f_flags
; /* show flags associated with a file */
91 int f_grouponly
; /* long listing without owner */
92 int f_humanize
; /* humanize the size field */
93 int f_inode
; /* print inode */
94 int f_listdir
; /* list actual directory, not contents */
95 int f_listdot
; /* list files beginning with . */
96 int f_longform
; /* long listing format */
97 int f_nonprint
; /* show unprintables as ? */
98 int f_nosort
; /* don't sort output */
99 int f_numericonly
; /* don't convert uid/gid to name */
100 int f_octal
; /* print octal escapes for nongraphic characters */
101 int f_octal_escape
; /* like f_octal but use C escapes if possible */
102 int f_recursive
; /* ls subdirectories also */
103 int f_reversesort
; /* reverse whatever sort is used */
104 int f_sectime
; /* print the real time for all files */
105 int f_singlecol
; /* use single column output */
106 int f_size
; /* list size in short listing */
107 int f_statustime
; /* use time of last mode change */
108 int f_stream
; /* stream format */
109 int f_type
; /* add type character for non-regular files */
110 int f_typedir
; /* add type character for directories */
111 int f_whiteout
; /* show whiteout entries */
114 ls_main(int argc
, char *argv
[])
116 static char dot
[] = ".", *dotav
[] = { dot
, NULL
};
122 setprogname(argv
[0]);
123 (void)setlocale(LC_ALL
, "");
125 /* Terminal defaults to -Cq, non-terminal defaults to -1. */
126 if (isatty(STDOUT_FILENO
)) {
127 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &win
) == 0 &&
129 termwidth
= win
.ws_col
;
130 f_column
= f_nonprint
= 1;
134 /* Root is -A automatically. */
138 fts_options
= FTS_PHYSICAL
;
139 while ((ch
= getopt(argc
, argv
, "1ABCFLRSTWabcdfghiklmnopqrstuwx")) != -1) {
142 * The -1, -C, -l, -m and -x options all override each other so
143 * shell aliasing works correctly.
147 f_column
= f_columnacross
= f_longform
= f_stream
= 0;
151 f_columnacross
= f_longform
= f_singlecol
= f_stream
=
155 if (f_grouponly
!= -1)
158 f_column
= f_columnacross
= f_singlecol
= f_stream
= 0;
162 f_column
= f_columnacross
= f_singlecol
= f_stream
= 0;
163 /* Never let -g take precedence over -l. */
168 f_column
= f_columnacross
= f_longform
= f_singlecol
=
173 f_column
= f_longform
= f_singlecol
= f_stream
= 0;
175 /* The -c and -u options override each other. */
188 fts_options
&= ~FTS_PHYSICAL
;
189 fts_options
|= FTS_LOGICAL
;
195 fts_options
|= FTS_SEEDOT
;
200 /* The -B option turns off the -b, -q and -w options. */
206 /* The -b option turns off the -B, -q and -w options. */
212 /* The -d option turns off the -R option. */
228 /* The -h option forces all sizes to be measured in bytes. */
236 f_column
= f_columnacross
= f_singlecol
= f_stream
= 0;
244 /* The -q option turns off the -B, -b and -w options. */
268 /* The -w option turns off the -B, -b and -q options. */
282 if (f_column
|| f_columnacross
|| f_stream
) {
283 if ((p
= getenv("COLUMNS")) != NULL
)
288 * If both -g and -l options, let -l take precedence.
290 if (f_grouponly
== -1)
294 * If not -F, -i, -l, -p, -S, -s or -t options, don't require stat
297 if (!f_inode
&& !f_longform
&& !f_size
&& !f_type
&& !f_typedir
&&
299 fts_options
|= FTS_NOSTAT
;
302 * If not -F, -d or -l options, follow any symbolic links listed on
305 if (!f_longform
&& !f_listdir
&& !f_type
)
306 fts_options
|= FTS_COMFOLLOW
;
309 * If -W, show whiteout entries
313 fts_options
|= FTS_WHITEOUT
;
316 /* If -l or -s, figure out block size. */
317 if (f_inode
|| f_longform
|| f_size
) {
319 (void)getbsize(NULL
, &blocksize
);
323 /* Select a sort function. */
327 sortfcn
= revnamecmp
;
330 sortfcn
= revsizecmp
;
335 else if (f_statustime
)
336 sortfcn
= revstatcmp
;
337 else /* Use modification time. */
352 else if (f_statustime
)
354 else /* Use modification time. */
360 /* Select a print function. */
362 printfcn
= printscol
;
363 else if (f_columnacross
)
364 printfcn
= printacol
;
366 printfcn
= printlong
;
368 printfcn
= printstream
;
373 traverse(argc
, argv
, fts_options
);
375 traverse(1, dotav
, fts_options
);
380 static int output
; /* If anything output. */
383 * Traverse() walks the logical directory structure specified by the argv list
384 * in the order specified by the mastercmp() comparison function. During the
385 * traversal it passes linked lists of structures to display() which represent
386 * a superset (may be exact set) of the files to be displayed.
389 traverse(int argc
, char *argv
[], int options
)
393 int ch_options
, error
;
396 fts_open(argv
, options
, f_nosort
? NULL
: mastercmp
)) == NULL
)
397 err(EXIT_FAILURE
, NULL
);
399 display(NULL
, fts_children(ftsp
, 0));
401 (void)fts_close(ftsp
);
406 * If not recursing down this tree and don't need stat info, just get
409 ch_options
= !f_recursive
&& options
& FTS_NOSTAT
? FTS_NAMEONLY
: 0;
411 while ((p
= fts_read(ftsp
)) != NULL
)
412 switch (p
->fts_info
) {
414 warnx("%s: directory causes a cycle", p
->fts_name
);
418 warnx("%s: %s", p
->fts_name
, strerror(p
->fts_errno
));
422 if (p
->fts_level
!= FTS_ROOTLEVEL
&&
423 p
->fts_name
[0] == '.' && !f_listdot
)
427 * If already output something, put out a newline as
428 * a separator. If multiple arguments, precede each
429 * directory with its name.
432 (void)printf("\n%s:\n", p
->fts_path
);
434 (void)printf("%s:\n", p
->fts_path
);
438 chp
= fts_children(ftsp
, ch_options
);
441 if (!f_recursive
&& chp
!= NULL
)
442 (void)fts_set(ftsp
, p
, FTS_SKIP
);
446 (void)fts_close(ftsp
);
449 err(EXIT_FAILURE
, "fts_read");
453 * Display() takes a linked list of FTSENT structures and passes the list
454 * along with any other necessary information to the print function. P
455 * points to the parent directory of the display list.
458 display(FTSENT
*p
, FTSENT
*list
)
464 u_int64_t btotal
, stotal
;
468 int maxmajor
, maxminor
;
470 int bcfile
, entries
, flen
, glen
, ulen
, maxflags
, maxgroup
;
472 int maxuser
, needstats
;
473 const char *user
, *group
;
474 char buf
[21]; /* 64 bits == 20 digits, +1 for NUL */
475 char nuser
[12], ngroup
[12];
479 * If list is NULL there are two possibilities: that the parent
480 * directory p has no children, or that fts_children() returned an
481 * error. We ignore the error case since it will be replicated
482 * on the next call to fts_read() on the post-order visit to the
483 * directory p, and will be signalled in traverse().
488 needstats
= f_inode
|| f_longform
|| f_size
;
490 maxinode
= maxnlink
= 0;
492 maxuser
= maxgroup
= maxflags
= maxlen
= 0;
493 btotal
= stotal
= maxblock
= maxsize
= 0;
494 maxmajor
= maxminor
= 0;
495 for (cur
= list
, entries
= 0; cur
; cur
= cur
->fts_link
) {
496 if (cur
->fts_info
== FTS_ERR
|| cur
->fts_info
== FTS_NS
) {
498 cur
->fts_name
, strerror(cur
->fts_errno
));
499 cur
->fts_number
= NO_PRINT
;
505 * P is NULL if list is the argv list, to which different rules
509 /* Directories will be displayed later. */
510 if (cur
->fts_info
== FTS_D
&& !f_listdir
) {
511 cur
->fts_number
= NO_PRINT
;
515 /* Only display dot file if -a/-A set. */
516 if (cur
->fts_name
[0] == '.' && !f_listdot
) {
517 cur
->fts_number
= NO_PRINT
;
521 if (cur
->fts_namelen
> maxlen
)
522 maxlen
= cur
->fts_namelen
;
525 if (sp
->st_blocks
> maxblock
)
526 maxblock
= sp
->st_blocks
;
527 if (sp
->st_ino
> maxinode
)
528 maxinode
= sp
->st_ino
;
529 if (sp
->st_nlink
> maxnlink
)
530 maxnlink
= sp
->st_nlink
;
531 if (sp
->st_size
> maxsize
)
532 maxsize
= sp
->st_size
;
533 if (S_ISCHR(sp
->st_mode
) || S_ISBLK(sp
->st_mode
)) {
535 if (major(sp
->st_rdev
) > maxmajor
)
536 maxmajor
= major(sp
->st_rdev
);
537 if (minor(sp
->st_rdev
) > maxminor
)
538 maxminor
= minor(sp
->st_rdev
);
541 btotal
+= sp
->st_blocks
;
542 stotal
+= sp
->st_size
;
545 (user
= user_from_uid(sp
->st_uid
, 0)) ==
547 (void)snprintf(nuser
, sizeof(nuser
),
552 (group
= group_from_gid(sp
->st_gid
, 0)) ==
554 (void)snprintf(ngroup
, sizeof(ngroup
),
558 if ((ulen
= strlen(user
)) > maxuser
)
560 if ((glen
= strlen(group
)) > maxgroup
)
564 flags_to_string((u_long
)sp
->st_flags
, "-");
565 if ((flen
= strlen(flags
)) > maxflags
)
570 if ((np
= malloc(sizeof(NAMES
) +
571 ulen
+ glen
+ flen
+ 2)) == NULL
)
572 err(EXIT_FAILURE
, NULL
);
574 np
->user
= &np
->data
[0];
575 (void)strcpy(np
->user
, user
);
576 np
->group
= &np
->data
[ulen
+ 1];
577 (void)strcpy(np
->group
, group
);
580 np
->flags
= &np
->data
[ulen
+ glen
+ 2];
581 (void)strcpy(np
->flags
, flags
);
584 cur
->fts_pointer
= np
;
600 d
.s_block
= 4; /* min buf length for humanize_number */
602 (void)snprintf(buf
, sizeof(buf
), "%llu",
603 (long long)howmany(maxblock
, blocksize
));
604 d
.s_block
= strlen(buf
);
606 d
.s_flags
= maxflags
;
607 d
.s_group
= maxgroup
;
608 (void)snprintf(buf
, sizeof(buf
), "%llu",
609 (unsigned long long)maxinode
);
610 d
.s_inode
= strlen(buf
);
611 (void)snprintf(buf
, sizeof(buf
), "%u", maxnlink
);
612 d
.s_nlink
= strlen(buf
);
614 d
.s_size
= 4; /* min buf length for humanize_number */
616 (void)snprintf(buf
, sizeof(buf
), "%llu",
618 d
.s_size
= strlen(buf
);
622 (void)snprintf(buf
, sizeof(buf
), "%u", maxmajor
);
623 d
.s_major
= strlen(buf
);
624 (void)snprintf(buf
, sizeof(buf
), "%u", maxminor
);
625 d
.s_minor
= strlen(buf
);
626 if (d
.s_major
+ d
.s_minor
+ 2 > d
.s_size
)
627 d
.s_size
= d
.s_major
+ d
.s_minor
+ 2;
628 else if (d
.s_size
- d
.s_minor
- 2 > d
.s_major
)
629 d
.s_major
= d
.s_size
- d
.s_minor
- 2;
640 for (cur
= list
; cur
; cur
= cur
->fts_link
)
641 free(cur
->fts_pointer
);
645 * Ordering for mastercmp:
646 * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories
647 * as larger than directories. Within either group, use the sort function.
648 * All other levels use the sort function. Error entries remain unsorted.
651 mastercmp(const FTSENT
**a
, const FTSENT
**b
)
655 a_info
= (*a
)->fts_info
;
656 if (a_info
== FTS_ERR
)
658 b_info
= (*b
)->fts_info
;
659 if (b_info
== FTS_ERR
)
662 if (a_info
== FTS_NS
|| b_info
== FTS_NS
) {
663 if (b_info
!= FTS_NS
)
665 else if (a_info
!= FTS_NS
)
668 return (namecmp(*a
, *b
));
671 if (a_info
!= b_info
&& !f_listdir
&&
672 (*a
)->fts_level
== FTS_ROOTLEVEL
) {
675 else if (b_info
== FTS_D
)
678 return (sortfcn(*a
, *b
));