2 * tiny-ls.c version 0.1.0: A minimalist 'ls'
3 * Copyright (C) 1996 Brian Candler <B.Candler@pobox.com>
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.
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.
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.
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.
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
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
40 * 1. requires lstat (BSD) - how do you do it without?
43 #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 */
48 /************************************************************************/
51 #if !defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
52 # include <linux/types.h>
54 # include <sys/types.h>
62 #ifdef BB_FEATURE_LS_TIMESTAMPS
66 #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
67 #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
68 #ifdef BB_FEATURE_LS_FILETYPES
69 #define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
73 #define MAJOR(dev) (((dev)>>8)&0xff)
74 #define MINOR(dev) ((dev)&0xff)
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) */
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) */
97 static unsigned char display_fmt = FMT_AUTO;
98 static unsigned short opts = 0;
99 static unsigned short column = 0;
101 #ifdef BB_FEATURE_AUTOWIDTH
102 static unsigned short terminal_width = 0, column_width = 0;
104 #define terminal_width TERMINAL_WIDTH
105 #define column_width COLUMN_WIDTH
108 #ifdef BB_FEATURE_LS_TIMESTAMPS
109 static unsigned char time_fmt = TIME_MOD;
112 #define wr(data,len) fwrite(data, 1, len, stdout)
114 static void writenum(long val, short minwidth)
118 char *p = scratch + sizeof(scratch);
120 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[] = " ";
145 #define nspaces ((sizeof spaces)-1) /* null terminator! */
147 short n = col - column;
151 while (n > nspaces) {
155 /* must be 1...(sizeof spaces) left */
161 #ifdef BB_FEATURE_LS_FILETYPES
162 static char append_char(mode_t mode)
164 if (!(opts & DISP_FTYPE))
166 if ((opts & DISP_EXEC) && S_ISREG(mode) && (mode & (S_IXUSR|S_IXGRP|S_IXOTH)))
168 return APPCHAR(mode);
174 ** Display a file or directory as a single item
175 ** (in either long or short format)
179 static void list_single(const char *name, struct stat *info, const char *fullname)
181 char scratch[PATH_MAX];
182 short len = strlen(name);
183 #ifdef BB_FEATURE_LS_FILETYPES
184 char append = append_char(info->st_mode);
187 if (display_fmt == FMT_LONG) {
188 mode_t mode = info->st_mode;
190 wr(modeString(mode), 10);
192 writenum((long)info->st_nlink,(short)5);
194 #ifdef BB_FEATURE_LS_USERNAME
195 if (!(opts & DISP_NUMERIC)) {
197 my_getpwuid( scratch, info->st_uid);
202 writenum((long) info->st_uid,(short)8);
208 writenum((long) info->st_uid,(short)8);
212 #ifdef BB_FEATURE_LS_USERNAME
213 if (!(opts & DISP_NUMERIC)) {
215 my_getgrgid( scratch, info->st_gid);
220 writenum((long) info->st_gid,(short)8);
223 writenum((long) info->st_gid,(short)8);
225 if (S_ISBLK(mode) || S_ISCHR(mode)) {
226 writenum((long)MAJOR(info->st_rdev),(short)3);
228 writenum((long)MINOR(info->st_rdev),(short)3);
231 writenum((long)info->st_size,(short)8);
233 #ifdef BB_FEATURE_LS_TIMESTAMPS
240 cal=info->st_ctime; break;
242 cal=info->st_atime; break;
244 cal=info->st_mtime; break;
247 if (opts & DISP_FULLTIME)
250 time_t age = time(NULL) - cal;
251 wr(string+4,7); /* mmm_dd_ */
252 if(age < 3600L*24*365/2 && age > -15*60)
253 /* hh:mm if less than 6 months old */
256 /* _yyyy otherwise */
262 fputs("--- -- ----- ", stdout);
267 len = readlink(fullname, scratch, sizeof scratch);
268 if (len > 0) fwrite(scratch, 1, len, stdout);
269 #ifdef BB_FEATURE_LS_FILETYPES
270 /* show type of destination */
271 if (opts & DISP_FTYPE) {
272 if (!stat(fullname, info)) {
273 append = append_char(info->st_mode);
275 fputc(append, stdout);
280 #ifdef BB_FEATURE_LS_FILETYPES
285 static short nexttab = 0;
287 /* sort out column alignment */
289 ; /* nothing to do */
290 else if (display_fmt == FMT_SINGLE)
293 if (nexttab + column_width > terminal_width
294 #ifndef BB_FEATURE_AUTOWIDTH
295 || nexttab + len >= terminal_width
302 /* work out where next column starts */
303 #ifdef BB_FEATURE_AUTOWIDTH
304 /* we know the calculated width is big enough */
305 nexttab = column + column_width + COLUMN_GAP;
307 /* might cover more than one fixed-width column */
310 nexttab += column_width + COLUMN_GAP;
311 while (nexttab < (column + len + COLUMN_GAP));
313 /* now write the data */
315 column = column + len;
316 #ifdef BB_FEATURE_LS_FILETYPES
318 wr(&append, 1), column++;
325 ** List the given file or directory, expanding a directory
326 ** to show its contents if required
330 static int list_item(const char *name)
334 struct dirent *entry;
335 char fullname[MAXNAMLEN+1], *fnend;
337 if (lstat(name, &info))
340 if (!S_ISDIR(info.st_mode) ||
341 (opts & DIR_NOLIST)) {
342 list_single(name, &info, name);
346 /* Otherwise, it's a directory we want to list the contents of */
348 if (opts & DISP_DIRNAME) { /* identify the directory */
350 wr("\n\n", 2), column = 0;
351 wr(name, strlen(name));
356 if (!dir) goto listerr;
357 #ifdef BB_FEATURE_AUTOWIDTH
359 while ((entry = readdir(dir)) != NULL) {
360 short w = strlen(entry->d_name);
361 if (column_width < w)
369 if (!dir) goto listerr;
373 /* List the contents */
375 strcpy(fullname,name); /* *** ignore '.' by itself */
376 fnend=fullname+strlen(fullname);
377 if (fnend[-1] != '/')
380 while ((entry = readdir(dir)) != NULL) {
381 const char *en=entry->d_name;
383 if (!en[1] || (en[1] == '.' && !en[2])) { /* . or .. */
384 if (!(opts & DISP_DOT))
387 else if (!(opts & DISP_HIDDEN))
390 /* FIXME: avoid stat if not required */
391 strcpy(fnend, entry->d_name);
392 if (lstat(fullname, &info))
393 goto direrr; /* (shouldn't fail) */
394 list_single(entry->d_name, &info, fullname);
407 static const char ls_usage[] = "ls [-1a"
408 #ifdef BB_FEATURE_LS_TIMESTAMPS
412 #ifdef BB_FEATURE_LS_TIMESTAMPS
416 #ifdef BB_FEATURE_LS_FILETYPES
419 #ifdef BB_FEATURE_LS_TIMESTAMPS
423 #ifdef BB_FEATURE_LS_FILETYPES
426 #ifdef FEATURE_RECURSIVE
429 "] [filenames...]\n";
432 ls_main(int argc, char * * argv)
436 /* process options */
437 while (argi < argc && argv[argi][0] == '-') {
438 const char *p = &argv[argi][1];
440 if (!*p) goto print_usage_message; /* "-" by itself not allowed */
442 if (!p[1]) { /* "--" forces end of options */
446 /* it's a long option name - we don't support them */
447 goto print_usage_message;
452 case 'l': display_fmt = FMT_LONG; break;
453 case '1': display_fmt = FMT_SINGLE; break;
454 case 'x': display_fmt = FMT_ROWS; break;
455 case 'C': display_fmt = FMT_COLUMNS; break;
456 #ifdef BB_FEATURE_LS_FILETYPES
457 case 'p': opts |= DISP_FTYPE; break;
458 case 'F': opts |= DISP_FTYPE|DISP_EXEC; break;
460 case 'A': opts |= DISP_HIDDEN; break;
461 case 'a': opts |= DISP_HIDDEN|DISP_DOT; break;
462 case 'n': opts |= DISP_NUMERIC; break;
463 case 'd': opts |= DIR_NOLIST; break;
464 #ifdef FEATURE_RECURSIVE
465 case 'R': opts |= DIR_RECURSE; break;
467 #ifdef BB_FEATURE_LS_TIMESTAMPS
468 case 'u': time_fmt = TIME_ACCESS; break;
469 case 'c': time_fmt = TIME_CHANGE; break;
470 case 'e': opts |= DISP_FULLTIME; break;
472 default: goto print_usage_message;
478 /* choose a display format */
479 if (display_fmt == FMT_AUTO)
480 display_fmt = isatty(fileno(stdout)) ? FMT_COLUMNS : FMT_SINGLE;
482 opts |= DISP_DIRNAME; /* 2 or more items? label directories */
483 #ifdef BB_FEATURE_AUTOWIDTH
484 /* could add a -w option and/or TIOCGWINSZ call */
485 if (terminal_width < 1) terminal_width = TERMINAL_WIDTH;
487 for (i = argi; i < argc; i++) {
488 int len = strlen(argv[i]);
489 if (column_width < len)
494 /* process files specified, or current directory if none */
499 i |= list_item(argv[argi++]);