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);
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 #define TERMINAL_WIDTH 80 /* use 79 if your terminal has linefold bug */
183 #define COLUMN_WIDTH 14 /* default if AUTOWIDTH not defined */
184 #define COLUMN_GAP 2 /* includes the file type char, if present */
185 #define HAS_REWINDDIR
187 /************************************************************************/
189 #include "internal.h"
190 #if !defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
191 # include <linux/types.h>
193 # include <sys/types.h>
195 #include <sys/stat.h>
201 #ifdef FEATURE_USERNAME
205 #ifdef FEATURE_TIMESTAMPS
209 #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
210 #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
211 #ifdef FEATURE_FILETYPECHAR
212 #define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
216 #define MAJOR(dev) (((dev)>>8)&0xff)
217 #define MINOR(dev) ((dev)&0xff)
220 #define MODE1 "rwxrwxrwx"
221 #define MODE0 "---------"
222 #define SMODE1 "..s..s..t"
223 #define SMODE0 "..S..S..T"
225 /* The 9 mode bits to test */
227 static const mode_t MBIT[] = {
228 S_IRUSR, S_IWUSR, S_IXUSR,
229 S_IRGRP, S_IWGRP, S_IXGRP,
230 S_IROTH, S_IWOTH, S_IXOTH
233 /* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
235 static const mode_t SBIT[] = {
242 #define FMT_LONG 1 /* one record per line, extended info */
243 #define FMT_SINGLE 2 /* one record per line */
244 #define FMT_ROWS 3 /* print across rows */
245 #define FMT_COLUMNS 3 /* fill columns (same, since we don't sort) */
248 #define TIME_CHANGE 1
249 #define TIME_ACCESS 2
251 #define DISP_FTYPE 1 /* show character for file type */
252 #define DISP_EXEC 2 /* show '*' if regular executable file */
253 #define DISP_HIDDEN 4 /* show files starting . (except . and ..) */
254 #define DISP_DOT 8 /* show . and .. */
255 #define DISP_NUMERIC 16 /* numeric uid and gid */
256 #define DISP_FULLTIME 32 /* show extended time display */
257 #define DIR_NOLIST 64 /* show directory as itself, not contents */
258 #define DISP_DIRNAME 128 /* show directory name (for internal use) */
259 #define DIR_RECURSE 256 /* -R (not yet implemented) */
261 static unsigned char display_fmt = FMT_AUTO;
262 static unsigned short opts = 0;
263 static unsigned short column = 0;
265 #ifdef FEATURE_AUTOWIDTH
266 static unsigned short terminal_width = 0, column_width = 0;
268 #define terminal_width TERMINAL_WIDTH
269 #define column_width COLUMN_WIDTH
272 #ifdef FEATURE_TIMESTAMPS
273 static unsigned char time_fmt = TIME_MOD;
276 #define wr(data,len) fwrite(data, 1, len, stdout)
278 static void writenum(long val, short minwidth)
282 char *p = scratch + sizeof(scratch);
284 short neg = (val < 0);
288 *--p = (val % 10) + '0', len++, val /= 10;
292 while (len < minwidth)
298 static void newline(void)
306 static void tab(short col)
308 static const char spaces[] = " ";
309 #define nspaces ((sizeof spaces)-1) /* null terminator! */
311 short n = col - column;
315 while (n > nspaces) {
319 /* must be 1...(sizeof spaces) left */
325 #ifdef FEATURE_FILETYPECHAR
326 static char append_char(mode_t mode)
328 if (!(opts & DISP_FTYPE))
330 if ((opts & DISP_EXEC) && S_ISREG(mode) && (mode & (S_IXUSR|S_IXGRP|S_IXOTH)))
332 return APPCHAR(mode);
338 ** Display a file or directory as a single item
339 ** (in either long or short format)
343 static void list_single(const char *name, struct stat *info)
345 char scratch[PATH_MAX];
346 short len = strlen(name);
347 #ifdef FEATURE_FILETYPECHAR
348 char append = append_char(info->st_mode);
351 if (display_fmt == FMT_LONG) {
352 mode_t mode = info->st_mode;
355 scratch[0] = TYPECHAR(mode);
358 scratch[i+1] = (mode & MBIT[i])
362 scratch[i+1] = (mode & MBIT[i])
368 writenum((long)info->st_nlink,(short)4);
370 #ifdef FEATURE_USERNAME
371 if (!(opts & DISP_NUMERIC)) {
372 struct passwd *pw = getpwuid(info->st_uid);
374 fputs(pw->pw_name, stdout);
376 writenum((long)info->st_uid,(short)0);
379 writenum((long)info->st_uid,(short)0);
381 #ifdef FEATURE_USERNAME
382 if (!(opts & DISP_NUMERIC)) {
383 struct group *gr = getgrgid(info->st_gid);
385 fputs(gr->gr_name, stdout);
387 writenum((long)info->st_gid,(short)0);
390 writenum((long)info->st_gid,(short)0);
392 if (S_ISBLK(mode) || S_ISCHR(mode)) {
393 writenum((long)MAJOR(info->st_rdev),(short)3);
395 writenum((long)MINOR(info->st_rdev),(short)3);
398 writenum((long)info->st_size,(short)8);
400 #ifdef FEATURE_TIMESTAMPS
407 cal=info->st_ctime; break;
409 cal=info->st_atime; break;
411 cal=info->st_mtime; break;
414 if (opts & DISP_FULLTIME)
417 time_t age = time(NULL) - cal;
418 wr(string+4,7); /* mmm_dd_ */
419 if(age < 3600L*24*365/2 && age > -15*60)
420 /* hh:mm if less than 6 months old */
423 /* _yyyy otherwise */
429 fputs("--- -- ----- ", stdout);
434 len = readlink(name, scratch, sizeof scratch);
435 if (len > 0) fwrite(scratch, 1, len, stdout);
436 #ifdef FEATURE_FILETYPECHAR
437 /* show type of destination */
438 if (opts & DISP_FTYPE) {
439 if (!stat(name, info)) {
440 append = append_char(info->st_mode);
442 fputc(append, stdout);
447 #ifdef FEATURE_FILETYPECHAR
452 static short nexttab = 0;
454 /* sort out column alignment */
456 ; /* nothing to do */
457 else if (display_fmt == FMT_SINGLE)
460 if (nexttab + column_width > terminal_width
461 #ifndef FEATURE_AUTOWIDTH
462 || nexttab + len >= terminal_width
469 /* work out where next column starts */
470 #ifdef FEATURE_AUTOWIDTH
471 /* we know the calculated width is big enough */
472 nexttab = column + column_width + COLUMN_GAP;
474 /* might cover more than one fixed-width column */
477 nexttab += column_width + COLUMN_GAP;
478 while (nexttab < (column + len + COLUMN_GAP));
480 /* now write the data */
482 column = column + len;
483 #ifdef FEATURE_FILETYPECHAR
485 wr(&append, 1), column++;
492 ** List the given file or directory, expanding a directory
493 ** to show its contents if required
497 static int list_item(const char *name)
501 struct dirent *entry;
502 char fullname[MAXNAMLEN+1], *fnend;
504 if (lstat(name, &info))
507 if (!S_ISDIR(info.st_mode) ||
508 (opts & DIR_NOLIST)) {
509 list_single(name, &info);
513 /* Otherwise, it's a directory we want to list the contents of */
515 if (opts & DISP_DIRNAME) { /* identify the directory */
517 wr("\n\n", 2), column = 0;
518 wr(name, strlen(name));
523 if (!dir) goto listerr;
524 #ifdef FEATURE_AUTOWIDTH
526 while ((entry = readdir(dir)) != NULL) {
527 short w = strlen(entry->d_name);
528 if (column_width < w)
536 if (!dir) goto listerr;
540 /* List the contents */
542 strcpy(fullname,name); /* *** ignore '.' by itself */
543 fnend=fullname+strlen(fullname);
544 if (fnend[-1] != '/')
547 while ((entry = readdir(dir)) != NULL) {
548 const char *en=entry->d_name;
550 if (!en[1] || (en[1] == '.' && !en[2])) { /* . or .. */
551 if (!(opts & DISP_DOT))
554 else if (!(opts & DISP_HIDDEN))
557 /* FIXME: avoid stat if not required */
558 strcpy(fnend, entry->d_name);
559 if (lstat(fullname, &info))
560 goto direrr; /* (shouldn't fail) */
561 list_single(entry->d_name, &info);
574 const char ls_usage[] = "ls [-1a"
575 #ifdef FEATURE_TIMESTAMPS
579 #ifdef FEATURE_TIMESTAMPS
583 #ifdef FEATURE_FILETYPECHAR
586 #ifdef FEATURE_TIMESTAMPS
590 #ifdef FEATURE_FILETYPECHAR
593 #ifdef FEATURE_RECURSIVE
596 "] [filenames...]\n";
599 ls_main(int argc, char * * argv)
603 /* process options */
604 while (argi < argc && argv[argi][0] == '-') {
605 const char *p = &argv[argi][1];
607 if (!*p) goto print_usage_message; /* "-" by itself not allowed */
609 if (!p[1]) { /* "--" forces end of options */
613 /* it's a long option name - we don't support them */
614 goto print_usage_message;
619 case 'l': display_fmt = FMT_LONG; break;
620 case '1': display_fmt = FMT_SINGLE; break;
621 case 'x': display_fmt = FMT_ROWS; break;
622 case 'C': display_fmt = FMT_COLUMNS; break;
623 #ifdef FEATURE_FILETYPECHAR
624 case 'p': opts |= DISP_FTYPE; break;
625 case 'F': opts |= DISP_FTYPE|DISP_EXEC; break;
627 case 'A': opts |= DISP_HIDDEN; break;
628 case 'a': opts |= DISP_HIDDEN|DISP_DOT; break;
629 case 'n': opts |= DISP_NUMERIC; break;
630 case 'd': opts |= DIR_NOLIST; break;
631 #ifdef FEATURE_RECURSIVE
632 case 'R': opts |= DIR_RECURSE; break;
634 #ifdef FEATURE_TIMESTAMPS
635 case 'u': time_fmt = TIME_ACCESS; break;
636 case 'c': time_fmt = TIME_CHANGE; break;
637 case 'e': opts |= DISP_FULLTIME; break;
639 default: goto print_usage_message;
645 /* choose a display format */
646 if (display_fmt == FMT_AUTO)
647 display_fmt = isatty(STDOUT_FILENO) ? FMT_COLUMNS : FMT_SINGLE;
649 opts |= DISP_DIRNAME; /* 2 or more items? label directories */
650 #ifdef FEATURE_AUTOWIDTH
651 /* could add a -w option and/or TIOCGWINSZ call */
652 if (terminal_width < 1) terminal_width = TERMINAL_WIDTH;
654 for (i = argi; i < argc; i++) {
655 int len = strlen(argv[i]);
656 if (column_width < len)
661 /* process files specified, or current directory if none */
666 i |= list_item(argv[argi++]);