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