import less(1)
[unleashed/tickless.git] / usr / src / lib / libast / common / path / pathkey.c
blobbc153ea7c8b614254e14462f8ec1cee9a71c1a64
1 /***********************************************************************
2 * *
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
8 * *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12 * *
13 * Information and Software Systems Research *
14 * AT&T Research *
15 * Florham Park NJ *
16 * *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
20 * *
21 ***********************************************************************/
22 #pragma prototyped
24 * Glenn Fowler
25 * AT&T Bell Laboratories
27 * generate 14 char lookup key for lang path in key
28 * based on 32-bit checksum on path
30 * if key==0 then space is malloc'd
31 * if attr != 0 then attribute var assignments placed here:
32 * ATTRIBUTES list of attribute names
35 #include <ast.h>
36 #include <ctype.h>
37 #include <fs3d.h>
38 #include <preroot.h>
40 char*
41 pathkey(char* key, char* attr, const char* lang, const char* tool, const char* apath)
43 register char* path = (char*)apath;
44 register char* s;
45 register char* k;
46 char* t;
47 char* flags;
48 char** p;
49 int c;
50 unsigned long n;
51 char buf[15];
52 char* usr[16];
53 char* env[elementsof(usr) + 3];
54 char* ver[2];
55 char tmp[PATH_MAX];
57 static char let[] = "ABCDEFGHIJKLMNOP";
59 if (!key)
60 key = buf;
61 if (tool && streq(tool, "mam"))
63 for (n = 0; *path; path++)
64 n = n * 0x63c63cd9L + *path + 0x9c39c33dL;
65 k = key;
66 for (n &= 0xffffffffL; n; n >>= 4)
67 *k++ = let[n & 0xf];
68 *k = 0;
70 else
72 for (c = 0; c < elementsof(env); c++)
73 env[c] = 0;
74 n = 0;
77 * trailing flags in path
80 if (flags = strchr(path, ' '))
82 if (flags == path)
83 flags = 0;
84 else
86 strcpy(tmp, path);
87 *(flags = tmp + (flags - path)) = 0;
88 path = tmp;
93 * 3D
96 if (fs3d(FS3D_TEST) && (c = mount(path, tmp, FS3D_GET|FS3D_ALL|FS3D_SIZE(PATH_MAX), NiL)) > 1 && c < PATH_MAX)
97 path = tmp;
100 * preroot
103 if (attr)
104 attr = strcopy(attr, "PREROOT='");
105 #if FS_PREROOT
106 if (k = getenv(PR_BASE))
108 if (s = strrchr(k, '/'))
109 k = s + 1;
110 n = memsum(k, strlen(k), n);
112 if (attr && (getpreroot(attr, path) || getpreroot(attr, NiL)))
113 attr += strlen(attr);
114 #else
115 if ((k = getenv("VIRTUAL_ROOT")) && *k == '/')
117 n = memsum(k, strlen(k), n);
118 if (attr)
119 attr = strcopy(attr, k);
121 #endif
124 * universe
127 if (attr)
128 attr = strcopy(attr, "' UNIVERSE='");
129 if (k = astconf("UNIVERSE", NiL, NiL))
131 n = memsum(k, strlen(k), n);
132 if (attr)
133 attr = strcopy(attr, k);
137 * environment
139 * ${PROBE_ATTRIBUTES} || ${VERSION_ENVIRONMENT} : list of alternate env vars
140 * ${VERSION_ENVIRONMENT} : list of alternate env vars
141 * ${VERSION_<lang>}
142 * ${VERSION_<base(path)>}
143 * ${<toupper(base(path))>VER}
144 * ${OBJTYPE}
147 if (attr)
148 *attr++ = '\'';
149 c = 0;
150 usr[c++] = "OBJTYPE";
151 if (!(k = getenv("PROBE_ATTRIBUTES")))
152 k = getenv("VERSION_ENVIRONMENT");
153 if (k)
154 while (c < (elementsof(usr) - 1))
156 while (*k && (*k == ':' || *k == ' '))
157 k++;
158 if (!*k)
159 break;
160 usr[c++] = k;
161 while (*k && *k != ':' && *k != ' ')
162 k++;
164 usr[c] = 0;
165 ver[0] = (char*)lang;
166 ver[1] = k = (s = strrchr(path, '/')) ? s + 1 : path;
167 s = buf;
168 if (isdigit(*k))
170 if (*k == '3' && *(k + 1) == 'b')
173 * cuteness never pays
176 k += 2;
177 *s++ = 'B';
178 *s++ = 'B';
179 *s++ = 'B';
181 else
182 *s++ = 'U';
184 for (; (c = *k) && s < &buf[sizeof(buf) - 1]; k++)
186 if (!isalnum(c))
187 c = '_';
188 else if (islower(c))
189 c = toupper(c);
190 *s++ = c;
192 *s = 0;
193 for (p = environ; *p; p++)
195 s = "VERSION_";
196 for (k = *p; *k && *k == *s; k++, s++);
197 if (*k && !*s)
199 for (c = 0; c < elementsof(ver); c++)
200 if (!env[c] && (s = ver[c]))
202 for (t = k; *t && *t != '=' && *t++ == *s; s++);
203 if (*t == '=' && (!*s || (s - ver[c]) > 1))
205 env[c] = *p;
206 goto found;
210 if (!env[2])
212 s = buf;
213 for (k = *p; *k && *s++ == *k; k++);
214 if ((s - buf) > 2 && k[0] == 'V' && k[1] == 'E' && k[2] == 'R' && k[3] == '=')
216 env[2] = *p;
217 goto found;
220 for (c = 0; c < elementsof(usr) && (s = usr[c]); c++)
221 if (!env[c + elementsof(env) - elementsof(usr)])
223 for (k = *p; *k && *k == *s; k++, s++);
224 if (*k == '=' && (!*s || *s == ':' || *s == ' '))
226 env[c + elementsof(env) - elementsof(usr)] = *p;
227 goto found;
230 found: ;
232 for (c = 0; c < elementsof(env); c++)
233 if (k = env[c])
235 if (attr)
237 *attr++ = ' ';
238 while ((*attr++ = *k++) != '=');
239 *attr++ = '\'';
240 attr = strcopy(attr, k);
241 *attr++ = '\'';
243 else
244 while (*k && *k++ != '=');
245 n = memsum(k, strlen(k), n);
247 if (attr)
249 attr = strcopy(attr, " ATTRIBUTES='PREROOT UNIVERSE");
250 for (c = 0; c < elementsof(env); c++)
251 if (k = env[c])
253 *attr++ = ' ';
254 while ((*attr = *k++) != '=')
255 attr++;
257 *attr++ = '\'';
258 *attr = 0;
262 * now the normal stuff
265 if (flags)
266 *flags = ' ';
267 s = path + strlen(path);
268 sfsprintf(key, 15, "%08lX", memsum(path, s - path, n));
269 k = key + 14;
270 *k = 0;
271 if (!flags)
272 t = path;
273 else if ((t = s - 4) < flags)
274 t = flags + 1;
275 for (;;)
277 if (--s < t)
279 if (t == path)
280 break;
281 s = flags - 2;
282 t = path;
284 if (*s != '/' && *s != ' ')
286 *--k = *s;
287 if (k <= key + 8)
288 break;
291 while (k > key + 8)
292 *--k = '.';
294 return key == buf ? strdup(key) : key;