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 | * |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * To achieve a small memory footprint, this version of 'ls' doesn't do any |
| 23 | * file sorting, and only has the most essential command line switches |
Eric Andersen | 77d9268 | 2001-05-23 20:32:09 +0000 | [diff] [blame] | 24 | * (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] | 25 | * linking in substantial chunks of libc can be disabled. |
| 26 | * |
| 27 | * Although I don't really want to add new features to this program to |
| 28 | * keep it small, I *am* interested to receive bug fixes and ways to make |
| 29 | * it more portable. |
| 30 | * |
| 31 | * KNOWN BUGS: |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 32 | * 1. ls -l of a directory doesn't give "total <blocks>" header |
| 33 | * 2. ls of a symlink to a directory doesn't list directory contents |
| 34 | * 3. hidden files can make column width too large |
| 35 | * |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 36 | * NON-OPTIMAL BEHAVIOUR: |
| 37 | * 1. autowidth reads directories twice |
| 38 | * 2. if you do a short directory listing without filetype characters |
| 39 | * appended, there's no need to stat each one |
| 40 | * PORTABILITY: |
| 41 | * 1. requires lstat (BSD) - how do you do it without? |
| 42 | */ |
| 43 | |
Eric Andersen | e57d54b | 2001-01-30 18:03:11 +0000 | [diff] [blame] | 44 | enum { |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 45 | TERMINAL_WIDTH = 80, /* use 79 if terminal has linefold bug */ |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 46 | COLUMN_GAP = 2, /* includes the file type char */ |
Eric Andersen | e57d54b | 2001-01-30 18:03:11 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 49 | /************************************************************************/ |
| 50 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 51 | #include <sys/types.h> |
| 52 | #include <sys/stat.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 53 | #include <stdio.h> |
| 54 | #include <unistd.h> |
| 55 | #include <dirent.h> |
| 56 | #include <errno.h> |
| 57 | #include <stdio.h> |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 58 | #include <string.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 59 | #include <stdlib.h> |
Eric Andersen | 5307eca | 2001-01-26 01:52:43 +0000 | [diff] [blame] | 60 | #include <fcntl.h> |
| 61 | #include <signal.h> |
Eric Andersen | 8d79ce8 | 2001-07-22 23:00:15 +0000 | [diff] [blame] | 62 | #include <termios.h> |
Rob Landley | b7128c6 | 2005-09-11 01:05:30 +0000 | [diff] [blame] | 63 | #include <getopt.h> /* struct option */ |
Eric Andersen | 5307eca | 2001-01-26 01:52:43 +0000 | [diff] [blame] | 64 | #include <sys/ioctl.h> |
Eric Andersen | 4f807a8 | 2004-07-26 09:11:12 +0000 | [diff] [blame] | 65 | #include <sys/sysmacros.h> /* major() and minor() */ |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 66 | #include "busybox.h" |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 67 | #ifdef CONFIG_SELINUX |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 68 | #include <selinux/selinux.h> /* for is_selinux_enabled() */ |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 69 | #endif |
Eric Andersen | 5307eca | 2001-01-26 01:52:43 +0000 | [diff] [blame] | 70 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 71 | #ifdef CONFIG_FEATURE_LS_TIMESTAMPS |
Eric Andersen | f1142c5 | 2001-02-20 06:16:29 +0000 | [diff] [blame] | 72 | #include <time.h> |
| 73 | #endif |
| 74 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 75 | /* what is the overall style of the listing */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 76 | #define STYLE_AUTO (0) |
| 77 | #define STYLE_COLUMNS (1U<<21) /* fill columns */ |
| 78 | #define STYLE_LONG (2U<<21) /* one record per line, extended info */ |
| 79 | #define STYLE_SINGLE (3U<<21) /* one record per line */ |
| 80 | |
| 81 | #define STYLE_MASK STYLE_SINGLE |
| 82 | #define STYLE_ONE_RECORD_FLAG STYLE_LONG |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 83 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 84 | /* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */ |
| 85 | /* what file information will be listed */ |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 86 | #define LIST_INO (1U<<0) |
| 87 | #define LIST_BLOCKS (1U<<1) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 88 | #define LIST_MODEBITS (1U<<2) |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 89 | #define LIST_NLINKS (1U<<3) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 90 | #define LIST_ID_NAME (1U<<4) |
| 91 | #define LIST_ID_NUMERIC (1U<<5) |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 92 | #define LIST_CONTEXT (1U<<6) |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 93 | #define LIST_SIZE (1U<<7) |
| 94 | #define LIST_DEV (1U<<8) |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 95 | #define LIST_DATE_TIME (1U<<9) |
| 96 | #define LIST_FULLTIME (1U<<10) |
| 97 | #define LIST_FILENAME (1U<<11) |
| 98 | #define LIST_SYMLINK (1U<<12) |
| 99 | #define LIST_FILETYPE (1U<<13) |
| 100 | #define LIST_EXEC (1U<<14) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 101 | |
| 102 | #define LIST_MASK ((LIST_EXEC << 1) - 1) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 103 | |
| 104 | /* what files will be displayed */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 105 | /* TODO -- We may be able to make DISP_NORMAL 0 to save a bit slot. */ |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 106 | #define DISP_NORMAL (1U<<14) /* show normal filenames */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 107 | #define DISP_DIRNAME (1U<<15) /* 2 or more items? label directories */ |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 108 | #define DISP_HIDDEN (1U<<16) /* show filenames starting with . */ |
| 109 | #define DISP_DOT (1U<<17) /* show . and .. */ |
| 110 | #define DISP_NOLIST (1U<<18) /* show directory as itself, not contents */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 111 | #define DISP_RECURSIVE (1U<<19) /* show directory and everything below it */ |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 112 | #define DISP_ROWS (1U<<20) /* print across rows */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 113 | |
| 114 | #define DISP_MASK (((DISP_ROWS << 1) - 1) & ~(DISP_NORMAL - 1)) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 115 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 116 | #ifdef CONFIG_FEATURE_LS_SORTFILES |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 117 | /* how will the files be sorted */ |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 118 | #define SORT_ORDER_FORWARD 0 /* sort in reverse order */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 119 | #define SORT_ORDER_REVERSE (1U<<27) /* sort in reverse order */ |
| 120 | |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 121 | #define SORT_NAME 0 /* sort by file name */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 122 | #define SORT_SIZE (1U<<28) /* sort by file size */ |
| 123 | #define SORT_ATIME (2U<<28) /* sort by last access time */ |
| 124 | #define SORT_CTIME (3U<<28) /* sort by last change time */ |
| 125 | #define SORT_MTIME (4U<<28) /* sort by last modification time */ |
| 126 | #define SORT_VERSION (5U<<28) /* sort by version */ |
| 127 | #define SORT_EXT (6U<<28) /* sort by file name extension */ |
| 128 | #define SORT_DIR (7U<<28) /* sort by file or directory */ |
| 129 | |
| 130 | #define SORT_MASK (7U<<28) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 131 | #endif |
| 132 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 133 | #ifdef CONFIG_FEATURE_LS_TIMESTAMPS |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 134 | /* which of the three times will be used */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 135 | #define TIME_MOD 0 |
| 136 | #define TIME_CHANGE (1U<<23) |
| 137 | #define TIME_ACCESS (1U<<24) |
| 138 | |
| 139 | #define TIME_MASK (3U<<23) |
| 140 | #endif |
| 141 | |
| 142 | #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS |
| 143 | #define FOLLOW_LINKS (1U<<25) |
| 144 | #endif |
| 145 | #ifdef CONFIG_FEATURE_HUMAN_READABLE |
| 146 | #define LS_DISP_HR (1U<<26) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 147 | #endif |
| 148 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 149 | #define LIST_SHORT (LIST_FILENAME) |
| 150 | #define LIST_ISHORT (LIST_INO | LIST_FILENAME) |
| 151 | #define LIST_LONG (LIST_MODEBITS | LIST_NLINKS | LIST_ID_NAME | LIST_SIZE | \ |
| 152 | LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK) |
| 153 | #define LIST_ILONG (LIST_INO | LIST_LONG) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 154 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 155 | #define SPLIT_DIR 1 |
| 156 | #define SPLIT_FILE 0 |
| 157 | #define SPLIT_SUBDIR 2 |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 158 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 159 | #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f) |
| 160 | #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)]) |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 161 | |
Eric Andersen | d598d41 | 2002-04-27 09:19:39 +0000 | [diff] [blame] | 162 | #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR) |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 163 | # 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] | 164 | #endif |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 165 | |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 166 | /* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */ |
| 167 | #ifdef CONFIG_FEATURE_LS_COLOR |
Paul Fox | 156dc41 | 2005-08-01 19:33:30 +0000 | [diff] [blame] | 168 | |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 169 | static int show_color = 0; |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 170 | |
Paul Fox | 156dc41 | 2005-08-01 19:33:30 +0000 | [diff] [blame] | 171 | /* long option entry used only for --color, which has no short option |
| 172 | * equivalent. */ |
"Vladimir N. Oleynik" | 1f0262b | 2005-10-20 11:17:48 +0000 | [diff] [blame] | 173 | static const struct option ls_color_opt[] = |
Paul Fox | 156dc41 | 2005-08-01 19:33:30 +0000 | [diff] [blame] | 174 | { |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 175 | {"color", optional_argument, NULL, 1}, |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 176 | {NULL, 0, NULL, 0} |
Paul Fox | 156dc41 | 2005-08-01 19:33:30 +0000 | [diff] [blame] | 177 | }; |
| 178 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 179 | #define COLOR(mode) ("\000\043\043\043\042\000\043\043"\ |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 180 | "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)]) |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 181 | #define ATTR(mode) ("\00\00\01\00\01\00\01\00"\ |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 182 | "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)]) |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 183 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 184 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 185 | /* |
| 186 | * a directory entry and its stat info are stored here |
| 187 | */ |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 188 | struct dnode { /* the basic node */ |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 189 | char *name; /* the dir entry name */ |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 190 | char *fullname; /* the dir entry name */ |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 191 | int allocated; |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 192 | struct stat dstat; /* the file stat info */ |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 193 | #ifdef CONFIG_SELINUX |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 194 | security_context_t sid; |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 195 | #endif |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 196 | struct dnode *next; /* point at the next node */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 197 | }; |
| 198 | typedef struct dnode dnode_t; |
| 199 | |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 200 | static struct dnode **list_dir(const char *); |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 201 | static struct dnode **dnalloc(int); |
| 202 | static int list_single(struct dnode *); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 203 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 204 | static unsigned int all_fmt; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 205 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 206 | #ifdef CONFIG_FEATURE_AUTOWIDTH |
Eric Andersen | 8efe967 | 2003-09-15 08:33:45 +0000 | [diff] [blame] | 207 | static int terminal_width = TERMINAL_WIDTH; |
Eric Andersen | e57d54b | 2001-01-30 18:03:11 +0000 | [diff] [blame] | 208 | static unsigned short tabstops = COLUMN_GAP; |
Eric Andersen | f5d5e77 | 2001-01-24 23:34:48 +0000 | [diff] [blame] | 209 | #else |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 210 | #define tabstops COLUMN_GAP |
| 211 | #define terminal_width TERMINAL_WIDTH |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 212 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 213 | |
Matt Kraai | 33fdae5 | 2000-10-13 17:59:43 +0000 | [diff] [blame] | 214 | static int status = EXIT_SUCCESS; |
| 215 | |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 216 | static struct dnode *my_stat(char *fullname, char *name) |
Matt Kraai | 9a6e67c | 2000-10-13 18:03:21 +0000 | [diff] [blame] | 217 | { |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 218 | struct stat dstat; |
| 219 | struct dnode *cur; |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 220 | #ifdef CONFIG_SELINUX |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 221 | security_context_t sid=NULL; |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 222 | #endif |
| 223 | int rc; |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 224 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 225 | #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 226 | if (all_fmt & FOLLOW_LINKS) { |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 227 | #ifdef CONFIG_SELINUX |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 228 | if (is_selinux_enabled()) { |
| 229 | rc=0; /* Set the number which means success before hand. */ |
| 230 | rc = getfilecon(fullname,&sid); |
| 231 | } |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 232 | #endif |
Rob Landley | c5b1d4d | 2006-03-13 15:45:16 +0000 | [diff] [blame] | 233 | rc = stat(fullname, &dstat); |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 234 | if(rc) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 235 | bb_perror_msg("%s", fullname); |
Matt Kraai | 9a6e67c | 2000-10-13 18:03:21 +0000 | [diff] [blame] | 236 | status = EXIT_FAILURE; |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 237 | return 0; |
Matt Kraai | 9a6e67c | 2000-10-13 18:03:21 +0000 | [diff] [blame] | 238 | } |
| 239 | } else |
| 240 | #endif |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 241 | { |
| 242 | #ifdef CONFIG_SELINUX |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 243 | if (is_selinux_enabled()) { |
| 244 | rc=0; /* Set the number which means success before hand. */ |
| 245 | rc = lgetfilecon(fullname,&sid); |
| 246 | } |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 247 | #endif |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 248 | rc = lstat(fullname, &dstat); |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 249 | if(rc) { |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 250 | bb_perror_msg("%s", fullname); |
| 251 | status = EXIT_FAILURE; |
| 252 | return 0; |
| 253 | } |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 254 | } |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 255 | |
| 256 | cur = (struct dnode *) xmalloc(sizeof(struct dnode)); |
| 257 | cur->fullname = fullname; |
| 258 | cur->name = name; |
| 259 | cur->dstat = dstat; |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 260 | #ifdef CONFIG_SELINUX |
| 261 | cur->sid = sid; |
| 262 | #endif |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 263 | return cur; |
Eric Andersen | 79565b6 | 2000-08-11 18:10:21 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 266 | /*----------------------------------------------------------------------*/ |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 267 | #ifdef CONFIG_FEATURE_LS_COLOR |
| 268 | static char fgcolor(mode_t mode) |
| 269 | { |
| 270 | /* Check wheter the file is existing (if so, color it red!) */ |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 271 | if (errno == ENOENT) { |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 272 | return '\037'; |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 273 | } |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 274 | if (LIST_EXEC && S_ISREG(mode) |
| 275 | && (mode & (S_IXUSR | S_IXGRP | S_IXOTH))) |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 276 | return COLOR(0xF000); /* File is executable ... */ |
| 277 | return COLOR(mode); |
| 278 | } |
| 279 | |
| 280 | /*----------------------------------------------------------------------*/ |
| 281 | static char bgcolor(mode_t mode) |
| 282 | { |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 283 | if (LIST_EXEC && S_ISREG(mode) |
| 284 | && (mode & (S_IXUSR | S_IXGRP | S_IXOTH))) |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 285 | return ATTR(0xF000); /* File is executable ... */ |
| 286 | return ATTR(mode); |
| 287 | } |
| 288 | #endif |
| 289 | |
| 290 | /*----------------------------------------------------------------------*/ |
Eric Andersen | d598d41 | 2002-04-27 09:19:39 +0000 | [diff] [blame] | 291 | #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 292 | static char append_char(mode_t mode) |
Eric Andersen | 79565b6 | 2000-08-11 18:10:21 +0000 | [diff] [blame] | 293 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 294 | if (!(all_fmt & LIST_FILETYPE)) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 295 | return '\0'; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 296 | if ((all_fmt & LIST_EXEC) && S_ISREG(mode) |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 297 | && (mode & (S_IXUSR | S_IXGRP | S_IXOTH))) |
| 298 | return '*'; |
| 299 | return APPCHAR(mode); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 300 | } |
| 301 | #endif |
Eric Andersen | 79565b6 | 2000-08-11 18:10:21 +0000 | [diff] [blame] | 302 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 303 | /*----------------------------------------------------------------------*/ |
Eric Andersen | cf1189f | 2000-11-29 21:52:06 +0000 | [diff] [blame] | 304 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 305 | #define countdirs(A,B) count_dirs((A), (B), 1) |
| 306 | #define countsubdirs(A,B) count_dirs((A), (B), 0) |
| 307 | |
| 308 | static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 309 | { |
| 310 | int i, dirs; |
| 311 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 312 | if (dn == NULL || nfiles < 1) |
| 313 | return (0); |
| 314 | dirs = 0; |
| 315 | for (i = 0; i < nfiles; i++) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 316 | if (S_ISDIR(dn[i]->dstat.st_mode) |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 317 | && (notsubdirs || |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 318 | ((dn[i]->name[0] != '.') || (dn[i]->name[1] |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 319 | && ((dn[i]->name[1] != '.') |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 320 | || dn[i]->name[2]))))) |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 321 | dirs++; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 322 | } |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 323 | return (dirs); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 326 | static int countfiles(struct dnode **dnp) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 327 | { |
| 328 | int nfiles; |
| 329 | struct dnode *cur; |
| 330 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 331 | if (dnp == NULL) |
| 332 | return (0); |
| 333 | nfiles = 0; |
| 334 | for (cur = dnp[0]; cur->next != NULL; cur = cur->next) |
| 335 | nfiles++; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 336 | nfiles++; |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 337 | return (nfiles); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | /* get memory to hold an array of pointers */ |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 341 | static struct dnode **dnalloc(int num) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 342 | { |
| 343 | struct dnode **p; |
| 344 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 345 | if (num < 1) |
| 346 | return (NULL); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 347 | |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 348 | p = (struct dnode **) xcalloc((size_t) num, (size_t) (sizeof(struct dnode *))); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 349 | return (p); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 352 | #ifdef CONFIG_FEATURE_LS_RECURSIVE |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 353 | static void dfree(struct dnode **dnp) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 354 | { |
| 355 | struct dnode *cur, *next; |
| 356 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 357 | if (dnp == NULL) |
| 358 | return; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 359 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 360 | cur = dnp[0]; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 361 | while (cur != NULL) { |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 362 | if(cur->allocated) |
| 363 | free(cur->fullname); /* free the filename */ |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 364 | next = cur->next; |
| 365 | free(cur); /* free the dnode */ |
| 366 | cur = next; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 367 | } |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 368 | free(dnp); /* free the array holding the dnode pointers */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 369 | } |
Eric Andersen | 20aab26 | 2001-07-19 22:28:02 +0000 | [diff] [blame] | 370 | #endif |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 371 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 372 | static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 373 | { |
| 374 | int dncnt, i, d; |
| 375 | struct dnode **dnp; |
| 376 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 377 | if (dn == NULL || nfiles < 1) |
| 378 | return (NULL); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 379 | |
| 380 | /* count how many dirs and regular files there are */ |
Eric Andersen | cf1189f | 2000-11-29 21:52:06 +0000 | [diff] [blame] | 381 | if (which == SPLIT_SUBDIR) |
| 382 | dncnt = countsubdirs(dn, nfiles); |
| 383 | else { |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 384 | dncnt = countdirs(dn, nfiles); /* assume we are looking for dirs */ |
Eric Andersen | cf1189f | 2000-11-29 21:52:06 +0000 | [diff] [blame] | 385 | if (which == SPLIT_FILE) |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 386 | dncnt = nfiles - dncnt; /* looking for files */ |
Eric Andersen | cf1189f | 2000-11-29 21:52:06 +0000 | [diff] [blame] | 387 | } |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 388 | |
| 389 | /* allocate a file array and a dir array */ |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 390 | dnp = dnalloc(dncnt); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 391 | |
| 392 | /* copy the entrys into the file or dir array */ |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 393 | for (d = i = 0; i < nfiles; i++) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 394 | if (S_ISDIR(dn[i]->dstat.st_mode)) { |
| 395 | if (which & (SPLIT_DIR|SPLIT_SUBDIR)) { |
| 396 | if ((which & SPLIT_DIR) |
| 397 | || ((dn[i]->name[0] != '.') |
| 398 | || (dn[i]->name[1] |
| 399 | && ((dn[i]->name[1] != '.') |
| 400 | || dn[i]->name[2])))) { |
| 401 | dnp[d++] = dn[i]; |
| 402 | } |
| 403 | } |
| 404 | } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) { |
| 405 | dnp[d++] = dn[i]; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 406 | } |
| 407 | } |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 408 | return (dnp); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /*----------------------------------------------------------------------*/ |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 412 | #ifdef CONFIG_FEATURE_LS_SORTFILES |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 413 | static int sortcmp(struct dnode *d1, struct dnode *d2) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 414 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 415 | unsigned int sort_opts = all_fmt & SORT_MASK; |
| 416 | int dif; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 417 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 418 | dif = 0; /* assume SORT_NAME */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 419 | if (sort_opts == SORT_SIZE) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 420 | dif = (int) (d2->dstat.st_size - d1->dstat.st_size); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 421 | } else if (sort_opts == SORT_ATIME) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 422 | dif = (int) (d2->dstat.st_atime - d1->dstat.st_atime); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 423 | } else if (sort_opts == SORT_CTIME) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 424 | dif = (int) (d2->dstat.st_ctime - d1->dstat.st_ctime); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 425 | } else if (sort_opts == SORT_MTIME) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 426 | dif = (int) (d2->dstat.st_mtime - d1->dstat.st_mtime); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 427 | } else if (sort_opts == SORT_DIR) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 428 | 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] | 429 | /* } else if (sort_opts == SORT_VERSION) { */ |
| 430 | /* } else if (sort_opts == SORT_EXT) { */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 433 | if (dif == 0) { |
| 434 | /* sort by name- may be a tie_breaker for time or size cmp */ |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 435 | #ifdef CONFIG_LOCALE_SUPPORT |
| 436 | dif = strcoll(d1->name, d2->name); |
| 437 | #else |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 438 | dif = strcmp(d1->name, d2->name); |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 439 | #endif |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 442 | if (all_fmt & SORT_ORDER_REVERSE) { |
| 443 | dif = -dif; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 444 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 445 | return (dif); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /*----------------------------------------------------------------------*/ |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 449 | static void shellsort(struct dnode **dn, int size) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 450 | { |
| 451 | struct dnode *temp; |
| 452 | int gap, i, j; |
| 453 | |
| 454 | /* shell short the array */ |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 455 | if (dn == NULL || size < 2) |
| 456 | return; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 457 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 458 | for (gap = size / 2; gap > 0; gap /= 2) { |
| 459 | for (i = gap; i < size; i++) { |
| 460 | for (j = i - gap; j >= 0; j -= gap) { |
| 461 | if (sortcmp(dn[j], dn[j + gap]) <= 0) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 462 | break; |
| 463 | /* they are out of order, swap them */ |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 464 | temp = dn[j]; |
| 465 | dn[j] = dn[j + gap]; |
| 466 | dn[j + gap] = temp; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | #endif |
| 472 | |
| 473 | /*----------------------------------------------------------------------*/ |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 474 | static void showfiles(struct dnode **dn, int nfiles) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 475 | { |
| 476 | int i, ncols, nrows, row, nc; |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 477 | int column = 0; |
| 478 | int nexttab = 0; |
| 479 | int column_width = 0; /* for STYLE_LONG and STYLE_SINGLE not used */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 480 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 481 | if (dn == NULL || nfiles < 1) |
| 482 | return; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 483 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 484 | if (all_fmt & STYLE_ONE_RECORD_FLAG) { |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 485 | ncols = 1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 486 | } else { |
| 487 | /* find the longest file name- use that as the column width */ |
| 488 | for (i = 0; i < nfiles; i++) { |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 489 | int len = strlen(dn[i]->name) + |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 490 | #ifdef CONFIG_SELINUX |
| 491 | ((all_fmt & LIST_CONTEXT) ? 33 : 0) + |
| 492 | #endif |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 493 | ((all_fmt & LIST_INO) ? 8 : 0) + |
| 494 | ((all_fmt & LIST_BLOCKS) ? 5 : 0); |
| 495 | if (column_width < len) |
| 496 | column_width = len; |
| 497 | } |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 498 | column_width += tabstops; |
| 499 | ncols = (int) (terminal_width / column_width); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Eric Andersen | e57d54b | 2001-01-30 18:03:11 +0000 | [diff] [blame] | 502 | if (ncols > 1) { |
| 503 | nrows = nfiles / ncols; |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 504 | if ((nrows * ncols) < nfiles) |
| 505 | nrows++; /* round up fractionals */ |
Eric Andersen | e57d54b | 2001-01-30 18:03:11 +0000 | [diff] [blame] | 506 | } else { |
| 507 | nrows = nfiles; |
| 508 | ncols = 1; |
| 509 | } |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 510 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 511 | for (row = 0; row < nrows; row++) { |
| 512 | for (nc = 0; nc < ncols; nc++) { |
Eric Andersen | 79565b6 | 2000-08-11 18:10:21 +0000 | [diff] [blame] | 513 | /* reach into the array based on the column and row */ |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 514 | i = (nc * nrows) + row; /* assume display by column */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 515 | if (all_fmt & DISP_ROWS) |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 516 | i = (row * ncols) + nc; /* display across row */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 517 | if (i < nfiles) { |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 518 | if (column > 0) { |
| 519 | nexttab -= column; |
| 520 | while (nexttab--) { |
| 521 | putchar(' '); |
| 522 | column++; |
| 523 | } |
Eric Andersen | 79565b6 | 2000-08-11 18:10:21 +0000 | [diff] [blame] | 524 | } |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 525 | nexttab = column + column_width; |
| 526 | column += list_single(dn[i]); |
Eric Andersen | 79565b6 | 2000-08-11 18:10:21 +0000 | [diff] [blame] | 527 | } |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 528 | } |
| 529 | putchar('\n'); |
| 530 | column = 0; |
Eric Andersen | a42982e | 2000-06-07 17:28:53 +0000 | [diff] [blame] | 531 | } |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /*----------------------------------------------------------------------*/ |
Glenn L McGrath | c4db083 | 2004-03-06 09:12:55 +0000 | [diff] [blame] | 535 | static void showdirs(struct dnode **dn, int ndirs, int first) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 536 | { |
| 537 | int i, nfiles; |
| 538 | struct dnode **subdnp; |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 539 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 540 | #ifdef CONFIG_FEATURE_LS_RECURSIVE |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 541 | int dndirs; |
| 542 | struct dnode **dnd; |
| 543 | #endif |
| 544 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 545 | if (dn == NULL || ndirs < 1) |
| 546 | return; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 547 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 548 | for (i = 0; i < ndirs; i++) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 549 | if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) { |
Glenn L McGrath | c4db083 | 2004-03-06 09:12:55 +0000 | [diff] [blame] | 550 | if (!first) |
| 551 | printf("\n"); |
| 552 | first = 0; |
| 553 | printf("%s:\n", dn[i]->fullname); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 554 | } |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 555 | subdnp = list_dir(dn[i]->fullname); |
| 556 | nfiles = countfiles(subdnp); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 557 | if (nfiles > 0) { |
| 558 | /* list all files at this level */ |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 559 | #ifdef CONFIG_FEATURE_LS_SORTFILES |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 560 | shellsort(subdnp, nfiles); |
| 561 | #endif |
| 562 | showfiles(subdnp, nfiles); |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 563 | #ifdef CONFIG_FEATURE_LS_RECURSIVE |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 564 | if (all_fmt & DISP_RECURSIVE) { |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 565 | /* recursive- list the sub-dirs */ |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 566 | dnd = splitdnarray(subdnp, nfiles, SPLIT_SUBDIR); |
| 567 | dndirs = countsubdirs(subdnp, nfiles); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 568 | if (dndirs > 0) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 569 | #ifdef CONFIG_FEATURE_LS_SORTFILES |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 570 | shellsort(dnd, dndirs); |
Matt Kraai | a5bd268 | 2000-10-28 06:40:09 +0000 | [diff] [blame] | 571 | #endif |
Glenn L McGrath | c4db083 | 2004-03-06 09:12:55 +0000 | [diff] [blame] | 572 | showdirs(dnd, dndirs, 0); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 573 | free(dnd); /* free the array of dnode pointers to the dirs */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 574 | } |
| 575 | } |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 576 | dfree(subdnp); /* free the dnodes and the fullname mem */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 577 | #endif |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | /*----------------------------------------------------------------------*/ |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 583 | static struct dnode **list_dir(const char *path) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 584 | { |
| 585 | struct dnode *dn, *cur, **dnp; |
| 586 | struct dirent *entry; |
| 587 | DIR *dir; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 588 | int i, nfiles; |
| 589 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 590 | if (path == NULL) |
| 591 | return (NULL); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 592 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 593 | dn = NULL; |
| 594 | nfiles = 0; |
Eric Andersen | e7e1e2d | 2000-10-12 22:40:14 +0000 | [diff] [blame] | 595 | dir = opendir(path); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 596 | if (dir == NULL) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 597 | bb_perror_msg("%s", path); |
Matt Kraai | 33fdae5 | 2000-10-13 17:59:43 +0000 | [diff] [blame] | 598 | status = EXIT_FAILURE; |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 599 | return (NULL); /* could not open the dir */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 600 | } |
| 601 | while ((entry = readdir(dir)) != NULL) { |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 602 | char *fullname; |
| 603 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 604 | /* 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] | 605 | if (entry->d_name[0] == '.') { |
| 606 | if ((entry->d_name[1] == 0 || ( |
| 607 | entry->d_name[1] == '.' |
| 608 | && entry->d_name[2] == 0)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 609 | && !(all_fmt & DISP_DOT)) |
Matt Kraai | 782ab3c | 2001-04-23 01:07:00 +0000 | [diff] [blame] | 610 | continue; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 611 | if (!(all_fmt & DISP_HIDDEN)) |
Matt Kraai | 782ab3c | 2001-04-23 01:07:00 +0000 | [diff] [blame] | 612 | continue; |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 613 | } |
| 614 | fullname = concat_path_file(path, entry->d_name); |
| 615 | cur = my_stat(fullname, strrchr(fullname, '/') + 1); |
| 616 | if (!cur) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 617 | continue; |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 618 | cur->allocated = 1; |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 619 | cur->next = dn; |
| 620 | dn = cur; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 621 | nfiles++; |
| 622 | } |
| 623 | closedir(dir); |
| 624 | |
| 625 | /* now that we know how many files there are |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 626 | ** allocate memory for an array to hold dnode pointers |
| 627 | */ |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 628 | if (dn == NULL) |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 629 | return (NULL); |
| 630 | dnp = dnalloc(nfiles); |
| 631 | for (i = 0, cur = dn; i < nfiles; i++) { |
| 632 | dnp[i] = cur; /* save pointer to node in array */ |
| 633 | cur = cur->next; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 636 | return (dnp); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | /*----------------------------------------------------------------------*/ |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 640 | static int list_single(struct dnode *dn) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 641 | { |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 642 | int i, column = 0; |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 643 | |
Matt Kraai | a1bbde7 | 2002-03-08 16:25:33 +0000 | [diff] [blame] | 644 | #ifdef CONFIG_FEATURE_LS_USERNAME |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 645 | char scratch[16]; |
Matt Kraai | a1bbde7 | 2002-03-08 16:25:33 +0000 | [diff] [blame] | 646 | #endif |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 647 | #ifdef CONFIG_FEATURE_LS_TIMESTAMPS |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 648 | char *filetime; |
| 649 | time_t ttime, age; |
| 650 | #endif |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 651 | #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 652 | struct stat info; |
| 653 | char append; |
| 654 | #endif |
| 655 | |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 656 | if (dn->fullname == NULL) |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 657 | return (0); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 658 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 659 | #ifdef CONFIG_FEATURE_LS_TIMESTAMPS |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 660 | ttime = dn->dstat.st_mtime; /* the default time */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 661 | if (all_fmt & TIME_ACCESS) |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 662 | ttime = dn->dstat.st_atime; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 663 | if (all_fmt & TIME_CHANGE) |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 664 | ttime = dn->dstat.st_ctime; |
| 665 | filetime = ctime(&ttime); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 666 | #endif |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 667 | #ifdef CONFIG_FEATURE_LS_FILETYPES |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 668 | append = append_char(dn->dstat.st_mode); |
| 669 | #endif |
| 670 | |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 671 | for (i = 0; i <= 31; i++) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 672 | switch (all_fmt & (1 << i)) { |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 673 | case LIST_INO: |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 674 | column += printf("%7ld ", (long int) dn->dstat.st_ino); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 675 | break; |
| 676 | case LIST_BLOCKS: |
Eric Andersen | 8a2e56c | 2000-09-21 02:23:30 +0000 | [diff] [blame] | 677 | #if _FILE_OFFSET_BITS == 64 |
Eric Andersen | 5e67887 | 2006-01-30 19:48:23 +0000 | [diff] [blame] | 678 | column += printf("%4lld ", (long long)dn->dstat.st_blocks >> 1); |
Eric Andersen | 8a2e56c | 2000-09-21 02:23:30 +0000 | [diff] [blame] | 679 | #else |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 680 | column += printf("%4ld ", dn->dstat.st_blocks >> 1); |
Eric Andersen | 8a2e56c | 2000-09-21 02:23:30 +0000 | [diff] [blame] | 681 | #endif |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 682 | break; |
| 683 | case LIST_MODEBITS: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 684 | column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode)); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 685 | break; |
| 686 | case LIST_NLINKS: |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 687 | column += printf("%4ld ", (long) dn->dstat.st_nlink); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 688 | break; |
| 689 | case LIST_ID_NAME: |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 690 | #ifdef CONFIG_FEATURE_LS_USERNAME |
Bernhard Reutner-Fischer | d5bd137 | 2005-09-20 21:06:17 +0000 | [diff] [blame] | 691 | bb_getpwuid(scratch, dn->dstat.st_uid, sizeof(scratch)); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 692 | printf("%-8.8s ", scratch); |
Bernhard Reutner-Fischer | d5bd137 | 2005-09-20 21:06:17 +0000 | [diff] [blame] | 693 | bb_getgrgid(scratch, dn->dstat.st_gid, sizeof(scratch)); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 694 | printf("%-8.8s", scratch); |
| 695 | column += 17; |
| 696 | break; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 697 | #endif |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 698 | case LIST_ID_NUMERIC: |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 699 | 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] | 700 | break; |
| 701 | case LIST_SIZE: |
| 702 | case LIST_DEV: |
| 703 | 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] | 704 | column += printf("%4d, %3d ", (int) major(dn->dstat.st_rdev), |
| 705 | (int) minor(dn->dstat.st_rdev)); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 706 | } else { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 707 | #ifdef CONFIG_FEATURE_HUMAN_READABLE |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 708 | if (all_fmt & LS_DISP_HR) { |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 709 | column += printf("%9s ", |
| 710 | make_human_readable_str(dn->dstat.st_size, 1, 0)); |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 711 | } else |
| 712 | #endif |
| 713 | { |
Eric Andersen | 8a2e56c | 2000-09-21 02:23:30 +0000 | [diff] [blame] | 714 | #if _FILE_OFFSET_BITS == 64 |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 715 | column += printf("%9lld ", (long long) dn->dstat.st_size); |
Eric Andersen | 8a2e56c | 2000-09-21 02:23:30 +0000 | [diff] [blame] | 716 | #else |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 717 | column += printf("%9ld ", dn->dstat.st_size); |
Eric Andersen | 8a2e56c | 2000-09-21 02:23:30 +0000 | [diff] [blame] | 718 | #endif |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 719 | } |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 720 | } |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 721 | break; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 722 | #ifdef CONFIG_FEATURE_LS_TIMESTAMPS |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 723 | case LIST_FULLTIME: |
Glenn L McGrath | 65b6d8b | 2004-01-18 05:41:30 +0000 | [diff] [blame] | 724 | printf("%24.24s ", filetime); |
| 725 | column += 25; |
| 726 | break; |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 727 | case LIST_DATE_TIME: |
Glenn L McGrath | 65b6d8b | 2004-01-18 05:41:30 +0000 | [diff] [blame] | 728 | if ((all_fmt & LIST_FULLTIME) == 0) { |
| 729 | age = time(NULL) - ttime; |
| 730 | printf("%6.6s ", filetime + 4); |
| 731 | if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) { |
| 732 | /* hh:mm if less than 6 months old */ |
| 733 | printf("%5.5s ", filetime + 11); |
| 734 | } else { |
| 735 | printf(" %4.4s ", filetime + 20); |
| 736 | } |
| 737 | column += 13; |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 738 | } |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 739 | break; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 740 | #endif |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 741 | #ifdef CONFIG_SELINUX |
| 742 | case LIST_CONTEXT: |
| 743 | { |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 744 | char context[80]; |
Rob Landley | 08abe64 | 2006-03-01 20:48:44 +0000 | [diff] [blame] | 745 | int len = 0; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 746 | |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 747 | if (dn->sid) { |
| 748 | /* I assume sid initilized with NULL */ |
| 749 | len = strlen(dn->sid)+1; |
| 750 | safe_strncpy(context, dn->sid, len); |
| 751 | freecon(dn->sid); |
| 752 | }else { |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 753 | safe_strncpy(context, "unknown", 8); |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 754 | } |
| 755 | printf("%-32s ", context); |
| 756 | column += MAX(33, len); |
| 757 | } |
| 758 | break; |
| 759 | #endif |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 760 | case LIST_FILENAME: |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 761 | #ifdef CONFIG_FEATURE_LS_COLOR |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 762 | errno = 0; |
| 763 | if (show_color && !lstat(dn->fullname, &info)) { |
| 764 | printf("\033[%d;%dm", bgcolor(info.st_mode), |
| 765 | fgcolor(info.st_mode)); |
| 766 | } |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 767 | #endif |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 768 | column += printf("%s", dn->name); |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 769 | #ifdef CONFIG_FEATURE_LS_COLOR |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 770 | if (show_color) { |
| 771 | printf("\033[0m"); |
| 772 | } |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 773 | #endif |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 774 | break; |
| 775 | case LIST_SYMLINK: |
| 776 | if (S_ISLNK(dn->dstat.st_mode)) { |
| 777 | char *lpath = xreadlink(dn->fullname); |
| 778 | |
| 779 | if (lpath) { |
| 780 | printf(" -> "); |
| 781 | #if defined(CONFIG_FEATURE_LS_FILETYPES) || defined (CONFIG_FEATURE_LS_COLOR) |
| 782 | if (!stat(dn->fullname, &info)) { |
| 783 | append = append_char(info.st_mode); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 784 | } |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 785 | #endif |
| 786 | #ifdef CONFIG_FEATURE_LS_COLOR |
| 787 | if (show_color) { |
| 788 | errno = 0; |
| 789 | printf("\033[%d;%dm", bgcolor(info.st_mode), |
| 790 | fgcolor(info.st_mode)); |
| 791 | } |
| 792 | #endif |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 793 | column += printf("%s", lpath) + 4; |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 794 | #ifdef CONFIG_FEATURE_LS_COLOR |
| 795 | if (show_color) { |
| 796 | printf("\033[0m"); |
| 797 | } |
| 798 | #endif |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 799 | free(lpath); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 800 | } |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 801 | } |
| 802 | break; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 803 | #ifdef CONFIG_FEATURE_LS_FILETYPES |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 804 | case LIST_FILETYPE: |
| 805 | if (append != '\0') { |
| 806 | printf("%1c", append); |
| 807 | column++; |
| 808 | } |
| 809 | break; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 810 | #endif |
| 811 | } |
| 812 | } |
| 813 | |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 814 | return column; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | /*----------------------------------------------------------------------*/ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 818 | |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 819 | /* "[-]Cadil1", POSIX mandated options, busybox always supports */ |
| 820 | /* "[-]gnsx", POSIX non-mandated options, busybox always supports */ |
| 821 | /* "[-]Ak" GNU options, busybox always supports */ |
| 822 | /* "[-]FLRctur", POSIX mandated options, busybox optionally supports */ |
| 823 | /* "[-]p", POSIX non-mandated options, busybox optionally supports */ |
| 824 | /* "[-]SXvThw", GNU options, busybox optionally supports */ |
| 825 | /* "[-]K", SELinux mandated options, busybox optionally supports */ |
| 826 | /* "[-]e", I think we made this one up */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 827 | |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 828 | #ifdef CONFIG_FEATURE_LS_TIMESTAMPS |
| 829 | # define LS_STR_TIMESTAMPS "cetu" |
| 830 | #else |
| 831 | # define LS_STR_TIMESTAMPS "" |
| 832 | #endif |
| 833 | |
| 834 | #ifdef CONFIG_FEATURE_LS_SORTFILES |
| 835 | # define LS_STR_SORTFILES "SXrv" |
| 836 | #else |
| 837 | # define LS_STR_SORTFILES "" |
| 838 | #endif |
| 839 | |
| 840 | #ifdef CONFIG_FEATURE_LS_FILETYPES |
| 841 | # define LS_STR_FILETYPES "Fp" |
| 842 | #else |
| 843 | # define LS_STR_FILETYPES "" |
| 844 | #endif |
| 845 | |
| 846 | #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS |
| 847 | # define LS_STR_FOLLOW_LINKS "L" |
| 848 | #else |
| 849 | # define LS_STR_FOLLOW_LINKS "" |
| 850 | #endif |
| 851 | |
| 852 | #ifdef CONFIG_FEATURE_LS_RECURSIVE |
| 853 | # define LS_STR_RECURSIVE "R" |
| 854 | #else |
| 855 | # define LS_STR_RECURSIVE "" |
| 856 | #endif |
| 857 | |
| 858 | #ifdef CONFIG_FEATURE_HUMAN_READABLE |
| 859 | # define LS_STR_HUMAN_READABLE "h" |
| 860 | #else |
| 861 | # define LS_STR_HUMAN_READABLE "" |
| 862 | #endif |
| 863 | |
| 864 | #ifdef CONFIG_SELINUX |
| 865 | # define LS_STR_SELINUX "K" |
| 866 | #else |
| 867 | # define LS_STR_SELINUX "" |
| 868 | #endif |
| 869 | |
| 870 | #ifdef CONFIG_FEATURE_AUTOWIDTH |
| 871 | # define LS_STR_AUTOWIDTH "T:w:" |
| 872 | #else |
| 873 | # define LS_STR_AUTOWIDTH "" |
| 874 | #endif |
| 875 | |
| 876 | static const char ls_options[]="Cadil1gnsxAk" \ |
| 877 | LS_STR_TIMESTAMPS \ |
| 878 | LS_STR_SORTFILES \ |
| 879 | LS_STR_FILETYPES \ |
| 880 | LS_STR_FOLLOW_LINKS \ |
| 881 | LS_STR_RECURSIVE \ |
| 882 | LS_STR_HUMAN_READABLE \ |
| 883 | LS_STR_SELINUX \ |
| 884 | LS_STR_AUTOWIDTH; |
| 885 | |
Glenn L McGrath | afc9ab8 | 2004-09-24 02:04:13 +0000 | [diff] [blame] | 886 | #define LIST_MASK_TRIGGER 0 |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 887 | #define STYLE_MASK_TRIGGER STYLE_MASK |
| 888 | #define SORT_MASK_TRIGGER SORT_MASK |
| 889 | #define DISP_MASK_TRIGGER DISP_ROWS |
| 890 | #define TIME_MASK_TRIGGER TIME_MASK |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 891 | |
| 892 | static const unsigned opt_flags[] = { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 893 | LIST_SHORT | STYLE_COLUMNS, /* C */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 894 | DISP_HIDDEN | DISP_DOT, /* a */ |
| 895 | DISP_NOLIST, /* d */ |
| 896 | LIST_INO, /* i */ |
| 897 | LIST_LONG | STYLE_LONG, /* l - remember LS_DISP_HR in mask! */ |
| 898 | LIST_SHORT | STYLE_SINGLE, /* 1 */ |
| 899 | 0, /* g - ingored */ |
| 900 | LIST_ID_NUMERIC, /* n */ |
| 901 | LIST_BLOCKS, /* s */ |
| 902 | DISP_ROWS, /* x */ |
| 903 | DISP_HIDDEN, /* A */ |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 904 | #ifdef CONFIG_SELINUX |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 905 | LIST_CONTEXT, /* k */ |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 906 | #else |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 907 | 0, /* k - ingored */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 908 | #endif |
| 909 | #ifdef CONFIG_FEATURE_LS_TIMESTAMPS |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 910 | # ifdef CONFIG_FEATURE_LS_SORTFILES |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 911 | TIME_CHANGE | SORT_CTIME, /* c */ |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 912 | # else |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 913 | TIME_CHANGE, /* c */ |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 914 | # endif |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 915 | LIST_FULLTIME, /* e */ |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 916 | # ifdef CONFIG_FEATURE_LS_SORTFILES |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 917 | SORT_MTIME, /* t */ |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 918 | # else |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 919 | 0, /* t - ignored -- is this correct? */ |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 920 | # endif |
| 921 | # ifdef CONFIG_FEATURE_LS_SORTFILES |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 922 | TIME_ACCESS | SORT_ATIME, /* u */ |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 923 | # else |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 924 | TIME_ACCESS, /* u */ |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 925 | # endif |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 926 | #endif |
| 927 | #ifdef CONFIG_FEATURE_LS_SORTFILES |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 928 | SORT_SIZE, /* S */ |
| 929 | SORT_EXT, /* X */ |
| 930 | SORT_ORDER_REVERSE, /* r */ |
| 931 | SORT_VERSION, /* v */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 932 | #endif |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 933 | #ifdef CONFIG_FEATURE_LS_FILETYPES |
| 934 | LIST_FILETYPE | LIST_EXEC, /* F */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 935 | LIST_FILETYPE, /* p */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 936 | #endif |
| 937 | #ifdef CONFIG_FEATURE_LS_FOLLOWLINKS |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 938 | FOLLOW_LINKS, /* L */ |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 939 | #endif |
| 940 | #ifdef CONFIG_FEATURE_LS_RECURSIVE |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 941 | DISP_RECURSIVE, /* R */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 942 | #endif |
| 943 | #ifdef CONFIG_FEATURE_HUMAN_READABLE |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 944 | LS_DISP_HR, /* h */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 945 | #endif |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 946 | #ifdef CONFIG_SELINUX |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 947 | LIST_MODEBITS|LIST_NLINKS|LIST_CONTEXT|LIST_SIZE|LIST_DATE_TIME, /* K */ |
| 948 | #endif |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 949 | #ifdef CONFIG_FEATURE_AUTOWIDTH |
| 950 | 0, 0, /* T, w - ignored */ |
| 951 | #endif |
Eric Andersen | d07cf59 | 2004-02-05 13:52:03 +0000 | [diff] [blame] | 952 | (1U<<31) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 953 | }; |
| 954 | |
| 955 | |
| 956 | /*----------------------------------------------------------------------*/ |
| 957 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 958 | int ls_main(int argc, char **argv) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 959 | { |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 960 | struct dnode **dnd; |
| 961 | struct dnode **dnf; |
| 962 | struct dnode **dnp; |
| 963 | struct dnode *dn; |
| 964 | struct dnode *cur; |
| 965 | long opt; |
| 966 | int nfiles = 0; |
| 967 | int dnfiles; |
| 968 | int dndirs; |
| 969 | int oi; |
| 970 | int ac; |
| 971 | int i; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 972 | char **av; |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 973 | #ifdef CONFIG_FEATURE_AUTOWIDTH |
| 974 | char *tabstops_str = NULL; |
| 975 | char *terminal_width_str = NULL; |
| 976 | #endif |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 977 | #ifdef CONFIG_FEATURE_LS_COLOR |
| 978 | char *color_opt; |
| 979 | #endif |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 980 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 981 | all_fmt = LIST_SHORT | DISP_NORMAL | STYLE_AUTO |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 982 | #ifdef CONFIG_FEATURE_LS_TIMESTAMPS |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 983 | | TIME_MOD |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 984 | #endif |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 985 | #ifdef CONFIG_FEATURE_LS_SORTFILES |
| 986 | | SORT_NAME | SORT_ORDER_FORWARD |
| 987 | #endif |
| 988 | ; |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 989 | |
Eric Andersen | 6d68781 | 2003-11-04 23:16:48 +0000 | [diff] [blame] | 990 | #ifdef CONFIG_FEATURE_AUTOWIDTH |
Eric Andersen | 8efe967 | 2003-09-15 08:33:45 +0000 | [diff] [blame] | 991 | /* Obtain the terminal width. */ |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 992 | get_terminal_width_height(STDOUT_FILENO, &terminal_width, NULL); |
Eric Andersen | 8efe967 | 2003-09-15 08:33:45 +0000 | [diff] [blame] | 993 | /* Go one less... */ |
| 994 | terminal_width--; |
Eric Andersen | 6d68781 | 2003-11-04 23:16:48 +0000 | [diff] [blame] | 995 | #endif |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 996 | |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 997 | #ifdef CONFIG_FEATURE_LS_COLOR |
Paul Fox | 156dc41 | 2005-08-01 19:33:30 +0000 | [diff] [blame] | 998 | bb_applet_long_options = ls_color_opt; |
Eric Andersen | 3ad0bd9 | 2002-03-20 09:13:48 +0000 | [diff] [blame] | 999 | #endif |
| 1000 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1001 | /* process options */ |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1002 | #ifdef CONFIG_FEATURE_AUTOWIDTH |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 1003 | opt = bb_getopt_ulflags(argc, argv, ls_options, &tabstops_str, &terminal_width_str |
| 1004 | #ifdef CONFIG_FEATURE_LS_COLOR |
| 1005 | , &color_opt |
| 1006 | #endif |
| 1007 | ); |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 1008 | if (tabstops_str) { |
| 1009 | tabstops = atoi(tabstops_str); |
| 1010 | } |
| 1011 | if (terminal_width_str) { |
| 1012 | terminal_width = atoi(terminal_width_str); |
| 1013 | } |
| 1014 | #else |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 1015 | opt = bb_getopt_ulflags(argc, argv, ls_options |
| 1016 | #ifdef CONFIG_FEATURE_LS_COLOR |
| 1017 | , &color_opt |
| 1018 | #endif |
| 1019 | ); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1020 | #endif |
Eric Andersen | d07cf59 | 2004-02-05 13:52:03 +0000 | [diff] [blame] | 1021 | for (i = 0; opt_flags[i] != (1U<<31); i++) { |
Glenn L McGrath | 792cae5 | 2004-01-18 05:15:16 +0000 | [diff] [blame] | 1022 | if (opt & (1 << i)) { |
| 1023 | unsigned int flags = opt_flags[i]; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 1024 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1025 | if (flags & LIST_MASK_TRIGGER) { |
| 1026 | all_fmt &= ~LIST_MASK; |
| 1027 | } |
| 1028 | if (flags & STYLE_MASK_TRIGGER) { |
| 1029 | all_fmt &= ~STYLE_MASK; |
| 1030 | } |
Eric Andersen | b7ebc61 | 2003-07-14 19:20:46 +0000 | [diff] [blame] | 1031 | #ifdef CONFIG_FEATURE_LS_SORTFILES |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1032 | if (flags & SORT_MASK_TRIGGER) { |
| 1033 | all_fmt &= ~SORT_MASK; |
| 1034 | } |
Eric Andersen | b7ebc61 | 2003-07-14 19:20:46 +0000 | [diff] [blame] | 1035 | #endif |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1036 | if (flags & DISP_MASK_TRIGGER) { |
| 1037 | all_fmt &= ~DISP_MASK; |
| 1038 | } |
Eric Andersen | b7ebc61 | 2003-07-14 19:20:46 +0000 | [diff] [blame] | 1039 | #ifdef CONFIG_FEATURE_LS_TIMESTAMPS |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1040 | if (flags & TIME_MASK_TRIGGER) { |
| 1041 | all_fmt &= ~TIME_MASK; |
| 1042 | } |
Eric Andersen | b7ebc61 | 2003-07-14 19:20:46 +0000 | [diff] [blame] | 1043 | #endif |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 1044 | if (flags & LIST_CONTEXT) { |
| 1045 | all_fmt |= STYLE_SINGLE; |
| 1046 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1047 | #ifdef CONFIG_FEATURE_HUMAN_READABLE |
| 1048 | if (opt == 'l') { |
| 1049 | all_fmt &= ~LS_DISP_HR; |
| 1050 | } |
| 1051 | #endif |
| 1052 | all_fmt |= flags; |
| 1053 | } |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Paul Fox | 156dc41 | 2005-08-01 19:33:30 +0000 | [diff] [blame] | 1056 | #ifdef CONFIG_FEATURE_LS_COLOR |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 1057 | { |
| 1058 | /* find color bit value - last position for short getopt */ |
Paul Fox | 156dc41 | 2005-08-01 19:33:30 +0000 | [diff] [blame] | 1059 | |
Paul Fox | 156dc41 | 2005-08-01 19:33:30 +0000 | [diff] [blame] | 1060 | #if CONFIG_FEATURE_LS_COLOR_IS_DEFAULT |
Paul Fox | 156dc41 | 2005-08-01 19:33:30 +0000 | [diff] [blame] | 1061 | char *p; |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 1062 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 1063 | if ((p = getenv ("LS_COLORS")) != NULL && |
Paul Fox | 156dc41 | 2005-08-01 19:33:30 +0000 | [diff] [blame] | 1064 | (*p == '\0' || (strcmp(p, "none") == 0))) { |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 1065 | ; |
Paul Fox | 156dc41 | 2005-08-01 19:33:30 +0000 | [diff] [blame] | 1066 | } else if (isatty(STDOUT_FILENO)) { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 1067 | show_color = 1; |
Paul Fox | 156dc41 | 2005-08-01 19:33:30 +0000 | [diff] [blame] | 1068 | } |
| 1069 | #endif |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 1070 | |
| 1071 | if((opt & (1 << i))) { /* next flag after short options */ |
| 1072 | if (color_opt == NULL || strcmp("always", color_opt) == 0) |
| 1073 | show_color = 1; |
| 1074 | else if (color_opt != NULL && strcmp("never", color_opt) == 0) |
| 1075 | show_color = 0; |
| 1076 | else if (color_opt != NULL && strcmp("auto", color_opt) == 0 && isatty(STDOUT_FILENO)) |
| 1077 | show_color = 1; |
| 1078 | } |
Paul Fox | 156dc41 | 2005-08-01 19:33:30 +0000 | [diff] [blame] | 1079 | } |
| 1080 | #endif |
| 1081 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1082 | /* sort out which command line options take precedence */ |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1083 | #ifdef CONFIG_FEATURE_LS_RECURSIVE |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1084 | if (all_fmt & DISP_NOLIST) |
| 1085 | all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1086 | #endif |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1087 | #if defined (CONFIG_FEATURE_LS_TIMESTAMPS) && defined (CONFIG_FEATURE_LS_SORTFILES) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1088 | if (all_fmt & TIME_CHANGE) |
| 1089 | all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME; |
| 1090 | if (all_fmt & TIME_ACCESS) |
| 1091 | all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1092 | #endif |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1093 | if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */ |
| 1094 | all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC); |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1095 | #ifdef CONFIG_FEATURE_LS_USERNAME |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1096 | if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC)) |
| 1097 | all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1098 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1099 | |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1100 | /* choose a display format */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1101 | if ((all_fmt & STYLE_MASK) == STYLE_AUTO) |
| 1102 | #if STYLE_AUTO != 0 |
| 1103 | all_fmt = (all_fmt & ~STYLE_MASK) |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 1104 | | (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1105 | #else |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 1106 | all_fmt |= (isatty(STDOUT_FILENO) ? STYLE_COLUMNS : STYLE_SINGLE); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1107 | #endif |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1108 | |
| 1109 | /* |
| 1110 | * when there are no cmd line args we have to supply a default "." arg. |
| 1111 | * we will create a second argv array, "av" that will hold either |
| 1112 | * our created "." arg, or the real cmd line args. The av array |
| 1113 | * just holds the pointers- we don't move the date the pointers |
| 1114 | * point to. |
| 1115 | */ |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 1116 | ac = argc - optind; /* how many cmd line args are left */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1117 | if (ac < 1) { |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 1118 | static const char * const dotdir[] = { "." }; |
| 1119 | |
| 1120 | av = (char **) dotdir; |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 1121 | ac = 1; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1122 | } else { |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 1123 | av = argv + optind; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | /* now, everything is in the av array */ |
| 1127 | if (ac > 1) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1128 | all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */ |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1129 | |
| 1130 | /* stuff the command line file names into an dnode array */ |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 1131 | dn = NULL; |
| 1132 | for (oi = 0; oi < ac; oi++) { |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 1133 | cur = my_stat(av[oi], av[oi]); |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 1134 | if (!cur) |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1135 | continue; |
"Vladimir N. Oleynik" | a8c23aa | 2005-09-05 15:06:57 +0000 | [diff] [blame] | 1136 | cur->allocated = 0; |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 1137 | cur->next = dn; |
| 1138 | dn = cur; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1139 | nfiles++; |
| 1140 | } |
| 1141 | |
| 1142 | /* now that we know how many files there are |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 1143 | ** allocate memory for an array to hold dnode pointers |
| 1144 | */ |
| 1145 | dnp = dnalloc(nfiles); |
| 1146 | for (i = 0, cur = dn; i < nfiles; i++) { |
| 1147 | dnp[i] = cur; /* save pointer to node in array */ |
| 1148 | cur = cur->next; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1151 | if (all_fmt & DISP_NOLIST) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1152 | #ifdef CONFIG_FEATURE_LS_SORTFILES |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1153 | shellsort(dnp, nfiles); |
| 1154 | #endif |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 1155 | if (nfiles > 0) |
| 1156 | showfiles(dnp, nfiles); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1157 | } else { |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 1158 | dnd = splitdnarray(dnp, nfiles, SPLIT_DIR); |
| 1159 | dnf = splitdnarray(dnp, nfiles, SPLIT_FILE); |
| 1160 | dndirs = countdirs(dnp, nfiles); |
| 1161 | dnfiles = nfiles - dndirs; |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1162 | if (dnfiles > 0) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1163 | #ifdef CONFIG_FEATURE_LS_SORTFILES |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1164 | shellsort(dnf, dnfiles); |
| 1165 | #endif |
| 1166 | showfiles(dnf, dnfiles); |
| 1167 | } |
| 1168 | if (dndirs > 0) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1169 | #ifdef CONFIG_FEATURE_LS_SORTFILES |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1170 | shellsort(dnd, dndirs); |
| 1171 | #endif |
Glenn L McGrath | c4db083 | 2004-03-06 09:12:55 +0000 | [diff] [blame] | 1172 | showdirs(dnd, dndirs, dnfiles == 0); |
Eric Andersen | 11c6552 | 2000-09-07 17:24:47 +0000 | [diff] [blame] | 1173 | } |
| 1174 | } |
Glenn L McGrath | e3906fc | 2002-08-22 18:13:54 +0000 | [diff] [blame] | 1175 | return (status); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1176 | } |