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