[BACK]Return to getcwd.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libc / gen

Annotation of src/lib/libc/gen/getcwd.c, Revision 1.49

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

CVSweb <webmaster@jp.NetBSD.org>