summaryrefslogtreecommitdiff
path: root/usr/src/lib/libast/common/path/pathcanon.c
blob: 72f4e6b44439f89f4befc285e4824ccdbd2a917e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/***********************************************************************
*                                                                      *
*               This software is part of the ast package               *
*          Copyright (c) 1985-2009 AT&T Intellectual Property          *
*                      and is licensed under the                       *
*                  Common Public License, Version 1.0                  *
*                    by AT&T Intellectual Property                     *
*                                                                      *
*                A copy of the License is available at                 *
*            http://www.opensource.org/licenses/cpl1.0.txt             *
*         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
*                                                                      *
*              Information and Software Systems Research               *
*                            AT&T Research                             *
*                           Florham Park NJ                            *
*                                                                      *
*                 Glenn Fowler <gsf@research.att.com>                  *
*                  David Korn <dgk@research.att.com>                   *
*                   Phong Vo <kpv@research.att.com>                    *
*                                                                      *
***********************************************************************/
#pragma prototyped
/*
 * Glenn Fowler
 * AT&T Research
 *
 * in-place path name canonicalization -- preserves the logical view
 * pointer to trailing 0 in path returned
 *
 *	remove redundant .'s and /'s
 *	move ..'s to the front
 *	/.. preserved (for pdu and newcastle hacks)
 *	FS_3D handles ...
 *	if (flags&PATH_PHYSICAL) then symlinks resolved at each component
 *	if (flags&PATH_DOTDOT) then each .. checked for access
 *	if (flags&PATH_EXISTS) then path must exist at each component
 *	if (flags&PATH_VERIFIED(n)) then first n chars of path exist
 * 
 * longer pathname possible if (flags&PATH_PHYSICAL) or FS_3D ... involved
 * 0 returned on error and if (flags&(PATH_DOTDOT|PATH_EXISTS)) then path
 * will contain the components following the failure point
 */

#include <ast.h>
#include <ls.h>
#include <fs3d.h>
#include <error.h>

char*
pathcanon(char* path, int flags)
{
	register char*	p;
	register char*	r;
	register char*	s;
	register char*	t;
	register int	dots;
	char*		phys;
	char*		v;
	int		loop;
	int		oerrno;
#if defined(FS_3D)
	long		visits = 0;
#endif

	oerrno = errno;
	dots = loop = 0;
	phys = path;
	v = path + ((flags >> 5) & 01777);
	if (*path == '/')
	{
		if (*(path + 1) == '/' && *astconf("PATH_LEADING_SLASHES", NiL, NiL) == '1')
			do path++; while (*path == '/' && *(path + 1) == '/');
		if (!*(path + 1))
			return path + 1;
	}
	p = r = s = t = path;
	for (;;) switch (*t++ = *s++)
	{
	case '.':
		dots++;
		break;
	case 0:
		s--;
		/*FALLTHROUGH*/
	case '/':
		while (*s == '/') s++;
		switch (dots)
		{
		case 1:
			t -= 2;
			break;
		case 2:
			if ((flags & (PATH_DOTDOT|PATH_EXISTS)) == PATH_DOTDOT && (t - 2) >= v)
			{
				struct stat	st;

				*(t - 2) = 0;
				if (stat(phys, &st))
				{
					strcpy(path, s);
					return 0;
				}
				*(t - 2) = '.';
			}
#if PRESERVE_TRAILING_SLASH
			if (t - 5 < r) r = t;
#else
			if (t - 5 < r)
			{
				if (t - 4 == r) t = r + 1;
				else r = t;
			}
#endif
			else for (t -= 5; t > r && *(t - 1) != '/'; t--);
			break;
		case 3:
#if defined(FS_3D)
			{
				char*		x;
				char*		o;
				int		c;

				o = t;
				if ((t -= 5) <= path) t = path + 1;
				c = *t;
				*t = 0;
				if (x = pathnext(phys, s - (*s != 0), &visits))
				{
					r = path;
					if (t == r + 1) x = r;
					v = s = t = x;
				}
				else
				{
					*t = c;
					t = o;
				}
			}
#else
			r = t;
#endif
			break;
		default:
			if ((flags & PATH_PHYSICAL) && loop < 32 && (t - 1) > path)
			{
				int	c;
				char	buf[PATH_MAX];

				c = *(t - 1);
				*(t - 1) = 0;
				dots = pathgetlink(phys, buf, sizeof(buf));
				*(t - 1) = c;
				if (dots > 0)
				{
					loop++;
					strcpy(buf + dots, s - (*s != 0));
					if (*buf == '/') p = r = path;
					v = s = t = p;
					strcpy(p, buf);
				}
				else if (dots < 0 && errno == ENOENT)
				{
					if (flags & PATH_EXISTS)
					{
						strcpy(path, s);
						return 0;
					}
					flags &= ~(PATH_PHYSICAL|PATH_DOTDOT);
				}
				dots = 4;
			}
			break;
		}
		if (dots >= 4 && (flags & PATH_EXISTS) && (t - 1) >= v && (t > path + 1 || t > path && *(t - 1) && *(t - 1) != '/'))
		{
			struct stat	st;

			*(t - 1) = 0;
			if (stat(phys, &st))
			{
				strcpy(path, s);
				return 0;
			}
			v = t;
			if (*s) *(t - 1) = '/';
		}
		if (!*s)
		{
			if (t > path && !*(t - 1)) t--;
			if (t == path) *t++ = '.';
#if DONT_PRESERVE_TRAILING_SLASH
			else if (t > path + 1 && *(t - 1) == '/') t--;
#else
			else if ((s <= path || *(s - 1) != '/') && t > path + 1 && *(t - 1) == '/') t--;
#endif
			*t = 0;
			errno = oerrno;
			return t;
		}
		dots = 0;
		p = t;
		break;
	default:
		dots = 4;
		break;
	}
}