2 * Mini ls implementation for busybox
4 * Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
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 GNU
14 * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 static const char ls_usage[] = "ls [OPTION]... [FILE]...\n"
31 "List information about the FILEs (the current directory by default).\n";
35 int directoryFlag=FALSE;
38 int dereferenceFlag=FALSE;
39 int recursiveFlag=FALSE;
41 static int fileAction(const char *fileName)
43 if ( allFlag==FALSE && ((strcmp(fileName, "..") == 0)
44 || (strcmp(fileName, ".") == 0)) ) {
47 //struct stat statBuf;
48 //if (stat(fileName, &statBuf) > 0) {
49 fprintf(stdout, "%s\n", fileName);
58 static int dirAction(const char *fileName)
63 fprintf(stdout, "%s\n", fileName);
65 dir = opendir( fileName);
67 perror("Can't open directory");
70 while ((entry = readdir(dir)) != NULL) {
71 recursiveAction( entry->d_name, recursiveFlag, dereferenceFlag, fileAction, dirAction);
76 int ls_main(int argc, char **argv)
80 getcwd( buf, NAME_MAX);
84 /* peel of the "ls" */
88 /* Parse any options */
89 while (**argv == '-') {
90 while (*++(*argv)) switch (**argv) {
107 dereferenceFlag = TRUE;
110 recursiveFlag = TRUE;
113 fprintf(stderr, "Usage: %s\n", ls_usage);
120 /* Ok, ready to do the deed now */
121 fprintf(stderr, "B\n");
123 fprintf(stderr, "C\n");
124 recursiveAction( *argv, recursiveFlag, dereferenceFlag, fileAction, dirAction);
134 #include "internal.h"
136 * tiny-ls.c version 0.1.0: A minimalist 'ls'
137 * Copyright (C) 1996 Brian Candler <B.Candler@pobox.com>
139 * This program is free software; you can redistribute it and/or modify
140 * it under the terms of the GNU General Public License as published by
141 * the Free Software Foundation; either version 2 of the License, or
142 * (at your option) any later version.
144 * This program is distributed in the hope that it will be useful,
145 * but WITHOUT ANY WARRANTY; without even the implied warranty of
146 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
147 * GNU General Public License for more details.
149 * You should have received a copy of the GNU General Public License
150 * along with this program; if not, write to the Free Software
151 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
155 * To achieve a small memory footprint, this version of 'ls' doesn't do any
156 * file sorting, and only has the most essential command line switches
157 * (i.e. the ones I couldn't live without :-) All features which involve
158 * linking in substantial chunks of libc can be disabled.
160 * Although I don't really want to add new features to this program to
161 * keep it small, I *am* interested to receive bug fixes and ways to make
165 * 1. messy output if you mix files and directories on the command line
166 * 2. ls -l of a directory doesn't give "total <blocks>" header
167 * 3. ls of a symlink to a directory doesn't list directory contents
168 * 4. hidden files can make column width too large
169 * NON-OPTIMAL BEHAVIOUR:
170 * 1. autowidth reads directories twice
171 * 2. if you do a short directory listing without filetype characters
172 * appended, there's no need to stat each one
174 * 1. requires lstat (BSD) - how do you do it without?
177 #define FEATURE_USERNAME /* show username/groupnames (libc6 uses NSS) */
178 #define FEATURE_TIMESTAMPS /* show file timestamps */
179 #define FEATURE_AUTOWIDTH /* calculate terminal & column widths */
180 #define FEATURE_FILETYPECHAR /* enable -p and -F */
182 #undef OP_BUF_SIZE 1024 /* leave undefined for unbuffered output */
184 #define TERMINAL_WIDTH 80 /* use 79 if your terminal has linefold bug */
185 #define COLUMN_WIDTH 14 /* default if AUTOWIDTH not defined */
186 #define COLUMN_GAP 2 /* includes the file type char, if present */
188 /************************************************************************/
190 #define HAS_REWINDDIR
192 #if 1 /* FIXME libc 6 */
193 # include <linux/types.h>
195 # include <sys/types.h>
197 #include <sys/stat.h>
203 #ifdef FEATURE_USERNAME
207 #ifdef FEATURE_TIMESTAMPS
211 #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
212 #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
213 #ifdef FEATURE_FILETYPECHAR
214 #define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
218 #define MAJOR(dev) (((dev)>>8)&0xff)
219 #define MINOR(dev) ((dev)&0xff)
222 #define MODE1 "rwxrwxrwx"
223 #define MODE0 "---------"
224 #define SMODE1 "..s..s..t"
225 #define SMODE0 "..S..S..T"
227 /* The 9 mode bits to test */
229 static const umode_t MBIT[] = {
230 S_IRUSR, S_IWUSR, S_IXUSR,
231 S_IRGRP, S_IWGRP, S_IXGRP,
232 S_IROTH, S_IWOTH, S_IXOTH
235 /* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
237 static const umode_t SBIT[] = {
244 #define FMT_LONG 1 /* one record per line, extended info */
245 #define FMT_SINGLE 2 /* one record per line */
246 #define FMT_ROWS 3 /* print across rows */
247 #define FMT_COLUMNS 3 /* fill columns (same, since we don't sort) */
250 #define TIME_CHANGE 1
251 #define TIME_ACCESS 2
253 #define DISP_FTYPE 1 /* show character for file type */
254 #define DISP_EXEC 2 /* show '*' if regular executable file */
255 #define DISP_HIDDEN 4 /* show files starting . (except . and ..) */
256 #define DISP_DOT 8 /* show . and .. */
257 #define DISP_NUMERIC 16 /* numeric uid and gid */
258 #define DISP_FULLTIME 32 /* show extended time display */
259 #define DIR_NOLIST 64 /* show directory as itself, not contents */
260 #define DISP_DIRNAME 128 /* show directory name (for internal use) */
261 #define DIR_RECURSE 256 /* -R (not yet implemented) */
263 static unsigned char display_fmt = FMT_AUTO;
264 static unsigned short opts = 0;
265 static unsigned short column = 0;
267 #ifdef FEATURE_AUTOWIDTH
268 static unsigned short terminal_width = 0, column_width = 0;
270 #define terminal_width TERMINAL_WIDTH
271 #define column_width COLUMN_WIDTH
274 #ifdef FEATURE_TIMESTAMPS
275 static unsigned char time_fmt = TIME_MOD;
278 #define wr(data,len) fwrite(data, 1, len, stdout)
280 static void writenum(long val, short minwidth)
284 char *p = scratch + sizeof(scratch);
286 short neg = (val < 0);
290 *--p = (val % 10) + '0', len++, val /= 10;
294 while (len < minwidth)
300 static void newline(void)
308 static void tab(short col)
310 static const char spaces[] = " ";
311 #define nspaces ((sizeof spaces)-1) /* null terminator! */
313 short n = col - column;
317 while (n > nspaces) {
321 /* must be 1...(sizeof spaces) left */
327 #ifdef FEATURE_FILETYPECHAR
328 static char append_char(umode_t mode)
330 if (!(opts & DISP_FTYPE))
332 if ((opts & DISP_EXEC) && S_ISREG(mode) && (mode & (S_IXUSR|S_IXGRP|S_IXOTH)))
334 return APPCHAR(mode);
340 ** Display a file or directory as a single item
341 ** (in either long or short format)
345 static void list_single(const char *name, struct stat *info)
348 short len = strlen(name);
349 #ifdef FEATURE_FILETYPECHAR
350 char append = append_char(info->st_mode);
353 if (display_fmt == FMT_LONG) {
354 umode_t mode = info->st_mode;
357 scratch[0] = TYPECHAR(mode);
360 scratch[i+1] = (mode & MBIT[i])
364 scratch[i+1] = (mode & MBIT[i])
370 writenum((long)info->st_nlink,(short)4);
372 #ifdef FEATURE_USERNAME
373 if (!(opts & DISP_NUMERIC)) {
374 struct passwd *pw = getpwuid(info->st_uid);
376 fputs(pw->pw_name, stdout);
378 writenum((long)info->st_uid,(short)0);
381 writenum((long)info->st_uid,(short)0);
383 #ifdef FEATURE_USERNAME
384 if (!(opts & DISP_NUMERIC)) {
385 struct group *gr = getgrgid(info->st_gid);
387 fputs(gr->gr_name, stdout);
389 writenum((long)info->st_gid,(short)0);
392 writenum((long)info->st_gid,(short)0);
394 if (S_ISBLK(mode) || S_ISCHR(mode)) {
395 writenum((long)MAJOR(info->st_rdev),(short)3);
397 writenum((long)MINOR(info->st_rdev),(short)3);
400 writenum((long)info->st_size,(short)8);
402 #ifdef FEATURE_TIMESTAMPS
409 cal=info->st_ctime; break;
411 cal=info->st_atime; break;
413 cal=info->st_mtime; break;
416 if (opts & DISP_FULLTIME)
419 time_t age = time(NULL) - cal;
420 wr(string+4,7); /* mmm_dd_ */
421 if(age < 3600L*24*365/2 && age > -15*60)
422 /* hh:mm if less than 6 months old */
425 /* _yyyy otherwise */
431 fputs("--- -- ----- ", stdout);
436 len = readlink(name, scratch, sizeof scratch);
437 if (len > 0) fwrite(scratch, 1, len, stdout);
438 #ifdef FEATURE_FILETYPECHAR
439 /* show type of destination */
440 if (opts & DISP_FTYPE) {
441 if (!stat(name, info)) {
442 append = append_char(info->st_mode);
444 fputc(append, stdout);
449 #ifdef FEATURE_FILETYPECHAR
454 static short nexttab = 0;
456 /* sort out column alignment */
458 ; /* nothing to do */
459 else if (display_fmt == FMT_SINGLE)
462 if (nexttab + column_width > terminal_width
463 #ifndef FEATURE_AUTOWIDTH
464 || nexttab + len >= terminal_width
471 /* work out where next column starts */
472 #ifdef FEATURE_AUTOWIDTH
473 /* we know the calculated width is big enough */
474 nexttab = column + column_width + COLUMN_GAP;
476 /* might cover more than one fixed-width column */
479 nexttab += column_width + COLUMN_GAP;
480 while (nexttab < (column + len + COLUMN_GAP));
482 /* now write the data */
484 column = column + len;
485 #ifdef FEATURE_FILETYPECHAR
487 wr(&append, 1), column++;
494 ** List the given file or directory, expanding a directory
495 ** to show its contents if required
499 static int list_item(const char *name)
503 struct dirent *entry;
504 char fullname[MAXNAMLEN+1], *fnend;
506 if (lstat(name, &info))
509 if (!S_ISDIR(info.st_mode) ||
510 (opts & DIR_NOLIST)) {
511 list_single(name, &info);
515 /* Otherwise, it's a directory we want to list the contents of */
517 if (opts & DISP_DIRNAME) { /* identify the directory */
519 wr("\n\n", 2), column = 0;
520 wr(name, strlen(name));
525 if (!dir) goto listerr;
526 #ifdef FEATURE_AUTOWIDTH
528 while ((entry = readdir(dir)) != NULL) {
529 short w = strlen(entry->d_name);
530 if (column_width < w)
538 if (!dir) goto listerr;
542 /* List the contents */
544 strcpy(fullname,name); /* *** ignore '.' by itself */
545 fnend=fullname+strlen(fullname);
546 if (fnend[-1] != '/')
549 while ((entry = readdir(dir)) != NULL) {
550 const char *en=entry->d_name;
552 if (!en[1] || (en[1] == '.' && !en[2])) { /* . or .. */
553 if (!(opts & DISP_DOT))
556 else if (!(opts & DISP_HIDDEN))
559 /* FIXME: avoid stat if not required */
560 strcpy(fnend, entry->d_name);
561 if (lstat(fullname, &info))
562 goto direrr; /* (shouldn't fail) */
563 list_single(entry->d_name, &info);
576 const char ls_usage[] = "Usage: ls [-1a"
577 #ifdef FEATURE_TIMESTAMPS
581 #ifdef FEATURE_TIMESTAMPS
585 #ifdef FEATURE_FILETYPECHAR
588 #ifdef FEATURE_TIMESTAMPS
592 #ifdef FEATURE_FILETYPECHAR
595 #ifdef FEATURE_RECURSIVE
598 "] [filenames...]\n";
601 ls_main(int argc, char * * argv)
605 /* process options */
606 while (argi < argc && argv[argi][0] == '-') {
607 const char *p = &argv[argi][1];
609 if (!*p) goto print_usage_message; /* "-" by itself not allowed */
611 if (!p[1]) { /* "--" forces end of options */
615 /* it's a long option name - we don't support them */
616 goto print_usage_message;
621 case 'l': display_fmt = FMT_LONG; break;
622 case '1': display_fmt = FMT_SINGLE; break;
623 case 'x': display_fmt = FMT_ROWS; break;
624 case 'C': display_fmt = FMT_COLUMNS; break;
625 #ifdef FEATURE_FILETYPECHAR
626 case 'p': opts |= DISP_FTYPE; break;
627 case 'F': opts |= DISP_FTYPE|DISP_EXEC; break;
629 case 'A': opts |= DISP_HIDDEN; break;
630 case 'a': opts |= DISP_HIDDEN|DISP_DOT; break;
631 case 'n': opts |= DISP_NUMERIC; break;
632 case 'd': opts |= DIR_NOLIST; break;
633 #ifdef FEATURE_RECURSIVE
634 case 'R': opts |= DIR_RECURSE; break;
636 #ifdef FEATURE_TIMESTAMPS
637 case 'u': time_fmt = TIME_ACCESS; break;
638 case 'c': time_fmt = TIME_CHANGE; break;
639 case 'e': opts |= DISP_FULLTIME; break;
641 default: goto print_usage_message;
647 /* choose a display format */
648 if (display_fmt == FMT_AUTO)
649 display_fmt = isatty(STDOUT_FILENO) ? FMT_COLUMNS : FMT_SINGLE;
651 opts |= DISP_DIRNAME; /* 2 or more items? label directories */
652 #ifdef FEATURE_AUTOWIDTH
653 /* could add a -w option and/or TIOCGWINSZ call */
654 if (terminal_width < 1) terminal_width = TERMINAL_WIDTH;
656 for (i = argi; i < argc; i++) {
657 int len = strlen(argv[i]);
658 if (column_width < len)
663 /* process files specified, or current directory if none */
668 i |= list_item(argv[argi++]);
673 fprintf(stderr, "Usage: %s\n", ls_usage);