blob: 20373eaaa94668adda547e16609dd47f89af0eef [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen9d3aba71999-10-06 09:04:55 +00002/*
Eric Andersencc8ed391999-10-05 16:24:54 +00003 * 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 Andersen9ffdaa62000-02-11 21:55:04 +000032 * 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 Andersencc8ed391999-10-05 16:24:54 +000036 * 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 Andersene49d5ec2000-02-08 19:58:47 +000044#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 Andersen3c163821999-10-14 22:16:57 +000047#define HAS_REWINDDIR
Eric Andersencc8ed391999-10-05 16:24:54 +000048
49/************************************************************************/
50
Eric Andersene77ae3a1999-10-19 20:03:34 +000051#include "internal.h"
Erik Andersene49d5ec2000-02-08 19:58:47 +000052# include <sys/types.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000053#include <stdio.h>
54#include <unistd.h>
55#include <dirent.h>
56#include <errno.h>
57#include <stdio.h>
Eric Andersene1850dd1999-11-19 05:42:32 +000058#ifdef BB_FEATURE_LS_TIMESTAMPS
Eric Andersencc8ed391999-10-05 16:24:54 +000059#include <time.h>
60#endif
61
62#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
63#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
Eric Andersene1850dd1999-11-19 05:42:32 +000064#ifdef BB_FEATURE_LS_FILETYPES
Eric Andersencc8ed391999-10-05 16:24:54 +000065#define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
66#endif
67
Eric Andersencc8ed391999-10-05 16:24:54 +000068#define FMT_AUTO 0
Erik Andersene49d5ec2000-02-08 19:58:47 +000069#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 Andersencc8ed391999-10-05 16:24:54 +000073
74#define TIME_MOD 0
75#define TIME_CHANGE 1
76#define TIME_ACCESS 2
77
Erik Andersene49d5ec2000-02-08 19:58:47 +000078#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 Andersena42982e2000-06-07 17:28:53 +000084#define DIR_NOLIST 64 /* show directory as itself, not contents */
Erik Andersene49d5ec2000-02-08 19:58:47 +000085#define DISP_DIRNAME 128 /* show directory name (for internal use) */
Eric Andersena42982e2000-06-07 17:28:53 +000086#define DISP_RECURSIVE 256 /* Do a recursive listing */
Eric Andersencc8ed391999-10-05 16:24:54 +000087
Erik Andersen1ad302a2000-03-24 00:54:46 +000088#ifndef MAJOR
89#define MAJOR(dev) (((dev)>>8)&0xff)
90#define MINOR(dev) ((dev)&0xff)
91#endif
92
Eric Andersen79565b62000-08-11 18:10:21 +000093#ifdef BB_FEATURE_LS_SORTFILES
94struct 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};
99typedef struct dnode dnode_t;
100#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000101static unsigned char display_fmt = FMT_AUTO;
102static unsigned short opts = 0;
103static unsigned short column = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000104
Eric Andersene1850dd1999-11-19 05:42:32 +0000105#ifdef BB_FEATURE_AUTOWIDTH
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000106static unsigned short terminal_width = 0;
107static unsigned short column_width = 0;
108static unsigned short toplevel_column_width = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000109#else
110#define terminal_width TERMINAL_WIDTH
111#define column_width COLUMN_WIDTH
112#endif
113
Eric Andersene1850dd1999-11-19 05:42:32 +0000114#ifdef BB_FEATURE_LS_TIMESTAMPS
Eric Andersencc8ed391999-10-05 16:24:54 +0000115static unsigned char time_fmt = TIME_MOD;
116#endif
117
118#define wr(data,len) fwrite(data, 1, len, stdout)
119
120static void writenum(long val, short minwidth)
121{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122 char scratch[128];
Eric Andersencc8ed391999-10-05 16:24:54 +0000123
124 char *p = scratch + sizeof(scratch);
125 short len = 0;
126 short neg = (val < 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127
128 if (neg)
129 val = -val;
Eric Andersencc8ed391999-10-05 16:24:54 +0000130 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
141static void newline(void)
142{
143 if (column > 0) {
144 wr("\n", 1);
145 column = 0;
146 }
147}
148
149static void tab(short col)
150{
151 static const char spaces[] = " ";
Erik Andersene49d5ec2000-02-08 19:58:47 +0000152
153#define nspaces ((sizeof spaces)-1) /* null terminator! */
154
Eric Andersencc8ed391999-10-05 16:24:54 +0000155 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 Andersene49d5ec2000-02-08 19:58:47 +0000166#undef nspaces
Eric Andersencc8ed391999-10-05 16:24:54 +0000167}
168
Eric Andersene1850dd1999-11-19 05:42:32 +0000169#ifdef BB_FEATURE_LS_FILETYPES
Eric Andersene77ae3a1999-10-19 20:03:34 +0000170static char append_char(mode_t mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000171{
172 if (!(opts & DISP_FTYPE))
173 return '\0';
Erik Andersene49d5ec2000-02-08 19:58:47 +0000174 if ((opts & DISP_EXEC) && S_ISREG(mode)
175 && (mode & (S_IXUSR | S_IXGRP | S_IXOTH))) return '*';
Eric Andersencc8ed391999-10-05 16:24:54 +0000176 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 Andersene49d5ec2000-02-08 19:58:47 +0000187static void list_single(const char *name, struct stat *info,
188 const char *fullname)
Eric Andersencc8ed391999-10-05 16:24:54 +0000189{
Erik Andersen4f3f7572000-04-28 00:18:56 +0000190 char scratch[BUFSIZ + 1];
Eric Andersencc8ed391999-10-05 16:24:54 +0000191 short len = strlen(name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000192
Eric Andersene1850dd1999-11-19 05:42:32 +0000193#ifdef BB_FEATURE_LS_FILETYPES
Eric Andersencc8ed391999-10-05 16:24:54 +0000194 char append = append_char(info->st_mode);
195#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000196
Eric Andersencc8ed391999-10-05 16:24:54 +0000197 if (display_fmt == FMT_LONG) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000198 mode_t mode = info->st_mode;
199
Eric Andersencc8ed391999-10-05 16:24:54 +0000200 newline();
Eric Andersen50d63601999-11-09 01:47:36 +0000201 wr(modeString(mode), 10);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000202 column = 10;
203 writenum((long) info->st_nlink, (short) 5);
Eric Andersencc8ed391999-10-05 16:24:54 +0000204 fputs(" ", stdout);
Eric Andersene1850dd1999-11-19 05:42:32 +0000205#ifdef BB_FEATURE_LS_USERNAME
Eric Andersencc8ed391999-10-05 16:24:54 +0000206 if (!(opts & DISP_NUMERIC)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000207 memset(scratch, 0, sizeof(scratch));
208 my_getpwuid(scratch, info->st_uid);
Erik Andersen1266a131999-12-29 22:19:46 +0000209 if (*scratch) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000210 fputs(scratch, stdout);
211 if (strlen(scratch) <= 8)
212 wr(" ", 9 - strlen(scratch));
213 } else {
214 writenum((long) info->st_uid, (short) 8);
Eric Andersen394f7641999-11-23 21:38:12 +0000215 fputs(" ", stdout);
216 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000217 } else
218#endif
Eric Andersen394f7641999-11-23 21:38:12 +0000219 {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000220 writenum((long) info->st_uid, (short) 8);
221 fputs(" ", stdout);
Eric Andersen394f7641999-11-23 21:38:12 +0000222 }
Eric Andersene1850dd1999-11-19 05:42:32 +0000223#ifdef BB_FEATURE_LS_USERNAME
Eric Andersencc8ed391999-10-05 16:24:54 +0000224 if (!(opts & DISP_NUMERIC)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225 memset(scratch, 0, sizeof(scratch));
226 my_getgrgid(scratch, info->st_gid);
Erik Andersen1266a131999-12-29 22:19:46 +0000227 if (*scratch) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000228 fputs(scratch, stdout);
229 if (strlen(scratch) <= 8)
230 wr(" ", 8 - strlen(scratch));
231 } else
232 writenum((long) info->st_gid, (short) 8);
Eric Andersencc8ed391999-10-05 16:24:54 +0000233 } else
234#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000235 writenum((long) info->st_gid, (short) 8);
Erik Andersen1266a131999-12-29 22:19:46 +0000236 //tab(26);
Eric Andersencc8ed391999-10-05 16:24:54 +0000237 if (S_ISBLK(mode) || S_ISCHR(mode)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000238 writenum((long) MAJOR(info->st_rdev), (short) 3);
Eric Andersencc8ed391999-10-05 16:24:54 +0000239 fputs(", ", stdout);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000240 writenum((long) MINOR(info->st_rdev), (short) 3);
241 } else
242 writenum((long) info->st_size, (short) 8);
Eric Andersencc8ed391999-10-05 16:24:54 +0000243 fputs(" ", stdout);
Erik Andersen1266a131999-12-29 22:19:46 +0000244 //tab(32);
Eric Andersene1850dd1999-11-19 05:42:32 +0000245#ifdef BB_FEATURE_LS_TIMESTAMPS
Eric Andersencc8ed391999-10-05 16:24:54 +0000246 {
247 time_t cal;
248 char *string;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000249
250 switch (time_fmt) {
Eric Andersencc8ed391999-10-05 16:24:54 +0000251 case TIME_CHANGE:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000252 cal = info->st_ctime;
253 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000254 case TIME_ACCESS:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000255 cal = info->st_atime;
256 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000257 default:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000258 cal = info->st_mtime;
259 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000260 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000261 string = ctime(&cal);
Eric Andersencc8ed391999-10-05 16:24:54 +0000262 if (opts & DISP_FULLTIME)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000263 wr(string, 24);
Eric Andersencc8ed391999-10-05 16:24:54 +0000264 else {
265 time_t age = time(NULL) - cal;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000266
267 wr(string + 4, 7); /* mmm_dd_ */
268 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60)
Eric Andersencc8ed391999-10-05 16:24:54 +0000269 /* hh:mm if less than 6 months old */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000270 wr(string + 11, 5);
Eric Andersencc8ed391999-10-05 16:24:54 +0000271 else
272 /* _yyyy otherwise */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000273 wr(string + 19, 5);
Eric Andersencc8ed391999-10-05 16:24:54 +0000274 }
275 wr(" ", 1);
276 }
277#else
278 fputs("--- -- ----- ", stdout);
279#endif
280 wr(name, len);
281 if (S_ISLNK(mode)) {
282 wr(" -> ", 4);
Eric Andersen07e52971999-11-07 07:38:08 +0000283 len = readlink(fullname, scratch, sizeof scratch);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000284 if (len > 0)
285 fwrite(scratch, 1, len, stdout);
Eric Andersene1850dd1999-11-19 05:42:32 +0000286#ifdef BB_FEATURE_LS_FILETYPES
Eric Andersencc8ed391999-10-05 16:24:54 +0000287 /* show type of destination */
288 if (opts & DISP_FTYPE) {
Eric Andersen07e52971999-11-07 07:38:08 +0000289 if (!stat(fullname, info)) {
Eric Andersencc8ed391999-10-05 16:24:54 +0000290 append = append_char(info->st_mode);
291 if (append)
292 fputc(append, stdout);
293 }
294 }
295#endif
296 }
Eric Andersene1850dd1999-11-19 05:42:32 +0000297#ifdef BB_FEATURE_LS_FILETYPES
Eric Andersencc8ed391999-10-05 16:24:54 +0000298 else if (append)
299 wr(&append, 1);
300#endif
301 } else {
302 static short nexttab = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000303
Eric Andersencc8ed391999-10-05 16:24:54 +0000304 /* sort out column alignment */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000305 if (column == 0); /* nothing to do */
Eric Andersencc8ed391999-10-05 16:24:54 +0000306 else if (display_fmt == FMT_SINGLE)
307 newline();
308 else {
309 if (nexttab + column_width > terminal_width
Eric Andersene1850dd1999-11-19 05:42:32 +0000310#ifndef BB_FEATURE_AUTOWIDTH
Erik Andersene49d5ec2000-02-08 19:58:47 +0000311 || nexttab + len >= terminal_width
Eric Andersencc8ed391999-10-05 16:24:54 +0000312#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000313 )
Eric Andersencc8ed391999-10-05 16:24:54 +0000314 newline();
315 else
316 tab(nexttab);
317 }
318 /* work out where next column starts */
Eric Andersene1850dd1999-11-19 05:42:32 +0000319#ifdef BB_FEATURE_AUTOWIDTH
Eric Andersencc8ed391999-10-05 16:24:54 +0000320 /* 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 Andersene1850dd1999-11-19 05:42:32 +0000332#ifdef BB_FEATURE_LS_FILETYPES
Eric Andersencc8ed391999-10-05 16:24:54 +0000333 if (append)
334 wr(&append, 1), column++;
335#endif
336 }
337}
338
Eric Andersen79565b62000-08-11 18:10:21 +0000339#ifdef BB_FEATURE_LS_SORTFILES
340void 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
359void 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 Andersencc8ed391999-10-05 16:24:54 +0000381/**
382 **
383 ** List the given file or directory, expanding a directory
384 ** to show its contents if required
385 **
386 **/
387
388static int list_item(const char *name)
389{
390 struct stat info;
391 DIR *dir;
392 struct dirent *entry;
Eric Andersen5d893b62000-07-06 01:57:20 +0000393 char fullname[BUFSIZ + 1], *fnend;
Eric Andersen79565b62000-08-11 18:10:21 +0000394#ifdef BB_FEATURE_LS_SORTFILES
395 int ni=0, nfiles=0;
396 struct dnode **dnp;
397 dnode_t *cur;
398#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000399
Eric Andersencc8ed391999-10-05 16:24:54 +0000400 if (lstat(name, &info))
401 goto listerr;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000402
403 if (!S_ISDIR(info.st_mode) || (opts & DIR_NOLIST)) {
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000404#ifdef BB_FEATURE_AUTOWIDTH
405 column_width = toplevel_column_width;
406#endif
Eric Andersen07e52971999-11-07 07:38:08 +0000407 list_single(name, &info, name);
Eric Andersencc8ed391999-10-05 16:24:54 +0000408 return 0;
409 }
410
411 /* Otherwise, it's a directory we want to list the contents of */
412
Erik Andersene49d5ec2000-02-08 19:58:47 +0000413 if (opts & DISP_DIRNAME) { /* identify the directory */
Eric Andersencc8ed391999-10-05 16:24:54 +0000414 if (column)
415 wr("\n\n", 2), column = 0;
416 wr(name, strlen(name));
417 wr(":\n", 2);
418 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000419
Eric Andersencc8ed391999-10-05 16:24:54 +0000420 dir = opendir(name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000421 if (!dir)
422 goto listerr;
Eric Andersene1850dd1999-11-19 05:42:32 +0000423#ifdef BB_FEATURE_AUTOWIDTH
Eric Andersencc8ed391999-10-05 16:24:54 +0000424 column_width = 0;
425 while ((entry = readdir(dir)) != NULL) {
426 short w = strlen(entry->d_name);
Eric Andersen79565b62000-08-11 18:10:21 +0000427#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 Andersene49d5ec2000-02-08 19:58:47 +0000439
Eric Andersencc8ed391999-10-05 16:24:54 +0000440 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 Andersene49d5ec2000-02-08 19:58:47 +0000448 if (!dir)
449 goto listerr;
Eric Andersencc8ed391999-10-05 16:24:54 +0000450#endif
451#endif
Eric Andersen79565b62000-08-11 18:10:21 +0000452#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 Andersencc8ed391999-10-05 16:24:54 +0000458
459 /* List the contents */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000460
461 strcpy(fullname, name); /* *** ignore '.' by itself */
462 fnend = fullname + strlen(fullname);
Eric Andersencc8ed391999-10-05 16:24:54 +0000463 if (fnend[-1] != '/')
464 *fnend++ = '/';
Erik Andersene49d5ec2000-02-08 19:58:47 +0000465
Eric Andersencc8ed391999-10-05 16:24:54 +0000466 while ((entry = readdir(dir)) != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000467 const char *en = entry->d_name;
468
Eric Andersencc8ed391999-10-05 16:24:54 +0000469 if (en[0] == '.') {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000470 if (!en[1] || (en[1] == '.' && !en[2])) { /* . or .. */
Eric Andersencc8ed391999-10-05 16:24:54 +0000471 if (!(opts & DISP_DOT))
472 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000473 } else if (!(opts & DISP_HIDDEN))
Eric Andersencc8ed391999-10-05 16:24:54 +0000474 continue;
475 }
476 /* FIXME: avoid stat if not required */
477 strcpy(fnend, entry->d_name);
Eric Andersen79565b62000-08-11 18:10:21 +0000478#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 Andersencc8ed391999-10-05 16:24:54 +0000486 if (lstat(fullname, &info))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000487 goto direrr; /* (shouldn't fail) */
Eric Andersen07e52971999-11-07 07:38:08 +0000488 list_single(entry->d_name, &info, fullname);
Eric Andersen79565b62000-08-11 18:10:21 +0000489#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000490 }
491 closedir(dir);
Eric Andersen79565b62000-08-11 18:10:21 +0000492#ifdef BB_FEATURE_LS_SORTFILES
493 shellsort(dnp, nfiles);
494 showdnodes(dnp, nfiles);
495#endif
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000496
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 Andersencc8ed391999-10-05 16:24:54 +0000505 return 0;
506
Erik Andersene49d5ec2000-02-08 19:58:47 +0000507 direrr:
508 closedir(dir);
509 listerr:
Eric Andersencc8ed391999-10-05 16:24:54 +0000510 newline();
Eric Andersen9d3aba71999-10-06 09:04:55 +0000511 perror(name);
Eric Andersencc8ed391999-10-05 16:24:54 +0000512 return 1;
513}
514
Eric Andersena42982e2000-06-07 17:28:53 +0000515#ifdef BB_FEATURE_LS_RECURSIVE
516static 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 Andersene49d5ec2000-02-08 19:58:47 +0000526extern int ls_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000527{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000528 int argi = 1, i;
529
Eric Andersencc8ed391999-10-05 16:24:54 +0000530 /* process options */
531 while (argi < argc && argv[argi][0] == '-') {
532 const char *p = &argv[argi][1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000533
534 if (!*p)
535 goto print_usage_message; /* "-" by itself not allowed */
Eric Andersencc8ed391999-10-05 16:24:54 +0000536 if (*p == '-') {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000537 if (!p[1]) { /* "--" forces end of options */
Eric Andersencc8ed391999-10-05 16:24:54 +0000538 argi++;
539 break;
540 }
541 /* it's a long option name - we don't support them */
542 goto print_usage_message;
543 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000544
Eric Andersencc8ed391999-10-05 16:24:54 +0000545 while (*p)
546 switch (*p++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000547 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 Andersene1850dd1999-11-19 05:42:32 +0000559#ifdef BB_FEATURE_LS_FILETYPES
Erik Andersene49d5ec2000-02-08 19:58:47 +0000560 case 'p':
561 opts |= DISP_FTYPE;
562 break;
563 case 'F':
564 opts |= DISP_FTYPE | DISP_EXEC;
565 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000566#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000567 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 Andersene1850dd1999-11-19 05:42:32 +0000579#ifdef BB_FEATURE_LS_TIMESTAMPS
Erik Andersene49d5ec2000-02-08 19:58:47 +0000580 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 Andersencc8ed391999-10-05 16:24:54 +0000589#endif
Eric Andersena42982e2000-06-07 17:28:53 +0000590#ifdef BB_FEATURE_LS_RECURSIVE
591 case 'R':
592 opts |= DISP_RECURSIVE;
593 break;
594#endif
Eric Andersen7c31ea42000-07-03 14:55:49 +0000595 case 'g': /* ignore -- for ftp servers */
596 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000597 default:
598 goto print_usage_message;
Eric Andersencc8ed391999-10-05 16:24:54 +0000599 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000600
Eric Andersencc8ed391999-10-05 16:24:54 +0000601 argi++;
602 }
603
604 /* choose a display format */
605 if (display_fmt == FMT_AUTO)
Eric Andersen08b10341999-11-19 02:38:58 +0000606 display_fmt = isatty(fileno(stdout)) ? FMT_COLUMNS : FMT_SINGLE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000607 if (argi < argc - 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000608 opts |= DISP_DIRNAME; /* 2 or more items? label directories */
Eric Andersene1850dd1999-11-19 05:42:32 +0000609#ifdef BB_FEATURE_AUTOWIDTH
Eric Andersencc8ed391999-10-05 16:24:54 +0000610 /* could add a -w option and/or TIOCGWINSZ call */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000611 if (terminal_width < 1)
612 terminal_width = TERMINAL_WIDTH;
613
Eric Andersencc8ed391999-10-05 16:24:54 +0000614 for (i = argi; i < argc; i++) {
615 int len = strlen(argv[i]);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000616
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000617 if (toplevel_column_width < len)
618 toplevel_column_width = len;
Eric Andersencc8ed391999-10-05 16:24:54 +0000619 }
620#endif
621
622 /* process files specified, or current directory if none */
Eric Andersena42982e2000-06-07 17:28:53 +0000623#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 Andersene49d5ec2000-02-08 19:58:47 +0000642 exit(i);
Eric Andersencc8ed391999-10-05 16:24:54 +0000643
Erik Andersene49d5ec2000-02-08 19:58:47 +0000644 print_usage_message:
645 usage(ls_usage);
646 exit(FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000647}