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> |
| 5 | * |
| 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 |
| 24 | * (i.e. the ones I couldn't live without :-) All features which involve |
| 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 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 44 | #define TERMINAL_WIDTH 80 /* use 79 if your terminal has linefold bug */ |
| 45 | #define COLUMN_WIDTH 14 /* default if AUTOWIDTH not defined */ |
| 46 | #define COLUMN_GAP 2 /* includes the file type char, if present */ |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 47 | #define HAS_REWINDDIR |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 48 | |
| 49 | /************************************************************************/ |
| 50 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 51 | #include "internal.h" |
Eric Andersen | bd22ed8 | 2000-07-08 18:55:24 +0000 | [diff] [blame] | 52 | //#if !defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) |
| 53 | //# include <linux/types.h> |
| 54 | //#else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 55 | # include <sys/types.h> |
Eric Andersen | bd22ed8 | 2000-07-08 18:55:24 +0000 | [diff] [blame] | 56 | //#endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 57 | #include <sys/stat.h> |
| 58 | #include <stdio.h> |
| 59 | #include <unistd.h> |
| 60 | #include <dirent.h> |
| 61 | #include <errno.h> |
| 62 | #include <stdio.h> |
Eric Andersen | e1850dd | 1999-11-19 05:42:32 +0000 | [diff] [blame] | 63 | #ifdef BB_FEATURE_LS_TIMESTAMPS |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 64 | #include <time.h> |
| 65 | #endif |
| 66 | |
| 67 | #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f) |
| 68 | #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)]) |
Eric Andersen | e1850dd | 1999-11-19 05:42:32 +0000 | [diff] [blame] | 69 | #ifdef BB_FEATURE_LS_FILETYPES |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 70 | #define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)]) |
| 71 | #endif |
| 72 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 73 | #define FMT_AUTO 0 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 74 | #define FMT_LONG 1 /* one record per line, extended info */ |
| 75 | #define FMT_SINGLE 2 /* one record per line */ |
| 76 | #define FMT_ROWS 3 /* print across rows */ |
| 77 | #define FMT_COLUMNS 3 /* fill columns (same, since we don't sort) */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 78 | |
| 79 | #define TIME_MOD 0 |
| 80 | #define TIME_CHANGE 1 |
| 81 | #define TIME_ACCESS 2 |
| 82 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 83 | #define DISP_FTYPE 1 /* show character for file type */ |
| 84 | #define DISP_EXEC 2 /* show '*' if regular executable file */ |
| 85 | #define DISP_HIDDEN 4 /* show files starting . (except . and ..) */ |
| 86 | #define DISP_DOT 8 /* show . and .. */ |
| 87 | #define DISP_NUMERIC 16 /* numeric uid and gid */ |
| 88 | #define DISP_FULLTIME 32 /* show extended time display */ |
Eric Andersen | a42982e | 2000-06-07 17:28:53 +0000 | [diff] [blame] | 89 | #define DIR_NOLIST 64 /* show directory as itself, not contents */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 90 | #define DISP_DIRNAME 128 /* show directory name (for internal use) */ |
Eric Andersen | a42982e | 2000-06-07 17:28:53 +0000 | [diff] [blame] | 91 | #define DISP_RECURSIVE 256 /* Do a recursive listing */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 92 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 93 | #ifndef MAJOR |
| 94 | #define MAJOR(dev) (((dev)>>8)&0xff) |
| 95 | #define MINOR(dev) ((dev)&0xff) |
| 96 | #endif |
| 97 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 98 | static unsigned char display_fmt = FMT_AUTO; |
| 99 | static unsigned short opts = 0; |
| 100 | static unsigned short column = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 101 | |
Eric Andersen | e1850dd | 1999-11-19 05:42:32 +0000 | [diff] [blame] | 102 | #ifdef BB_FEATURE_AUTOWIDTH |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 103 | static unsigned short terminal_width = 0; |
| 104 | static unsigned short column_width = 0; |
| 105 | static unsigned short toplevel_column_width = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 106 | #else |
| 107 | #define terminal_width TERMINAL_WIDTH |
| 108 | #define column_width COLUMN_WIDTH |
| 109 | #endif |
| 110 | |
Eric Andersen | e1850dd | 1999-11-19 05:42:32 +0000 | [diff] [blame] | 111 | #ifdef BB_FEATURE_LS_TIMESTAMPS |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 112 | static unsigned char time_fmt = TIME_MOD; |
| 113 | #endif |
| 114 | |
| 115 | #define wr(data,len) fwrite(data, 1, len, stdout) |
| 116 | |
| 117 | static void writenum(long val, short minwidth) |
| 118 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 119 | char scratch[128]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 120 | |
| 121 | char *p = scratch + sizeof(scratch); |
| 122 | short len = 0; |
| 123 | short neg = (val < 0); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 124 | |
| 125 | if (neg) |
| 126 | val = -val; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 127 | do |
| 128 | *--p = (val % 10) + '0', len++, val /= 10; |
| 129 | while (val); |
| 130 | if (neg) |
| 131 | *--p = '-', len++; |
| 132 | while (len < minwidth) |
| 133 | *--p = ' ', len++; |
| 134 | wr(p, len); |
| 135 | column += len; |
| 136 | } |
| 137 | |
| 138 | static void newline(void) |
| 139 | { |
| 140 | if (column > 0) { |
| 141 | wr("\n", 1); |
| 142 | column = 0; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | static void tab(short col) |
| 147 | { |
| 148 | static const char spaces[] = " "; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 149 | |
| 150 | #define nspaces ((sizeof spaces)-1) /* null terminator! */ |
| 151 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 152 | short n = col - column; |
| 153 | |
| 154 | if (n > 0) { |
| 155 | column = col; |
| 156 | while (n > nspaces) { |
| 157 | wr(spaces, nspaces); |
| 158 | n -= nspaces; |
| 159 | } |
| 160 | /* must be 1...(sizeof spaces) left */ |
| 161 | wr(spaces, n); |
| 162 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 163 | #undef nspaces |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Eric Andersen | e1850dd | 1999-11-19 05:42:32 +0000 | [diff] [blame] | 166 | #ifdef BB_FEATURE_LS_FILETYPES |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 167 | static char append_char(mode_t mode) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 168 | { |
| 169 | if (!(opts & DISP_FTYPE)) |
| 170 | return '\0'; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 171 | if ((opts & DISP_EXEC) && S_ISREG(mode) |
| 172 | && (mode & (S_IXUSR | S_IXGRP | S_IXOTH))) return '*'; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 173 | return APPCHAR(mode); |
| 174 | } |
| 175 | #endif |
| 176 | |
| 177 | /** |
| 178 | ** |
| 179 | ** Display a file or directory as a single item |
| 180 | ** (in either long or short format) |
| 181 | ** |
| 182 | **/ |
| 183 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 184 | static void list_single(const char *name, struct stat *info, |
| 185 | const char *fullname) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 186 | { |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 187 | char scratch[BUFSIZ + 1]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 188 | short len = strlen(name); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 189 | |
Eric Andersen | e1850dd | 1999-11-19 05:42:32 +0000 | [diff] [blame] | 190 | #ifdef BB_FEATURE_LS_FILETYPES |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 191 | char append = append_char(info->st_mode); |
| 192 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 193 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 194 | if (display_fmt == FMT_LONG) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 195 | mode_t mode = info->st_mode; |
| 196 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 197 | newline(); |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 198 | wr(modeString(mode), 10); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 199 | column = 10; |
| 200 | writenum((long) info->st_nlink, (short) 5); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 201 | fputs(" ", stdout); |
Eric Andersen | e1850dd | 1999-11-19 05:42:32 +0000 | [diff] [blame] | 202 | #ifdef BB_FEATURE_LS_USERNAME |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 203 | if (!(opts & DISP_NUMERIC)) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 204 | memset(scratch, 0, sizeof(scratch)); |
| 205 | my_getpwuid(scratch, info->st_uid); |
Erik Andersen | 1266a13 | 1999-12-29 22:19:46 +0000 | [diff] [blame] | 206 | if (*scratch) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 207 | fputs(scratch, stdout); |
| 208 | if (strlen(scratch) <= 8) |
| 209 | wr(" ", 9 - strlen(scratch)); |
| 210 | } else { |
| 211 | writenum((long) info->st_uid, (short) 8); |
Eric Andersen | 394f764 | 1999-11-23 21:38:12 +0000 | [diff] [blame] | 212 | fputs(" ", stdout); |
| 213 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 214 | } else |
| 215 | #endif |
Eric Andersen | 394f764 | 1999-11-23 21:38:12 +0000 | [diff] [blame] | 216 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 217 | writenum((long) info->st_uid, (short) 8); |
| 218 | fputs(" ", stdout); |
Eric Andersen | 394f764 | 1999-11-23 21:38:12 +0000 | [diff] [blame] | 219 | } |
Eric Andersen | e1850dd | 1999-11-19 05:42:32 +0000 | [diff] [blame] | 220 | #ifdef BB_FEATURE_LS_USERNAME |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 221 | if (!(opts & DISP_NUMERIC)) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 222 | memset(scratch, 0, sizeof(scratch)); |
| 223 | my_getgrgid(scratch, info->st_gid); |
Erik Andersen | 1266a13 | 1999-12-29 22:19:46 +0000 | [diff] [blame] | 224 | if (*scratch) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 225 | fputs(scratch, stdout); |
| 226 | if (strlen(scratch) <= 8) |
| 227 | wr(" ", 8 - strlen(scratch)); |
| 228 | } else |
| 229 | writenum((long) info->st_gid, (short) 8); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 230 | } else |
| 231 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 232 | writenum((long) info->st_gid, (short) 8); |
Erik Andersen | 1266a13 | 1999-12-29 22:19:46 +0000 | [diff] [blame] | 233 | //tab(26); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 234 | if (S_ISBLK(mode) || S_ISCHR(mode)) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 235 | writenum((long) MAJOR(info->st_rdev), (short) 3); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 236 | fputs(", ", stdout); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 237 | writenum((long) MINOR(info->st_rdev), (short) 3); |
| 238 | } else |
| 239 | writenum((long) info->st_size, (short) 8); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 240 | fputs(" ", stdout); |
Erik Andersen | 1266a13 | 1999-12-29 22:19:46 +0000 | [diff] [blame] | 241 | //tab(32); |
Eric Andersen | e1850dd | 1999-11-19 05:42:32 +0000 | [diff] [blame] | 242 | #ifdef BB_FEATURE_LS_TIMESTAMPS |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 243 | { |
| 244 | time_t cal; |
| 245 | char *string; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 246 | |
| 247 | switch (time_fmt) { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 248 | case TIME_CHANGE: |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 249 | cal = info->st_ctime; |
| 250 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 251 | case TIME_ACCESS: |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 252 | cal = info->st_atime; |
| 253 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 254 | default: |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 255 | cal = info->st_mtime; |
| 256 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 257 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 258 | string = ctime(&cal); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 259 | if (opts & DISP_FULLTIME) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 260 | wr(string, 24); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 261 | else { |
| 262 | time_t age = time(NULL) - cal; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 263 | |
| 264 | wr(string + 4, 7); /* mmm_dd_ */ |
| 265 | if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 266 | /* hh:mm if less than 6 months old */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 267 | wr(string + 11, 5); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 268 | else |
| 269 | /* _yyyy otherwise */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 270 | wr(string + 19, 5); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 271 | } |
| 272 | wr(" ", 1); |
| 273 | } |
| 274 | #else |
| 275 | fputs("--- -- ----- ", stdout); |
| 276 | #endif |
| 277 | wr(name, len); |
| 278 | if (S_ISLNK(mode)) { |
| 279 | wr(" -> ", 4); |
Eric Andersen | 07e5297 | 1999-11-07 07:38:08 +0000 | [diff] [blame] | 280 | len = readlink(fullname, scratch, sizeof scratch); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 281 | if (len > 0) |
| 282 | fwrite(scratch, 1, len, stdout); |
Eric Andersen | e1850dd | 1999-11-19 05:42:32 +0000 | [diff] [blame] | 283 | #ifdef BB_FEATURE_LS_FILETYPES |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 284 | /* show type of destination */ |
| 285 | if (opts & DISP_FTYPE) { |
Eric Andersen | 07e5297 | 1999-11-07 07:38:08 +0000 | [diff] [blame] | 286 | if (!stat(fullname, info)) { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 287 | append = append_char(info->st_mode); |
| 288 | if (append) |
| 289 | fputc(append, stdout); |
| 290 | } |
| 291 | } |
| 292 | #endif |
| 293 | } |
Eric Andersen | e1850dd | 1999-11-19 05:42:32 +0000 | [diff] [blame] | 294 | #ifdef BB_FEATURE_LS_FILETYPES |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 295 | else if (append) |
| 296 | wr(&append, 1); |
| 297 | #endif |
| 298 | } else { |
| 299 | static short nexttab = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 300 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 301 | /* sort out column alignment */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 302 | if (column == 0); /* nothing to do */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 303 | else if (display_fmt == FMT_SINGLE) |
| 304 | newline(); |
| 305 | else { |
| 306 | if (nexttab + column_width > terminal_width |
Eric Andersen | e1850dd | 1999-11-19 05:42:32 +0000 | [diff] [blame] | 307 | #ifndef BB_FEATURE_AUTOWIDTH |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 308 | || nexttab + len >= terminal_width |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 309 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 310 | ) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 311 | newline(); |
| 312 | else |
| 313 | tab(nexttab); |
| 314 | } |
| 315 | /* work out where next column starts */ |
Eric Andersen | e1850dd | 1999-11-19 05:42:32 +0000 | [diff] [blame] | 316 | #ifdef BB_FEATURE_AUTOWIDTH |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 317 | /* we know the calculated width is big enough */ |
| 318 | nexttab = column + column_width + COLUMN_GAP; |
| 319 | #else |
| 320 | /* might cover more than one fixed-width column */ |
| 321 | nexttab = column; |
| 322 | do |
| 323 | nexttab += column_width + COLUMN_GAP; |
| 324 | while (nexttab < (column + len + COLUMN_GAP)); |
| 325 | #endif |
| 326 | /* now write the data */ |
| 327 | wr(name, len); |
| 328 | column = column + len; |
Eric Andersen | e1850dd | 1999-11-19 05:42:32 +0000 | [diff] [blame] | 329 | #ifdef BB_FEATURE_LS_FILETYPES |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 330 | if (append) |
| 331 | wr(&append, 1), column++; |
| 332 | #endif |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | ** |
| 338 | ** List the given file or directory, expanding a directory |
| 339 | ** to show its contents if required |
| 340 | ** |
| 341 | **/ |
| 342 | |
| 343 | static int list_item(const char *name) |
| 344 | { |
| 345 | struct stat info; |
| 346 | DIR *dir; |
| 347 | struct dirent *entry; |
Eric Andersen | 5d893b6 | 2000-07-06 01:57:20 +0000 | [diff] [blame] | 348 | char fullname[BUFSIZ + 1], *fnend; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 349 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 350 | if (lstat(name, &info)) |
| 351 | goto listerr; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 352 | |
| 353 | if (!S_ISDIR(info.st_mode) || (opts & DIR_NOLIST)) { |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 354 | #ifdef BB_FEATURE_AUTOWIDTH |
| 355 | column_width = toplevel_column_width; |
| 356 | #endif |
Eric Andersen | 07e5297 | 1999-11-07 07:38:08 +0000 | [diff] [blame] | 357 | list_single(name, &info, name); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | /* Otherwise, it's a directory we want to list the contents of */ |
| 362 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 363 | if (opts & DISP_DIRNAME) { /* identify the directory */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 364 | if (column) |
| 365 | wr("\n\n", 2), column = 0; |
| 366 | wr(name, strlen(name)); |
| 367 | wr(":\n", 2); |
| 368 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 369 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 370 | dir = opendir(name); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 371 | if (!dir) |
| 372 | goto listerr; |
Eric Andersen | e1850dd | 1999-11-19 05:42:32 +0000 | [diff] [blame] | 373 | #ifdef BB_FEATURE_AUTOWIDTH |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 374 | column_width = 0; |
| 375 | while ((entry = readdir(dir)) != NULL) { |
| 376 | short w = strlen(entry->d_name); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 377 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 378 | if (column_width < w) |
| 379 | column_width = w; |
| 380 | } |
| 381 | #ifdef HAS_REWINDDIR |
| 382 | rewinddir(dir); |
| 383 | #else |
| 384 | closedir(dir); |
| 385 | dir = opendir(name); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 386 | if (!dir) |
| 387 | goto listerr; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 388 | #endif |
| 389 | #endif |
| 390 | |
| 391 | /* List the contents */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 392 | |
| 393 | strcpy(fullname, name); /* *** ignore '.' by itself */ |
| 394 | fnend = fullname + strlen(fullname); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 395 | if (fnend[-1] != '/') |
| 396 | *fnend++ = '/'; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 397 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 398 | while ((entry = readdir(dir)) != NULL) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 399 | const char *en = entry->d_name; |
| 400 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 401 | if (en[0] == '.') { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 402 | if (!en[1] || (en[1] == '.' && !en[2])) { /* . or .. */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 403 | if (!(opts & DISP_DOT)) |
| 404 | continue; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 405 | } else if (!(opts & DISP_HIDDEN)) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 406 | continue; |
| 407 | } |
| 408 | /* FIXME: avoid stat if not required */ |
| 409 | strcpy(fnend, entry->d_name); |
| 410 | if (lstat(fullname, &info)) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 411 | goto direrr; /* (shouldn't fail) */ |
Eric Andersen | 07e5297 | 1999-11-07 07:38:08 +0000 | [diff] [blame] | 412 | list_single(entry->d_name, &info, fullname); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 413 | } |
| 414 | closedir(dir); |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 415 | |
| 416 | if (opts & DISP_DIRNAME) { /* separate the directory */ |
| 417 | if (column) { |
| 418 | wr("\n", 1); |
| 419 | } |
| 420 | wr("\n", 1); |
| 421 | column = 0; |
| 422 | } |
| 423 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 424 | return 0; |
| 425 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 426 | direrr: |
| 427 | closedir(dir); |
| 428 | listerr: |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 429 | newline(); |
Eric Andersen | 9d3aba7 | 1999-10-06 09:04:55 +0000 | [diff] [blame] | 430 | perror(name); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 431 | return 1; |
| 432 | } |
| 433 | |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 434 | static const char ls_usage[] = "ls [-1a" |
Eric Andersen | e1850dd | 1999-11-19 05:42:32 +0000 | [diff] [blame] | 435 | #ifdef BB_FEATURE_LS_TIMESTAMPS |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 436 | "c" |
| 437 | #endif |
| 438 | "d" |
Eric Andersen | e1850dd | 1999-11-19 05:42:32 +0000 | [diff] [blame] | 439 | #ifdef BB_FEATURE_LS_TIMESTAMPS |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 440 | "e" |
| 441 | #endif |
| 442 | "ln" |
Eric Andersen | e1850dd | 1999-11-19 05:42:32 +0000 | [diff] [blame] | 443 | #ifdef BB_FEATURE_LS_FILETYPES |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 444 | "p" |
| 445 | #endif |
Eric Andersen | e1850dd | 1999-11-19 05:42:32 +0000 | [diff] [blame] | 446 | #ifdef BB_FEATURE_LS_TIMESTAMPS |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 447 | "u" |
| 448 | #endif |
| 449 | "xAC" |
Eric Andersen | e1850dd | 1999-11-19 05:42:32 +0000 | [diff] [blame] | 450 | #ifdef BB_FEATURE_LS_FILETYPES |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 451 | "F" |
| 452 | #endif |
Eric Andersen | a42982e | 2000-06-07 17:28:53 +0000 | [diff] [blame] | 453 | #ifdef BB_FEATURE_LS_RECURSIVE |
| 454 | "R" |
| 455 | #endif |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 456 | "] [filenames...]\n" |
| 457 | #ifndef BB_FEATURE_TRIVIAL_HELP |
| 458 | "\nList directory contents\n\n" |
Erik Andersen | 9cf3bfa | 2000-04-13 18:49:43 +0000 | [diff] [blame] | 459 | "Options:\n" |
| 460 | "\t-a\tdo not hide entries starting with .\n" |
| 461 | #ifdef BB_FEATURE_LS_TIMESTAMPS |
| 462 | "\t-c\twith -l: show ctime (the time of last\n" |
| 463 | "\t\tmodification of file status information)\n" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 464 | #endif |
Erik Andersen | 9cf3bfa | 2000-04-13 18:49:43 +0000 | [diff] [blame] | 465 | "\t-d\tlist directory entries instead of contents\n" |
| 466 | #ifdef BB_FEATURE_LS_TIMESTAMPS |
| 467 | "\t-e\tlist both full date and full time\n" |
| 468 | #endif |
| 469 | "\t-l\tuse a long listing format\n" |
| 470 | "\t-n\tlist numeric UIDs and GIDs instead of names\n" |
| 471 | #ifdef BB_FEATURE_LS_FILETYPES |
| 472 | "\t-p\tappend indicator (one of /=@|) to entries\n" |
| 473 | #endif |
| 474 | #ifdef BB_FEATURE_LS_TIMESTAMPS |
| 475 | "\t-u\twith -l: show access time (the time of last\n" |
| 476 | "\t\taccess of the file)\n" |
| 477 | #endif |
| 478 | "\t-x\tlist entries by lines instead of by columns\n" |
| 479 | "\t-A\tdo not list implied . and ..\n" |
| 480 | "\t-C\tlist entries by columns\n" |
| 481 | #ifdef BB_FEATURE_LS_FILETYPES |
| 482 | "\t-F\tappend indicator (one of */=@|) to entries\n" |
| 483 | #endif |
Eric Andersen | a42982e | 2000-06-07 17:28:53 +0000 | [diff] [blame] | 484 | #ifdef BB_FEATURE_LS_RECURSIVE |
| 485 | "\t-R\tlist subdirectories recursively\n" |
| 486 | #endif |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 487 | #endif |
Erik Andersen | 9cf3bfa | 2000-04-13 18:49:43 +0000 | [diff] [blame] | 488 | ; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 489 | |
Eric Andersen | a42982e | 2000-06-07 17:28:53 +0000 | [diff] [blame] | 490 | |
| 491 | #ifdef BB_FEATURE_LS_RECURSIVE |
| 492 | static int dirAction(const char *fileName, struct stat *statbuf, void* junk) |
| 493 | { |
| 494 | int i; |
| 495 | fprintf(stdout, "\n%s:\n", fileName); |
| 496 | i = list_item(fileName); |
| 497 | newline(); |
| 498 | return (i); |
| 499 | } |
| 500 | #endif |
| 501 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 502 | extern int ls_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 503 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 504 | int argi = 1, i; |
| 505 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 506 | /* process options */ |
| 507 | while (argi < argc && argv[argi][0] == '-') { |
| 508 | const char *p = &argv[argi][1]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 509 | |
| 510 | if (!*p) |
| 511 | goto print_usage_message; /* "-" by itself not allowed */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 512 | if (*p == '-') { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 513 | if (!p[1]) { /* "--" forces end of options */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 514 | argi++; |
| 515 | break; |
| 516 | } |
| 517 | /* it's a long option name - we don't support them */ |
| 518 | goto print_usage_message; |
| 519 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 520 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 521 | while (*p) |
| 522 | switch (*p++) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 523 | case 'l': |
| 524 | display_fmt = FMT_LONG; |
| 525 | break; |
| 526 | case '1': |
| 527 | display_fmt = FMT_SINGLE; |
| 528 | break; |
| 529 | case 'x': |
| 530 | display_fmt = FMT_ROWS; |
| 531 | break; |
| 532 | case 'C': |
| 533 | display_fmt = FMT_COLUMNS; |
| 534 | break; |
Eric Andersen | e1850dd | 1999-11-19 05:42:32 +0000 | [diff] [blame] | 535 | #ifdef BB_FEATURE_LS_FILETYPES |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 536 | case 'p': |
| 537 | opts |= DISP_FTYPE; |
| 538 | break; |
| 539 | case 'F': |
| 540 | opts |= DISP_FTYPE | DISP_EXEC; |
| 541 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 542 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 543 | case 'A': |
| 544 | opts |= DISP_HIDDEN; |
| 545 | break; |
| 546 | case 'a': |
| 547 | opts |= DISP_HIDDEN | DISP_DOT; |
| 548 | break; |
| 549 | case 'n': |
| 550 | opts |= DISP_NUMERIC; |
| 551 | break; |
| 552 | case 'd': |
| 553 | opts |= DIR_NOLIST; |
| 554 | break; |
Eric Andersen | e1850dd | 1999-11-19 05:42:32 +0000 | [diff] [blame] | 555 | #ifdef BB_FEATURE_LS_TIMESTAMPS |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 556 | case 'u': |
| 557 | time_fmt = TIME_ACCESS; |
| 558 | break; |
| 559 | case 'c': |
| 560 | time_fmt = TIME_CHANGE; |
| 561 | break; |
| 562 | case 'e': |
| 563 | opts |= DISP_FULLTIME; |
| 564 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 565 | #endif |
Eric Andersen | a42982e | 2000-06-07 17:28:53 +0000 | [diff] [blame] | 566 | #ifdef BB_FEATURE_LS_RECURSIVE |
| 567 | case 'R': |
| 568 | opts |= DISP_RECURSIVE; |
| 569 | break; |
| 570 | #endif |
Eric Andersen | 7c31ea4 | 2000-07-03 14:55:49 +0000 | [diff] [blame] | 571 | case 'g': /* ignore -- for ftp servers */ |
| 572 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 573 | default: |
| 574 | goto print_usage_message; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 575 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 576 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 577 | argi++; |
| 578 | } |
| 579 | |
| 580 | /* choose a display format */ |
| 581 | if (display_fmt == FMT_AUTO) |
Eric Andersen | 08b1034 | 1999-11-19 02:38:58 +0000 | [diff] [blame] | 582 | display_fmt = isatty(fileno(stdout)) ? FMT_COLUMNS : FMT_SINGLE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 583 | if (argi < argc - 1) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 584 | opts |= DISP_DIRNAME; /* 2 or more items? label directories */ |
Eric Andersen | e1850dd | 1999-11-19 05:42:32 +0000 | [diff] [blame] | 585 | #ifdef BB_FEATURE_AUTOWIDTH |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 586 | /* could add a -w option and/or TIOCGWINSZ call */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 587 | if (terminal_width < 1) |
| 588 | terminal_width = TERMINAL_WIDTH; |
| 589 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 590 | for (i = argi; i < argc; i++) { |
| 591 | int len = strlen(argv[i]); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 592 | |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 593 | if (toplevel_column_width < len) |
| 594 | toplevel_column_width = len; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 595 | } |
| 596 | #endif |
| 597 | |
| 598 | /* process files specified, or current directory if none */ |
Eric Andersen | a42982e | 2000-06-07 17:28:53 +0000 | [diff] [blame] | 599 | #ifdef BB_FEATURE_LS_RECURSIVE |
| 600 | if (opts & DISP_RECURSIVE) { |
| 601 | i = 0; |
| 602 | if (argi == argc) { |
| 603 | i = recursiveAction(".", TRUE, FALSE, FALSE, NULL, dirAction, NULL); |
| 604 | } |
| 605 | while (argi < argc) { |
| 606 | i |= recursiveAction(argv[argi++], TRUE, FALSE, FALSE, NULL, dirAction, NULL); |
| 607 | } |
| 608 | } else |
| 609 | #endif |
| 610 | { |
| 611 | i = 0; |
| 612 | if (argi == argc) |
| 613 | i = list_item("."); |
| 614 | while (argi < argc) |
| 615 | i |= list_item(argv[argi++]); |
| 616 | newline(); |
| 617 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 618 | exit(i); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 619 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 620 | print_usage_message: |
| 621 | usage(ls_usage); |
| 622 | exit(FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 623 | } |