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

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

CVSweb <webmaster@jp.NetBSD.org>