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