File: lscolors.c

package info (click to toggle)
yafc 1.1.1.dfsg.1-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,248 kB
  • ctags: 1,679
  • sloc: ansic: 19,338; sh: 10,365; makefile: 155; perl: 38; ruby: 33
file content (136 lines) | stat: -rw-r--r-- 3,388 bytes parent folder | download | duplicates (4)
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
/* $Id: lscolors.c,v 1.5 2003/07/12 10:25:41 mhe Exp $
 *
 * lscolors.c -- parsing of env variable LS_COLORS
 *
 * Yet Another FTP Client
 * Copyright (C) 1998-2001, Martin Hedenfalk <mhe@stacken.kth.se>
 *
 * 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. See COPYING for more details.
 */

#include "syshdr.h"
#include "rfile.h"
#include "xmalloc.h"
#include "strq.h"

typedef struct {
	char *msk;
	char *clr;
} lscolor;

static bool colors_initialized = false;
static int number_of_colors = 0;

/* these are the default colors */
static char *diclr="01;34", *lnclr="01;36", *ficlr="00", *exclr="01;32";
static char *bdclr="40;01;33", *cdclr="40;01;33", *piclr="40;33", *soclr="01;35";
static char *lc="\x1B[", *rc="m", *ec="\x1B[0m";

static lscolor *clrs;

char *endcolor(void)
{
	return ec;
}

void init_colors(void)
{
	int i = 0;
	char *p, *e, *q, *org;
	unsigned int n;

	if(colors_initialized)
		return;

	p = getenv("LS_COLORS");
	if(!p)
		p = getenv("LS_COLOURS");

	if(p && *p) {
		org = p = xstrdup(p);
		n = strqnchr(p, ':')+2;
		clrs = (lscolor *)xmalloc(n * sizeof(lscolor));
		e = strqsep(&p, ':');
		while(e && *e) {
			q = strqsep(&e, '=');
			clrs[i].msk = xstrdup(q);
			q = strqsep(&e, ':');
			clrs[i].clr = xstrdup(q);

			e = strqsep(&p, ':');

			if(strcmp(clrs[i].msk, "di")==0) /* directory */
				diclr = clrs[i].clr;
			else if(strcmp(clrs[i].msk, "ln")==0) /* link */
				lnclr = clrs[i].clr;
			else if(strcmp(clrs[i].msk, "fi")==0) /* normal file */
				ficlr = clrs[i].clr;
			else if(strcmp(clrs[i].msk, "ex")==0) /* executable */
				exclr = clrs[i].clr;
			else if(strcmp(clrs[i].msk, "bd")==0) /* block device */
				bdclr = clrs[i].clr;
			else if(strcmp(clrs[i].msk, "cd")==0) /* character device */
				cdclr = clrs[i].clr;
			else if(strcmp(clrs[i].msk, "pi")==0) /* pipe */
				piclr = clrs[i].clr;
			else if(strcmp(clrs[i].msk, "so")==0) /* socket */
				soclr = clrs[i].clr;
			else if(strcmp(clrs[i].msk, "lc") == 0) /* left code */
				lc = clrs[i].clr;
			else if(strcmp(clrs[i].msk, "rc") == 0) /* right code */
				rc = clrs[i].clr;
			else if(strcmp(clrs[i].msk, "ec") == 0) /* end code (lc+fi+rc) */
				ec = clrs[i].clr;
			else
				i++;
		}
		number_of_colors = i;
		free(org);
		if(!ec) {
			ec = xmalloc(strlen(lc)+strlen(ficlr)+strlen(rc)+1);
			sprintf(ec, "%s%s%s", lc, ficlr, rc);
		}
	}
}

void rfile_parse_colors(rfile *f)
{
	char *e, *q;
	int i;

	/* colors should already been initialized by init_colors */

	e = 0;
	if(risdir(f))
		e = diclr;
	else if(rislink(f))
		e = lnclr;
	else if(rischardev(f))
		e = cdclr;
	else if(risblockdev(f))
		e = bdclr;
	else if(rispipe(f))
		e = piclr;
	else if(rissock(f))
		e = soclr;

	/* do this before checking for executable, because
	 * on [v]fat filesystems, all files are 'executable'
	 */
	for(i=0;i<number_of_colors && !e;i++) {
		if(fnmatch(clrs[i].msk, base_name_ptr(f->path), 0) == 0)
			e = clrs[i].clr;
	}

	/* a file is considered executable if there is
	 * an 'x' anywhere in its permission string */
	if(!e && risexec(f))
		e = exclr;

	q = e ? e : ficlr;
	f->color = (char *)xmalloc(strlen(lc) + strlen(q) + strlen(rc) + 1);
	sprintf(f->color, "%s%s%s", lc, q, rc);
}