-
Notifications
You must be signed in to change notification settings - Fork 4
/
rehash.c
154 lines (134 loc) · 2.69 KB
/
rehash.c
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
/*
rehash.c of gulam -- rehash executable file names 08/03/86
copyright (c) 1987 pm@cwru.edu
*/
#include "ue.h"
/* uses a table created by tbl.c:
key == leaf name of the executable
elm == full path name, incl drive
*/
static struct TBLE *hp[2];
static int hx;
/* see if fnm p is in hash table hp[0], or hp[1] and return its full
path */
uchar *hashlookup(int i, uchar *p)
{
struct TBLE *h;
h = tblfind(hp[i], p);
return h ? h->elm : NULL;
}
/* Add to hash table: prg-name, full pathname full path == dir | pnm
*/
static void addname(uchar *dir, uchar *pnm)
{
uchar *p;
uchar *nm;
uchar *pt;
pt = str3cat(dir, DS0, pnm);
nm = gstrdup(pnm);
if (nm == NULL || pt == NULL)
return;
/* freeing? forget it! */
deleteext(nm, p); /* delete extensions (in some OS) */
tblinsert(hp[hx], nm, pt);
}
/* find exec files in dir p; dont alter p */
static void findexecs(uchar *p)
{
uchar *q;
uchar *pbs;
uchar *cwd;
int n;
if (p[0] == '.' && p[1] == '\0')
hx = 0;
if (hp[hx] == NULL)
hp[hx] = tblcreate();
cwd = gfgetcwd();
cd(p);
if (emsg || (valu < 0))
goto freecwd;
wls(0); /* don't use wlstbl */
if ((q = strg) != NULL)
{
pbs = p + strlen(p) - 1;
if (*pbs != DSC)
pbs = NULL;
else
*pbs = '\0';
n = executables(q);
for (; *q && (n-- > 0); q += strlen(q) + 1)
addname(p, q);
gfree(strg); /* got this from wls(0) */
strg = NULL; /* ow we will try to output it */
if (pbs)
*pbs = DSC;
}
freecwd:if (cwd)
{
cd(cwd);
gfree(cwd);
}
emsg = NULL;
valu = 0L;
}
void which(int f)
{
WS *ws;
ws = initws();
strwcat(ws, tblstr(hp[0], f), 0);
strwcat(ws, "\r\n** cwd **\r\n", 0);
strwcat(ws, tblstr(hp[1], f), 0);
if (ws)
strg = ws->ps;
gfree(ws);
}
void cmdwhich(uchar *arg)
{
uchar *p;
uchar *q;
struct TBLE *a;
int i;
UNUSED(arg);
p = lexgetword();
if (*p)
{
for (q = NULL, i = 0; i < 2; i++)
if ((a = tblfind(hp[i], p)) != NULL)
q = sprintp("%s is external cmd %s", p, a->elm);
if (q == NULL)
q = sprintp("%s is not in hash tbl", p);
strg = gstrdup(q);
} else
which(1);
}
void rehash(uchar *arg) /* rebuild the table of executable file names */
{
uchar *q;
uchar *p;
int n;
WS *ws;
UNUSED(arg);
for (n = 0; n < 2;)
{
if (hp[n])
tblfree(hp[n]);
hp[n++] = NULL;
}
q = ggetenv("PATH");
if (q == NULL)
return;
hx = 1;
ws = lex(q, COMMA, EMPTY2);
p = ws->ps;
n = ws->ns;
while ((n > 0) && (q = nthstr(p, --n)) != NULL && *q)
{
findexecs(q);
n--; /* skip the ',' separator */
}
if (hx == 1)
{
hp[0] = hp[1];
hp[1] = NULL;
}
}