|
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; |