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