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
|
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1985-2009 AT&T Intellectual Property *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.opensource.org/licenses/cpl1.0.txt *
* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* David Korn <dgk@research.att.com> *
* Phong Vo <kpv@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
/*
* Glenn Fowler
* AT&T Research
*
* include style search support
*/
#include <ast.h>
#include <error.h>
#include <ls.h>
#define directory(p,s) (stat((p),(s))>=0&&S_ISDIR((s)->st_mode))
#define regular(p,s) (stat((p),(s))>=0&&(S_ISREG((s)->st_mode)||streq(p,"/dev/null")))
typedef struct Dir_s /* directory list element */
{
struct Dir_s* next; /* next in list */
char dir[1]; /* directory path */
} Dir_t;
static struct /* directory list state */
{
Dir_t* head; /* directory list head */
Dir_t* tail; /* directory list tail */
} state;
/*
* append dir to pathfind() include list
*/
int
pathinclude(const char* dir)
{
register Dir_t* dp;
struct stat st;
if (dir && *dir && !streq(dir, ".") && directory(dir, &st))
{
for (dp = state.head; dp; dp = dp->next)
if (streq(dir, dp->dir))
return 0;
if (!(dp = oldof(0, Dir_t, 1, strlen(dir))))
return -1;
strcpy(dp->dir, dir);
dp->next = 0;
if (state.tail)
state.tail = state.tail->next = dp;
else
state.head = state.tail = dp;
}
return 0;
}
/*
* return path to name using pathinclude() list
* path placed in <buf,size>
* if lib!=0 then pathpath() attempted after include search
* if type!=0 and name has no '.' then file.type also attempted
* any *: prefix in lib is ignored (discipline library dictionary support)
*/
char*
pathfind(const char* name, const char* lib, const char* type, char* buf, size_t size)
{
register Dir_t* dp;
register char* s;
char tmp[PATH_MAX];
struct stat st;
if (((s = strrchr(name, '/')) || (s = (char*)name)) && strchr(s, '.'))
type = 0;
/*
* always check the unadorned path first
* this handles . and absolute paths
*/
if (regular(name, &st))
{
strncopy(buf, name, size);
return buf;
}
if (type)
{
sfsprintf(buf, size, "%s.%s", name, type);
if (regular(buf, &st))
return buf;
}
if (*name == '/')
return 0;
/*
* check the directory of the including file
* on the assumption that error_info.file is properly stacked
*/
if (error_info.file && (s = strrchr(error_info.file, '/')))
{
sfsprintf(buf, size, "%-.*s%s", s - error_info.file + 1, error_info.file, name);
if (regular(buf, &st))
return buf;
if (type)
{
sfsprintf(buf, size, "%-.*s%s%.s", s - error_info.file + 1, error_info.file, name, type);
if (regular(buf, &st))
return buf;
}
}
/*
* check the include dir list
*/
for (dp = state.head; dp; dp = dp->next)
{
sfsprintf(tmp, sizeof(tmp), "%s/%s", dp->dir, name);
if (pathpath(buf, tmp, "", PATH_REGULAR))
return buf;
if (type)
{
sfsprintf(tmp, sizeof(tmp), "%s/%s.%s", dp->dir, name, type);
if (pathpath(buf, tmp, "", PATH_REGULAR))
return buf;
}
}
/*
* finally a lib related search on PATH
*/
if (lib)
{
if (s = strrchr((char*)lib, ':'))
lib = (const char*)s + 1;
sfsprintf(tmp, sizeof(tmp), "lib/%s/%s", lib, name);
if (pathpath(buf, tmp, "", PATH_REGULAR))
return buf;
if (type)
{
sfsprintf(tmp, sizeof(tmp), "lib/%s/%s.%s", lib, name, type);
if (pathpath(buf, tmp, "", PATH_REGULAR))
return buf;
}
}
return 0;
}
|