summaryrefslogtreecommitdiff
path: root/usr/src/cmd/make/lib/vroot/vroot.cc
blob: 785a0bc638c2fd86046c1c5700017ef3c776d18f (plain)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
 * Use is subject to license terms.
 */


#include <stdlib.h>
#include <string.h>

#include <vroot/vroot.h>
#include <vroot/args.h>

#include <string.h>
#include <sys/param.h>
#include <sys/file.h>

typedef struct {
	short		init;
	pathpt		vector;
	const char	*env_var;
} vroot_patht;

typedef struct {
	vroot_patht	vroot;
	vroot_patht	path;
	char		full_path[MAXPATHLEN+1];
	char		*vroot_start;
	char		*path_start;
	char		*filename_start;
	int		scan_vroot_first;
	int		cpp_style_path;
} vroot_datat, *vroot_datapt;

static vroot_datat	vroot_data= {
	{ 0, NULL, "VIRTUAL_ROOT"},
	{ 0, NULL, "PATH"},
	"", NULL, NULL, NULL, 0, 1};

void
add_dir_to_path(const char *path, register pathpt *pointer, register int position)
{
	register int		size= 0;
	register int		length;
	register char		*name;
	register pathcellpt	p;
	pathpt			new_path;

	if (*pointer != NULL) {
		for (p= &((*pointer)[0]); p->path != NULL; p++, size++);
		if (position < 0)
			position= size;}
	else
		if (position < 0)
			position= 0;
	if (position >= size) {
		new_path= (pathpt)calloc((unsigned)(position+2), sizeof(pathcellt));
		if (*pointer != NULL) {
			memcpy((char *)new_path,(char *)(*pointer),  size*sizeof(pathcellt));
			free((char *)(*pointer));};
		*pointer= new_path;};
	length= strlen(path);
	name= (char *)malloc((unsigned)(length+1));
	(void)strcpy(name, path);
	if ((*pointer)[position].path != NULL)
		free((*pointer)[position].path);
	(*pointer)[position].path= name;
	(*pointer)[position].length= length;
}

pathpt
parse_path_string(register char *string, register int remove_slash)
{
	register char		*p;
	pathpt			result= NULL;

	if (string != NULL)
		for (; 1; string= p+1) {
			if (p= strchr(string, ':')) *p= 0;
			if ((remove_slash == 1) && !strcmp(string, "/"))
				add_dir_to_path("", &result, -1);
			else
				add_dir_to_path(string, &result, -1);
			if (p) *p= ':';
			else return(result);};
	return((pathpt)NULL);
}

const char *
get_vroot_name(void)
{
	return(vroot_data.vroot.env_var);
}

const char *
get_path_name(void)
{
	return(vroot_data.path.env_var);
}

void
flush_path_cache(void)
{
	vroot_data.path.init= 0;
}

void
flush_vroot_cache(void)
{
	vroot_data.vroot.init= 0;
}

void
scan_path_first(void)
{
	vroot_data.scan_vroot_first= 0;
}

void
scan_vroot_first(void)
{
	vroot_data.scan_vroot_first= 1;
}

void
set_path_style(int style)
{
	vroot_data.cpp_style_path= style;
}

char *
get_vroot_path(register char **vroot, register char **path, register char **filename)
{
	if (vroot != NULL) {
		if ((*vroot= vroot_data.vroot_start) == NULL)
		if ((*vroot= vroot_data.path_start) == NULL)
		*vroot= vroot_data.filename_start;};
	if (path != NULL) {
		if ((*path= vroot_data.path_start) == NULL)
		*path= vroot_data.filename_start;};
	if (filename != NULL)
		*filename= vroot_data.filename_start;
	return(vroot_data.full_path);
}

void
translate_with_thunk(register char *filename, int (*thunk) (char *), pathpt path_vector, pathpt vroot_vector, rwt rw)
{
	register pathcellt	*vp;
	pathcellt		*pp;
	register pathcellt	*pp1;
	register char		*p;
	int			flags[256];

/* Setup path to use */
	if (rw == rw_write)
		pp1= NULL;		/* Do not use path when writing */
	else {
		if (path_vector == VROOT_DEFAULT) {
			if (!vroot_data.path.init) {
				vroot_data.path.init= 1;
				vroot_data.path.vector= parse_path_string(getenv(vroot_data.path.env_var), 0);};
			path_vector= vroot_data.path.vector;};
		pp1= path_vector == NULL ? NULL : &(path_vector)[0];};

/* Setup vroot to use */
	if (vroot_vector == VROOT_DEFAULT) {
		if (!vroot_data.vroot.init) {
			vroot_data.vroot.init= 1;
			vroot_data.vroot.vector= parse_path_string(getenv(vroot_data.vroot.env_var), 1);};
		vroot_vector= vroot_data.vroot.vector;};
	vp= vroot_vector == NULL ? NULL : &(vroot_vector)[0];

/* Setup to remember pieces */
	vroot_data.vroot_start= NULL;
	vroot_data.path_start= NULL;
	vroot_data.filename_start= NULL;

	int flen = strlen(filename);
	if(flen >= MAXPATHLEN) {
		errno = ENAMETOOLONG;
		return;
	}

	switch ((vp ?1:0) + (pp1 ? 2:0)) {
	    case 0:	/* No path. No vroot. */
	    use_name:
		(void)strcpy(vroot_data.full_path, filename);
		vroot_data.filename_start= vroot_data.full_path;
		(void)(*thunk)(vroot_data.full_path);
		return;
	    case 1:	/* No path. Vroot */
		if (filename[0] != '/') goto use_name;
		for (; vp->path != NULL; vp++) {
			if((1 + flen + vp->length) >= MAXPATHLEN) {
				errno = ENAMETOOLONG;
				continue;
			}
			p= vroot_data.full_path;
			(void)strcpy(vroot_data.vroot_start= p, vp->path);
			p+= vp->length;
			(void)strcpy(vroot_data.filename_start= p, filename);
			if ((*thunk)(vroot_data.full_path)) return;};
		(void)strcpy(vroot_data.full_path, filename);
		return;
	    case 2:	/* Path. No vroot. */
		if (vroot_data.cpp_style_path) {
			if (filename[0] == '/') goto use_name;
		} else {
			if (strchr(filename, '/') != NULL) goto use_name;
		};
		for (; pp1->path != NULL; pp1++) {
			p= vroot_data.full_path;
			if((1 + flen + pp1->length) >= MAXPATHLEN) {
				errno = ENAMETOOLONG;
				continue;
			}
			if (vroot_data.cpp_style_path) {
				(void)strcpy(vroot_data.path_start= p, pp1->path);
				p+= pp1->length;
				*p++= '/';
			} else {
				if (pp1->length != 0) {
					(void)strcpy(vroot_data.path_start= p,
					    pp1->path);
					p+= pp1->length;
					*p++= '/';
				};
			};
			(void)strcpy(vroot_data.filename_start= p, filename);
			if ((*thunk)(vroot_data.full_path)) return;};
		(void)strcpy(vroot_data.full_path, filename);
		return;
	    case 3: {	/* Path. Vroot. */
			int *rel_path, path_len= 1;
		if (vroot_data.scan_vroot_first == 0) {
			for (pp= pp1; pp->path != NULL; pp++) path_len++;
			rel_path= flags;
			for (path_len-= 2; path_len >= 0; path_len--) rel_path[path_len]= 0;
			for (; vp->path != NULL; vp++)
				for (pp= pp1, path_len= 0; pp->path != NULL; pp++, path_len++) {
					int len = 0;
					if (rel_path[path_len] == 1) continue;
					if (pp->path[0] != '/') rel_path[path_len]= 1;
					p= vroot_data.full_path;
					if ((filename[0] == '/') || (pp->path[0] == '/')) {
						if(vp->length >= MAXPATHLEN) {
							errno = ENAMETOOLONG;
							continue;
						}
						(void)strcpy(vroot_data.vroot_start= p, vp->path); p+= vp->length;
						len += vp->length;
					};
					if (vroot_data.cpp_style_path) {
						if (filename[0] != '/') {
							if(1 + len + pp->length >= MAXPATHLEN) {
								errno = ENAMETOOLONG;
								continue;
							}
							(void)strcpy(vroot_data.path_start= p, pp->path); p+= pp->length;
							*p++= '/';
							len += 1 + pp->length;
						};
					} else {
						if (strchr(filename, '/') == NULL) {
							if (pp->length != 0) {
								if(1 + len + pp->length >= MAXPATHLEN) {
									errno = ENAMETOOLONG;
									continue;
								}
								(void)strcpy(vroot_data.path_start= p,
								    pp->path);
								p+= pp->length;
								*p++= '/';
								len += 1 + pp->length;
							}
						}
					};
					(void)strcpy(vroot_data.filename_start= p, filename);
					if ((*thunk)(vroot_data.full_path)) return;};}
		else { pathcellt *vp1= vp;
			for (pp= pp1, path_len= 0; pp->path != NULL; pp++, path_len++)
				for (vp= vp1; vp->path != NULL; vp++) {
					int len = 0;
					p= vroot_data.full_path;
					if ((filename[0] == '/') || (pp->path[0] == '/')) {
						if(vp->length >= MAXPATHLEN) {
							errno = ENAMETOOLONG;
							continue;
						}
						(void)strcpy(vroot_data.vroot_start= p, vp->path); p+= vp->length;
						len += vp->length;
					}
					if (vroot_data.cpp_style_path) {
						if (filename[0] != '/') {
							if(1 + len + pp->length >= MAXPATHLEN) {
								errno = ENAMETOOLONG;
								continue;
							}
							(void)strcpy(vroot_data.path_start= p, pp->path); p+= pp->length;
							*p++= '/';
							len += 1 + pp->length;
						}
					} else {
						if (strchr(filename, '/') == NULL) {
							if(1 + len + pp->length >= MAXPATHLEN) {
								errno = ENAMETOOLONG;
								continue;
							}
							(void)strcpy(vroot_data.path_start= p, pp->path); p+= pp->length;
							*p++= '/';
							len += 1 + pp->length;
						}
					}
					(void)strcpy(vroot_data.filename_start= p, filename);
					if ((*thunk)(vroot_data.full_path)) return;};};
		(void)strcpy(vroot_data.full_path, filename);
		return;};};
}