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