blob: 3b380671d1835be17ae45745ec9b37ae5872c561 [file] [log] [blame]
Eric Andersen9d3aba71999-10-06 09:04:55 +00001/*
Eric Andersencc8ed391999-10-05 16:24:54 +00002 * tiny-ls.c version 0.1.0: A minimalist 'ls'
3 * Copyright (C) 1996 Brian Candler <B.Candler@pobox.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20/*
21 * To achieve a small memory footprint, this version of 'ls' doesn't do any
22 * file sorting, and only has the most essential command line switches
23 * (i.e. the ones I couldn't live without :-) All features which involve
24 * linking in substantial chunks of libc can be disabled.
25 *
26 * Although I don't really want to add new features to this program to
27 * keep it small, I *am* interested to receive bug fixes and ways to make
28 * it more portable.
29 *
30 * KNOWN BUGS:
31 * 1. messy output if you mix files and directories on the command line
32 * 2. ls -l of a directory doesn't give "total <blocks>" header
33 * 3. ls of a symlink to a directory doesn't list directory contents
34 * 4. hidden files can make column width too large
35 * NON-OPTIMAL BEHAVIOUR:
36 * 1. autowidth reads directories twice
37 * 2. if you do a short directory listing without filetype characters
38 * appended, there's no need to stat each one
39 * PORTABILITY:
40 * 1. requires lstat (BSD) - how do you do it without?
41 */
42
Eric Andersencc8ed391999-10-05 16:24:54 +000043#define TERMINAL_WIDTH 80 /* use 79 if your terminal has linefold bug */
44#define COLUMN_WIDTH 14 /* default if AUTOWIDTH not defined */
45#define COLUMN_GAP 2 /* includes the file type char, if present */
Eric Andersen3c163821999-10-14 22:16:57 +000046#define HAS_REWINDDIR
Eric Andersencc8ed391999-10-05 16:24:54 +000047
48/************************************************************************/
49
Eric Andersene77ae3a1999-10-19 20:03:34 +000050#include "internal.h"
51#if !defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Eric Andersencc8ed391999-10-05 16:24:54 +000052# include <linux/types.h>
53#else
54# include <sys/types.h>
55#endif
56#include <sys/stat.h>
57#include <stdio.h>
58#include <unistd.h>
59#include <dirent.h>
60#include <errno.h>
61#include <stdio.h>
Eric Andersene1850dd1999-11-19 05:42:32 +000062#ifdef BB_FEATURE_LS_TIMESTAMPS
Eric Andersencc8ed391999-10-05 16:24:54 +000063#include <time.h>
64#endif
65
66#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
67#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
Eric Andersene1850dd1999-11-19 05:42:32 +000068#ifdef BB_FEATURE_LS_FILETYPES
Eric Andersencc8ed391999-10-05 16:24:54 +000069#define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
70#endif
71
72#ifndef MAJOR
73#define MAJOR(dev) (((dev)>>8)&0xff)
74#define MINOR(dev) ((dev)&0xff)
75#endif
76
Eric Andersencc8ed391999-10-05 16:24:54 +000077#define FMT_AUTO 0
78#define FMT_LONG 1 /* one record per line, extended info */
79#define FMT_SINGLE 2 /* one record per line */
80#define FMT_ROWS 3 /* print across rows */
81#define FMT_COLUMNS 3 /* fill columns (same, since we don't sort) */
82
83#define TIME_MOD 0
84#define TIME_CHANGE 1
85#define TIME_ACCESS 2
86
87#define DISP_FTYPE 1 /* show character for file type */
88#define DISP_EXEC 2 /* show '*' if regular executable file */
89#define DISP_HIDDEN 4 /* show files starting . (except . and ..) */
90#define DISP_DOT 8 /* show . and .. */
91#define DISP_NUMERIC 16 /* numeric uid and gid */
92#define DISP_FULLTIME 32 /* show extended time display */
93#define DIR_NOLIST 64 /* show directory as itself, not contents */
94#define DISP_DIRNAME 128 /* show directory name (for internal use) */
95#define DIR_RECURSE 256 /* -R (not yet implemented) */
96
97static unsigned char display_fmt = FMT_AUTO;
98static unsigned short opts = 0;
99static unsigned short column = 0;
100
Eric Andersene1850dd1999-11-19 05:42:32 +0000101#ifdef BB_FEATURE_AUTOWIDTH
Eric Andersencc8ed391999-10-05 16:24:54 +0000102static unsigned short terminal_width = 0, column_width = 0;
103#else
104#define terminal_width TERMINAL_WIDTH
105#define column_width COLUMN_WIDTH
106#endif
107
Eric Andersene1850dd1999-11-19 05:42:32 +0000108#ifdef BB_FEATURE_LS_TIMESTAMPS
Eric Andersencc8ed391999-10-05 16:24:54 +0000109static unsigned char time_fmt = TIME_MOD;
110#endif
111
112#define wr(data,len) fwrite(data, 1, len, stdout)
113
114static void writenum(long val, short minwidth)
115{
Eric Andersene77ae3a1999-10-19 20:03:34 +0000116 char scratch[128];
Eric Andersencc8ed391999-10-05 16:24:54 +0000117
118 char *p = scratch + sizeof(scratch);
119 short len = 0;
120 short neg = (val < 0);
121
122 if (neg) val = -val;
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
134static void newline(void)
135{
136 if (column > 0) {
137 wr("\n", 1);
138 column = 0;
139 }
140}
141
142static void tab(short col)
143{
144 static const char spaces[] = " ";
145 #define nspaces ((sizeof spaces)-1) /* null terminator! */
146
147 short n = col - column;
148
149 if (n > 0) {
150 column = col;
151 while (n > nspaces) {
152 wr(spaces, nspaces);
153 n -= nspaces;
154 }
155 /* must be 1...(sizeof spaces) left */
156 wr(spaces, n);
157 }
158 #undef nspaces
159}
160
Eric Andersene1850dd1999-11-19 05:42:32 +0000161#ifdef BB_FEATURE_LS_FILETYPES
Eric Andersene77ae3a1999-10-19 20:03:34 +0000162static char append_char(mode_t mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000163{
164 if (!(opts & DISP_FTYPE))
165 return '\0';
166 if ((opts & DISP_EXEC) && S_ISREG(mode) && (mode & (S_IXUSR|S_IXGRP|S_IXOTH)))
167 return '*';
168 return APPCHAR(mode);
169}
170#endif
171
172/**
173 **
174 ** Display a file or directory as a single item
175 ** (in either long or short format)
176 **
177 **/
178
Eric Andersen07e52971999-11-07 07:38:08 +0000179static void list_single(const char *name, struct stat *info, const char *fullname)
Eric Andersencc8ed391999-10-05 16:24:54 +0000180{
Eric Andersene77ae3a1999-10-19 20:03:34 +0000181 char scratch[PATH_MAX];
Eric Andersencc8ed391999-10-05 16:24:54 +0000182 short len = strlen(name);
Eric Andersene1850dd1999-11-19 05:42:32 +0000183#ifdef BB_FEATURE_LS_FILETYPES
Eric Andersencc8ed391999-10-05 16:24:54 +0000184 char append = append_char(info->st_mode);
185#endif
186
187 if (display_fmt == FMT_LONG) {
Eric Andersene77ae3a1999-10-19 20:03:34 +0000188 mode_t mode = info->st_mode;
Eric Andersencc8ed391999-10-05 16:24:54 +0000189 newline();
Eric Andersen50d63601999-11-09 01:47:36 +0000190 wr(modeString(mode), 10);
Eric Andersencc8ed391999-10-05 16:24:54 +0000191 column=10;
Eric Andersen50d63601999-11-09 01:47:36 +0000192 writenum((long)info->st_nlink,(short)5);
Eric Andersencc8ed391999-10-05 16:24:54 +0000193 fputs(" ", stdout);
Eric Andersene1850dd1999-11-19 05:42:32 +0000194#ifdef BB_FEATURE_LS_USERNAME
Eric Andersencc8ed391999-10-05 16:24:54 +0000195 if (!(opts & DISP_NUMERIC)) {
Eric Andersen50d63601999-11-09 01:47:36 +0000196 scratch[8]='\0';
197 my_getpwuid( scratch, info->st_uid);
198 if (*scratch)
199 fputs(scratch, stdout);
Eric Andersencc8ed391999-10-05 16:24:54 +0000200 else
201 writenum((long)info->st_uid,(short)0);
202 } else
203#endif
204 writenum((long)info->st_uid,(short)0);
Eric Andersen50d63601999-11-09 01:47:36 +0000205 tab(16);
Eric Andersene1850dd1999-11-19 05:42:32 +0000206#ifdef BB_FEATURE_LS_USERNAME
Eric Andersencc8ed391999-10-05 16:24:54 +0000207 if (!(opts & DISP_NUMERIC)) {
Eric Andersen50d63601999-11-09 01:47:36 +0000208 scratch[8]='\0';
209 my_getgrgid( scratch, info->st_gid);
210 if (*scratch)
211 fputs(scratch, stdout);
Eric Andersencc8ed391999-10-05 16:24:54 +0000212 else
213 writenum((long)info->st_gid,(short)0);
214 } else
215#endif
216 writenum((long)info->st_gid,(short)0);
Eric Andersen50d63601999-11-09 01:47:36 +0000217 tab(17);
Eric Andersencc8ed391999-10-05 16:24:54 +0000218 if (S_ISBLK(mode) || S_ISCHR(mode)) {
219 writenum((long)MAJOR(info->st_rdev),(short)3);
220 fputs(", ", stdout);
221 writenum((long)MINOR(info->st_rdev),(short)3);
222 }
223 else
224 writenum((long)info->st_size,(short)8);
225 fputs(" ", stdout);
Eric Andersene1850dd1999-11-19 05:42:32 +0000226#ifdef BB_FEATURE_LS_TIMESTAMPS
Eric Andersencc8ed391999-10-05 16:24:54 +0000227 {
228 time_t cal;
229 char *string;
230
231 switch(time_fmt) {
232 case TIME_CHANGE:
233 cal=info->st_ctime; break;
234 case TIME_ACCESS:
235 cal=info->st_atime; break;
236 default:
237 cal=info->st_mtime; break;
238 }
239 string=ctime(&cal);
240 if (opts & DISP_FULLTIME)
241 wr(string,24);
242 else {
243 time_t age = time(NULL) - cal;
244 wr(string+4,7); /* mmm_dd_ */
245 if(age < 3600L*24*365/2 && age > -15*60)
246 /* hh:mm if less than 6 months old */
247 wr(string+11,5);
248 else
249 /* _yyyy otherwise */
250 wr(string+19,5);
251 }
252 wr(" ", 1);
253 }
254#else
255 fputs("--- -- ----- ", stdout);
256#endif
257 wr(name, len);
258 if (S_ISLNK(mode)) {
259 wr(" -> ", 4);
Eric Andersen07e52971999-11-07 07:38:08 +0000260 len = readlink(fullname, scratch, sizeof scratch);
Eric Andersencc8ed391999-10-05 16:24:54 +0000261 if (len > 0) fwrite(scratch, 1, len, stdout);
Eric Andersene1850dd1999-11-19 05:42:32 +0000262#ifdef BB_FEATURE_LS_FILETYPES
Eric Andersencc8ed391999-10-05 16:24:54 +0000263 /* show type of destination */
264 if (opts & DISP_FTYPE) {
Eric Andersen07e52971999-11-07 07:38:08 +0000265 if (!stat(fullname, info)) {
Eric Andersencc8ed391999-10-05 16:24:54 +0000266 append = append_char(info->st_mode);
267 if (append)
268 fputc(append, stdout);
269 }
270 }
271#endif
272 }
Eric Andersene1850dd1999-11-19 05:42:32 +0000273#ifdef BB_FEATURE_LS_FILETYPES
Eric Andersencc8ed391999-10-05 16:24:54 +0000274 else if (append)
275 wr(&append, 1);
276#endif
277 } else {
278 static short nexttab = 0;
279
280 /* sort out column alignment */
281 if (column == 0)
282 ; /* nothing to do */
283 else if (display_fmt == FMT_SINGLE)
284 newline();
285 else {
286 if (nexttab + column_width > terminal_width
Eric Andersene1850dd1999-11-19 05:42:32 +0000287#ifndef BB_FEATURE_AUTOWIDTH
Eric Andersencc8ed391999-10-05 16:24:54 +0000288 || nexttab + len >= terminal_width
289#endif
290 )
291 newline();
292 else
293 tab(nexttab);
294 }
295 /* work out where next column starts */
Eric Andersene1850dd1999-11-19 05:42:32 +0000296#ifdef BB_FEATURE_AUTOWIDTH
Eric Andersencc8ed391999-10-05 16:24:54 +0000297 /* we know the calculated width is big enough */
298 nexttab = column + column_width + COLUMN_GAP;
299#else
300 /* might cover more than one fixed-width column */
301 nexttab = column;
302 do
303 nexttab += column_width + COLUMN_GAP;
304 while (nexttab < (column + len + COLUMN_GAP));
305#endif
306 /* now write the data */
307 wr(name, len);
308 column = column + len;
Eric Andersene1850dd1999-11-19 05:42:32 +0000309#ifdef BB_FEATURE_LS_FILETYPES
Eric Andersencc8ed391999-10-05 16:24:54 +0000310 if (append)
311 wr(&append, 1), column++;
312#endif
313 }
314}
315
316/**
317 **
318 ** List the given file or directory, expanding a directory
319 ** to show its contents if required
320 **
321 **/
322
323static int list_item(const char *name)
324{
325 struct stat info;
326 DIR *dir;
327 struct dirent *entry;
328 char fullname[MAXNAMLEN+1], *fnend;
329
330 if (lstat(name, &info))
331 goto listerr;
332
333 if (!S_ISDIR(info.st_mode) ||
334 (opts & DIR_NOLIST)) {
Eric Andersen07e52971999-11-07 07:38:08 +0000335 list_single(name, &info, name);
Eric Andersencc8ed391999-10-05 16:24:54 +0000336 return 0;
337 }
338
339 /* Otherwise, it's a directory we want to list the contents of */
340
341 if (opts & DISP_DIRNAME) { /* identify the directory */
342 if (column)
343 wr("\n\n", 2), column = 0;
344 wr(name, strlen(name));
345 wr(":\n", 2);
346 }
347
348 dir = opendir(name);
349 if (!dir) goto listerr;
Eric Andersene1850dd1999-11-19 05:42:32 +0000350#ifdef BB_FEATURE_AUTOWIDTH
Eric Andersencc8ed391999-10-05 16:24:54 +0000351 column_width = 0;
352 while ((entry = readdir(dir)) != NULL) {
353 short w = strlen(entry->d_name);
354 if (column_width < w)
355 column_width = w;
356 }
357#ifdef HAS_REWINDDIR
358 rewinddir(dir);
359#else
360 closedir(dir);
361 dir = opendir(name);
362 if (!dir) goto listerr;
363#endif
364#endif
365
366 /* List the contents */
367
368 strcpy(fullname,name); /* *** ignore '.' by itself */
369 fnend=fullname+strlen(fullname);
370 if (fnend[-1] != '/')
371 *fnend++ = '/';
372
373 while ((entry = readdir(dir)) != NULL) {
374 const char *en=entry->d_name;
375 if (en[0] == '.') {
376 if (!en[1] || (en[1] == '.' && !en[2])) { /* . or .. */
377 if (!(opts & DISP_DOT))
378 continue;
379 }
380 else if (!(opts & DISP_HIDDEN))
381 continue;
382 }
383 /* FIXME: avoid stat if not required */
384 strcpy(fnend, entry->d_name);
385 if (lstat(fullname, &info))
386 goto direrr; /* (shouldn't fail) */
Eric Andersen07e52971999-11-07 07:38:08 +0000387 list_single(entry->d_name, &info, fullname);
Eric Andersencc8ed391999-10-05 16:24:54 +0000388 }
389 closedir(dir);
390 return 0;
391
392direrr:
393 closedir(dir);
394listerr:
395 newline();
Eric Andersen9d3aba71999-10-06 09:04:55 +0000396 perror(name);
Eric Andersencc8ed391999-10-05 16:24:54 +0000397 return 1;
398}
399
Eric Andersenf5a38381999-10-19 22:26:25 +0000400static const char ls_usage[] = "ls [-1a"
Eric Andersene1850dd1999-11-19 05:42:32 +0000401#ifdef BB_FEATURE_LS_TIMESTAMPS
Eric Andersencc8ed391999-10-05 16:24:54 +0000402 "c"
403#endif
404 "d"
Eric Andersene1850dd1999-11-19 05:42:32 +0000405#ifdef BB_FEATURE_LS_TIMESTAMPS
Eric Andersencc8ed391999-10-05 16:24:54 +0000406 "e"
407#endif
408 "ln"
Eric Andersene1850dd1999-11-19 05:42:32 +0000409#ifdef BB_FEATURE_LS_FILETYPES
Eric Andersencc8ed391999-10-05 16:24:54 +0000410 "p"
411#endif
Eric Andersene1850dd1999-11-19 05:42:32 +0000412#ifdef BB_FEATURE_LS_TIMESTAMPS
Eric Andersencc8ed391999-10-05 16:24:54 +0000413 "u"
414#endif
415 "xAC"
Eric Andersene1850dd1999-11-19 05:42:32 +0000416#ifdef BB_FEATURE_LS_FILETYPES
Eric Andersencc8ed391999-10-05 16:24:54 +0000417 "F"
418#endif
419#ifdef FEATURE_RECURSIVE
420 "R"
421#endif
422 "] [filenames...]\n";
423
424extern int
Eric Andersen9d3aba71999-10-06 09:04:55 +0000425ls_main(int argc, char * * argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000426{
427 int argi=1, i;
428
429 /* process options */
430 while (argi < argc && argv[argi][0] == '-') {
431 const char *p = &argv[argi][1];
432
433 if (!*p) goto print_usage_message; /* "-" by itself not allowed */
434 if (*p == '-') {
435 if (!p[1]) { /* "--" forces end of options */
436 argi++;
437 break;
438 }
439 /* it's a long option name - we don't support them */
440 goto print_usage_message;
441 }
442
443 while (*p)
444 switch (*p++) {
445 case 'l': display_fmt = FMT_LONG; break;
446 case '1': display_fmt = FMT_SINGLE; break;
447 case 'x': display_fmt = FMT_ROWS; break;
448 case 'C': display_fmt = FMT_COLUMNS; break;
Eric Andersene1850dd1999-11-19 05:42:32 +0000449#ifdef BB_FEATURE_LS_FILETYPES
Eric Andersencc8ed391999-10-05 16:24:54 +0000450 case 'p': opts |= DISP_FTYPE; break;
451 case 'F': opts |= DISP_FTYPE|DISP_EXEC; break;
452#endif
453 case 'A': opts |= DISP_HIDDEN; break;
454 case 'a': opts |= DISP_HIDDEN|DISP_DOT; break;
455 case 'n': opts |= DISP_NUMERIC; break;
456 case 'd': opts |= DIR_NOLIST; break;
457#ifdef FEATURE_RECURSIVE
458 case 'R': opts |= DIR_RECURSE; break;
459#endif
Eric Andersene1850dd1999-11-19 05:42:32 +0000460#ifdef BB_FEATURE_LS_TIMESTAMPS
Eric Andersencc8ed391999-10-05 16:24:54 +0000461 case 'u': time_fmt = TIME_ACCESS; break;
462 case 'c': time_fmt = TIME_CHANGE; break;
463 case 'e': opts |= DISP_FULLTIME; break;
464#endif
465 default: goto print_usage_message;
466 }
467
468 argi++;
469 }
470
471 /* choose a display format */
472 if (display_fmt == FMT_AUTO)
Eric Andersen08b10341999-11-19 02:38:58 +0000473 display_fmt = isatty(fileno(stdout)) ? FMT_COLUMNS : FMT_SINGLE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000474 if (argi < argc - 1)
475 opts |= DISP_DIRNAME; /* 2 or more items? label directories */
Eric Andersene1850dd1999-11-19 05:42:32 +0000476#ifdef BB_FEATURE_AUTOWIDTH
Eric Andersencc8ed391999-10-05 16:24:54 +0000477 /* could add a -w option and/or TIOCGWINSZ call */
478 if (terminal_width < 1) terminal_width = TERMINAL_WIDTH;
479
480 for (i = argi; i < argc; i++) {
481 int len = strlen(argv[i]);
482 if (column_width < len)
483 column_width = len;
484 }
485#endif
486
487 /* process files specified, or current directory if none */
488 i=0;
489 if (argi == argc)
490 i = list_item(".");
491 while (argi < argc)
492 i |= list_item(argv[argi++]);
493 newline();
Eric Andersen2c103011999-10-13 22:56:11 +0000494 exit( i);
Eric Andersencc8ed391999-10-05 16:24:54 +0000495
496print_usage_message:
Eric Andersenb0e9a701999-10-18 22:28:26 +0000497 usage (ls_usage);
Eric Andersen2c103011999-10-13 22:56:11 +0000498 exit( FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000499}
Eric Andersen9d3aba71999-10-06 09:04:55 +0000500