1 /* $NetBSD: ls.c,v 1.76 2017/02/06 21:06:04 rin 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.76 2017/02/06 21:06:04 rin 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 */
114 int f_fullpath
; /* print full pathname, not filename */
115 int f_leafonly
; /* when recursing, print leaf names only */
121 (void)fprintf(stderr
,
122 "usage: %s [-1AaBbCcdFfghikLlMmnOoPpqRrSsTtuWwXx] [file ...]\n",
129 ls_main(int argc
, char *argv
[])
131 static char dot
[] = ".", *dotav
[] = { dot
, NULL
};
137 setprogname(argv
[0]);
138 (void)setlocale(LC_ALL
, "");
140 /* Terminal defaults to -Cq, non-terminal defaults to -1. */
141 if (isatty(STDOUT_FILENO
)) {
142 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &win
) == 0 &&
144 termwidth
= win
.ws_col
;
145 f_column
= f_nonprint
= 1;
149 /* Root is -A automatically. */
153 fts_options
= FTS_PHYSICAL
;
154 while ((ch
= getopt(argc
, argv
, "1AaBbCcdFfghikLlMmnOoPpqRrSsTtuWwXx"))
158 * The -1, -C, -l, -m and -x options all override each other so
159 * shell aliasing works correctly.
163 f_column
= f_columnacross
= f_longform
= f_stream
= 0;
167 f_columnacross
= f_longform
= f_singlecol
= f_stream
=
171 if (f_grouponly
!= -1)
174 f_column
= f_columnacross
= f_singlecol
= f_stream
= 0;
178 f_column
= f_columnacross
= f_singlecol
= f_stream
= 0;
179 /* Never let -g take precedence over -l. */
184 f_column
= f_columnacross
= f_longform
= f_singlecol
=
189 f_column
= f_longform
= f_singlecol
= f_stream
= 0;
191 /* The -c and -u options override each other. */
204 fts_options
&= ~FTS_PHYSICAL
;
205 fts_options
|= FTS_LOGICAL
;
214 fts_options
|= FTS_SEEDOT
;
219 /* The -B option turns off the -b, -q and -w options. */
225 /* The -b option turns off the -B, -q and -w options. */
231 /* The -d option turns off the -R option. */
244 /* The -h option forces all sizes to be measured in bytes. */
257 f_column
= f_columnacross
= f_singlecol
= f_stream
= 0;
271 /* The -q option turns off the -B, -b and -w options. */
295 /* The -w option turns off the -B, -b and -q options. */
302 fts_options
|= FTS_XDEV
;
312 if (f_column
|| f_columnacross
|| f_stream
) {
313 if ((p
= getenv("COLUMNS")) != NULL
)
318 * If both -g and -l options, let -l take precedence.
320 if (f_grouponly
== -1)
324 * If not -F, -i, -l, -p, -S, -s or -t options, don't require stat
327 if (!f_inode
&& !f_longform
&& !f_size
&& !f_type
&& !f_typedir
&&
329 fts_options
|= FTS_NOSTAT
;
332 * If not -F, -d or -l options, follow any symbolic links listed on
335 if (!f_longform
&& !f_listdir
&& !f_type
)
336 fts_options
|= FTS_COMFOLLOW
;
339 * If -W, show whiteout entries
343 fts_options
|= FTS_WHITEOUT
;
346 /* If -i, -l, or -s, figure out block size. */
347 if (f_inode
|| f_longform
|| f_size
) {
349 (void)getbsize(NULL
, &blocksize
);
353 /* Select a sort function. */
357 sortfcn
= revnamecmp
;
360 sortfcn
= revsizecmp
;
365 else if (f_statustime
)
366 sortfcn
= revstatcmp
;
367 else /* Use modification time. */
382 else if (f_statustime
)
384 else /* Use modification time. */
390 /* Select a print function. */
392 printfcn
= printscol
;
393 else if (f_columnacross
)
394 printfcn
= printacol
;
396 printfcn
= printlong
;
398 printfcn
= printstream
;
403 traverse(argc
, argv
, fts_options
);
405 traverse(1, dotav
, fts_options
);
410 static int output
; /* If anything output. */
413 * Traverse() walks the logical directory structure specified by the argv list
414 * in the order specified by the mastercmp() comparison function. During the
415 * traversal it passes linked lists of structures to display() which represent
416 * a superset (may be exact set) of the files to be displayed.
419 traverse(int argc
, char *argv
[], int options
)
423 int ch_options
, error
;
426 fts_open(argv
, options
, f_nosort
? NULL
: mastercmp
)) == NULL
)
427 err(EXIT_FAILURE
, NULL
);
429 display(NULL
, fts_children(ftsp
, 0));
431 (void)fts_close(ftsp
);
436 * If not recursing down this tree and don't need stat info, just get
439 ch_options
= !f_recursive
&& options
& FTS_NOSTAT
? FTS_NAMEONLY
: 0;
441 while ((p
= fts_read(ftsp
)) != NULL
)
442 switch (p
->fts_info
) {
444 warnx("%s: directory causes a cycle", p
->fts_name
);
448 warnx("%s: %s", p
->fts_name
, strerror(p
->fts_errno
));
452 if (p
->fts_level
!= FTS_ROOTLEVEL
&&
453 p
->fts_name
[0] == '.' && !f_listdot
)
457 * If already output something, put out a newline as
458 * a separator. If multiple arguments, precede each
459 * directory with its name.
463 (void)printf("\n%s:\n", p
->fts_path
);
465 (void)printf("%s:\n", p
->fts_path
);
470 chp
= fts_children(ftsp
, ch_options
);
473 if (!f_recursive
&& chp
!= NULL
)
474 (void)fts_set(ftsp
, p
, FTS_SKIP
);
478 (void)fts_close(ftsp
);
481 err(EXIT_FAILURE
, "fts_read");
485 * Display() takes a linked list of FTSENT structures and passes the list
486 * along with any other necessary information to the print function. P
487 * points to the parent directory of the display list.
490 display(FTSENT
*p
, FTSENT
*list
)
496 u_int64_t btotal
, stotal
;
500 int maxmajor
, maxminor
;
502 int bcfile
, entries
, flen
, glen
, ulen
, maxflags
, maxgroup
;
504 int maxuser
, needstats
;
505 const char *user
, *group
;
506 char buf
[21]; /* 64 bits == 20 digits, +1 for NUL */
507 char nuser
[12], ngroup
[12];
511 * If list is NULL there are two possibilities: that the parent
512 * directory p has no children, or that fts_children() returned an
513 * error. We ignore the error case since it will be replicated
514 * on the next call to fts_read() on the post-order visit to the
515 * directory p, and will be signalled in traverse().
520 needstats
= f_inode
|| f_longform
|| f_size
;
522 maxinode
= maxnlink
= 0;
524 maxuser
= maxgroup
= maxflags
= maxlen
= 0;
525 btotal
= stotal
= maxblock
= maxsize
= 0;
526 maxmajor
= maxminor
= 0;
527 for (cur
= list
, entries
= 0; cur
; cur
= cur
->fts_link
) {
528 if (cur
->fts_info
== FTS_ERR
|| cur
->fts_info
== FTS_NS
) {
530 cur
->fts_name
, strerror(cur
->fts_errno
));
531 cur
->fts_number
= NO_PRINT
;
537 * P is NULL if list is the argv list, to which different rules
541 /* Directories will be displayed later. */
542 if (cur
->fts_info
== FTS_D
&& !f_listdir
) {
543 cur
->fts_number
= NO_PRINT
;
547 /* Only display dot file if -a/-A set. */
548 if (cur
->fts_name
[0] == '.' && !f_listdot
) {
549 cur
->fts_number
= NO_PRINT
;
553 if (cur
->fts_namelen
> maxlen
)
554 maxlen
= cur
->fts_namelen
;
557 if (sp
->st_blocks
> maxblock
)
558 maxblock
= sp
->st_blocks
;
559 if (sp
->st_ino
> maxinode
)
560 maxinode
= sp
->st_ino
;
561 if (sp
->st_nlink
> maxnlink
)
562 maxnlink
= sp
->st_nlink
;
563 if (sp
->st_size
> maxsize
)
564 maxsize
= sp
->st_size
;
565 if (S_ISCHR(sp
->st_mode
) || S_ISBLK(sp
->st_mode
)) {
567 if (major(sp
->st_rdev
) > maxmajor
)
568 maxmajor
= major(sp
->st_rdev
);
569 if (minor(sp
->st_rdev
) > maxminor
)
570 maxminor
= minor(sp
->st_rdev
);
573 btotal
+= sp
->st_blocks
;
574 stotal
+= sp
->st_size
;
577 (user
= user_from_uid(sp
->st_uid
, 0)) ==
579 (void)snprintf(nuser
, sizeof(nuser
),
584 (group
= group_from_gid(sp
->st_gid
, 0)) ==
586 (void)snprintf(ngroup
, sizeof(ngroup
),
590 if ((ulen
= strlen(user
)) > maxuser
)
592 if ((glen
= strlen(group
)) > maxgroup
)
596 flags_to_string((u_long
)sp
->st_flags
, "-");
597 if ((flen
= strlen(flags
)) > maxflags
)
602 if ((np
= malloc(sizeof(NAMES
) +
603 ulen
+ glen
+ flen
+ 2)) == NULL
)
604 err(EXIT_FAILURE
, NULL
);
606 np
->user
= &np
->data
[0];
607 (void)strcpy(np
->user
, user
);
608 np
->group
= &np
->data
[ulen
+ 1];
609 (void)strcpy(np
->group
, group
);
612 np
->flags
= &np
->data
[ulen
+ glen
+ 2];
613 (void)strcpy(np
->flags
, flags
);
616 cur
->fts_pointer
= np
;
632 d
.s_block
= 4; /* min buf length for humanize_number */
634 (void)snprintf(buf
, sizeof(buf
), "%lld",
635 (long long)howmany(maxblock
, blocksize
));
636 d
.s_block
= strlen(buf
);
637 if (f_commas
) /* allow for commas before every third digit */
638 d
.s_block
+= (d
.s_block
- 1) / 3;
640 d
.s_flags
= maxflags
;
641 d
.s_group
= maxgroup
;
642 (void)snprintf(buf
, sizeof(buf
), "%llu",
643 (unsigned long long)maxinode
);
644 d
.s_inode
= strlen(buf
);
645 (void)snprintf(buf
, sizeof(buf
), "%u", maxnlink
);
646 d
.s_nlink
= strlen(buf
);
648 d
.s_size
= 4; /* min buf length for humanize_number */
650 (void)snprintf(buf
, sizeof(buf
), "%lld",
652 d
.s_size
= strlen(buf
);
653 if (f_commas
) /* allow for commas before every third digit */
654 d
.s_size
+= (d
.s_size
- 1) / 3;
658 (void)snprintf(buf
, sizeof(buf
), "%d", maxmajor
);
659 d
.s_major
= strlen(buf
);
660 (void)snprintf(buf
, sizeof(buf
), "%d", maxminor
);
661 d
.s_minor
= strlen(buf
);
662 if (d
.s_major
+ d
.s_minor
+ 2 > d
.s_size
)
663 d
.s_size
= d
.s_major
+ d
.s_minor
+ 2;
664 else if (d
.s_size
- d
.s_minor
- 2 > d
.s_major
)
665 d
.s_major
= d
.s_size
- d
.s_minor
- 2;
676 for (cur
= list
; cur
; cur
= cur
->fts_link
)
677 free(cur
->fts_pointer
);
681 * Ordering for mastercmp:
682 * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories
683 * as larger than directories. Within either group, use the sort function.
684 * All other levels use the sort function. Error entries remain unsorted.
687 mastercmp(const FTSENT
**a
, const FTSENT
**b
)
691 a_info
= (*a
)->fts_info
;
692 if (a_info
== FTS_ERR
)
694 b_info
= (*b
)->fts_info
;
695 if (b_info
== FTS_ERR
)
698 if (a_info
== FTS_NS
|| b_info
== FTS_NS
) {
699 if (b_info
!= FTS_NS
)
701 else if (a_info
!= FTS_NS
)
704 return (namecmp(*a
, *b
));
707 if (a_info
!= b_info
&& !f_listdir
&&
708 (*a
)->fts_level
== FTS_ROOTLEVEL
) {
711 else if (b_info
== FTS_D
)
714 return (sortfcn(*a
, *b
));