xref: /illumos-gate/usr/src/contrib/ast/src/lib/libast/path/pathaccess.c (revision b30d193948be5a7794d7ae3ba0ed9c2f72c88e0f)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2011 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                 Eclipse Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *          http://www.eclipse.org/org/documents/epl-v10.html           *
11 *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
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 #define _AST_API_H	1
36 
37 #include <ast.h>
38 
39 char*
pathaccess(char * path,const char * dirs,const char * a,const char * b,int mode)40 pathaccess(char* path, const char* dirs, const char* a, const char* b, int mode)
41 {
42 	return pathaccess_20100601(dirs, a, b, mode, path, PATH_MAX);
43 }
44 
45 #undef	_AST_API_H
46 
47 #include <ast_api.h>
48 
49 char*
pathaccess_20100601(register const char * dirs,const char * a,const char * b,register int mode,register char * path,size_t size)50 pathaccess_20100601(register const char* dirs, const char* a, const char* b, register int mode, register char* path, size_t size)
51 {
52 	int		sib = a && a[0] == '.' && a[1] == '.' && a[2] == 0;
53 	int		sep = ':';
54 	char		cwd[PATH_MAX];
55 
56 	do
57 	{
58 		dirs = pathcat(dirs, sep, a, b, path, size);
59 		pathcanon(path, size, 0);
60 		if ((!sib || *path == '/') && pathexists(path, mode))
61 		{
62 			if (*path == '/' || !(mode & PATH_ABSOLUTE))
63 				return path;
64 			dirs = getcwd(cwd, sizeof(cwd));
65 			sep = 0;
66 		}
67 	} while (dirs);
68 	return 0;
69 }
70