1 /* vi: set sw=4 ts=4: */
3 * tiny-ls.c version 0.1.0: A minimalist 'ls'
4 * Copyright (C) 1996 Brian Candler <B.Candler@pobox.com>
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.
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.
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.
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.
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
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
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
41 * 1. requires lstat (BSD) - how do you do it without?
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 */
49 /************************************************************************/
52 # include <sys/types.h>
59 #ifdef BB_FEATURE_LS_TIMESTAMPS
63 #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
64 #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
65 #ifdef BB_FEATURE_LS_FILETYPES
66 #define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
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) */
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 */
85 #define DIR_NOLIST 64 /* show directory as itself, not contents */
86 #define DISP_DIRNAME 128 /* show directory name (for internal use) */
87 #define DISP_RECURSIVE 256 /* Do a recursive listing */
90 #define MAJOR(dev) (((dev)>>8)&0xff)
91 #define MINOR(dev) ((dev)&0xff)
94 static unsigned char display_fmt = FMT_AUTO;
95 static unsigned short opts = 0;
96 static unsigned short column = 0;
98 #ifdef BB_FEATURE_AUTOWIDTH
99 static unsigned short terminal_width = 0;
100 static unsigned short column_width = 0;
101 static unsigned short toplevel_column_width = 0;
103 #define terminal_width TERMINAL_WIDTH
104 #define column_width COLUMN_WIDTH
107 #ifdef BB_FEATURE_LS_TIMESTAMPS
108 static unsigned char time_fmt = TIME_MOD;
111 #define wr(data,len) fwrite(data, 1, len, stdout)
113 static void writenum(long val, short minwidth)
117 char *p = scratch + sizeof(scratch);
119 short neg = (val < 0);
124 *--p = (val % 10) + '0', len++, val /= 10;
128 while (len < minwidth)
134 static void newline(void)
142 static void tab(short col)
144 static const char spaces[] = " ";
146 #define nspaces ((sizeof spaces)-1) /* null terminator! */
148 short n = col - column;
152 while (n > nspaces) {
156 /* must be 1...(sizeof spaces) left */
162 #ifdef BB_FEATURE_LS_FILETYPES
163 static char append_char(mode_t mode)
165 if (!(opts & DISP_FTYPE))
167 if ((opts & DISP_EXEC) && S_ISREG(mode)
168 && (mode & (S_IXUSR | S_IXGRP | S_IXOTH))) return '*';
169 return APPCHAR(mode);
175 ** Display a file or directory as a single item
176 ** (in either long or short format)
180 static void list_single(const char *name, struct stat *info,
181 const char *fullname)
183 char scratch[BUFSIZ + 1];
184 short len = strlen(name);
186 #ifdef BB_FEATURE_LS_FILETYPES
187 char append = append_char(info->st_mode);
190 if (display_fmt == FMT_LONG) {
191 mode_t mode = info->st_mode;
194 wr(modeString(mode), 10);
196 writenum((long) info->st_nlink, (short) 5);
198 #ifdef BB_FEATURE_LS_USERNAME
199 if (!(opts & DISP_NUMERIC)) {
200 memset(scratch, 0, sizeof(scratch));
201 my_getpwuid(scratch, info->st_uid);
203 fputs(scratch, stdout);
204 if (strlen(scratch) <= 8)
205 wr(" ", 9 - strlen(scratch));
207 writenum((long) info->st_uid, (short) 8);
213 writenum((long) info->st_uid, (short) 8);
216 #ifdef BB_FEATURE_LS_USERNAME
217 if (!(opts & DISP_NUMERIC)) {
218 memset(scratch, 0, sizeof(scratch));
219 my_getgrgid(scratch, info->st_gid);
221 fputs(scratch, stdout);
222 if (strlen(scratch) <= 8)
223 wr(" ", 8 - strlen(scratch));
225 writenum((long) info->st_gid, (short) 8);
228 writenum((long) info->st_gid, (short) 8);
230 if (S_ISBLK(mode) || S_ISCHR(mode)) {
231 writenum((long) MAJOR(info->st_rdev), (short) 3);
233 writenum((long) MINOR(info->st_rdev), (short) 3);
235 writenum((long) info->st_size, (short) 8);
238 #ifdef BB_FEATURE_LS_TIMESTAMPS
245 cal = info->st_ctime;
248 cal = info->st_atime;
251 cal = info->st_mtime;
254 string = ctime(&cal);
255 if (opts & DISP_FULLTIME)
258 time_t age = time(NULL) - cal;
260 wr(string + 4, 7); /* mmm_dd_ */
261 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60)
262 /* hh:mm if less than 6 months old */
265 /* _yyyy otherwise */
271 fputs("--- -- ----- ", stdout);
276 len = readlink(fullname, scratch, sizeof scratch);
278 fwrite(scratch, 1, len, stdout);
279 #ifdef BB_FEATURE_LS_FILETYPES
280 /* show type of destination */
281 if (opts & DISP_FTYPE) {
282 if (!stat(fullname, info)) {
283 append = append_char(info->st_mode);
285 fputc(append, stdout);
290 #ifdef BB_FEATURE_LS_FILETYPES
295 static short nexttab = 0;
297 /* sort out column alignment */
298 if (column == 0); /* nothing to do */
299 else if (display_fmt == FMT_SINGLE)
302 if (nexttab + column_width > terminal_width
303 #ifndef BB_FEATURE_AUTOWIDTH
304 || nexttab + len >= terminal_width
311 /* work out where next column starts */
312 #ifdef BB_FEATURE_AUTOWIDTH
313 /* we know the calculated width is big enough */
314 nexttab = column + column_width + COLUMN_GAP;
316 /* might cover more than one fixed-width column */
319 nexttab += column_width + COLUMN_GAP;
320 while (nexttab < (column + len + COLUMN_GAP));
322 /* now write the data */
324 column = column + len;
325 #ifdef BB_FEATURE_LS_FILETYPES
327 wr(&append, 1), column++;
334 ** List the given file or directory, expanding a directory
335 ** to show its contents if required
339 static int list_item(const char *name)
343 struct dirent *entry;
344 char fullname[BUFSIZ + 1], *fnend;
346 if (lstat(name, &info))
349 if (!S_ISDIR(info.st_mode) || (opts & DIR_NOLIST)) {
350 #ifdef BB_FEATURE_AUTOWIDTH
351 column_width = toplevel_column_width;
353 list_single(name, &info, name);
357 /* Otherwise, it's a directory we want to list the contents of */
359 if (opts & DISP_DIRNAME) { /* identify the directory */
361 wr("\n\n", 2), column = 0;
362 wr(name, strlen(name));
369 #ifdef BB_FEATURE_AUTOWIDTH
371 while ((entry = readdir(dir)) != NULL) {
372 short w = strlen(entry->d_name);
374 if (column_width < w)
387 /* List the contents */
389 strcpy(fullname, name); /* *** ignore '.' by itself */
390 fnend = fullname + strlen(fullname);
391 if (fnend[-1] != '/')
394 while ((entry = readdir(dir)) != NULL) {
395 const char *en = entry->d_name;
398 if (!en[1] || (en[1] == '.' && !en[2])) { /* . or .. */
399 if (!(opts & DISP_DOT))
401 } else if (!(opts & DISP_HIDDEN))
404 /* FIXME: avoid stat if not required */
405 strcpy(fnend, entry->d_name);
406 if (lstat(fullname, &info))
407 goto direrr; /* (shouldn't fail) */
408 list_single(entry->d_name, &info, fullname);
412 if (opts & DISP_DIRNAME) { /* separate the directory */
430 #ifdef BB_FEATURE_LS_RECURSIVE
431 static int dirAction(const char *fileName, struct stat *statbuf, void* junk)
434 fprintf(stdout, "\n%s:\n", fileName);
435 i = list_item(fileName);
441 extern int ls_main(int argc, char **argv)
445 /* process options */
446 while (argi < argc && argv[argi][0] == '-') {
447 const char *p = &argv[argi][1];
450 goto print_usage_message; /* "-" by itself not allowed */
452 if (!p[1]) { /* "--" forces end of options */
456 /* it's a long option name - we don't support them */
457 goto print_usage_message;
463 display_fmt = FMT_LONG;
466 display_fmt = FMT_SINGLE;
469 display_fmt = FMT_ROWS;
472 display_fmt = FMT_COLUMNS;
474 #ifdef BB_FEATURE_LS_FILETYPES
479 opts |= DISP_FTYPE | DISP_EXEC;
486 opts |= DISP_HIDDEN | DISP_DOT;
489 opts |= DISP_NUMERIC;
494 #ifdef BB_FEATURE_LS_TIMESTAMPS
496 time_fmt = TIME_ACCESS;
499 time_fmt = TIME_CHANGE;
502 opts |= DISP_FULLTIME;
505 #ifdef BB_FEATURE_LS_RECURSIVE
507 opts |= DISP_RECURSIVE;
510 case 'g': /* ignore -- for ftp servers */
513 goto print_usage_message;
519 /* choose a display format */
520 if (display_fmt == FMT_AUTO)
521 display_fmt = isatty(fileno(stdout)) ? FMT_COLUMNS : FMT_SINGLE;
523 opts |= DISP_DIRNAME; /* 2 or more items? label directories */
524 #ifdef BB_FEATURE_AUTOWIDTH
525 /* could add a -w option and/or TIOCGWINSZ call */
526 if (terminal_width < 1)
527 terminal_width = TERMINAL_WIDTH;
529 for (i = argi; i < argc; i++) {
530 int len = strlen(argv[i]);
532 if (toplevel_column_width < len)
533 toplevel_column_width = len;
537 /* process files specified, or current directory if none */
538 #ifdef BB_FEATURE_LS_RECURSIVE
539 if (opts & DISP_RECURSIVE) {
542 i = recursiveAction(".", TRUE, FALSE, FALSE, NULL, dirAction, NULL);
544 while (argi < argc) {
545 i |= recursiveAction(argv[argi++], TRUE, FALSE, FALSE, NULL, dirAction, NULL);
554 i |= list_item(argv[argi++]);