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. messy output if you mix files and directories on the command line
33 * 2. ls -l of a directory doesn't give "total <blocks>" header
34 * 3. ls of a symlink to a directory doesn't list directory contents
35 * 4. 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 #if !defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
53 # include <linux/types.h>
55 # include <sys/types.h>
63 #ifdef BB_FEATURE_LS_TIMESTAMPS
67 #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
68 #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
69 #ifdef BB_FEATURE_LS_FILETYPES
70 #define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
74 #define MAJOR(dev) (((dev)>>8)&0xff)
75 #define MINOR(dev) ((dev)&0xff)
79 #define FMT_LONG 1 /* one record per line, extended info */
80 #define FMT_SINGLE 2 /* one record per line */
81 #define FMT_ROWS 3 /* print across rows */
82 #define FMT_COLUMNS 3 /* fill columns (same, since we don't sort) */
88 #define DISP_FTYPE 1 /* show character for file type */
89 #define DISP_EXEC 2 /* show '*' if regular executable file */
90 #define DISP_HIDDEN 4 /* show files starting . (except . and ..) */
91 #define DISP_DOT 8 /* show . and .. */
92 #define DISP_NUMERIC 16 /* numeric uid and gid */
93 #define DISP_FULLTIME 32 /* show extended time display */
94 #define DIR_NOLIST 64 /* show directory as itself, not contents */
95 #define DISP_DIRNAME 128 /* show directory name (for internal use) */
96 #define DIR_RECURSE 256 /* -R (not yet implemented) */
98 static unsigned char display_fmt = FMT_AUTO;
99 static unsigned short opts = 0;
100 static unsigned short column = 0;
102 #ifdef BB_FEATURE_AUTOWIDTH
103 static unsigned short terminal_width = 0, column_width = 0;
105 #define terminal_width TERMINAL_WIDTH
106 #define column_width COLUMN_WIDTH
109 #ifdef BB_FEATURE_LS_TIMESTAMPS
110 static unsigned char time_fmt = TIME_MOD;
113 #define wr(data,len) fwrite(data, 1, len, stdout)
115 static void writenum(long val, short minwidth)
119 char *p = scratch + sizeof(scratch);
121 short neg = (val < 0);
126 *--p = (val % 10) + '0', len++, val /= 10;
130 while (len < minwidth)
136 static void newline(void)
144 static void tab(short col)
146 static const char spaces[] = " ";
148 #define nspaces ((sizeof spaces)-1) /* null terminator! */
150 short n = col - column;
154 while (n > nspaces) {
158 /* must be 1...(sizeof spaces) left */
164 #ifdef BB_FEATURE_LS_FILETYPES
165 static char append_char(mode_t mode)
167 if (!(opts & DISP_FTYPE))
169 if ((opts & DISP_EXEC) && S_ISREG(mode)
170 && (mode & (S_IXUSR | S_IXGRP | S_IXOTH))) return '*';
171 return APPCHAR(mode);
177 ** Display a file or directory as a single item
178 ** (in either long or short format)
182 static void list_single(const char *name, struct stat *info,
183 const char *fullname)
185 char scratch[PATH_MAX + 1];
186 short len = strlen(name);
188 #ifdef BB_FEATURE_LS_FILETYPES
189 char append = append_char(info->st_mode);
192 if (display_fmt == FMT_LONG) {
193 mode_t mode = info->st_mode;
196 wr(modeString(mode), 10);
198 writenum((long) info->st_nlink, (short) 5);
200 #ifdef BB_FEATURE_LS_USERNAME
201 if (!(opts & DISP_NUMERIC)) {
202 memset(scratch, 0, sizeof(scratch));
203 my_getpwuid(scratch, info->st_uid);
205 fputs(scratch, stdout);
206 if (strlen(scratch) <= 8)
207 wr(" ", 9 - strlen(scratch));
209 writenum((long) info->st_uid, (short) 8);
215 writenum((long) info->st_uid, (short) 8);
218 #ifdef BB_FEATURE_LS_USERNAME
219 if (!(opts & DISP_NUMERIC)) {
220 memset(scratch, 0, sizeof(scratch));
221 my_getgrgid(scratch, info->st_gid);
223 fputs(scratch, stdout);
224 if (strlen(scratch) <= 8)
225 wr(" ", 8 - strlen(scratch));
227 writenum((long) info->st_gid, (short) 8);
230 writenum((long) info->st_gid, (short) 8);
232 if (S_ISBLK(mode) || S_ISCHR(mode)) {
233 writenum((long) MAJOR(info->st_rdev), (short) 3);
235 writenum((long) MINOR(info->st_rdev), (short) 3);
237 writenum((long) info->st_size, (short) 8);
240 #ifdef BB_FEATURE_LS_TIMESTAMPS
247 cal = info->st_ctime;
250 cal = info->st_atime;
253 cal = info->st_mtime;
256 string = ctime(&cal);
257 if (opts & DISP_FULLTIME)
260 time_t age = time(NULL) - cal;
262 wr(string + 4, 7); /* mmm_dd_ */
263 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60)
264 /* hh:mm if less than 6 months old */
267 /* _yyyy otherwise */
273 fputs("--- -- ----- ", stdout);
278 len = readlink(fullname, scratch, sizeof scratch);
280 fwrite(scratch, 1, len, stdout);
281 #ifdef BB_FEATURE_LS_FILETYPES
282 /* show type of destination */
283 if (opts & DISP_FTYPE) {
284 if (!stat(fullname, info)) {
285 append = append_char(info->st_mode);
287 fputc(append, stdout);
292 #ifdef BB_FEATURE_LS_FILETYPES
297 static short nexttab = 0;
299 /* sort out column alignment */
300 if (column == 0); /* nothing to do */
301 else if (display_fmt == FMT_SINGLE)
304 if (nexttab + column_width > terminal_width
305 #ifndef BB_FEATURE_AUTOWIDTH
306 || nexttab + len >= terminal_width
313 /* work out where next column starts */
314 #ifdef BB_FEATURE_AUTOWIDTH
315 /* we know the calculated width is big enough */
316 nexttab = column + column_width + COLUMN_GAP;
318 /* might cover more than one fixed-width column */
321 nexttab += column_width + COLUMN_GAP;
322 while (nexttab < (column + len + COLUMN_GAP));
324 /* now write the data */
326 column = column + len;
327 #ifdef BB_FEATURE_LS_FILETYPES
329 wr(&append, 1), column++;
336 ** List the given file or directory, expanding a directory
337 ** to show its contents if required
341 static int list_item(const char *name)
345 struct dirent *entry;
346 char fullname[MAXNAMLEN + 1], *fnend;
348 if (lstat(name, &info))
351 if (!S_ISDIR(info.st_mode) || (opts & DIR_NOLIST)) {
352 list_single(name, &info, name);
356 /* Otherwise, it's a directory we want to list the contents of */
358 if (opts & DISP_DIRNAME) { /* identify the directory */
360 wr("\n\n", 2), column = 0;
361 wr(name, strlen(name));
368 #ifdef BB_FEATURE_AUTOWIDTH
370 while ((entry = readdir(dir)) != NULL) {
371 short w = strlen(entry->d_name);
373 if (column_width < w)
386 /* List the contents */
388 strcpy(fullname, name); /* *** ignore '.' by itself */
389 fnend = fullname + strlen(fullname);
390 if (fnend[-1] != '/')
393 while ((entry = readdir(dir)) != NULL) {
394 const char *en = entry->d_name;
397 if (!en[1] || (en[1] == '.' && !en[2])) { /* . or .. */
398 if (!(opts & DISP_DOT))
400 } else if (!(opts & DISP_HIDDEN))
403 /* FIXME: avoid stat if not required */
404 strcpy(fnend, entry->d_name);
405 if (lstat(fullname, &info))
406 goto direrr; /* (shouldn't fail) */
407 list_single(entry->d_name, &info, fullname);
420 static const char ls_usage[] = "ls [-1a"
421 #ifdef BB_FEATURE_LS_TIMESTAMPS
425 #ifdef BB_FEATURE_LS_TIMESTAMPS
429 #ifdef BB_FEATURE_LS_FILETYPES
432 #ifdef BB_FEATURE_LS_TIMESTAMPS
436 #ifdef BB_FEATURE_LS_FILETYPES
439 #ifdef FEATURE_RECURSIVE
442 "] [filenames...]\n";
444 extern int ls_main(int argc, char **argv)
448 /* process options */
449 while (argi < argc && argv[argi][0] == '-') {
450 const char *p = &argv[argi][1];
453 goto print_usage_message; /* "-" by itself not allowed */
455 if (!p[1]) { /* "--" forces end of options */
459 /* it's a long option name - we don't support them */
460 goto print_usage_message;
466 display_fmt = FMT_LONG;
469 display_fmt = FMT_SINGLE;
472 display_fmt = FMT_ROWS;
475 display_fmt = FMT_COLUMNS;
477 #ifdef BB_FEATURE_LS_FILETYPES
482 opts |= DISP_FTYPE | DISP_EXEC;
489 opts |= DISP_HIDDEN | DISP_DOT;
492 opts |= DISP_NUMERIC;
497 #ifdef FEATURE_RECURSIVE
502 #ifdef BB_FEATURE_LS_TIMESTAMPS
504 time_fmt = TIME_ACCESS;
507 time_fmt = TIME_CHANGE;
510 opts |= DISP_FULLTIME;
514 goto print_usage_message;
520 /* choose a display format */
521 if (display_fmt == FMT_AUTO)
522 display_fmt = isatty(fileno(stdout)) ? FMT_COLUMNS : FMT_SINGLE;
524 opts |= DISP_DIRNAME; /* 2 or more items? label directories */
525 #ifdef BB_FEATURE_AUTOWIDTH
526 /* could add a -w option and/or TIOCGWINSZ call */
527 if (terminal_width < 1)
528 terminal_width = TERMINAL_WIDTH;
530 for (i = argi; i < argc; i++) {
531 int len = strlen(argv[i]);
533 if (column_width < len)
538 /* process files specified, or current directory if none */
543 i |= list_item(argv[argi++]);