Annotation of src/lib/libc/gen/getcwd.c, Revision 1.46.2.3
1.46.2.3! bouyer 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.46.2.3! bouyer 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.46.2.3! bouyer 53: #include <ssp/ssp.h>
1.17 sommerfe 54:
55: #include "extern.h"
1.7 jtc 56:
57: #ifdef __weak_alias
1.46.2.3! bouyer 58: __weak_alias(getcwd,_getcwd)
! 59: __weak_alias(_sys_getcwd,_getcwd)
1.24 mycroft 60: __weak_alias(realpath,_realpath)
1.46.2.2 bouyer 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.40 elad 74: int idx = 0, n, nlnk = 0;
1.36 enami 75: const char *q;
76: char *p, wbuf[2][MAXPATHLEN];
77: size_t len;
1.22 lukem 78:
79: _DIAGASSERT(resolved != NULL);
1.8 perry 80:
1.46 dholland 81: /* POSIX sez we must test for this */
82: if (path == NULL) {
83: errno = EINVAL;
84: return NULL;
85: }
86:
1.36 enami 87: /*
88: * Build real path one by one with paying an attention to .,
89: * .. and symbolic link.
90: */
91:
92: /*
93: * `p' is where we'll put a new component with prepending
94: * a delimiter.
95: */
96: p = resolved;
97:
98: if (*path == 0) {
99: *p = 0;
100: errno = ENOENT;
1.8 perry 101: return (NULL);
102: }
103:
1.36 enami 104: /* If relative path, start from current working directory. */
105: if (*path != '/') {
1.43 christos 106: /* check for resolved pointer to appease coverity */
107: if (resolved && getcwd(resolved, MAXPATHLEN) == NULL) {
1.36 enami 108: p[0] = '.';
109: p[1] = 0;
110: return (NULL);
111: }
112: len = strlen(resolved);
113: if (len > 1)
114: p += len;
1.31 itojun 115: }
1.36 enami 116:
1.8 perry 117: loop:
1.36 enami 118: /* Skip any slash. */
119: while (*path == '/')
120: path++;
121:
122: if (*path == 0) {
123: if (p == resolved)
124: *p++ = '/';
125: *p = 0;
126: return (resolved);
127: }
128:
129: /* Find the end of this component. */
130: q = path;
131: do
132: q++;
133: while (*q != '/' && *q != 0);
134:
135: /* Test . or .. */
136: if (path[0] == '.') {
137: if (q - path == 1) {
138: path = q;
1.8 perry 139: goto loop;
140: }
1.36 enami 141: if (path[1] == '.' && q - path == 2) {
142: /* Trim the last component. */
143: if (p != resolved)
144: while (*--p != '/')
145: ;
146: path = q;
147: goto loop;
1.8 perry 148: }
149: }
150:
1.36 enami 151: /* Append this component. */
152: if (p - resolved + 1 + q - path + 1 > MAXPATHLEN) {
1.31 itojun 153: errno = ENAMETOOLONG;
1.36 enami 154: if (p == resolved)
155: *p++ = '/';
156: *p = 0;
157: return (NULL);
1.31 itojun 158: }
1.36 enami 159: p[0] = '/';
160: memcpy(&p[1], path,
161: /* LINTED We know q > path. */
162: q - path);
163: p[1 + q - path] = 0;
1.8 perry 164:
165: /*
1.36 enami 166: * If this component is a symlink, toss it and prepend link
167: * target to unresolved path.
168: */
169: if (lstat(resolved, &sb) == -1) {
170: return (NULL);
171: }
172: if (S_ISLNK(sb.st_mode)) {
173: if (nlnk++ >= MAXSYMLINKS) {
174: errno = ELOOP;
175: return (NULL);
176: }
177: n = readlink(resolved, wbuf[idx], sizeof(wbuf[0]) - 1);
178: if (n < 0)
179: return (NULL);
180: if (n == 0) {
181: errno = ENOENT;
182: return (NULL);
183: }
1.8 perry 184:
1.36 enami 185: /* Append unresolved path to link target and switch to it. */
186: if (n + (len = strlen(q)) + 1 > sizeof(wbuf[0])) {
1.8 perry 187: errno = ENAMETOOLONG;
1.36 enami 188: return (NULL);
1.31 itojun 189: }
1.36 enami 190: memcpy(&wbuf[idx][n], q, len + 1);
191: path = wbuf[idx];
192: idx ^= 1;
193:
194: /* If absolute symlink, start from root. */
195: if (*path == '/')
196: p = resolved;
197: goto loop;
1.8 perry 198: }
1.38 enami 199: if (*q == '/' && !S_ISDIR(sb.st_mode)) {
200: errno = ENOTDIR;
201: return (NULL);
202: }
1.8 perry 203:
1.36 enami 204: /* Advance both resolved and unresolved path. */
205: p += 1 + q - path;
206: path = q;
207: goto loop;
1.8 perry 208: }
209:
1.16 sommerfe 210: char *
1.46.2.3! bouyer 211: __ssp_real(getcwd)(char *pt, size_t size)
1.16 sommerfe 212: {
1.35 enami 213: char *npt;
214:
1.16 sommerfe 215: /*
1.35 enami 216: * If a buffer is specified, the size has to be non-zero.
1.16 sommerfe 217: */
1.35 enami 218: if (pt != NULL) {
219: if (size == 0) {
220: /* __getcwd(pt, 0) results ERANGE. */
1.16 sommerfe 221: errno = EINVAL;
222: return (NULL);
223: }
1.35 enami 224: if (__getcwd(pt, size) >= 0)
225: return (pt);
226: return (NULL);
1.16 sommerfe 227: }
1.35 enami 228:
229: /*
230: * If no buffer specified by the user, allocate one as necessary.
231: */
232: size = 1024 >> 1;
233: do {
234: if ((npt = realloc(pt, size <<= 1)) == NULL)
235: break;
236: pt = npt;
237: if (__getcwd(pt, size) >= 0)
238: return (pt);
239: } while (size <= MAXPATHLEN * 4 && errno == ERANGE);
240:
241: free(pt);
242: return (NULL);
1.16 sommerfe 243: }
CVSweb <webmaster@jp.NetBSD.org>