xref: /titanic_44/usr/src/lib/libast/common/path/pathaccess.c (revision 8339b41da2395f0525c46ceedfeb01961893ec44)
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
23 /*
24  * Glenn Fowler
25  * AT&T Bell Laboratories
26  *
27  * return path to file a/b with access mode using : separated dirs
28  * both a and b may be 0
29  * if a==".." then relative paths in dirs are ignored
30  * if (mode&PATH_REGULAR) then path must not be a directory
31  * if (mode&PATH_ABSOLUTE) then path must be rooted
32  * path returned in path buffer
33  */
34 
35 #include <ast.h>
36 
37 char*
38 pathaccess(register char* path, register const char* dirs, const char* a, const char* b, register int mode)
39 {
40 	int		sib = a && a[0] == '.' && a[1] == '.' && a[2] == 0;
41 	int		sep = ':';
42 	char		cwd[PATH_MAX];
43 
44 	do
45 	{
46 		dirs = pathcat(path, dirs, sep, a, b);
47 		pathcanon(path, 0);
48 		if ((!sib || *path == '/') && pathexists(path, mode))
49 		{
50 			if (*path == '/' || !(mode & PATH_ABSOLUTE))
51 				return path;
52 			dirs = getcwd(cwd, sizeof(cwd));
53 			sep = 0;
54 		}
55 	} while (dirs);
56 	return 0;
57 }
58