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