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