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 #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 FMT_LONG 1 /* one record per line, extended info */
75 #define FMT_SINGLE 2 /* one record per line */
76 #define FMT_ROWS 3 /* print across rows */
77 #define FMT_COLUMNS 3 /* fill columns (same, since we don't sort) */
83 #define DISP_FTYPE 1 /* show character for file type */
84 #define DISP_EXEC 2 /* show '*' if regular executable file */
85 #define DISP_HIDDEN 4 /* show files starting . (except . and ..) */
86 #define DISP_DOT 8 /* show . and .. */
87 #define DISP_NUMERIC 16 /* numeric uid and gid */
88 #define DISP_FULLTIME 32 /* show extended time display */
89 #define DIR_NOLIST 64 /* show directory as itself, not contents */
90 #define DISP_DIRNAME 128 /* show directory name (for internal use) */
93 #define MAJOR(dev) (((dev)>>8)&0xff)
94 #define MINOR(dev) ((dev)&0xff)
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;
103 static unsigned short column_width = 0;
104 static unsigned short toplevel_column_width = 0;
106 #define terminal_width TERMINAL_WIDTH
107 #define column_width COLUMN_WIDTH
110 #ifdef BB_FEATURE_LS_TIMESTAMPS
111 static unsigned char time_fmt = TIME_MOD;
114 #define wr(data,len) fwrite(data, 1, len, stdout)
116 static void writenum(long val, short minwidth)
120 char *p = scratch + sizeof(scratch);
122 short neg = (val < 0);
127 *--p = (val % 10) + '0', len++, val /= 10;
131 while (len < minwidth)
137 static void newline(void)
145 static void tab(short col)
147 static const char spaces[] = " ";
149 #define nspaces ((sizeof spaces)-1) /* null terminator! */
151 short n = col - column;
155 while (n > nspaces) {
159 /* must be 1...(sizeof spaces) left */
165 #ifdef BB_FEATURE_LS_FILETYPES
166 static char append_char(mode_t mode)
168 if (!(opts & DISP_FTYPE))
170 if ((opts & DISP_EXEC) && S_ISREG(mode)
171 && (mode & (S_IXUSR | S_IXGRP | S_IXOTH))) return '*';
172 return APPCHAR(mode);
178 ** Display a file or directory as a single item
179 ** (in either long or short format)
183 static void list_single(const char *name, struct stat *info,
184 const char *fullname)
186 char scratch[BUFSIZ + 1];
187 short len = strlen(name);
189 #ifdef BB_FEATURE_LS_FILETYPES
190 char append = append_char(info->st_mode);
193 if (display_fmt == FMT_LONG) {
194 mode_t mode = info->st_mode;
197 wr(modeString(mode), 10);
199 writenum((long) info->st_nlink, (short) 5);
201 #ifdef BB_FEATURE_LS_USERNAME
202 if (!(opts & DISP_NUMERIC)) {
203 memset(scratch, 0, sizeof(scratch));
204 my_getpwuid(scratch, info->st_uid);
206 fputs(scratch, stdout);
207 if (strlen(scratch) <= 8)
208 wr(" ", 9 - strlen(scratch));
210 writenum((long) info->st_uid, (short) 8);
216 writenum((long) info->st_uid, (short) 8);
219 #ifdef BB_FEATURE_LS_USERNAME
220 if (!(opts & DISP_NUMERIC)) {
221 memset(scratch, 0, sizeof(scratch));
222 my_getgrgid(scratch, info->st_gid);
224 fputs(scratch, stdout);
225 if (strlen(scratch) <= 8)
226 wr(" ", 8 - strlen(scratch));
228 writenum((long) info->st_gid, (short) 8);
231 writenum((long) info->st_gid, (short) 8);
233 if (S_ISBLK(mode) || S_ISCHR(mode)) {
234 writenum((long) MAJOR(info->st_rdev), (short) 3);
236 writenum((long) MINOR(info->st_rdev), (short) 3);
238 writenum((long) info->st_size, (short) 8);
241 #ifdef BB_FEATURE_LS_TIMESTAMPS
248 cal = info->st_ctime;
251 cal = info->st_atime;
254 cal = info->st_mtime;
257 string = ctime(&cal);
258 if (opts & DISP_FULLTIME)
261 time_t age = time(NULL) - cal;
263 wr(string + 4, 7); /* mmm_dd_ */
264 if (age < 3600L * 24 * 365 / 2 && age > -15 * 60)
265 /* hh:mm if less than 6 months old */
268 /* _yyyy otherwise */
274 fputs("--- -- ----- ", stdout);
279 len = readlink(fullname, scratch, sizeof scratch);
281 fwrite(scratch, 1, len, stdout);
282 #ifdef BB_FEATURE_LS_FILETYPES
283 /* show type of destination */
284 if (opts & DISP_FTYPE) {
285 if (!stat(fullname, info)) {
286 append = append_char(info->st_mode);
288 fputc(append, stdout);
293 #ifdef BB_FEATURE_LS_FILETYPES
298 static short nexttab = 0;
300 /* sort out column alignment */
301 if (column == 0); /* nothing to do */
302 else if (display_fmt == FMT_SINGLE)
305 if (nexttab + column_width > terminal_width
306 #ifndef BB_FEATURE_AUTOWIDTH
307 || nexttab + len >= terminal_width
314 /* work out where next column starts */
315 #ifdef BB_FEATURE_AUTOWIDTH
316 /* we know the calculated width is big enough */
317 nexttab = column + column_width + COLUMN_GAP;
319 /* might cover more than one fixed-width column */
322 nexttab += column_width + COLUMN_GAP;
323 while (nexttab < (column + len + COLUMN_GAP));
325 /* now write the data */
327 column = column + len;
328 #ifdef BB_FEATURE_LS_FILETYPES
330 wr(&append, 1), column++;
337 ** List the given file or directory, expanding a directory
338 ** to show its contents if required
342 static int list_item(const char *name)
346 struct dirent *entry;
347 char fullname[MAXNAMLEN + 1], *fnend;
349 if (lstat(name, &info))
352 if (!S_ISDIR(info.st_mode) || (opts & DIR_NOLIST)) {
353 #ifdef BB_FEATURE_AUTOWIDTH
354 column_width = toplevel_column_width;
356 list_single(name, &info, name);
360 /* Otherwise, it's a directory we want to list the contents of */
362 if (opts & DISP_DIRNAME) { /* identify the directory */
364 wr("\n\n", 2), column = 0;
365 wr(name, strlen(name));
372 #ifdef BB_FEATURE_AUTOWIDTH
374 while ((entry = readdir(dir)) != NULL) {
375 short w = strlen(entry->d_name);
377 if (column_width < w)
390 /* List the contents */
392 strcpy(fullname, name); /* *** ignore '.' by itself */
393 fnend = fullname + strlen(fullname);
394 if (fnend[-1] != '/')
397 while ((entry = readdir(dir)) != NULL) {
398 const char *en = entry->d_name;
401 if (!en[1] || (en[1] == '.' && !en[2])) { /* . or .. */
402 if (!(opts & DISP_DOT))
404 } else if (!(opts & DISP_HIDDEN))
407 /* FIXME: avoid stat if not required */
408 strcpy(fnend, entry->d_name);
409 if (lstat(fullname, &info))
410 goto direrr; /* (shouldn't fail) */
411 list_single(entry->d_name, &info, fullname);
415 if (opts & DISP_DIRNAME) { /* separate the directory */
433 static const char ls_usage[] = "ls [-1a"
434 #ifdef BB_FEATURE_LS_TIMESTAMPS
438 #ifdef BB_FEATURE_LS_TIMESTAMPS
442 #ifdef BB_FEATURE_LS_FILETYPES
445 #ifdef BB_FEATURE_LS_TIMESTAMPS
449 #ifdef BB_FEATURE_LS_FILETYPES
452 "] [filenames...]\n\n"
454 "\t-a\tdo not hide entries starting with .\n"
455 #ifdef BB_FEATURE_LS_TIMESTAMPS
456 "\t-c\twith -l: show ctime (the time of last\n"
457 "\t\tmodification of file status information)\n"
459 "\t-d\tlist directory entries instead of contents\n"
460 #ifdef BB_FEATURE_LS_TIMESTAMPS
461 "\t-e\tlist both full date and full time\n"
463 "\t-l\tuse a long listing format\n"
464 "\t-n\tlist numeric UIDs and GIDs instead of names\n"
465 #ifdef BB_FEATURE_LS_FILETYPES
466 "\t-p\tappend indicator (one of /=@|) to entries\n"
468 #ifdef BB_FEATURE_LS_TIMESTAMPS
469 "\t-u\twith -l: show access time (the time of last\n"
470 "\t\taccess of the file)\n"
472 "\t-x\tlist entries by lines instead of by columns\n"
473 "\t-A\tdo not list implied . and ..\n"
474 "\t-C\tlist entries by columns\n"
475 #ifdef BB_FEATURE_LS_FILETYPES
476 "\t-F\tappend indicator (one of */=@|) to entries\n"
480 extern int ls_main(int argc, char **argv)
484 /* process options */
485 while (argi < argc && argv[argi][0] == '-') {
486 const char *p = &argv[argi][1];
489 goto print_usage_message; /* "-" by itself not allowed */
491 if (!p[1]) { /* "--" forces end of options */
495 /* it's a long option name - we don't support them */
496 goto print_usage_message;
502 display_fmt = FMT_LONG;
505 display_fmt = FMT_SINGLE;
508 display_fmt = FMT_ROWS;
511 display_fmt = FMT_COLUMNS;
513 #ifdef BB_FEATURE_LS_FILETYPES
518 opts |= DISP_FTYPE | DISP_EXEC;
525 opts |= DISP_HIDDEN | DISP_DOT;
528 opts |= DISP_NUMERIC;
533 #ifdef BB_FEATURE_LS_TIMESTAMPS
535 time_fmt = TIME_ACCESS;
538 time_fmt = TIME_CHANGE;
541 opts |= DISP_FULLTIME;
545 goto print_usage_message;
551 /* choose a display format */
552 if (display_fmt == FMT_AUTO)
553 display_fmt = isatty(fileno(stdout)) ? FMT_COLUMNS : FMT_SINGLE;
555 opts |= DISP_DIRNAME; /* 2 or more items? label directories */
556 #ifdef BB_FEATURE_AUTOWIDTH
557 /* could add a -w option and/or TIOCGWINSZ call */
558 if (terminal_width < 1)
559 terminal_width = TERMINAL_WIDTH;
561 for (i = argi; i < argc; i++) {
562 int len = strlen(argv[i]);
564 if (toplevel_column_width < len)
565 toplevel_column_width = len;
569 /* process files specified, or current directory if none */
574 i |= list_item(argv[argi++]);