Annotation of src/lib/libc/gen/getcwd.c, Revision 1.50.4.1
1.50.4.1! yamt 1: /* $NetBSD$ */
1.4 cgd 2:
1.1 cgd 3: /*
1.8 perry 4: * Copyright (c) 1989, 1991, 1993, 1995
1.4 cgd 5: * The Regents of the University of California. All rights reserved.
1.1 cgd 6: *
1.8 perry 7: * This code is derived from software contributed to Berkeley by
8: * Jan-Simon Pendry.
9: *
1.1 cgd 10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
1.32 agc 18: * 3. Neither the name of the University nor the names of its contributors
1.1 cgd 19: * may be used to endorse or promote products derived from this software
20: * without specific prior written permission.
21: *
22: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32: * SUCH DAMAGE.
33: */
34:
1.6 christos 35: #include <sys/cdefs.h>
1.1 cgd 36: #if defined(LIBC_SCCS) && !defined(lint)
1.4 cgd 37: #if 0
1.8 perry 38: static char sccsid[] = "@(#)getcwd.c 8.5 (Berkeley) 2/7/95";
1.4 cgd 39: #else
1.50.4.1! yamt 40: __RCSID("$NetBSD$");
1.4 cgd 41: #endif
1.1 cgd 42: #endif /* LIBC_SCCS and not lint */
43:
1.7 jtc 44: #include "namespace.h"
1.1 cgd 45: #include <sys/param.h>
46: #include <sys/stat.h>
1.8 perry 47:
1.22 lukem 48: #include <assert.h>
1.1 cgd 49: #include <errno.h>
50: #include <stdlib.h>
51: #include <string.h>
52: #include <unistd.h>
1.50 joerg 53: #include <ssp/ssp.h>
1.17 sommerfe 54:
55: #include "extern.h"
1.7 jtc 56:
57: #ifdef __weak_alias
1.50 joerg 58: __weak_alias(getcwd,_getcwd)
59: __weak_alias(_sys_getcwd,_getcwd)
1.24 mycroft 60: __weak_alias(realpath,_realpath)
1.49 tron 61: #endif
1.1 cgd 62:
1.8 perry 63: /*
1.36 enami 64: * char *realpath(const char *path, char resolved[MAXPATHLEN]);
1.8 perry 65: *
66: * Find the real name of path, by removing all ".", ".." and symlink
67: * components. Returns (resolved) on success, or (NULL) on failure,
68: * in which case the path which caused trouble is left in (resolved).
69: */
70: char *
1.36 enami 71: realpath(const char *path, char *resolved)
1.8 perry 72: {
73: struct stat sb;
1.50.4.1! yamt 74: int idx = 0, nlnk = 0;
1.36 enami 75: const char *q;
76: char *p, wbuf[2][MAXPATHLEN];
77: size_t len;
1.50.4.1! yamt 78: ssize_t n;
1.22 lukem 79:
80: _DIAGASSERT(resolved != NULL);
1.8 perry 81:
1.46 dholland 82: /* POSIX sez we must test for this */
83: if (path == NULL) {
84: errno = EINVAL;
85: return NULL;
86: }
87:
1.36 enami 88: /*
89: * Build real path one by one with paying an attention to .,
90: * .. and symbolic link.
91: */
92:
93: /*
94: * `p' is where we'll put a new component with prepending
95: * a delimiter.
96: */
97: p = resolved;
98:
99: if (*path == 0) {
100: *p = 0;
101: errno = ENOENT;
1.8 perry 102: return (NULL);
103: }
104:
1.36 enami 105: /* If relative path, start from current working directory. */
106: if (*path != '/') {
1.43 christos 107: /* check for resolved pointer to appease coverity */
108: if (resolved && getcwd(resolved, MAXPATHLEN) == NULL) {
1.36 enami 109: p[0] = '.';
110: p[1] = 0;
111: return (NULL);
112: }
113: len = strlen(resolved);
114: if (len > 1)
115: p += len;
1.31 itojun 116: }
1.36 enami 117:
1.8 perry 118: loop:
1.36 enami 119: /* Skip any slash. */
120: while (*path == '/')
121: path++;
122:
123: if (*path == 0) {
124: if (p == resolved)
125: *p++ = '/';
126: *p = 0;
127: return (resolved);
128: }
129:
130: /* Find the end of this component. */
131: q = path;
132: do
133: q++;
134: while (*q != '/' && *q != 0);
135:
136: /* Test . or .. */
137: if (path[0] == '.') {
138: if (q - path == 1) {
139: path = q;
1.8 perry 140: goto loop;
141: }
1.36 enami 142: if (path[1] == '.' && q - path == 2) {
143: /* Trim the last component. */
144: if (p != resolved)
145: while (*--p != '/')
146: ;
147: path = q;
148: goto loop;
1.8 perry 149: }
150: }
151:
1.36 enami 152: /* Append this component. */
153: if (p - resolved + 1 + q - path + 1 > MAXPATHLEN) {
1.31 itojun 154: errno = ENAMETOOLONG;
1.36 enami 155: if (p == resolved)
156: *p++ = '/';
157: *p = 0;
158: return (NULL);
1.31 itojun 159: }
1.36 enami 160: p[0] = '/';
161: memcpy(&p[1], path,
162: /* LINTED We know q > path. */
163: q - path);
164: p[1 + q - path] = 0;
1.8 perry 165:
166: /*
1.36 enami 167: * If this component is a symlink, toss it and prepend link
168: * target to unresolved path.
169: */
170: if (lstat(resolved, &sb) == -1) {
171: return (NULL);
172: }
173: if (S_ISLNK(sb.st_mode)) {
174: if (nlnk++ >= MAXSYMLINKS) {
175: errno = ELOOP;
176: return (NULL);
177: }
178: n = readlink(resolved, wbuf[idx], sizeof(wbuf[0]) - 1);
179: if (n < 0)
180: return (NULL);
181: if (n == 0) {
182: errno = ENOENT;
183: return (NULL);
184: }
1.8 perry 185:
1.36 enami 186: /* Append unresolved path to link target and switch to it. */
187: if (n + (len = strlen(q)) + 1 > sizeof(wbuf[0])) {
1.8 perry 188: errno = ENAMETOOLONG;
1.36 enami 189: return (NULL);
1.31 itojun 190: }
1.36 enami 191: memcpy(&wbuf[idx][n], q, len + 1);
192: path = wbuf[idx];
193: idx ^= 1;
194:
195: /* If absolute symlink, start from root. */
196: if (*path == '/')
197: p = resolved;
198: goto loop;
1.8 perry 199: }
1.38 enami 200: if (*q == '/' && !S_ISDIR(sb.st_mode)) {
201: errno = ENOTDIR;
202: return (NULL);
203: }
1.8 perry 204:
1.36 enami 205: /* Advance both resolved and unresolved path. */
206: p += 1 + q - path;
207: path = q;
208: goto loop;
1.8 perry 209: }
210:
1.16 sommerfe 211: char *
1.50 joerg 212: __ssp_real(getcwd)(char *pt, size_t size)
1.16 sommerfe 213: {
1.35 enami 214: char *npt;
215:
1.16 sommerfe 216: /*
1.35 enami 217: * If a buffer is specified, the size has to be non-zero.
1.16 sommerfe 218: */
1.35 enami 219: if (pt != NULL) {
220: if (size == 0) {
221: /* __getcwd(pt, 0) results ERANGE. */
1.16 sommerfe 222: errno = EINVAL;
223: return (NULL);
224: }
1.35 enami 225: if (__getcwd(pt, size) >= 0)
226: return (pt);
227: return (NULL);
1.16 sommerfe 228: }
1.35 enami 229:
230: /*
231: * If no buffer specified by the user, allocate one as necessary.
232: */
233: size = 1024 >> 1;
234: do {
235: if ((npt = realloc(pt, size <<= 1)) == NULL)
236: break;
237: pt = npt;
238: if (__getcwd(pt, size) >= 0)
239: return (pt);
240: } while (size <= MAXPATHLEN * 4 && errno == ERANGE);
241:
242: free(pt);
243: return (NULL);
1.16 sommerfe 244: }
CVSweb <webmaster@jp.NetBSD.org>