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