Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 9d3aba7 | 1999-10-06 09:04:55 +0000 | [diff] [blame] | 2 | /* |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 3 | * tiny-ls.c version 0.1.0: A minimalist 'ls' |
| 4 | * Copyright (C) 1996 Brian Candler <B.Candler@pobox.com> |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 5 | * |
Bernhard Reutner-Fischer | cb44816 | 2006-04-12 07:35:12 +0000 | [diff] [blame] | 6 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * To achieve a small memory footprint, this version of 'ls' doesn't do any |
| 11 | * file sorting, and only has the most essential command line switches |
Eric Andersen | 77d9268 | 2001-05-23 20:32:09 +0000 | [diff] [blame] | 12 | * (i.e., the ones I couldn't live without :-) All features which involve |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 13 | * linking in substantial chunks of libc can be disabled. |
| 14 | * |
| 15 | * Although I don't really want to add new features to this program to |
| 16 | * keep it small, I *am* interested to receive bug fixes and ways to make |
| 17 | * it more portable. |
| 18 | * |
| 19 | * KNOWN BUGS: |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 20 | * 1. ls -l of a directory doesn't give "total <blocks>" header |
| 21 | * 2. ls of a symlink to a directory doesn't list directory contents |
| 22 | * 3. hidden files can make column width too large |
| 23 | * |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 24 | * NON-OPTIMAL BEHAVIOUR: |
| 25 | * 1. autowidth reads directories twice |
| 26 | * 2. if you do a short directory listing without filetype characters |
| 27 | * appended, there's no need to stat each one |
| 28 | * PORTABILITY: |
| 29 | * 1. requires lstat (BSD) - how do you do it without? |
| 30 | */ |
| 31 | |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 32 | #include "busybox.h" |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 33 | #include <getopt.h> |
Eric Andersen | f1142c5 | 2001-02-20 06:16:29 +0000 | [diff] [blame] | 34 | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 35 | enum { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 36 | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 37 | TERMINAL_WIDTH = 80, /* use 79 if terminal has linefold bug */ |
| 38 | COLUMN_GAP = 2, /* includes the file type char */ |
| 39 | |
| 40 | /* what is the overall style of the listing */ |
| 41 | STYLE_COLUMNS = 1 << 21, /* fill columns */ |
| 42 | STYLE_LONG = 2 << 21, /* one record per line, extended info */ |
| 43 | STYLE_SINGLE = 3 << 21, /* one record per line */ |
| 44 | STYLE_MASK = STYLE_SINGLE, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 45 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 46 | /* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */ |
| 47 | /* what file information will be listed */ |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 48 | LIST_INO = 1 << 0, |
| 49 | LIST_BLOCKS = 1 << 1, |
| 50 | LIST_MODEBITS = 1 << 2, |
| 51 | LIST_NLINKS = 1 << 3, |
| 52 | LIST_ID_NAME = 1 << 4, |
| 53 | LIST_ID_NUMERIC = 1 << 5, |
| 54 | LIST_CONTEXT = 1 << 6, |
| 55 | LIST_SIZE = 1 << 7, |
| 56 | LIST_DEV = 1 << 8, |
| 57 | LIST_DATE_TIME = 1 << 9, |
| 58 | LIST_FULLTIME = 1 << 10, |
| 59 | LIST_FILENAME = 1 << 11, |
| 60 | LIST_SYMLINK = 1 << 12, |
| 61 | LIST_FILETYPE = 1 << 13, |
| 62 | LIST_EXEC = 1 << 14, |
| 63 | LIST_MASK = (LIST_EXEC << 1) - 1, |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 64 | |
| 65 | /* what files will be displayed */ |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 66 | DISP_DIRNAME = 1 << 15, /* 2 or more items? label directories */ |
Denis Vlasenko | 656f746 | 2006-10-28 13:02:55 +0000 | [diff] [blame] | 67 | DISP_HIDDEN = 1 << 16, /* show filenames starting with . */ |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 68 | DISP_DOT = 1 << 17, /* show . and .. */ |
| 69 | DISP_NOLIST = 1 << 18, /* show directory as itself, not contents */ |
| 70 | DISP_RECURSIVE = 1 << 19, /* show directory and everything below it */ |
| 71 | DISP_ROWS = 1 << 20, /* print across rows */ |
| 72 | DISP_MASK = ((DISP_ROWS << 1) - 1) & ~(DISP_DIRNAME - 1), |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 73 | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 74 | /* how will the files be sorted (CONFIG_FEATURE_LS_SORTFILES) */ |
| 75 | SORT_FORWARD = 0, /* sort in reverse order */ |
| 76 | SORT_REVERSE = 1 << 27, /* sort in reverse order */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 77 | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 78 | SORT_NAME = 0, /* sort by file name */ |
| 79 | SORT_SIZE = 1 << 28, /* sort by file size */ |
| 80 | SORT_ATIME = 2 << 28, /* sort by last access time */ |
| 81 | SORT_CTIME = 3 << 28, /* sort by last change time */ |
| 82 | SORT_MTIME = 4 << 28, /* sort by last modification time */ |
| 83 | SORT_VERSION = 5 << 28, /* sort by version */ |
| 84 | SORT_EXT = 6 << 28, /* sort by file name extension */ |
| 85 | SORT_DIR = 7 << 28, /* sort by file or directory */ |
| 86 | SORT_MASK = (7 << 28) * ENABLE_FEATURE_LS_SORTFILES, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 87 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 88 | /* which of the three times will be used */ |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 89 | TIME_CHANGE = (1 << 23) * ENABLE_FEATURE_LS_TIMESTAMPS, |
| 90 | TIME_ACCESS = (1 << 24) * ENABLE_FEATURE_LS_TIMESTAMPS, |
| 91 | TIME_MASK = (3 << 23) * ENABLE_FEATURE_LS_TIMESTAMPS, |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 92 | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 93 | FOLLOW_LINKS = (1 << 25) * ENABLE_FEATURE_LS_FOLLOWLINKS, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 94 | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 95 | LS_DISP_HR = (1 << 26) * ENABLE_FEATURE_HUMAN_READABLE, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 96 | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 97 | LIST_SHORT = LIST_FILENAME, |
| 98 | LIST_LONG = LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \ |
| 99 | LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK, |
| 100 | |
| 101 | SPLIT_DIR = 1, |
| 102 | SPLIT_FILE = 0, |
| 103 | SPLIT_SUBDIR = 2, |
| 104 | |
| 105 | }; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 106 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 107 | #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f) |
| 108 | #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)]) |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 109 | #define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)]) |
| 110 | #define COLOR(mode) ("\000\043\043\043\042\000\043\043"\ |
| 111 | "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)]) |
| 112 | #define ATTR(mode) ("\00\00\01\00\01\00\01\00"\ |
| 113 | "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)]) |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 114 | |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 115 | /* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */ |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 116 | #if ENABLE_FEATURE_LS_COLOR |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 117 | static int show_color; |
Paul Fox | 156dc41 | 2005-08-01 19:33:30 +0000 | [diff] [blame] | 118 | /* long option entry used only for --color, which has no short option |
Denis Vlasenko | 656f746 | 2006-10-28 13:02:55 +0000 | [diff] [blame] | 119 | * equivalent */ |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 120 | static const struct option ls_color_opt[] = { |
| 121 | { "color", optional_argument, NULL, 1 }, |
| 122 | { NULL, 0, NULL, 0 } |
Paul Fox | 156dc41 | 2005-08-01 19:33:30 +0000 | [diff] [blame] | 123 | }; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 124 | #else |
| 125 | enum { show_color = 0 }; |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 126 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 127 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 128 | /* |
| 129 | * a directory entry and its stat info are stored here |
| 130 | */ |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 131 | struct dnode { /* the basic node */ |
| 132 | char *name; /* the dir entry name */ |
| 133 | char *fullname; /* the dir entry name */ |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 134 | int allocated; |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 135 | struct stat dstat; /* the file stat info */ |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 136 | USE_SELINUX(security_context_t sid;) |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 137 | struct dnode *next; /* point at the next node */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 138 | }; |
| 139 | typedef struct dnode dnode_t; |
| 140 | |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 141 | static struct dnode **list_dir(const char *); |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 142 | static struct dnode **dnalloc(int); |
| 143 | static int list_single(struct dnode *); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 144 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 145 | static unsigned all_fmt; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 146 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 147 | #if ENABLE_FEATURE_AUTOWIDTH |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 148 | static unsigned tabstops = COLUMN_GAP; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 149 | static unsigned terminal_width = TERMINAL_WIDTH; |
Eric Andersen | f5d5e77 | 2001-01-24 23:34:48 +0000 | [diff] [blame] | 150 | #else |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 151 | enum { |
| 152 | tabstops = COLUMN_GAP, |
| 153 | terminal_width = TERMINAL_WIDTH, |
| 154 | }; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 155 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 156 | |
Matt Kraai | 33fdae5 | 2000-10-13 17:59:43 +0000 | [diff] [blame] | 157 | static int status = EXIT_SUCCESS; |
| 158 | |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 159 | static struct dnode *my_stat(char *fullname, char *name) |
Matt Kraai | 9a6e67c | 2000-10-13 18:03:21 +0000 | [diff] [blame] | 160 | { |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 161 | struct stat dstat; |
| 162 | struct dnode *cur; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 163 | USE_SELINUX(security_context_t sid = NULL;) |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 164 | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 165 | if (all_fmt & FOLLOW_LINKS) { |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 166 | #if ENABLE_SELINUX |
Bernhard Reutner-Fischer | d2c306e | 2006-05-29 12:10:23 +0000 | [diff] [blame] | 167 | if (is_selinux_enabled()) { |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 168 | getfilecon(fullname, &sid); |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 169 | } |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 170 | #endif |
Bernhard Reutner-Fischer | d2c306e | 2006-05-29 12:10:23 +0000 | [diff] [blame] | 171 | if (stat(fullname, &dstat)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 172 | bb_perror_msg("%s", fullname); |
Matt Kraai | 9a6e67c | 2000-10-13 18:03:21 +0000 | [diff] [blame] | 173 | status = EXIT_FAILURE; |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 174 | return 0; |
Matt Kraai | 9a6e67c | 2000-10-13 18:03:21 +0000 | [diff] [blame] | 175 | } |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 176 | } else { |
| 177 | #if ENABLE_SELINUX |
| 178 | if (is_selinux_enabled()) { |
| 179 | lgetfilecon(fullname,&sid); |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 180 | } |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 181 | #endif |
Bernhard Reutner-Fischer | d2c306e | 2006-05-29 12:10:23 +0000 | [diff] [blame] | 182 | if (lstat(fullname, &dstat)) { |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 183 | bb_perror_msg("%s", fullname); |
| 184 | status = EXIT_FAILURE; |
| 185 | return 0; |
| 186 | } |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 187 | } |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 188 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 189 | cur = xmalloc(sizeof(struct dnode)); |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 190 | cur->fullname = fullname; |
| 191 | cur->name = name; |
| 192 | cur->dstat = dstat; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 193 | USE_SELINUX(cur->sid = sid;) |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 194 | return cur; |
Eric Andersen | 79565b6 | 2000-08-11 18:10:21 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 197 | #if ENABLE_FEATURE_LS_COLOR |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 198 | static char fgcolor(mode_t mode) |
| 199 | { |
| 200 | /* Check wheter the file is existing (if so, color it red!) */ |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 201 | if (errno == ENOENT) |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 202 | return '\037'; |
Rob Landley | 9947a24 | 2006-06-15 22:11:10 +0000 | [diff] [blame] | 203 | if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH))) |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 204 | return COLOR(0xF000); /* File is executable ... */ |
| 205 | return COLOR(mode); |
| 206 | } |
| 207 | |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 208 | static char bgcolor(mode_t mode) |
| 209 | { |
Rob Landley | 9947a24 | 2006-06-15 22:11:10 +0000 | [diff] [blame] | 210 | if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH))) |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 211 | return ATTR(0xF000); /* File is executable ... */ |
| 212 | return ATTR(mode); |
| 213 | } |
| 214 | #endif |
| 215 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 216 | #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 217 | static char append_char(mode_t mode) |
Eric Andersen | 79565b6 | 2000-08-11 18:10:21 +0000 | [diff] [blame] | 218 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 219 | if (!(all_fmt & LIST_FILETYPE)) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 220 | return '\0'; |
Rob Landley | 9947a24 | 2006-06-15 22:11:10 +0000 | [diff] [blame] | 221 | if (S_ISDIR(mode)) |
| 222 | return '/'; |
| 223 | if (!(all_fmt & LIST_EXEC)) |
| 224 | return '\0'; |
| 225 | if (S_ISREG(mode) && (mode & (S_IXUSR | S_IXGRP | S_IXOTH))) |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 226 | return '*'; |
| 227 | return APPCHAR(mode); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 228 | } |
| 229 | #endif |
Eric Andersen | 79565b6 | 2000-08-11 18:10:21 +0000 | [diff] [blame] | 230 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 231 | #define countdirs(A, B) count_dirs((A), (B), 1) |
| 232 | #define countsubdirs(A, B) count_dirs((A), (B), 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 233 | static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 234 | { |
| 235 | int i, dirs; |
| 236 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 237 | if (!dn) |
| 238 | return 0; |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 239 | dirs = 0; |
| 240 | for (i = 0; i < nfiles; i++) { |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 241 | char *name; |
| 242 | if (!S_ISDIR(dn[i]->dstat.st_mode)) |
| 243 | continue; |
| 244 | name = dn[i]->name; |
| 245 | if (notsubdirs |
| 246 | || name[0]!='.' || (name[1] && (name[1]!='.' || name[2])) |
| 247 | ) { |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 248 | dirs++; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 249 | } |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 250 | } |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 251 | return dirs; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 254 | static int countfiles(struct dnode **dnp) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 255 | { |
| 256 | int nfiles; |
| 257 | struct dnode *cur; |
| 258 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 259 | if (dnp == NULL) |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 260 | return 0; |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 261 | nfiles = 0; |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 262 | for (cur = dnp[0]; cur->next; cur = cur->next) |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 263 | nfiles++; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 264 | nfiles++; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 265 | return nfiles; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* get memory to hold an array of pointers */ |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 269 | static struct dnode **dnalloc(int num) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 270 | { |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 271 | if (num < 1) |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 272 | return NULL; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 273 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 274 | return xzalloc(num * sizeof(struct dnode *)); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 277 | #if ENABLE_FEATURE_LS_RECURSIVE |
Rob Landley | 2631486 | 2006-05-02 19:46:52 +0000 | [diff] [blame] | 278 | static void dfree(struct dnode **dnp, int nfiles) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 279 | { |
Rob Landley | 2631486 | 2006-05-02 19:46:52 +0000 | [diff] [blame] | 280 | int i; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 281 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 282 | if (dnp == NULL) |
| 283 | return; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 284 | |
Rob Landley | 2631486 | 2006-05-02 19:46:52 +0000 | [diff] [blame] | 285 | for (i = 0; i < nfiles; i++) { |
| 286 | struct dnode *cur = dnp[i]; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 287 | if (cur->allocated) |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 288 | free(cur->fullname); /* free the filename */ |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 289 | free(cur); /* free the dnode */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 290 | } |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 291 | free(dnp); /* free the array holding the dnode pointers */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 292 | } |
Rob Landley | c44bc98 | 2006-05-28 01:19:06 +0000 | [diff] [blame] | 293 | #else |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 294 | #define dfree(...) do {} while(0) |
Eric Andersen | 20aab26 | 2001-07-19 22:28:02 +0000 | [diff] [blame] | 295 | #endif |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 296 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 297 | static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 298 | { |
| 299 | int dncnt, i, d; |
| 300 | struct dnode **dnp; |
| 301 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 302 | if (dn == NULL || nfiles < 1) |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 303 | return NULL; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 304 | |
| 305 | /* count how many dirs and regular files there are */ |
Eric Andersen | cf1189f | 2000-11-29 21:52:06 +0000 | [diff] [blame] | 306 | if (which == SPLIT_SUBDIR) |
| 307 | dncnt = countsubdirs(dn, nfiles); |
| 308 | else { |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 309 | dncnt = countdirs(dn, nfiles); /* assume we are looking for dirs */ |
Eric Andersen | cf1189f | 2000-11-29 21:52:06 +0000 | [diff] [blame] | 310 | if (which == SPLIT_FILE) |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 311 | dncnt = nfiles - dncnt; /* looking for files */ |
Eric Andersen | cf1189f | 2000-11-29 21:52:06 +0000 | [diff] [blame] | 312 | } |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 313 | |
| 314 | /* allocate a file array and a dir array */ |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 315 | dnp = dnalloc(dncnt); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 316 | |
| 317 | /* copy the entrys into the file or dir array */ |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 318 | for (d = i = 0; i < nfiles; i++) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 319 | if (S_ISDIR(dn[i]->dstat.st_mode)) { |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 320 | char *name; |
| 321 | if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) |
| 322 | continue; |
| 323 | name = dn[i]->name; |
| 324 | if ((which & SPLIT_DIR) |
| 325 | || name[0]!='.' || (name[1] && (name[1]!='.' || name[2])) |
| 326 | ) { |
| 327 | dnp[d++] = dn[i]; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 328 | } |
| 329 | } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) { |
| 330 | dnp[d++] = dn[i]; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 331 | } |
| 332 | } |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 333 | return dnp; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 336 | #if ENABLE_FEATURE_LS_SORTFILES |
Rob Landley | 425e758 | 2006-05-03 20:22:03 +0000 | [diff] [blame] | 337 | static int sortcmp(const void *a, const void *b) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 338 | { |
Rob Landley | 425e758 | 2006-05-03 20:22:03 +0000 | [diff] [blame] | 339 | struct dnode *d1 = *(struct dnode **)a; |
| 340 | struct dnode *d2 = *(struct dnode **)b; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 341 | unsigned sort_opts = all_fmt & SORT_MASK; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 342 | int dif; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 343 | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 344 | dif = 0; /* assume SORT_NAME */ |
| 345 | // TODO: use pre-initialized function pointer |
| 346 | // instead of branch forest |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 347 | if (sort_opts == SORT_SIZE) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 348 | dif = (int) (d2->dstat.st_size - d1->dstat.st_size); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 349 | } else if (sort_opts == SORT_ATIME) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 350 | dif = (int) (d2->dstat.st_atime - d1->dstat.st_atime); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 351 | } else if (sort_opts == SORT_CTIME) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 352 | dif = (int) (d2->dstat.st_ctime - d1->dstat.st_ctime); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 353 | } else if (sort_opts == SORT_MTIME) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 354 | dif = (int) (d2->dstat.st_mtime - d1->dstat.st_mtime); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 355 | } else if (sort_opts == SORT_DIR) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 356 | dif = S_ISDIR(d2->dstat.st_mode) - S_ISDIR(d1->dstat.st_mode); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 357 | /* } else if (sort_opts == SORT_VERSION) { */ |
| 358 | /* } else if (sort_opts == SORT_EXT) { */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 361 | if (dif == 0) { |
Denis Vlasenko | fcdb00f | 2006-11-21 00:09:37 +0000 | [diff] [blame] | 362 | /* sort by name - may be a tie_breaker for time or size cmp */ |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 363 | if (ENABLE_LOCALE_SUPPORT) dif = strcoll(d1->name, d2->name); |
| 364 | else dif = strcmp(d1->name, d2->name); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 367 | if (all_fmt & SORT_REVERSE) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 368 | dif = -dif; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 369 | } |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 370 | return dif; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Rob Landley | 425e758 | 2006-05-03 20:22:03 +0000 | [diff] [blame] | 373 | static void dnsort(struct dnode **dn, int size) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 374 | { |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 375 | qsort(dn, size, sizeof(*dn), sortcmp); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 376 | } |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 377 | #else |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 378 | #define dnsort(dn, size) do {} while(0) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 379 | #endif |
| 380 | |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 381 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 382 | static void showfiles(struct dnode **dn, int nfiles) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 383 | { |
| 384 | int i, ncols, nrows, row, nc; |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 385 | int column = 0; |
| 386 | int nexttab = 0; |
| 387 | int column_width = 0; /* for STYLE_LONG and STYLE_SINGLE not used */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 388 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 389 | if (dn == NULL || nfiles < 1) |
| 390 | return; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 391 | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 392 | if (all_fmt & STYLE_LONG) { |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 393 | ncols = 1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 394 | } else { |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 395 | /* find the longest file name, use that as the column width */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 396 | for (i = 0; i < nfiles; i++) { |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 397 | int len = strlen(dn[i]->name); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 398 | if (column_width < len) |
| 399 | column_width = len; |
| 400 | } |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 401 | column_width += tabstops + |
| 402 | USE_SELINUX( ((all_fmt & LIST_CONTEXT) ? 33 : 0) + ) |
| 403 | ((all_fmt & LIST_INO) ? 8 : 0) + |
| 404 | ((all_fmt & LIST_BLOCKS) ? 5 : 0); |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 405 | ncols = (int) (terminal_width / column_width); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Eric Andersen | e57d54b | 2001-01-30 18:03:11 +0000 | [diff] [blame] | 408 | if (ncols > 1) { |
| 409 | nrows = nfiles / ncols; |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 410 | if (nrows * ncols < nfiles) |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 411 | nrows++; /* round up fractionals */ |
Eric Andersen | e57d54b | 2001-01-30 18:03:11 +0000 | [diff] [blame] | 412 | } else { |
| 413 | nrows = nfiles; |
| 414 | ncols = 1; |
| 415 | } |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 416 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 417 | for (row = 0; row < nrows; row++) { |
| 418 | for (nc = 0; nc < ncols; nc++) { |
Eric Andersen | 79565b6 | 2000-08-11 18:10:21 +0000 | [diff] [blame] | 419 | /* reach into the array based on the column and row */ |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 420 | i = (nc * nrows) + row; /* assume display by column */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 421 | if (all_fmt & DISP_ROWS) |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 422 | i = (row * ncols) + nc; /* display across row */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 423 | if (i < nfiles) { |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 424 | if (column > 0) { |
| 425 | nexttab -= column; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 426 | printf("%*s", nexttab, ""); |
| 427 | column += nexttab; |
| 428 | } |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 429 | nexttab = column + column_width; |
| 430 | column += list_single(dn[i]); |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 431 | } |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 432 | } |
| 433 | putchar('\n'); |
| 434 | column = 0; |
Eric Andersen | a42982e | 2000-06-07 17:28:53 +0000 | [diff] [blame] | 435 | } |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 438 | |
Glenn L McGrath | c4db083 | 2004-03-06 09:12:55 +0000 | [diff] [blame] | 439 | static void showdirs(struct dnode **dn, int ndirs, int first) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 440 | { |
| 441 | int i, nfiles; |
| 442 | struct dnode **subdnp; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 443 | int dndirs; |
| 444 | struct dnode **dnd; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 445 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 446 | if (dn == NULL || ndirs < 1) |
| 447 | return; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 448 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 449 | for (i = 0; i < ndirs; i++) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 450 | if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) { |
Glenn L McGrath | c4db083 | 2004-03-06 09:12:55 +0000 | [diff] [blame] | 451 | if (!first) |
Denis Vlasenko | c6f188d | 2006-10-26 00:37:00 +0000 | [diff] [blame] | 452 | puts(""); |
Glenn L McGrath | c4db083 | 2004-03-06 09:12:55 +0000 | [diff] [blame] | 453 | first = 0; |
| 454 | printf("%s:\n", dn[i]->fullname); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 455 | } |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 456 | subdnp = list_dir(dn[i]->fullname); |
| 457 | nfiles = countfiles(subdnp); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 458 | if (nfiles > 0) { |
| 459 | /* list all files at this level */ |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 460 | dnsort(subdnp, nfiles); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 461 | showfiles(subdnp, nfiles); |
Rob Landley | 2b8a05a | 2006-06-20 17:43:01 +0000 | [diff] [blame] | 462 | if (ENABLE_FEATURE_LS_RECURSIVE) { |
| 463 | if (all_fmt & DISP_RECURSIVE) { |
| 464 | /* recursive- list the sub-dirs */ |
| 465 | dnd = splitdnarray(subdnp, nfiles, SPLIT_SUBDIR); |
| 466 | dndirs = countsubdirs(subdnp, nfiles); |
| 467 | if (dndirs > 0) { |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 468 | dnsort(dnd, dndirs); |
Rob Landley | 2b8a05a | 2006-06-20 17:43:01 +0000 | [diff] [blame] | 469 | showdirs(dnd, dndirs, 0); |
| 470 | /* free the array of dnode pointers to the dirs */ |
| 471 | free(dnd); |
| 472 | } |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 473 | } |
Rob Landley | 2b8a05a | 2006-06-20 17:43:01 +0000 | [diff] [blame] | 474 | /* free the dnodes and the fullname mem */ |
| 475 | dfree(subdnp, nfiles); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 476 | } |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 481 | |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 482 | static struct dnode **list_dir(const char *path) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 483 | { |
| 484 | struct dnode *dn, *cur, **dnp; |
| 485 | struct dirent *entry; |
| 486 | DIR *dir; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 487 | int i, nfiles; |
| 488 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 489 | if (path == NULL) |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 490 | return NULL; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 491 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 492 | dn = NULL; |
| 493 | nfiles = 0; |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 494 | dir = warn_opendir(path); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 495 | if (dir == NULL) { |
Matt Kraai | 33fdae5 | 2000-10-13 17:59:43 +0000 | [diff] [blame] | 496 | status = EXIT_FAILURE; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 497 | return NULL; /* could not open the dir */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 498 | } |
| 499 | while ((entry = readdir(dir)) != NULL) { |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 500 | char *fullname; |
| 501 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 502 | /* are we going to list the file- it may be . or .. or a hidden file */ |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 503 | if (entry->d_name[0] == '.') { |
| 504 | if ((entry->d_name[1] == 0 || ( |
| 505 | entry->d_name[1] == '.' |
| 506 | && entry->d_name[2] == 0)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 507 | && !(all_fmt & DISP_DOT)) |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 508 | continue; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 509 | if (!(all_fmt & DISP_HIDDEN)) |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 510 | continue; |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 511 | } |
| 512 | fullname = concat_path_file(path, entry->d_name); |
| 513 | cur = my_stat(fullname, strrchr(fullname, '/') + 1); |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 514 | if (!cur) { |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 515 | free(fullname); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 516 | continue; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 517 | } |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 518 | cur->allocated = 1; |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 519 | cur->next = dn; |
| 520 | dn = cur; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 521 | nfiles++; |
| 522 | } |
| 523 | closedir(dir); |
| 524 | |
| 525 | /* now that we know how many files there are |
Denis Vlasenko | 656f746 | 2006-10-28 13:02:55 +0000 | [diff] [blame] | 526 | * allocate memory for an array to hold dnode pointers |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 527 | */ |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 528 | if (dn == NULL) |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 529 | return NULL; |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 530 | dnp = dnalloc(nfiles); |
| 531 | for (i = 0, cur = dn; i < nfiles; i++) { |
| 532 | dnp[i] = cur; /* save pointer to node in array */ |
| 533 | cur = cur->next; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 536 | return dnp; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 539 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 540 | static int list_single(struct dnode *dn) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 541 | { |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 542 | int i, column = 0; |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 543 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 544 | #if ENABLE_FEATURE_LS_USERNAME |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 545 | char scratch[16]; |
Matt Kraai | a1bbde7 | 2002-03-08 16:25:33 +0000 | [diff] [blame] | 546 | #endif |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 547 | #if ENABLE_FEATURE_LS_TIMESTAMPS |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 548 | char *filetime; |
| 549 | time_t ttime, age; |
| 550 | #endif |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 551 | #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 552 | struct stat info; |
| 553 | char append; |
| 554 | #endif |
| 555 | |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 556 | if (dn->fullname == NULL) |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 557 | return 0; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 558 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 559 | #if ENABLE_FEATURE_LS_TIMESTAMPS |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 560 | ttime = dn->dstat.st_mtime; /* the default time */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 561 | if (all_fmt & TIME_ACCESS) |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 562 | ttime = dn->dstat.st_atime; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 563 | if (all_fmt & TIME_CHANGE) |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 564 | ttime = dn->dstat.st_ctime; |
| 565 | filetime = ctime(&ttime); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 566 | #endif |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 567 | #if ENABLE_FEATURE_LS_FILETYPES |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 568 | append = append_char(dn->dstat.st_mode); |
| 569 | #endif |
| 570 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 571 | for (i = 0; i <= 31; i++) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 572 | switch (all_fmt & (1 << i)) { |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 573 | case LIST_INO: |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 574 | column += printf("%7ld ", (long) dn->dstat.st_ino); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 575 | break; |
| 576 | case LIST_BLOCKS: |
Denis Vlasenko | cf30cc8 | 2006-11-24 14:53:18 +0000 | [diff] [blame] | 577 | column += printf("%4"OFF_FMT"d ", (off_t) dn->dstat.st_blocks >> 1); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 578 | break; |
| 579 | case LIST_MODEBITS: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 580 | column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode)); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 581 | break; |
| 582 | case LIST_NLINKS: |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 583 | column += printf("%4ld ", (long) dn->dstat.st_nlink); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 584 | break; |
| 585 | case LIST_ID_NAME: |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 586 | #if ENABLE_FEATURE_LS_USERNAME |
Bernhard Reutner-Fischer | d5bd137 | 2005-09-20 21:06:17 +0000 | [diff] [blame] | 587 | bb_getpwuid(scratch, dn->dstat.st_uid, sizeof(scratch)); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 588 | printf("%-8.8s ", scratch); |
Bernhard Reutner-Fischer | d5bd137 | 2005-09-20 21:06:17 +0000 | [diff] [blame] | 589 | bb_getgrgid(scratch, dn->dstat.st_gid, sizeof(scratch)); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 590 | printf("%-8.8s", scratch); |
| 591 | column += 17; |
| 592 | break; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 593 | #endif |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 594 | case LIST_ID_NUMERIC: |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 595 | column += printf("%-8d %-8d", dn->dstat.st_uid, dn->dstat.st_gid); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 596 | break; |
| 597 | case LIST_SIZE: |
| 598 | case LIST_DEV: |
| 599 | if (S_ISBLK(dn->dstat.st_mode) || S_ISCHR(dn->dstat.st_mode)) { |
Eric Andersen | 4f807a8 | 2004-07-26 09:11:12 +0000 | [diff] [blame] | 600 | column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev), |
| 601 | (int) minor(dn->dstat.st_rdev)); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 602 | } else { |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 603 | if (all_fmt & LS_DISP_HR) { |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 604 | column += printf("%9s ", |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 605 | make_human_readable_str(dn->dstat.st_size, 1, 0)); |
| 606 | } else { |
Denis Vlasenko | cf30cc8 | 2006-11-24 14:53:18 +0000 | [diff] [blame] | 607 | column += printf("%9"OFF_FMT"d ", (off_t) dn->dstat.st_size); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 608 | } |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 609 | } |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 610 | break; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 611 | #if ENABLE_FEATURE_LS_TIMESTAMPS |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 612 | case LIST_FULLTIME: |
Glenn L McGrath | 65b6d8b | 2004-01-18 05:41:30 +0000 | [diff] [blame] | 613 | printf("%24.24s ", filetime); |
| 614 | column += 25; |
| 615 | break; |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 616 | case LIST_DATE_TIME: |
Glenn L McGrath | 65b6d8b | 2004-01-18 05:41:30 +0000 | [diff] [blame] | 617 | if ((all_fmt & LIST_FULLTIME) == 0) { |
| 618 | age = time(NULL) - ttime; |
| 619 | printf("%6.6s ", filetime + 4); |
| 620 | if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) { |
| 621 | /* hh:mm if less than 6 months old */ |
| 622 | printf("%5.5s ", filetime + 11); |
| 623 | } else { |
| 624 | printf(" %4.4s ", filetime + 20); |
| 625 | } |
| 626 | column += 13; |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 627 | } |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 628 | break; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 629 | #endif |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 630 | #if ENABLE_SELINUX |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 631 | case LIST_CONTEXT: |
| 632 | { |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 633 | char context[80]; |
Rob Landley | 08abe64 | 2006-03-01 20:48:44 +0000 | [diff] [blame] | 634 | int len = 0; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 635 | |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 636 | if (dn->sid) { |
Denis Vlasenko | 656f746 | 2006-10-28 13:02:55 +0000 | [diff] [blame] | 637 | /* I assume sid initilized with NULL */ |
| 638 | len = strlen(dn->sid) + 1; |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 639 | safe_strncpy(context, dn->sid, len); |
| 640 | freecon(dn->sid); |
| 641 | } else { |
| 642 | safe_strncpy(context, "unknown", 8); |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 643 | } |
| 644 | printf("%-32s ", context); |
| 645 | column += MAX(33, len); |
| 646 | } |
| 647 | break; |
| 648 | #endif |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 649 | case LIST_FILENAME: |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 650 | errno = 0; |
| 651 | if (show_color && !lstat(dn->fullname, &info)) { |
| 652 | printf("\033[%d;%dm", bgcolor(info.st_mode), |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 653 | fgcolor(info.st_mode)); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 654 | } |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 655 | column += printf("%s", dn->name); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 656 | if (show_color) { |
| 657 | printf("\033[0m"); |
| 658 | } |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 659 | break; |
| 660 | case LIST_SYMLINK: |
| 661 | if (S_ISLNK(dn->dstat.st_mode)) { |
| 662 | char *lpath = xreadlink(dn->fullname); |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 663 | if (!lpath) break; |
| 664 | printf(" -> "); |
| 665 | #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR |
| 666 | if (!stat(dn->fullname, &info)) { |
| 667 | append = append_char(info.st_mode); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 668 | } |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 669 | #endif |
| 670 | if (show_color) { |
| 671 | errno = 0; |
| 672 | printf("\033[%d;%dm", bgcolor(info.st_mode), |
| 673 | fgcolor(info.st_mode)); |
| 674 | } |
| 675 | column += printf("%s", lpath) + 4; |
| 676 | if (show_color) { |
| 677 | printf("\033[0m"); |
| 678 | } |
| 679 | free(lpath); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 680 | } |
| 681 | break; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 682 | #if ENABLE_FEATURE_LS_FILETYPES |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 683 | case LIST_FILETYPE: |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 684 | if (append) { |
| 685 | putchar(append); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 686 | column++; |
| 687 | } |
| 688 | break; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 689 | #endif |
| 690 | } |
| 691 | } |
| 692 | |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 693 | return column; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 696 | /* "[-]Cadil1", POSIX mandated options, busybox always supports */ |
| 697 | /* "[-]gnsx", POSIX non-mandated options, busybox always supports */ |
| 698 | /* "[-]Ak" GNU options, busybox always supports */ |
| 699 | /* "[-]FLRctur", POSIX mandated options, busybox optionally supports */ |
| 700 | /* "[-]p", POSIX non-mandated options, busybox optionally supports */ |
| 701 | /* "[-]SXvThw", GNU options, busybox optionally supports */ |
| 702 | /* "[-]K", SELinux mandated options, busybox optionally supports */ |
| 703 | /* "[-]e", I think we made this one up */ |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 704 | static const char ls_options[] = "Cadil1gnsxAk" |
| 705 | USE_FEATURE_LS_TIMESTAMPS("cetu") |
| 706 | USE_FEATURE_LS_SORTFILES("SXrv") |
| 707 | USE_FEATURE_LS_FILETYPES("Fp") |
| 708 | USE_FEATURE_LS_FOLLOWLINKS("L") |
| 709 | USE_FEATURE_LS_RECURSIVE("R") |
| 710 | USE_FEATURE_HUMAN_READABLE("h") |
| 711 | USE_SELINUX("K") |
| 712 | USE_FEATURE_AUTOWIDTH("T:w:"); |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 713 | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 714 | enum { |
| 715 | LIST_MASK_TRIGGER = 0, |
| 716 | STYLE_MASK_TRIGGER = STYLE_MASK, |
| 717 | DISP_MASK_TRIGGER = DISP_ROWS, |
| 718 | SORT_MASK_TRIGGER = SORT_MASK, |
| 719 | }; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 720 | |
| 721 | static const unsigned opt_flags[] = { |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 722 | LIST_SHORT | STYLE_COLUMNS, /* C */ |
| 723 | DISP_HIDDEN | DISP_DOT, /* a */ |
| 724 | DISP_NOLIST, /* d */ |
| 725 | LIST_INO, /* i */ |
| 726 | LIST_LONG | STYLE_LONG, /* l - remember LS_DISP_HR in mask! */ |
| 727 | LIST_SHORT | STYLE_SINGLE, /* 1 */ |
| 728 | 0, /* g - ingored */ |
| 729 | LIST_ID_NUMERIC, /* n */ |
| 730 | LIST_BLOCKS, /* s */ |
| 731 | DISP_ROWS, /* x */ |
| 732 | DISP_HIDDEN, /* A */ |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 733 | ENABLE_SELINUX * LIST_CONTEXT, /* k (ignored if !SELINUX) */ |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 734 | #if ENABLE_FEATURE_LS_TIMESTAMPS |
| 735 | TIME_CHANGE | (ENABLE_FEATURE_LS_SORTFILES * SORT_CTIME), /* c */ |
| 736 | LIST_FULLTIME, /* e */ |
| 737 | ENABLE_FEATURE_LS_SORTFILES * SORT_MTIME, /* t */ |
| 738 | TIME_ACCESS | (ENABLE_FEATURE_LS_SORTFILES * SORT_ATIME), /* u */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 739 | #endif |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 740 | #if ENABLE_FEATURE_LS_SORTFILES |
| 741 | SORT_SIZE, /* S */ |
| 742 | SORT_EXT, /* X */ |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 743 | SORT_REVERSE, /* r */ |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 744 | SORT_VERSION, /* v */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 745 | #endif |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 746 | #if ENABLE_FEATURE_LS_FILETYPES |
| 747 | LIST_FILETYPE | LIST_EXEC, /* F */ |
| 748 | LIST_FILETYPE, /* p */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 749 | #endif |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 750 | #if ENABLE_FEATURE_LS_FOLLOWLINKS |
| 751 | FOLLOW_LINKS, /* L */ |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 752 | #endif |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 753 | #if ENABLE_FEATURE_LS_RECURSIVE |
| 754 | DISP_RECURSIVE, /* R */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 755 | #endif |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 756 | #if ENABLE_FEATURE_HUMAN_READABLE |
| 757 | LS_DISP_HR, /* h */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 758 | #endif |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 759 | #if ENABLE_SELINUX |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 760 | LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */ |
| 761 | #endif |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 762 | #if ENABLE_FEATURE_AUTOWIDTH |
Denis Vlasenko | 656f746 | 2006-10-28 13:02:55 +0000 | [diff] [blame] | 763 | 0, 0, /* T, w - ignored */ |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 764 | #endif |
Eric Andersen | d07cf59 | 2004-02-05 13:52:03 +0000 | [diff] [blame] | 765 | (1U<<31) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 766 | }; |
| 767 | |
| 768 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 769 | int ls_main(int argc, char **argv) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 770 | { |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 771 | struct dnode **dnd; |
| 772 | struct dnode **dnf; |
| 773 | struct dnode **dnp; |
| 774 | struct dnode *dn; |
| 775 | struct dnode *cur; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 776 | unsigned opt; |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 777 | int nfiles = 0; |
| 778 | int dnfiles; |
| 779 | int dndirs; |
| 780 | int oi; |
| 781 | int ac; |
| 782 | int i; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 783 | char **av; |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 784 | USE_FEATURE_AUTOWIDTH(char *tabstops_str = NULL;) |
| 785 | USE_FEATURE_AUTOWIDTH(char *terminal_width_str = NULL;) |
| 786 | USE_FEATURE_LS_COLOR(char *color_opt;) |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 787 | |
Rob Landley | 2b8a05a | 2006-06-20 17:43:01 +0000 | [diff] [blame] | 788 | all_fmt = LIST_SHORT | |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 789 | (ENABLE_FEATURE_LS_SORTFILES * (SORT_NAME | SORT_FORWARD)); |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 790 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 791 | #if ENABLE_FEATURE_AUTOWIDTH |
Denis Vlasenko | 656f746 | 2006-10-28 13:02:55 +0000 | [diff] [blame] | 792 | /* Obtain the terminal width */ |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 793 | get_terminal_width_height(STDOUT_FILENO, &terminal_width, NULL); |
Eric Andersen | 8efe967 | 2003-09-15 08:33:45 +0000 | [diff] [blame] | 794 | /* Go one less... */ |
| 795 | terminal_width--; |
Eric Andersen | 6d68781 | 2003-11-04 23:16:48 +0000 | [diff] [blame] | 796 | #endif |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 797 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 798 | /* process options */ |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 799 | USE_FEATURE_LS_COLOR(applet_long_options = ls_color_opt;) |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 800 | #if ENABLE_FEATURE_AUTOWIDTH |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 801 | opt = getopt32(argc, argv, ls_options, &tabstops_str, &terminal_width_str |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 802 | USE_FEATURE_LS_COLOR(, &color_opt)); |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 803 | if (tabstops_str) |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 804 | tabstops = xatou(tabstops_str); |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 805 | if (terminal_width_str) |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 806 | terminal_width = xatou(terminal_width_str); |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 807 | #else |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 808 | opt = getopt32(argc, argv, ls_options USE_FEATURE_LS_COLOR(, &color_opt)); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 809 | #endif |
Eric Andersen | d07cf59 | 2004-02-05 13:52:03 +0000 | [diff] [blame] | 810 | for (i = 0; opt_flags[i] != (1U<<31); i++) { |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 811 | if (opt & (1 << i)) { |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 812 | unsigned flags = opt_flags[i]; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 813 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 814 | if (flags & LIST_MASK_TRIGGER) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 815 | all_fmt &= ~LIST_MASK; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 816 | if (flags & STYLE_MASK_TRIGGER) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 817 | all_fmt &= ~STYLE_MASK; |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 818 | if (flags & SORT_MASK_TRIGGER) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 819 | all_fmt &= ~SORT_MASK; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 820 | if (flags & DISP_MASK_TRIGGER) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 821 | all_fmt &= ~DISP_MASK; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 822 | if (flags & TIME_MASK) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 823 | all_fmt &= ~TIME_MASK; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 824 | if (flags & LIST_CONTEXT) |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 825 | all_fmt |= STYLE_SINGLE; |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 826 | if (LS_DISP_HR && opt == 'l') |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 827 | all_fmt &= ~LS_DISP_HR; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 828 | all_fmt |= flags; |
| 829 | } |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 832 | #if ENABLE_FEATURE_LS_COLOR |
| 833 | /* find color bit value - last position for short getopt */ |
| 834 | if (ENABLE_FEATURE_LS_COLOR_IS_DEFAULT && isatty(STDOUT_FILENO)) { |
| 835 | char *p = getenv("LS_COLORS"); |
| 836 | /* LS_COLORS is unset, or (not empty && not "none") ? */ |
| 837 | if (!p || (p[0] && strcmp(p, "none"))) |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 838 | show_color = 1; |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 839 | } |
| 840 | if (opt & (1 << i)) { /* next flag after short options */ |
| 841 | if (!color_opt || !strcmp("always", color_opt)) |
| 842 | show_color = 1; |
| 843 | else if (color_opt && !strcmp("never", color_opt)) |
| 844 | show_color = 0; |
| 845 | else if (color_opt && !strcmp("auto", color_opt) && isatty(STDOUT_FILENO)) |
| 846 | show_color = 1; |
Paul Fox | 156dc41 | 2005-08-01 19:33:30 +0000 | [diff] [blame] | 847 | } |
| 848 | #endif |
| 849 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 850 | /* sort out which command line options take precedence */ |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 851 | if (ENABLE_FEATURE_LS_RECURSIVE && (all_fmt & DISP_NOLIST)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 852 | all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */ |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 853 | if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) { |
| 854 | if (all_fmt & TIME_CHANGE) |
| 855 | all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME; |
| 856 | if (all_fmt & TIME_ACCESS) |
| 857 | all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME; |
| 858 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 859 | if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */ |
| 860 | all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC); |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 861 | if (ENABLE_FEATURE_LS_USERNAME) |
| 862 | if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC)) |
| 863 | all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 864 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 865 | /* choose a display format */ |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 866 | if (!(all_fmt & STYLE_MASK)) |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 867 | all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 868 | |
| 869 | /* |
| 870 | * when there are no cmd line args we have to supply a default "." arg. |
| 871 | * we will create a second argv array, "av" that will hold either |
| 872 | * our created "." arg, or the real cmd line args. The av array |
| 873 | * just holds the pointers- we don't move the date the pointers |
| 874 | * point to. |
| 875 | */ |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 876 | ac = argc - optind; /* how many cmd line args are left */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 877 | if (ac < 1) { |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 878 | static const char *const dotdir[] = { "." }; |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 879 | |
| 880 | av = (char **) dotdir; |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 881 | ac = 1; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 882 | } else { |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 883 | av = argv + optind; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | /* now, everything is in the av array */ |
| 887 | if (ac > 1) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 888 | all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 889 | |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 890 | /* stuff the command line file names into a dnode array */ |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 891 | dn = NULL; |
| 892 | for (oi = 0; oi < ac; oi++) { |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 893 | cur = my_stat(av[oi], av[oi]); |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 894 | if (!cur) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 895 | continue; |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 896 | cur->allocated = 0; |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 897 | cur->next = dn; |
| 898 | dn = cur; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 899 | nfiles++; |
| 900 | } |
| 901 | |
| 902 | /* now that we know how many files there are |
Denis Vlasenko | 656f746 | 2006-10-28 13:02:55 +0000 | [diff] [blame] | 903 | * allocate memory for an array to hold dnode pointers |
| 904 | */ |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 905 | dnp = dnalloc(nfiles); |
| 906 | for (i = 0, cur = dn; i < nfiles; i++) { |
| 907 | dnp[i] = cur; /* save pointer to node in array */ |
| 908 | cur = cur->next; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 911 | if (all_fmt & DISP_NOLIST) { |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 912 | dnsort(dnp, nfiles); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 913 | if (nfiles > 0) |
| 914 | showfiles(dnp, nfiles); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 915 | } else { |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 916 | dnd = splitdnarray(dnp, nfiles, SPLIT_DIR); |
| 917 | dnf = splitdnarray(dnp, nfiles, SPLIT_FILE); |
| 918 | dndirs = countdirs(dnp, nfiles); |
| 919 | dnfiles = nfiles - dndirs; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 920 | if (dnfiles > 0) { |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 921 | dnsort(dnf, dnfiles); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 922 | showfiles(dnf, dnfiles); |
Rob Landley | 2631486 | 2006-05-02 19:46:52 +0000 | [diff] [blame] | 923 | if (ENABLE_FEATURE_CLEAN_UP) |
| 924 | free(dnf); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 925 | } |
| 926 | if (dndirs > 0) { |
Denis Vlasenko | 94cf69f | 2006-10-28 12:37:51 +0000 | [diff] [blame] | 927 | dnsort(dnd, dndirs); |
Glenn L McGrath | c4db083 | 2004-03-06 09:12:55 +0000 | [diff] [blame] | 928 | showdirs(dnd, dndirs, dnfiles == 0); |
Rob Landley | 2631486 | 2006-05-02 19:46:52 +0000 | [diff] [blame] | 929 | if (ENABLE_FEATURE_CLEAN_UP) |
| 930 | free(dnd); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 931 | } |
| 932 | } |
Rob Landley | 2631486 | 2006-05-02 19:46:52 +0000 | [diff] [blame] | 933 | if (ENABLE_FEATURE_CLEAN_UP) |
| 934 | dfree(dnp, nfiles); |
Denis Vlasenko | 5c75960 | 2006-10-28 12:37:16 +0000 | [diff] [blame] | 935 | return status; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 936 | } |