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