1*4887Schin /***********************************************************************
2*4887Schin *                                                                      *
3*4887Schin *               This software is part of the ast package               *
4*4887Schin *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5*4887Schin *                      and is licensed under the                       *
6*4887Schin *                  Common Public License, Version 1.0                  *
7*4887Schin *                      by AT&T Knowledge Ventures                      *
8*4887Schin *                                                                      *
9*4887Schin *                A copy of the License is available at                 *
10*4887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11*4887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*4887Schin *                                                                      *
13*4887Schin *              Information and Software Systems Research               *
14*4887Schin *                            AT&T Research                             *
15*4887Schin *                           Florham Park NJ                            *
16*4887Schin *                                                                      *
17*4887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
18*4887Schin *                  David Korn <dgk@research.att.com>                   *
19*4887Schin *                   Phong Vo <kpv@research.att.com>                    *
20*4887Schin *                                                                      *
21*4887Schin ***********************************************************************/
22*4887Schin #pragma prototyped
23*4887Schin /*
24*4887Schin  * Glenn Fowler
25*4887Schin  * AT&T Research
26*4887Schin  *
27*4887Schin  * return full path to p with mode access using $PATH
28*4887Schin  * a!=0 enables related root search
29*4887Schin  * a!=0 && a!="" searches a dir first
30*4887Schin  * the related root must have a bin subdir
31*4887Schin  * p==0 sets the cached relative dir to a
32*4887Schin  * full path returned in path buffer
33*4887Schin  * if path==0 then the space is malloc'd
34*4887Schin  */
35*4887Schin 
36*4887Schin #include <ast.h>
37*4887Schin 
38*4887Schin char*
39*4887Schin pathpath(register char* path, const char* p, const char* a, int mode)
40*4887Schin {
41*4887Schin 	register char*	s;
42*4887Schin 	char*		x;
43*4887Schin 	char		buf[PATH_MAX];
44*4887Schin 
45*4887Schin 	static char*	cmd;
46*4887Schin 
47*4887Schin 	if (!path)
48*4887Schin 		path = buf;
49*4887Schin 	if (!p)
50*4887Schin 	{
51*4887Schin 		if (cmd)
52*4887Schin 			free(cmd);
53*4887Schin 		cmd = a ? strdup(a) : (char*)0;
54*4887Schin 		return 0;
55*4887Schin 	}
56*4887Schin 	if (strlen(p) < PATH_MAX)
57*4887Schin 	{
58*4887Schin 		strcpy(path, p);
59*4887Schin 		if (pathexists(path, mode))
60*4887Schin 		{
61*4887Schin 			if (*p != '/' && (mode & PATH_ABSOLUTE))
62*4887Schin 			{
63*4887Schin 				getcwd(buf, sizeof(buf));
64*4887Schin 				s = buf + strlen(buf);
65*4887Schin 				sfsprintf(s, sizeof(buf) - (s - buf), "/%s", p);
66*4887Schin 				if (path != buf)
67*4887Schin 					strcpy(path, buf);
68*4887Schin 			}
69*4887Schin 			return (path == buf) ? strdup(path) : path;
70*4887Schin 		}
71*4887Schin 	}
72*4887Schin 	if (*p == '/')
73*4887Schin 		a = 0;
74*4887Schin 	else if (s = (char*)a)
75*4887Schin 	{
76*4887Schin 		x = s;
77*4887Schin 		if (strchr(p, '/'))
78*4887Schin 		{
79*4887Schin 			a = p;
80*4887Schin 			p = "..";
81*4887Schin 		}
82*4887Schin 		else
83*4887Schin 			a = 0;
84*4887Schin 		if ((!cmd || *cmd) && (strchr(s, '/') || (s = cmd)))
85*4887Schin 		{
86*4887Schin 			if (!cmd && *s == '/')
87*4887Schin 				cmd = strdup(s);
88*4887Schin 			if (strlen(s) < (sizeof(buf) - 6))
89*4887Schin 			{
90*4887Schin 				s = strcopy(path, s);
91*4887Schin 				for (;;)
92*4887Schin 				{
93*4887Schin 					do if (s <= path) goto normal; while (*--s == '/');
94*4887Schin 					do if (s <= path) goto normal; while (*--s != '/');
95*4887Schin 					strcpy(s + 1, "bin");
96*4887Schin 					if (pathexists(path, PATH_EXECUTE))
97*4887Schin 					{
98*4887Schin 						if (s = pathaccess(path, path, p, a, mode))
99*4887Schin 							return path == buf ? strdup(s) : s;
100*4887Schin 						goto normal;
101*4887Schin 					}
102*4887Schin 				}
103*4887Schin 			normal: ;
104*4887Schin 			}
105*4887Schin 		}
106*4887Schin 	}
107*4887Schin 	x = !a && strchr(p, '/') ? "" : pathbin();
108*4887Schin 	if (!(s = pathaccess(path, x, p, a, mode)) && !*x && (x = getenv("FPATH")))
109*4887Schin 		s = pathaccess(path, x, p, a, mode);
110*4887Schin 	return (s && path == buf) ? strdup(s) : s;
111*4887Schin }
112