jdk/src/solaris/native/java/io/canonicalize_md.c
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 6850 56966b0a6a0d
child 12047 320a714614e9
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6850
diff changeset
     2
 * Copyright (c) 1994, 2010, 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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include <alloca.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/* Note: The comments in this file use the terminology
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
         defined in the java.io.File class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/* Check the given name sequence to see if it can be further collapsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
   Return zero if not, otherwise return the number of names in the sequence. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
collapsible(char *names)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    char *p = names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    int dots = 0, n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    while (*p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        if ((p[0] == '.') && ((p[1] == '\0')
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                              || (p[1] == '/')
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                              || ((p[1] == '.') && ((p[2] == '\0')
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                                                    || (p[2] == '/'))))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            dots = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        while (*p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            if (*p == '/') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    return (dots ? n : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
/* Split the names in the given name sequence,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
   replacing slashes with nulls and filling in the given index array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
splitNames(char *names, char **ix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    char *p = names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    while (*p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        ix[i++] = p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        while (*p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            if (*p == '/') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                *p++ = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
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
/* Join the names in the given name sequence, ignoring names whose index
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
   entries have been cleared and replacing nulls with slashes as needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
joinNames(char *names, int nc, char **ix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    char *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    for (i = 0, p = names; i < nc; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (!ix[i]) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            p[-1] = '/';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (p == ix[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            p += strlen(p) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            char *q = ix[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            while ((*p++ = *q++));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    *p = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
/* Collapse "." and ".." names in the given path wherever possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
   A "." name may always be eliminated; a ".." name may be eliminated if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
   follows a name that is neither "." nor "..".  This is a syntactic operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
   that performs no filesystem queries, so it should only be used to cleanup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
   after invoking the realpath() procedure. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
collapse(char *path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    char *names = (path[0] == '/') ? path + 1 : path; /* Preserve first '/' */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    int nc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    char **ix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    char *p, *q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    nc = collapsible(names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    if (nc < 2) return;         /* Nothing to do */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    ix = (char **)alloca(nc * sizeof(char *));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    splitNames(names, ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    for (i = 0; i < nc; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        int dots = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        /* Find next occurrence of "." or ".." */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            char *p = ix[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if (p[0] == '.') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                if (p[1] == '\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    dots = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                if ((p[1] == '.') && (p[2] == '\0')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    dots = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        } while (i < nc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (i >= nc) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        /* At this point i is the index of either a "." or a "..", so take the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
           appropriate action and then continue the outer loop */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (dots == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            /* Remove this instance of "." */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            ix[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            /* If there is a preceding name, remove both that name and this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
               instance of ".."; otherwise, leave the ".." as is */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            for (j = i - 1; j >= 0; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                if (ix[j]) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            if (j < 0) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            ix[j] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            ix[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        /* i will be incremented at the top of the loop */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    joinNames(names, nc, ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
/* Convert a pathname to canonical form.  The input path is assumed to contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
   no duplicate slashes.  On Solaris we can use realpath() to do most of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
   work, though once that's done we still must collapse any remaining "." and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
   ".." names by hand. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
canonicalize(char *original, char *resolved, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    if (len < PATH_MAX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        errno = EINVAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    if (strlen(original) > PATH_MAX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        errno = ENAMETOOLONG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /* First try realpath() on the entire path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    if (realpath(original, resolved)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        /* That worked, so return it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        collapse(resolved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        /* Something's bogus in the original path, so remove names from the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
           until either some subpath works or we run out of names */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        char *p, *end, *r = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        char path[PATH_MAX + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        strncpy(path, original, sizeof(path));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (path[PATH_MAX] != '\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            errno = ENAMETOOLONG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        end = path + strlen(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        for (p = end; p > path;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            /* Skip last element */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            while ((--p > path) && (*p != '/'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            if (p == path) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            /* Try realpath() on this subpath */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            *p = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            r = realpath(path, resolved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            *p = (p == end) ? '\0' : '/';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            if (r != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                /* The subpath has a canonical path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            else if (errno == ENOENT || errno == ENOTDIR || errno == EACCES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                /* If the lookup of a particular subpath fails because the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                   does not exist, because it is of the wrong type, or because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                   access is denied, then remove its last name and try again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                   Other I/O problems cause an error return. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (r != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            /* Append unresolved subpath to resolved subpath */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            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
   249
            if (rn + (int)strlen(p) >= len) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                /* Buffer overflow */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                errno = ENAMETOOLONG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            if ((rn > 0) && (r[rn - 1] == '/') && (*p == '/')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                /* Avoid duplicate slashes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            strcpy(r + rn, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            collapse(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            /* Nothing resolved, so just return the original path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            strcpy(resolved, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            collapse(resolved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
}