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