View | Details | Raw Unified | Return to bug 18900
Collapse All | Expand All

(-)./ls.c (-3 / +12 lines)
Lines 100-105 Link Here
100
int f_timesort;			/* sort by time vice name */
100
int f_timesort;			/* sort by time vice name */
101
int f_type;			/* add type character for non-regular files */
101
int f_type;			/* add type character for non-regular files */
102
int f_whiteout;			/* show whiteout entries */
102
int f_whiteout;			/* show whiteout entries */
103
int f_color;			/* add type in color for non-regular files */
103
104
104
int rval;
105
int rval;
105
106
Lines 137-143 Link Here
137
		f_listdot = 1;
138
		f_listdot = 1;
138
139
139
	fts_options = FTS_PHYSICAL;
140
	fts_options = FTS_PHYSICAL;
140
	while ((ch = getopt(argc, argv, "1ABCFHLPRTWabcdfgikloqrstu")) != -1) {
141
	while ((ch = getopt(argc, argv, "1ABCFGHLPRTWabcdfgikloqrstu")) != -1) {
141
		switch (ch) {
142
		switch (ch) {
142
		/*
143
		/*
143
		 * The -1, -C and -l options all override each other so shell
144
		 * The -1, -C and -l options all override each other so shell
Lines 175-180 Link Here
175
		case 'H':
176
		case 'H':
176
		        fts_options |= FTS_COMFOLLOW;
177
		        fts_options |= FTS_COMFOLLOW;
177
			break;
178
			break;
179
		case 'G':
180
			if (isatty(STDOUT_FILENO))
181
				f_color = 1;
182
			break;
178
		case 'L':
183
		case 'L':
179
			fts_options &= ~FTS_PHYSICAL;
184
			fts_options &= ~FTS_PHYSICAL;
180
			fts_options |= FTS_LOGICAL;
185
			fts_options |= FTS_LOGICAL;
Lines 245-262 Link Here
245
	argc -= optind;
250
	argc -= optind;
246
	argv += optind;
251
	argv += optind;
247
252
253
	if (f_color)
254
		parsecolors(getenv("LSCOLORS"));
255
248
	/*
256
	/*
249
	 * If not -F, -i, -l, -s or -t options, don't require stat
257
	 * If not -F, -i, -l, -s or -t options, don't require stat
250
	 * information.
258
	 * information.
251
	 */
259
	 */
252
	if (!f_inode && !f_longform && !f_size && !f_timesort && !f_type)
260
	if (!f_inode && !f_longform && !f_size && !f_timesort && !f_type
261
	    && !f_color)
253
		fts_options |= FTS_NOSTAT;
262
		fts_options |= FTS_NOSTAT;
254
263
255
	/*
264
	/*
256
	 * If not -F, -d or -l options, follow any symbolic links listed on
265
	 * If not -F, -d or -l options, follow any symbolic links listed on
257
	 * the command line.
266
	 * the command line.
258
	 */
267
	 */
259
	if (!f_longform && !f_listdir && !f_type)
268
	if (!f_longform && !f_listdir && !f_type && !f_color)
260
		fts_options |= FTS_COMFOLLOW;
269
		fts_options |= FTS_COMFOLLOW;
261
270
262
	/*
271
	/*
(-)./print.c (+129 lines)
Lines 56-61 Link Here
56
#include <string.h>
56
#include <string.h>
57
#include <time.h>
57
#include <time.h>
58
#include <unistd.h>
58
#include <unistd.h>
59
#include <ctype.h>
59
60
60
#include "ls.h"
61
#include "ls.h"
61
#include "extern.h"
62
#include "extern.h"
Lines 67-72 Link Here
67
68
68
#define	IS_NOPRINT(p)	((p)->fts_number == NO_PRINT)
69
#define	IS_NOPRINT(p)	((p)->fts_number == NO_PRINT)
69
70
71
/* Most of these are taken from <sys/stat.h> */
72
typedef enum Colors {
73
    C_DIR,     /* directory */
74
    C_LNK,     /* symbolic link */
75
    C_SOCK,    /* socket */
76
    C_FIFO,    /* pipe */
77
    C_EXEC,    /* executable */
78
    C_BLK,     /* block special */
79
    C_CHR,     /* character special */
80
    C_SUID,    /* setuid executable */
81
    C_SGID,    /* setgid executable */
82
    C_WSDIR,   /* directory writeble to others, with sticky bit */
83
    C_WDIR,    /* directory writeble to others, without sticky bit */
84
    C_NUMCOLORS        /* just a place-holder */
85
} Colors ;
86
87
char *defcolors = "4x5x2x3x1x464301060203";
88
89
static int colors[C_NUMCOLORS][2];
90
70
void
91
void
71
printscol(dp)
92
printscol(dp)
72
	DISPLAY *dp;
93
	DISPLAY *dp;
Lines 127-136 Link Here
127
			printtime(sp->st_ctime);
148
			printtime(sp->st_ctime);
128
		else
149
		else
129
			printtime(sp->st_mtime);
150
			printtime(sp->st_mtime);
151
		if (f_color)
152
			(void)colortype(sp->st_mode);
130
		if (f_octal || f_octal_escape) (void)prn_octal(p->fts_name);
153
		if (f_octal || f_octal_escape) (void)prn_octal(p->fts_name);
131
		else (void)printf("%s", p->fts_name);
154
		else (void)printf("%s", p->fts_name);
132
		if (f_type)
155
		if (f_type)
133
			(void)printtype(sp->st_mode);
156
			(void)printtype(sp->st_mode);
157
		if (f_color)
158
			(void)printf("\033[m");
134
		if (S_ISLNK(sp->st_mode))
159
		if (S_ISLNK(sp->st_mode))
135
			printlink(p);
160
			printlink(p);
136
		(void)putchar('\n');
161
		(void)putchar('\n');
Lines 198-203 Link Here
198
			    dp->s_block);
223
			    dp->s_block);
199
			if ((base += numrows) >= num)
224
			if ((base += numrows) >= num)
200
				break;
225
				break;
226
			/*
227
			 * some terminals get confused if we mix tabs
228
			 * with color sequences
229
			 */
230
			if (f_color)
231
				while ((cnt = (chcnt + 1)) <= endcol) {
232
					(void)putchar(' ');
233
					chcnt = cnt;
234
				}
235
			else
201
			while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
236
			while ((cnt = ((chcnt + tabwidth) & ~(tabwidth - 1)))
202
			    <= endcol){
237
			    <= endcol){
203
				(void)putchar(f_notabs ? ' ' : '\t');
238
				(void)putchar(f_notabs ? ' ' : '\t');
Lines 228-235 Link Here
228
	if (f_size)
263
	if (f_size)
229
		chcnt += printf("%*qd ",
264
		chcnt += printf("%*qd ",
230
		    (int)sizefield, howmany(sp->st_blocks, blocksize));
265
		    (int)sizefield, howmany(sp->st_blocks, blocksize));
266
	if (f_color)
267
		(void)colortype(sp->st_mode);
231
	chcnt += (f_octal || f_octal_escape) ? prn_octal(p->fts_name)
268
	chcnt += (f_octal || f_octal_escape) ? prn_octal(p->fts_name)
232
	                                     : printf("%s", p->fts_name);
269
	                                     : printf("%s", p->fts_name);
270
	if (f_color)
271
		printf("\033[m");
233
	if (f_type)
272
	if (f_type)
234
		chcnt += printtype(sp->st_mode);
273
		chcnt += printtype(sp->st_mode);
235
	return (chcnt);
274
	return (chcnt);
Lines 293-298 Link Here
293
	return (0);
332
	return (0);
294
}
333
}
295
334
335
void
336
printcolor(c)
337
       Colors c;
338
{
339
	printf("\033[");
340
	if (colors[c][0] != -1) {
341
		printf("3%d", colors[c][0]);
342
		if (colors[c][1] != -1)
343
		    printf(";");
344
	}
345
	if (colors[c][1] != -1)
346
	    printf("4%d", colors[c][1]);
347
	printf("m");
348
}
349
350
int
351
colortype(mode)
352
       mode_t mode;
353
{
354
	switch(mode & S_IFMT) {
355
	      case S_IFDIR:
356
		if (mode & S_IWOTH)
357
		    if (mode & S_ISTXT)
358
			printcolor(C_WSDIR);
359
		    else
360
			printcolor(C_WDIR);
361
		else
362
		    printcolor(C_DIR);
363
		return(1);
364
	      case S_IFLNK:
365
		printcolor(C_LNK);
366
		return(1);
367
	      case S_IFSOCK:
368
		printcolor(C_SOCK);
369
		return(1);
370
	      case S_IFIFO:
371
		printcolor(C_FIFO);
372
		return(1);
373
	      case S_IFBLK:
374
		printcolor(C_BLK);
375
		return(1);
376
	      case S_IFCHR:
377
		printcolor(C_CHR);
378
		return(1);
379
	}
380
	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
381
		if (mode & S_ISUID)
382
		    printcolor(C_SUID);
383
		else if (mode & S_ISGID)
384
		    printcolor(C_SGID);
385
		else
386
		    printcolor(C_EXEC);
387
		return(1);
388
	}
389
	return(0);
390
}
391
392
void
393
parsecolors(cs)
394
char *cs;
395
{
396
	int i, j, len;
397
	char c[2];
398
	if (cs == NULL)    cs = ""; /* LSCOLORS not set */
399
	len = strlen(cs);
400
	for (i = 0 ; i < C_NUMCOLORS ; i++) {
401
		if (len <= 2*i) {
402
			c[0] = defcolors[2*i];
403
			c[1] = defcolors[2*i+1];
404
		}
405
		else {
406
			c[0] = cs[2*i];
407
			c[1] = cs[2*i+1];
408
		}
409
		for (j = 0 ; j < 2 ; j++) {
410
			if ((c[j] < '0' || c[j] > '7') &&
411
			    tolower(c[j]) != 'x') {
412
				fprintf(stderr,
413
					"error: invalid character '%c' in LSCOLORS env var\n",
414
					c[j]);
415
				c[j] = defcolors[2*i+j];
416
			}
417
			if (c[j] == 'x')
418
			    colors[i][j] = -1;
419
			else
420
			    colors[i][j] = c[j]-'0';
421
		}
422
	}
423
}
424
 
296
static void
425
static void
297
printlink(p)
426
printlink(p)
298
	FTSENT *p;
427
	FTSENT *p;
(-)./extern.h (+2 lines)
Lines 51-53 Link Here
51
void	 usage __P((void));
51
void	 usage __P((void));
52
int	 len_octal __P((char *, int));
52
int	 len_octal __P((char *, int));
53
int	 prn_octal __P((char *));
53
int	 prn_octal __P((char *));
54
void	 parsecolors __P((char *cs));
55
int	 colortype __P((mode_t mode));
(-)./ls.h (+1 lines)
Lines 53-58 Link Here
53
extern int f_statustime;	/* use time of last mode change */
53
extern int f_statustime;	/* use time of last mode change */
54
extern int f_notabs;		/* don't use tab-separated multi-col output */
54
extern int f_notabs;		/* don't use tab-separated multi-col output */
55
extern int f_type;		/* add type character for non-regular files */
55
extern int f_type;		/* add type character for non-regular files */
56
extern int f_color;		/* add type in color for non-regular files */
56
57
57
typedef struct {
58
typedef struct {
58
	FTSENT *list;
59
	FTSENT *list;
(-)./ls.1 (-1 / +75 lines)
Lines 43-49 Link Here
43
.Nd list directory contents
43
.Nd list directory contents
44
.Sh SYNOPSIS
44
.Sh SYNOPSIS
45
.Nm ls
45
.Nm ls
46
.Op Fl ABCFHLPRTWabcdfgikloqrstu1
46
.Op Fl ABCFGHLPRTWabcdfgikloqrstu1
47
.Op Ar file ...
47
.Op Ar file ...
48
.Sh DESCRIPTION
48
.Sh DESCRIPTION
49
For each operand that names a
49
For each operand that names a
Lines 90-95 Link Here
90
a percent sign (%) after each whiteout,
90
a percent sign (%) after each whiteout,
91
and a vertical bar (|) after each that is a
91
and a vertical bar (|) after each that is a
92
.Tn FIFO .
92
.Tn FIFO .
93
.It Fl G
94
Use ANSI color sequences to distinguish file types. (See
95
.Ev LSCOLORS
96
below.) In addition to those mentioned above in
97
.Fl F ,
98
some extra attributes (setuid bit set, etc.) are also displayed.
93
.It Fl H
99
.It Fl H
94
Symbolic links on the command line are followed.  This option is assumed if
100
Symbolic links on the command line are followed.  This option is assumed if
95
none of the
101
none of the
Lines 378-383 Link Here
378
See
384
See
379
.Xr environ 7
385
.Xr environ 7
380
for more information.
386
for more information.
387
.It LSCOLORS
388
The value of this variable describes what color to use for which
389
attribute when the color output
390
.Pq Fl G
391
is specified.  This string is a concatenation of pairs of the format
392
.Sy fb ,
393
where
394
.Sy f
395
is the foreground color and
396
.Sy b
397
is the background color.
398
.Pp
399
The color designators are as follows:
400
.Pp
401
.Bl -tag -width 4n -offset indent -compact
402
.It Sy 0
403
black
404
.It Sy 1
405
red
406
.It Sy 2
407
green
408
.It Sy 3
409
yellow
410
.It Sy 4
411
blue
412
.It Sy 5
413
magenta
414
.It Sy 6
415
cyan
416
.It Sy 7
417
white
418
.It Sy x
419
default foreground or background
420
.El
421
.Pp
422
(Note: the above are standard ANSI colors.  The actual display may
423
differ depending on the color capabilities of your terminal.)
424
.Pp
425
The order of the attributes are as follows:
426
.Pp
427
.Bl -enum -offset indent -compact
428
.It
429
directory
430
.It
431
symbolic link
432
.It
433
socket
434
.It
435
pipe
436
.It
437
executable
438
.It
439
block special
440
.It
441
character special
442
.It
443
executable with setuid bit set
444
.It
445
executable with setgid bit set
446
.It
447
directory writable to others, with sticky bit
448
.It
449
directory writable to others, without sticky bit
450
.El
451
.Pp
452
The default is "4x5x2x3x1x464301060203", i.e., blue foreground and
453
default background for regular directories, black foreground and red
454
background for setuid executables, etc.
381
.It Ev LS_COLWIDTHS
455
.It Ev LS_COLWIDTHS
382
If this variable is set, it is considered to be a
456
If this variable is set, it is considered to be a
383
colon-delimited list of minimum column widths.  Unreasonable
457
colon-delimited list of minimum column widths.  Unreasonable

Return to bug 18900