[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.39

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

CVSweb <webmaster@jp.NetBSD.org>