jdk/src/java.base/unix/native/libjava/canonicalize_md.c
author igerasim
Tue, 28 Oct 2014 15:36:27 +0300
changeset 27263 819f5f87d485
parent 25859 3317bb8137f4
permissions -rw-r--r--
8023173: FileDescriptor should respect append flag Reviewed-by: martin, alanb, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12047
diff changeset
     2
 * Copyright (c) 1994, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * Pathname canonicalization for Unix file systems
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <sys/stat.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <limits.h>
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 7668
diff changeset
    36
#if !defined(_ALLBSD_SOURCE)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include <alloca.h>
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 7668
diff changeset
    38
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/* Note: The comments in this file use the terminology
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
         defined in the java.io.File class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/* Check the given name sequence to see if it can be further collapsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
   Return zero if not, otherwise return the number of names in the sequence. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
collapsible(char *names)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    char *p = names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    int dots = 0, n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    while (*p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if ((p[0] == '.') && ((p[1] == '\0')
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                              || (p[1] == '/')
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                              || ((p[1] == '.') && ((p[2] == '\0')
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                                                    || (p[2] == '/'))))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            dots = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        while (*p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            if (*p == '/') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    return (dots ? n : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
/* Split the names in the given name sequence,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
   replacing slashes with nulls and filling in the given index array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
splitNames(char *names, char **ix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    char *p = names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    while (*p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        ix[i++] = p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        while (*p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            if (*p == '/') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                *p++ = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
/* Join the names in the given name sequence, ignoring names whose index
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
   entries have been cleared and replacing nulls with slashes as needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
joinNames(char *names, int nc, char **ix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    char *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    for (i = 0, p = names; i < nc; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (!ix[i]) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            p[-1] = '/';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (p == ix[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            p += strlen(p) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            char *q = ix[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            while ((*p++ = *q++));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    *p = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
/* Collapse "." and ".." names in the given path wherever possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
   A "." name may always be eliminated; a ".." name may be eliminated if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
   follows a name that is neither "." nor "..".  This is a syntactic operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
   that performs no filesystem queries, so it should only be used to cleanup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
   after invoking the realpath() procedure. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
collapse(char *path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    char *names = (path[0] == '/') ? path + 1 : path; /* Preserve first '/' */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    int nc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    char **ix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    char *p, *q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    nc = collapsible(names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    if (nc < 2) return;         /* Nothing to do */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    ix = (char **)alloca(nc * sizeof(char *));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    splitNames(names, ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    for (i = 0; i < nc; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        int dots = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        /* Find next occurrence of "." or ".." */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            char *p = ix[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (p[0] == '.') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                if (p[1] == '\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    dots = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                if ((p[1] == '.') && (p[2] == '\0')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    dots = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        } while (i < nc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (i >= nc) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        /* At this point i is the index of either a "." or a "..", so take the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
           appropriate action and then continue the outer loop */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (dots == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            /* Remove this instance of "." */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            ix[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            /* If there is a preceding name, remove both that name and this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
               instance of ".."; otherwise, leave the ".." as is */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            for (j = i - 1; j >= 0; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                if (ix[j]) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            if (j < 0) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            ix[j] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            ix[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        /* i will be incremented at the top of the loop */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    joinNames(names, nc, ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
/* Convert a pathname to canonical form.  The input path is assumed to contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
   no duplicate slashes.  On Solaris we can use realpath() to do most of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
   work, though once that's done we still must collapse any remaining "." and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
   ".." names by hand. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
canonicalize(char *original, char *resolved, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    if (len < PATH_MAX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        errno = EINVAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    if (strlen(original) > PATH_MAX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        errno = ENAMETOOLONG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /* First try realpath() on the entire path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    if (realpath(original, resolved)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        /* That worked, so return it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        collapse(resolved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        /* Something's bogus in the original path, so remove names from the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
           until either some subpath works or we run out of names */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        char *p, *end, *r = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        char path[PATH_MAX + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        strncpy(path, original, sizeof(path));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (path[PATH_MAX] != '\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            errno = ENAMETOOLONG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        end = path + strlen(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        for (p = end; p > path;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            /* Skip last element */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            while ((--p > path) && (*p != '/'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            if (p == path) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            /* Try realpath() on this subpath */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            *p = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            r = realpath(path, resolved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            *p = (p == end) ? '\0' : '/';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            if (r != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                /* The subpath has a canonical path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            else if (errno == ENOENT || errno == ENOTDIR || errno == EACCES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                /* If the lookup of a particular subpath fails because the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                   does not exist, because it is of the wrong type, or because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                   access is denied, then remove its last name and try again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                   Other I/O problems cause an error return. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (r != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            /* Append unresolved subpath to resolved subpath */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            int rn = strlen(r);
6850
56966b0a6a0d 6989466: Miscellaneous compiler warnings in java/lang, java/util, java/io, sun/misc native code
alanb
parents: 5506
diff changeset
   251
            if (rn + (int)strlen(p) >= len) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                /* Buffer overflow */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                errno = ENAMETOOLONG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if ((rn > 0) && (r[rn - 1] == '/') && (*p == '/')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                /* Avoid duplicate slashes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            strcpy(r + rn, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            collapse(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            /* Nothing resolved, so just return the original path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            strcpy(resolved, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            collapse(resolved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
}