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
22 // I started writing a newer small one, but it isn't done yet....
32 static const char ls_usage[] = "ls [OPTION]... [FILE]...\n"
33 "List information about the FILEs (the current directory by default).\n";
37 int directoryFlag=FALSE;
40 int dereferenceFlag=FALSE;
41 int recursiveFlag=FALSE;
43 static int fileAction(const char *fileName)
45 if ( allFlag==FALSE && ((strcmp(fileName, "..") == 0)
46 || (strcmp(fileName, ".") == 0)) ) {
49 //struct stat statBuf;
50 //if (stat(fileName, &statBuf) > 0) {
51 fprintf(stdout, "%s\n", fileName);
60 static int dirAction(const char *fileName)
65 fprintf(stdout, "%s\n", fileName);
67 dir = opendir( fileName);
69 perror("Can't open directory");
72 while ((entry = readdir(dir)) != NULL) {
73 recursiveAction( entry->d_name, recursiveFlag, dereferenceFlag, FALSE, fileAction, dirAction);
78 int ls_main(int argc, char **argv)
82 getcwd( buf, NAME_MAX);
86 /* peel of the "ls" */
90 /* Parse any options */
91 while (**argv == '-') {
92 while (*++(*argv)) switch (**argv) {
100 directoryFlag = TRUE;
109 dereferenceFlag = TRUE;
112 recursiveFlag = TRUE;
121 /* Ok, ready to do the deed now */
122 fprintf(stderr, "B\n");
124 fprintf(stderr, "C\n");
125 recursiveAction( *argv, recursiveFlag, dereferenceFlag, fileAction, dirAction);
135 #include "internal.h"
137 * tiny-ls.c version 0.1.0: A minimalist 'ls'
138 * Copyright (C) 1996 Brian Candler <B.Candler@pobox.com>
140 * This program is free software; you can redistribute it and/or modify
141 * it under the terms of the GNU General Public License as published by
142 * the Free Software Foundation; either version 2 of the License, or
143 * (at your option) any later version.
145 * This program is distributed in the hope that it will be useful,
146 * but WITHOUT ANY WARRANTY; without even the implied warranty of
147 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
148 * GNU General Public License for more details.
150 * You should have received a copy of the GNU General Public License
151 * along with this program; if not, write to the Free Software
152 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
156 * To achieve a small memory footprint, this version of 'ls' doesn't do any
157 * file sorting, and only has the most essential command line switches
158 * (i.e. the ones I couldn't live without :-) All features which involve
159 * linking in substantial chunks of libc can be disabled.
161 * Although I don't really want to add new features to this program to
162 * keep it small, I *am* interested to receive bug fixes and ways to make
166 * 1. messy output if you mix files and directories on the command line
167 * 2. ls -l of a directory doesn't give "total <blocks>" header
168 * 3. ls of a symlink to a directory doesn't list directory contents
169 * 4. hidden files can make column width too large
170 * NON-OPTIMAL BEHAVIOUR:
171 * 1. autowidth reads directories twice
172 * 2. if you do a short directory listing without filetype characters
173 * appended, there's no need to stat each one
175 * 1. requires lstat (BSD) - how do you do it without?
178 #define FEATURE_USERNAME /* show username/groupnames (libc6 uses NSS) */
179 #define FEATURE_TIMESTAMPS /* show file timestamps */
180 #define FEATURE_AUTOWIDTH /* calculate terminal & column widths */
181 #define FEATURE_FILETYPECHAR /* enable -p and -F */
183 #define TERMINAL_WIDTH 80 /* use 79 if your terminal has linefold bug */
184 #define COLUMN_WIDTH 14 /* default if AUTOWIDTH not defined */
185 #define COLUMN_GAP 2 /* includes the file type char, if present */
186 #define HAS_REWINDDIR
188 /************************************************************************/
191 #if 1 /* FIXME libc 6 */
192 # include <linux/types.h>
194 # include <sys/types.h>
196 #include <sys/stat.h>
202 #ifdef FEATURE_USERNAME
206 #ifdef FEATURE_TIMESTAMPS
210 #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
211 #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
212 #ifdef FEATURE_FILETYPECHAR
213 #define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
217 #define MAJOR(dev) (((dev)>>8)&0xff)
218 #define MINOR(dev) ((dev)&0xff)
221 #define MODE1 "rwxrwxrwx"
222 #define MODE0 "---------"
223 #define SMODE1 "..s..s..t"
224 #define SMODE0 "..S..S..T"
226 /* The 9 mode bits to test */
228 static const umode_t MBIT[] = {
229 S_IRUSR, S_IWUSR, S_IXUSR,
230 S_IRGRP, S_IWGRP, S_IXGRP,
231 S_IROTH, S_IWOTH, S_IXOTH
234 /* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
236 static const umode_t SBIT[] = {
243 #define FMT_LONG 1 /* one record per line, extended info */
244 #define FMT_SINGLE 2 /* one record per line */
245 #define FMT_ROWS 3 /* print across rows */
246 #define FMT_COLUMNS 3 /* fill columns (same, since we don't sort) */
249 #define TIME_CHANGE 1
250 #define TIME_ACCESS 2
252 #define DISP_FTYPE 1 /* show character for file type */
253 #define DISP_EXEC 2 /* show '*' if regular executable file */
254 #define DISP_HIDDEN 4 /* show files starting . (except . and ..) */
255 #define DISP_DOT 8 /* show . and .. */
256 #define DISP_NUMERIC 16 /* numeric uid and gid */
257 #define DISP_FULLTIME 32 /* show extended time display */
258 #define DIR_NOLIST 64 /* show directory as itself, not contents */
259 #define DISP_DIRNAME 128 /* show directory name (for internal use) */
260 #define DIR_RECURSE 256 /* -R (not yet implemented) */
262 static unsigned char display_fmt = FMT_AUTO;
263 static unsigned short opts = 0;
264 static unsigned short column = 0;
266 #ifdef FEATURE_AUTOWIDTH
267 static unsigned short terminal_width = 0, column_width = 0;
269 #define terminal_width TERMINAL_WIDTH
270 #define column_width COLUMN_WIDTH
273 #ifdef FEATURE_TIMESTAMPS
274 static unsigned char time_fmt = TIME_MOD;
277 #define wr(data,len) fwrite(data, 1, len, stdout)
279 static void writenum(long val, short minwidth)
283 char *p = scratch + sizeof(scratch);
285 short neg = (val < 0);
289 *--p = (val % 10) + '0', len++, val /= 10;
293 while (len < minwidth)
299 static void newline(void)
307 static void tab(short col)
309 static const char spaces[] = " ";
310 #define nspaces ((sizeof spaces)-1) /* null terminator! */
312 short n = col - column;
316 while (n > nspaces) {
320 /* must be 1...(sizeof spaces) left */
326 #ifdef FEATURE_FILETYPECHAR
327 static char append_char(umode_t mode)
329 if (!(opts & DISP_FTYPE))
331 if ((opts & DISP_EXEC) && S_ISREG(mode) && (mode & (S_IXUSR|S_IXGRP|S_IXOTH)))
333 return APPCHAR(mode);
339 ** Display a file or directory as a single item
340 ** (in either long or short format)
344 static void list_single(const char *name, struct stat *info)
347 short len = strlen(name);
348 #ifdef FEATURE_FILETYPECHAR
349 char append = append_char(info->st_mode);
352 if (display_fmt == FMT_LONG) {
353 umode_t mode = info->st_mode;
356 scratch[0] = TYPECHAR(mode);
359 scratch[i+1] = (mode & MBIT[i])
363 scratch[i+1] = (mode & MBIT[i])
369 writenum((long)info->st_nlink,(short)4);
371 #ifdef FEATURE_USERNAME
372 if (!(opts & DISP_NUMERIC)) {
373 struct passwd *pw = getpwuid(info->st_uid);
375 fputs(pw->pw_name, stdout);
377 writenum((long)info->st_uid,(short)0);
380 writenum((long)info->st_uid,(short)0);
382 #ifdef FEATURE_USERNAME
383 if (!(opts & DISP_NUMERIC)) {
384 struct group *gr = getgrgid(info->st_gid);
386 fputs(gr->gr_name, stdout);
388 writenum((long)info->st_gid,(short)0);
391 writenum((long)info->st_gid,(short)0);
393 if (S_ISBLK(mode) || S_ISCHR(mode)) {
394 writenum((long)MAJOR(info->st_rdev),(short)3);
396 writenum((long)MINOR(info->st_rdev),(short)3);
399 writenum((long)info->st_size,(short)8);
401 #ifdef FEATURE_TIMESTAMPS
408 cal=info->st_ctime; break;
410 cal=info->st_atime; break;
412 cal=info->st_mtime; break;
415 if (opts & DISP_FULLTIME)
418 time_t age = time(NULL) - cal;
419 wr(string+4,7); /* mmm_dd_ */
420 if(age < 3600L*24*365/2 && age > -15*60)
421 /* hh:mm if less than 6 months old */
424 /* _yyyy otherwise */
430 fputs("--- -- ----- ", stdout);
435 len = readlink(name, scratch, sizeof scratch);
436 if (len > 0) fwrite(scratch, 1, len, stdout);
437 #ifdef FEATURE_FILETYPECHAR
438 /* show type of destination */
439 if (opts & DISP_FTYPE) {
440 if (!stat(name, info)) {
441 append = append_char(info->st_mode);
443 fputc(append, stdout);
448 #ifdef FEATURE_FILETYPECHAR
453 static short nexttab = 0;
455 /* sort out column alignment */
457 ; /* nothing to do */
458 else if (display_fmt == FMT_SINGLE)
461 if (nexttab + column_width > terminal_width
462 #ifndef FEATURE_AUTOWIDTH
463 || nexttab + len >= terminal_width
470 /* work out where next column starts */
471 #ifdef FEATURE_AUTOWIDTH
472 /* we know the calculated width is big enough */
473 nexttab = column + column_width + COLUMN_GAP;
475 /* might cover more than one fixed-width column */
478 nexttab += column_width + COLUMN_GAP;
479 while (nexttab < (column + len + COLUMN_GAP));
481 /* now write the data */
483 column = column + len;
484 #ifdef FEATURE_FILETYPECHAR
486 wr(&append, 1), column++;
493 ** List the given file or directory, expanding a directory
494 ** to show its contents if required
498 static int list_item(const char *name)
502 struct dirent *entry;
503 char fullname[MAXNAMLEN+1], *fnend;
505 if (lstat(name, &info))
508 if (!S_ISDIR(info.st_mode) ||
509 (opts & DIR_NOLIST)) {
510 list_single(name, &info);
514 /* Otherwise, it's a directory we want to list the contents of */
516 if (opts & DISP_DIRNAME) { /* identify the directory */
518 wr("\n\n", 2), column = 0;
519 wr(name, strlen(name));
524 if (!dir) goto listerr;
525 #ifdef FEATURE_AUTOWIDTH
527 while ((entry = readdir(dir)) != NULL) {
528 short w = strlen(entry->d_name);
529 if (column_width < w)
537 if (!dir) goto listerr;
541 /* List the contents */
543 strcpy(fullname,name); /* *** ignore '.' by itself */
544 fnend=fullname+strlen(fullname);
545 if (fnend[-1] != '/')
548 while ((entry = readdir(dir)) != NULL) {
549 const char *en=entry->d_name;
551 if (!en[1] || (en[1] == '.' && !en[2])) { /* . or .. */
552 if (!(opts & DISP_DOT))
555 else if (!(opts & DISP_HIDDEN))
558 /* FIXME: avoid stat if not required */
559 strcpy(fnend, entry->d_name);
560 if (lstat(fullname, &info))
561 goto direrr; /* (shouldn't fail) */
562 list_single(entry->d_name, &info);
575 const char ls_usage[] = "ls [-1a"
576 #ifdef FEATURE_TIMESTAMPS
580 #ifdef FEATURE_TIMESTAMPS
584 #ifdef FEATURE_FILETYPECHAR
587 #ifdef FEATURE_TIMESTAMPS
591 #ifdef FEATURE_FILETYPECHAR
594 #ifdef FEATURE_RECURSIVE
597 "] [filenames...]\n";
600 ls_main(int argc, char * * argv)
604 /* process options */
605 while (argi < argc && argv[argi][0] == '-') {
606 const char *p = &argv[argi][1];
608 if (!*p) goto print_usage_message; /* "-" by itself not allowed */
610 if (!p[1]) { /* "--" forces end of options */
614 /* it's a long option name - we don't support them */
615 goto print_usage_message;
620 case 'l': display_fmt = FMT_LONG; break;
621 case '1': display_fmt = FMT_SINGLE; break;
622 case 'x': display_fmt = FMT_ROWS; break;
623 case 'C': display_fmt = FMT_COLUMNS; break;
624 #ifdef FEATURE_FILETYPECHAR
625 case 'p': opts |= DISP_FTYPE; break;
626 case 'F': opts |= DISP_FTYPE|DISP_EXEC; break;
628 case 'A': opts |= DISP_HIDDEN; break;
629 case 'a': opts |= DISP_HIDDEN|DISP_DOT; break;
630 case 'n': opts |= DISP_NUMERIC; break;
631 case 'd': opts |= DIR_NOLIST; break;
632 #ifdef FEATURE_RECURSIVE
633 case 'R': opts |= DIR_RECURSE; break;
635 #ifdef FEATURE_TIMESTAMPS
636 case 'u': time_fmt = TIME_ACCESS; break;
637 case 'c': time_fmt = TIME_CHANGE; break;
638 case 'e': opts |= DISP_FULLTIME; break;
640 default: goto print_usage_message;
646 /* choose a display format */
647 if (display_fmt == FMT_AUTO)
648 display_fmt = isatty(STDOUT_FILENO) ? FMT_COLUMNS : FMT_SINGLE;
650 opts |= DISP_DIRNAME; /* 2 or more items? label directories */
651 #ifdef FEATURE_AUTOWIDTH
652 /* could add a -w option and/or TIOCGWINSZ call */
653 if (terminal_width < 1) terminal_width = TERMINAL_WIDTH;
655 for (i = argi; i < argc; i++) {
656 int len = strlen(argv[i]);
657 if (column_width < len)
662 /* process files specified, or current directory if none */
667 i |= list_item(argv[argi++]);