aboutsummaryrefslogtreecommitdiff
path: root/ls.c
blob: 78193e7c150e21018b85cc90f7520d20e19af29c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/*
 * tiny-ls.c version 0.1.0: A minimalist 'ls'
 * Copyright (C) 1996 Brian Candler <B.Candler@pobox.com>
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * To achieve a small memory footprint, this version of 'ls' doesn't do any
 * file sorting, and only has the most essential command line switches
 * (i.e. the ones I couldn't live without :-) All features which involve
 * linking in substantial chunks of libc can be disabled.
 *
 * Although I don't really want to add new features to this program to
 * keep it small, I *am* interested to receive bug fixes and ways to make
 * it more portable.
 *
 * KNOWN BUGS:
 * 1. messy output if you mix files and directories on the command line
 * 2. ls -l of a directory doesn't give "total <blocks>" header
 * 3. ls of a symlink to a directory doesn't list directory contents
 * 4. hidden files can make column width too large
 * NON-OPTIMAL BEHAVIOUR:
 * 1. autowidth reads directories twice
 * 2. if you do a short directory listing without filetype characters
 *    appended, there's no need to stat each one
 * PORTABILITY:
 * 1. requires lstat (BSD) - how do you do it without?
 */

#define TERMINAL_WIDTH	80	/* use 79 if your terminal has linefold bug */
#define	COLUMN_WIDTH	14	/* default if AUTOWIDTH not defined */
#define COLUMN_GAP	2	/* includes the file type char, if present */
#define HAS_REWINDDIR

/************************************************************************/

#include "internal.h"
#if !defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
# include <linux/types.h> 
#else
# include <sys/types.h> 
#endif
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#ifdef BB_FEATURE_LS_TIMESTAMPS
#include <time.h>
#endif

#define TYPEINDEX(mode)	(((mode) >> 12) & 0x0f)
#define TYPECHAR(mode)	("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
#ifdef BB_FEATURE_LS_FILETYPES
#define APPCHAR(mode)	("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
#endif

#ifndef MAJOR
#define MAJOR(dev) (((dev)>>8)&0xff)
#define MINOR(dev) ((dev)&0xff)
#endif

#define FMT_AUTO	0
#define FMT_LONG	1	/* one record per line, extended info */
#define FMT_SINGLE	2	/* one record per line */
#define FMT_ROWS	3	/* print across rows */
#define FMT_COLUMNS	3	/* fill columns (same, since we don't sort) */

#define TIME_MOD	0
#define TIME_CHANGE	1
#define TIME_ACCESS	2

#define DISP_FTYPE	1	/* show character for file type */
#define DISP_EXEC	2	/* show '*' if regular executable file */
#define DISP_HIDDEN	4	/* show files starting . (except . and ..) */
#define DISP_DOT	8	/* show . and .. */
#define DISP_NUMERIC	16	/* numeric uid and gid */
#define DISP_FULLTIME	32	/* show extended time display */
#define DIR_NOLIST	64	/* show directory as itself, not contents */
#define DISP_DIRNAME	128	/* show directory name (for internal use) */
#define DIR_RECURSE	256	/* -R (not yet implemented) */

static unsigned char	display_fmt = FMT_AUTO;
static unsigned short	opts = 0;
static unsigned short	column = 0;

#ifdef BB_FEATURE_AUTOWIDTH
static unsigned short terminal_width = 0, column_width = 0;
#else
#define terminal_width	TERMINAL_WIDTH
#define column_width	COLUMN_WIDTH
#endif

#ifdef BB_FEATURE_LS_TIMESTAMPS
static unsigned char time_fmt = TIME_MOD;
#endif

#define wr(data,len) fwrite(data, 1, len, stdout)

static void writenum(long val, short minwidth)
{
	char	scratch[128];

	char *p = scratch + sizeof(scratch);
	short len = 0;
	short neg = (val < 0);
	
	if (neg) val = -val;
	do
		*--p = (val % 10) + '0', len++, val /= 10;
	while (val);
	if (neg)
		*--p = '-', len++;
	while (len < minwidth)
		*--p = ' ', len++;
	wr(p, len);
	column += len;
}

static void newline(void)
{
	if (column > 0) {
		wr("\n", 1);
		column = 0;
	}
}

static void tab(short col)
{
	static const char spaces[] = "                ";
	#define nspaces ((sizeof spaces)-1)	/* null terminator! */
	
	short n = col - column;

	if (n > 0) {
		column = col;
		while (n > nspaces) {
			wr(spaces, nspaces);
			n -= nspaces;
		}
		/* must be 1...(sizeof spaces) left */
		wr(spaces, n);
	}
	#undef nspaces
}

#ifdef BB_FEATURE_LS_FILETYPES
static char append_char(mode_t mode)
{
	if (!(opts & DISP_FTYPE))
		return '\0';
	if ((opts & DISP_EXEC) && S_ISREG(mode) && (mode & (S_IXUSR|S_IXGRP|S_IXOTH)))
		return '*';
	return APPCHAR(mode);
}
#endif

/**
 **
 ** Display a file or directory as a single item
 ** (in either long or short format)
 **
 **/

static void list_single(const char *name, struct stat *info, const char *fullname)
{
	char scratch[PATH_MAX];
	short len = strlen(name);
#ifdef BB_FEATURE_LS_FILETYPES
	char append = append_char(info->st_mode);
#endif
	
	if (display_fmt == FMT_LONG) {
		mode_t mode = info->st_mode; 
		newline();
		wr(modeString(mode), 10);
		column=10;
		writenum((long)info->st_nlink,(short)5);
		fputs(" ", stdout);
#ifdef BB_FEATURE_LS_USERNAME
		if (!(opts & DISP_NUMERIC)) {
			memset ( scratch, 0, sizeof (scratch));
			my_getpwuid( scratch, info->st_uid);
			if (*scratch) {
			    fputs(scratch, stdout);
			    if ( strlen( scratch) <= 8 )
				wr("          ", 9-strlen( scratch));
			}
			else {
				writenum((long) info->st_uid,(short)8);
				fputs(" ", stdout);
			}
		} else
#endif
		{
		    writenum((long) info->st_uid,(short)8);
		    fputs(" ", stdout);
		}
#ifdef BB_FEATURE_LS_USERNAME
		if (!(opts & DISP_NUMERIC)) {
			memset ( scratch, 0, sizeof (scratch));
			my_getgrgid( scratch, info->st_gid);
			if (*scratch) {
			    fputs(scratch, stdout);
			    if ( strlen( scratch) <= 8 )
				wr("         ", 8-strlen( scratch));
			}
			else 
			    writenum((long) info->st_gid,(short)8);
		} else
#endif
		writenum((long) info->st_gid,(short)8);
		//tab(26);
		if (S_ISBLK(mode) || S_ISCHR(mode)) {
			writenum((long)MAJOR(info->st_rdev),(short)3);
			fputs(", ", stdout);
			writenum((long)MINOR(info->st_rdev),(short)3);
		}
		else
			writenum((long)info->st_size,(short)8);
		fputs(" ", stdout);
		//tab(32);
#ifdef BB_FEATURE_LS_TIMESTAMPS
		{
			time_t cal;
			char *string;
			
			switch(time_fmt) {
			case TIME_CHANGE:
				cal=info->st_ctime; break;
			case TIME_ACCESS:
				cal=info->st_atime; break;
			default:
				cal=info->st_mtime; break;
			}
			string=ctime(&cal);
			if (opts & DISP_FULLTIME)
				wr(string,24);
			else {
				time_t age = time(NULL) - cal;
				wr(string+4,7);	/* mmm_dd_ */
				if(age < 3600L*24*365/2 && age > -15*60)
					/* hh:mm if less than 6 months old */
					wr(string+11,5);
				else
					/* _yyyy otherwise */
					wr(string+19,5);
			}
			wr(" ", 1);
		}
#else
		fputs("--- -- ----- ", stdout);
#endif
		wr(name, len);
		if (S_ISLNK(mode)) {
			wr(" -> ", 4);
			len = readlink(fullname, scratch, sizeof scratch);
			if (len > 0) fwrite(scratch, 1, len, stdout);
#ifdef BB_FEATURE_LS_FILETYPES
			/* show type of destination */
			if (opts & DISP_FTYPE) {
				if (!stat(fullname, info)) {
					append = append_char(info->st_mode);
					if (append)
						fputc(append, stdout);
				}
			}
#endif
		}
#ifdef BB_FEATURE_LS_FILETYPES
		else if (append)
			wr(&append, 1);
#endif
	} else {
		static short nexttab = 0;
		
		/* sort out column alignment */
		if (column == 0)
			; /* nothing to do */
		else if (display_fmt == FMT_SINGLE)
			newline();
		else {
			if (nexttab + column_width > terminal_width
#ifndef BB_FEATURE_AUTOWIDTH
			|| nexttab + len >= terminal_width
#endif
			)
				newline();
			else
				tab(nexttab);
		}
		/* work out where next column starts */
#ifdef BB_FEATURE_AUTOWIDTH
		/* we know the calculated width is big enough */
		nexttab = column + column_width + COLUMN_GAP;
#else
		/* might cover more than one fixed-width column */
		nexttab = column;
		do
			nexttab += column_width + COLUMN_GAP;
		while (nexttab < (column + len + COLUMN_GAP));
#endif
		/* now write the data */
		wr(name, len);
		column = column + len;
#ifdef BB_FEATURE_LS_FILETYPES
		if (append)
			wr(&append, 1), column++;
#endif
	}
}

/**
 **
 ** List the given file or directory, expanding a directory
 ** to show its contents if required
 **
 **/

static int list_item(const char *name)
{
	struct stat info;
	DIR *dir;
	struct dirent *entry;
	char fullname[MAXNAMLEN+1], *fnend;
	
	if (lstat(name, &info))
		goto listerr;
	
	if (!S_ISDIR(info.st_mode) || 
	    (opts & DIR_NOLIST)) {
		list_single(name, &info, name);
		return 0;
	}

	/* Otherwise, it's a directory we want to list the contents of */

	if (opts & DISP_DIRNAME) {   /* identify the directory */
		if (column)
			wr("\n\n", 2), column = 0;
		wr(name, strlen(name));
		wr(":\n", 2);
	}
	
	dir = opendir(name);
	if (!dir) goto listerr;
#ifdef BB_FEATURE_AUTOWIDTH
	column_width = 0;
	while ((entry = readdir(dir)) != NULL) {
		short w = strlen(entry->d_name);
		if (column_width < w)
			column_width = w;
	}
#ifdef HAS_REWINDDIR
	rewinddir(dir);
#else
	closedir(dir);
	dir = opendir(name);
	if (!dir) goto listerr;
#endif
#endif

	/* List the contents */
	
	strcpy(fullname,name);	/* *** ignore '.' by itself */
	fnend=fullname+strlen(fullname);
	if (fnend[-1] != '/')
		*fnend++ = '/';
	
	while ((entry = readdir(dir)) != NULL) {
		const char *en=entry->d_name;
		if (en[0] == '.') {
			if (!en[1] || (en[1] == '.' && !en[2])) { /* . or .. */
				if (!(opts & DISP_DOT))
					continue;
			}
			else if (!(opts & DISP_HIDDEN))
				continue;
		}
		/* FIXME: avoid stat if not required */
		strcpy(fnend, entry->d_name);
		if (lstat(fullname, &info))
			goto direrr; /* (shouldn't fail) */
		list_single(entry->d_name, &info, fullname);
	}
	closedir(dir);
	return 0;

direrr:
	closedir(dir);	
listerr:
	newline();
	perror(name);
	return 1;
}

static const char ls_usage[] = "ls [-1a"
#ifdef BB_FEATURE_LS_TIMESTAMPS
	"c"
#endif
	"d"
#ifdef BB_FEATURE_LS_TIMESTAMPS
	"e"
#endif
	"ln"
#ifdef BB_FEATURE_LS_FILETYPES
	"p"
#endif
#ifdef BB_FEATURE_LS_TIMESTAMPS
	"u"
#endif
	"xAC"
#ifdef BB_FEATURE_LS_FILETYPES
	"F"
#endif
#ifdef FEATURE_RECURSIVE
	"R"
#endif
	"] [filenames...]\n";

extern int
ls_main(int argc, char * * argv)
{
	int argi=1, i;
	
	/* process options */
	while (argi < argc && argv[argi][0] == '-') {
		const char *p = &argv[argi][1];
		
		if (!*p) goto print_usage_message;	/* "-" by itself not allowed */
		if (*p == '-') {
			if (!p[1]) {	/* "--" forces end of options */
				argi++;
				break;
			}
			/* it's a long option name - we don't support them */
			goto print_usage_message;
		}
		
		while (*p)
			switch (*p++) {
			case 'l':	display_fmt = FMT_LONG; break;
			case '1':	display_fmt = FMT_SINGLE; break;
			case 'x':	display_fmt = FMT_ROWS; break;
			case 'C':	display_fmt = FMT_COLUMNS; break;
#ifdef BB_FEATURE_LS_FILETYPES
			case 'p':	opts |= DISP_FTYPE; break;
			case 'F':	opts |= DISP_FTYPE|DISP_EXEC; break;
#endif
			case 'A':	opts |= DISP_HIDDEN; break;
			case 'a':	opts |= DISP_HIDDEN|DISP_DOT; break;
			case 'n':	opts |= DISP_NUMERIC; break;
			case 'd':	opts |= DIR_NOLIST; break;
#ifdef FEATURE_RECURSIVE
			case 'R':	opts |= DIR_RECURSE; break;
#endif
#ifdef BB_FEATURE_LS_TIMESTAMPS
			case 'u':	time_fmt = TIME_ACCESS; break;
			case 'c':	time_fmt = TIME_CHANGE; break;
			case 'e':	opts |= DISP_FULLTIME; break;
#endif
			default:	goto print_usage_message;
			}
		
		argi++;
	}

	/* choose a display format */
	if (display_fmt == FMT_AUTO)
		display_fmt = isatty(fileno(stdout)) ? FMT_COLUMNS : FMT_SINGLE;
	if (argi < argc - 1)
		opts |= DISP_DIRNAME; /* 2 or more items? label directories */
#ifdef BB_FEATURE_AUTOWIDTH
	/* could add a -w option and/or TIOCGWINSZ call */
	if (terminal_width < 1) terminal_width = TERMINAL_WIDTH;
	
	for (i = argi; i < argc; i++) {
		int len = strlen(argv[i]);
		if (column_width < len)
			column_width = len;
	}
#endif

	/* process files specified, or current directory if none */
	i=0;
	if (argi == argc)
		i = list_item(".");
	while (argi < argc)
		i |= list_item(argv[argi++]);
	newline();
	exit( i);

print_usage_message:
	usage (ls_usage);
	exit( FALSE);
}