]>
git.saurik.com Git - apple/file_cmds.git/blob - ls/ls.c
c943b9c91a6957448311538bd522e03212e9e755
1 /* $NetBSD: ls.c,v 1.31 1998/08/19 01:44:19 thorpej 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
41 __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\
42 The Regents of the University of California. All rights reserved.\n");
47 static char sccsid
[] = "@(#)ls.c 8.7 (Berkeley) 8/5/94";
49 __RCSID("$NetBSD: ls.c,v 1.31 1998/08/19 01:44:19 thorpej Exp $");
53 #include <sys/types.h>
55 #include <sys/ioctl.h>
56 #include <sys/param.h>
72 int main
__P((int, char *[]));
74 static void display
__P((FTSENT
*, FTSENT
*));
75 static int mastercmp
__P((const FTSENT
**, const FTSENT
**));
76 static void traverse
__P((int, char **, int));
78 static void (*printfcn
) __P((DISPLAY
*));
79 static int (*sortfcn
) __P((const FTSENT
*, const FTSENT
*));
85 long blocksize
; /* block size units */
86 int termwidth
= 80; /* default terminal width */
87 int sortkey
= BY_NAME
;
90 int f_accesstime
; /* use time of last access */
91 int f_column
; /* columnated format */
92 int f_columnacross
; /* columnated format, sorted across */
93 int f_flags
; /* show flags associated with a file */
94 int f_inode
; /* print inode */
95 int f_listdir
; /* list actual directory, not contents */
96 int f_listdot
; /* list files beginning with . */
97 int f_longform
; /* long listing format */
98 int f_nonprint
; /* show unprintables as ? */
99 int f_nosort
; /* don't sort output */
100 int f_numericonly
; /* don't convert uid/gid to name */
101 int f_recursive
; /* ls subdirectories also */
102 int f_reversesort
; /* reverse whatever sort is used */
103 int f_sectime
; /* print the real time for all files */
104 int f_singlecol
; /* use single column output */
105 int f_size
; /* list size in short listing */
106 int f_statustime
; /* use time of last mode change */
107 int f_type
; /* add type character for non-regular files */
108 int f_whiteout
; /* show whiteout entries */
115 static char dot
[] = ".", *dotav
[] = { dot
, NULL
};
117 int ch
, fts_options
, notused
;
121 /* Terminal defaults to -Cq, non-terminal defaults to -1. */
122 if (isatty(STDOUT_FILENO
)) {
123 if ((p
= getenv("COLUMNS")) != NULL
)
125 else if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &win
) == 0 &&
127 termwidth
= win
.ws_col
;
128 f_column
= f_nonprint
= 1;
132 /* Root is -A automatically. */
136 fts_options
= FTS_PHYSICAL
;
137 while ((ch
= getopt(argc
, argv
, "1ACFLRSTWacdfgiklnoqrstuxv")) != -1) {
140 * The -1, -C, -l and -x options all override each other so
141 * shell aliasing works correctly.
145 f_column
= f_columnacross
= f_longform
= 0;
149 f_columnacross
= f_longform
= f_singlecol
= 0;
153 f_column
= f_columnacross
= f_singlecol
= 0;
157 f_column
= f_longform
= f_singlecol
= 0;
159 /* The -c and -u options override each other. */
172 fts_options
&= ~FTS_PHYSICAL
;
173 fts_options
|= FTS_LOGICAL
;
179 fts_options
|= FTS_SEEDOT
;
184 /* The -d option turns off the -R option. */
192 case 'g': /* Compatibility with 4.3BSD. */
240 * If not -F, -i, -l, -S, -s or -t options, don't require stat
243 if (!f_inode
&& !f_longform
&& !f_size
&& !f_type
&&
245 fts_options
|= FTS_NOSTAT
;
248 * If not -F, -d or -l options, follow any symbolic links listed on
251 if (!f_longform
&& !f_listdir
&& !f_type
)
252 fts_options
|= FTS_COMFOLLOW
;
255 * If -W, show whiteout entries
259 fts_options
|= FTS_WHITEOUT
;
262 /* If -l or -s, figure out block size. */
263 if (f_longform
|| f_size
) {
265 (void)getbsize(¬used
, &blocksize
);
269 /* Select a sort function. */
273 sortfcn
= revnamecmp
;
276 sortfcn
= revsizecmp
;
281 else if (f_statustime
)
282 sortfcn
= revstatcmp
;
283 else /* Use modification time. */
298 else if (f_statustime
)
300 else /* Use modification time. */
306 /* Select a print function. */
308 printfcn
= printscol
;
309 else if (f_columnacross
)
310 printfcn
= printacol
;
312 printfcn
= printlong
;
317 traverse(argc
, argv
, fts_options
);
319 traverse(1, dotav
, fts_options
);
324 static int output
; /* If anything output. */
327 * Traverse() walks the logical directory structure specified by the argv list
328 * in the order specified by the mastercmp() comparison function. During the
329 * traversal it passes linked lists of structures to display() which represent
330 * a superset (may be exact set) of the files to be displayed.
333 traverse(argc
, argv
, options
)
340 char pbuf
[MAXPATHLEN
+ 1], *path
;
343 fts_open(argv
, options
, f_nosort
? NULL
: mastercmp
)) == NULL
)
346 display(NULL
, fts_children(ftsp
, 0));
351 * If not recursing down this tree and don't need stat info, just get
354 ch_options
= !f_recursive
&& options
& FTS_NOSTAT
? FTS_NAMEONLY
: 0;
356 while ((p
= fts_read(ftsp
)) != NULL
)
357 switch (p
->fts_info
) {
359 warnx("%s: directory causes a cycle", p
->fts_name
);
363 warnx("%s: %s", p
->fts_name
, strerror(p
->fts_errno
));
366 if (p
->fts_level
!= FTS_ROOTLEVEL
&&
367 p
->fts_name
[0] == '.' && !f_listdot
)
371 * If already output something, put out a newline as
372 * a separator. If multiple arguments, precede each
373 * directory with its name.
376 prcopy(p
->fts_path
, pbuf
, p
->fts_pathlen
+1);
382 (void)printf("\n%s:\n", path
);
384 (void)printf("%s:\n", path
);
388 chp
= fts_children(ftsp
, ch_options
);
391 if (!f_recursive
&& chp
!= NULL
)
392 (void)fts_set(ftsp
, p
, FTS_SKIP
);
400 * Display() takes a linked list of FTSENT structures and passes the list
401 * along with any other necessary information to the print function. P
402 * points to the parent directory of the display list.
412 u_int64_t btotal
, maxblock
, maxsize
;
413 int maxinode
, maxnlink
, maxmajor
, maxminor
;
414 int bcfile
, entries
, flen
, glen
, ulen
, maxflags
, maxgroup
, maxlen
;
415 int maxuser
, needstats
;
416 const char *user
, *group
;
417 char ubuf
[21]="", gbuf
[21]="", buf
[21]; /* 64 bits == 20 digits, +1 for NUL */
418 char nuser
[12], ngroup
[12];
422 /* This outrageous construct just to shut up a GCC warning. */
427 * If list is NULL there are two possibilities: that the parent
428 * directory p has no children, or that fts_children() returned an
429 * error. We ignore the error case since it will be replicated
430 * on the next call to fts_read() on the post-order visit to the
431 * directory p, and will be signalled in traverse().
436 needstats
= f_inode
|| f_longform
|| f_size
;
438 maxinode
= maxnlink
= 0;
440 maxuser
= maxgroup
= maxflags
= maxlen
= 0;
441 btotal
= maxblock
= maxsize
= 0;
442 maxmajor
= maxminor
= 0;
443 for (cur
= list
, entries
= 0; cur
; cur
= cur
->fts_link
) {
444 if (cur
->fts_info
== FTS_ERR
|| cur
->fts_info
== FTS_NS
) {
446 cur
->fts_name
, strerror(cur
->fts_errno
));
447 cur
->fts_number
= NO_PRINT
;
452 * P is NULL if list is the argv list, to which different rules
456 /* Directories will be displayed later. */
457 if (cur
->fts_info
== FTS_D
&& !f_listdir
) {
458 cur
->fts_number
= NO_PRINT
;
462 /* Only display dot file if -a/-A set. */
463 if (cur
->fts_name
[0] == '.' && !f_listdot
) {
464 cur
->fts_number
= NO_PRINT
;
468 if (cur
->fts_namelen
> maxlen
)
469 maxlen
= cur
->fts_namelen
;
472 if (sp
->st_blocks
> maxblock
)
473 maxblock
= sp
->st_blocks
;
474 if (sp
->st_ino
> maxinode
)
475 maxinode
= sp
->st_ino
;
476 if (sp
->st_nlink
> maxnlink
)
477 maxnlink
= sp
->st_nlink
;
478 if (sp
->st_size
> maxsize
)
479 maxsize
= sp
->st_size
;
480 if (S_ISCHR(sp
->st_mode
) || S_ISBLK(sp
->st_mode
)) {
482 if (major(sp
->st_rdev
) > maxmajor
)
483 maxmajor
= major(sp
->st_rdev
);
484 if (minor(sp
->st_rdev
) > maxminor
)
485 maxminor
= minor(sp
->st_rdev
);
488 btotal
+= sp
->st_blocks
;
491 (void)snprintf(nuser
, sizeof(nuser
),
493 (void)snprintf(ngroup
, sizeof(ngroup
),
498 if ((user
= user_from_uid(sp
->st_uid
, 0)) == NULL
) {
499 (void)snprintf(ubuf
, sizeof(ubuf
), "%d", (int)sp
->st_uid
);
502 if ((group
= group_from_gid(sp
->st_gid
, 0)) == NULL
) {
503 (void)snprintf(gbuf
, sizeof(gbuf
), "%d", (int)sp
->st_gid
);
507 if ((ulen
= strlen(user
)) > maxuser
)
509 if ((glen
= strlen(group
)) > maxgroup
)
513 flags_to_string(sp
->st_flags
, "-");
514 if ((flen
= strlen(flags
)) > maxflags
)
519 if ((np
= malloc(sizeof(NAMES
) +
520 ulen
+ glen
+ flen
+ 3)) == NULL
)
523 np
->user
= &np
->data
[0];
524 (void)strcpy(np
->user
, user
);
525 np
->group
= &np
->data
[ulen
+ 1];
526 (void)strcpy(np
->group
, group
);
529 np
->flags
= &np
->data
[ulen
+ glen
+ 2];
530 (void)strcpy(np
->flags
, flags
);
532 cur
->fts_pointer
= np
;
546 (void)snprintf(buf
, sizeof(buf
), "%qu", (long long)maxblock
);
547 d
.s_block
= strlen(buf
);
548 d
.s_flags
= maxflags
;
549 d
.s_group
= maxgroup
;
550 (void)snprintf(buf
, sizeof(buf
), "%u", maxinode
);
551 d
.s_inode
= strlen(buf
);
552 (void)snprintf(buf
, sizeof(buf
), "%u", maxnlink
);
553 d
.s_nlink
= strlen(buf
);
554 (void)snprintf(buf
, sizeof(buf
), "%qu", (long long)maxsize
);
555 d
.s_size
= strlen(buf
);
558 (void)snprintf(buf
, sizeof(buf
), "%u", maxmajor
);
559 d
.s_major
= strlen(buf
);
560 (void)snprintf(buf
, sizeof(buf
), "%u", maxminor
);
561 d
.s_minor
= strlen(buf
);
562 if (d
.s_major
+ d
.s_minor
+ 2 > d
.s_size
)
563 d
.s_size
= d
.s_major
+ d
.s_minor
+ 2;
564 else if (d
.s_size
- d
.s_minor
- 2 > d
.s_major
)
565 d
.s_major
= d
.s_size
- d
.s_minor
- 2;
576 for (cur
= list
; cur
; cur
= cur
->fts_link
)
577 free(cur
->fts_pointer
);
581 * Ordering for mastercmp:
582 * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories
583 * as larger than directories. Within either group, use the sort function.
584 * All other levels use the sort function. Error entries remain unsorted.
588 const FTSENT
**a
, **b
;
592 a_info
= (*a
)->fts_info
;
593 if (a_info
== FTS_ERR
)
595 b_info
= (*b
)->fts_info
;
596 if (b_info
== FTS_ERR
)
599 if (a_info
== FTS_NS
|| b_info
== FTS_NS
) {
600 if (b_info
!= FTS_NS
)
602 else if (a_info
!= FTS_NS
)
605 return (namecmp(*a
, *b
));
608 if (a_info
!= b_info
&& !f_listdir
&&
609 (*a
)->fts_level
== FTS_ROOTLEVEL
) {
612 else if (b_info
== FTS_D
)
615 return (sortfcn(*a
, *b
));