src/java.instrument/windows/native/libinstrument/FileSystemSupport_md.c
author pliden
Wed, 20 Nov 2019 10:37:46 +0100
changeset 59151 8babf00875bc
parent 54319 55025f677f68
permissions -rw-r--r--
8234382: Test tools/javac/processing/model/testgetallmembers/Main.java using too small heap Reviewed-by: eosterlund, tschatzl
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54319
55025f677f68 8221532: Incorrect copyright header in FileSystemSupport_md.c
dtitov
parents: 53313
diff changeset
     2
 * Copyright (c) 2004, 2018, 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
53313
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
    26
#include <stdio.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <malloc.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "FileSystemSupport_md.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Windows implementation of file system support functions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#define slash           '\\'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#define altSlash        '/'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
static int isSlash(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    return (c == '\\') || (c == '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
static int isLetter(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
char pathSeparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    return ';';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/* filename are case insensitive on windows */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
int filenameStrcmp(const char* s1, const char* s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    return strcasecmp(s1, s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
char* basePath(const char* path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    char* pos = strchr(path, slash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    char* last = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    while (pos != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        last = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        pos = strchr(pos, slash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    if (last == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return (char*)path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        int len = (int)(last - path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        char* str = (char*)malloc(len+1);
53313
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
    70
        if (str == NULL) {
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
    71
            fprintf(stderr, "OOM error in native tmp buffer allocation");
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
    72
            return NULL;
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
    73
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            memcpy(str, path, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        str[len] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
/* -- Normalization - src/windows/classes/java/io/Win32FileSystem.java */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
/* A normal Win32 pathname contains no duplicate slashes, except possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * for a UNC prefix, and does not end with a slash.  It may be the empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * string.  Normalized Win32 pathnames have the convenient property that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * the length of the prefix almost uniquely identifies the type of the path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * and whether it is absolute or relative:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *      0  relative to both drive and directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *      1  drive-relative (begins with '\\')
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *      2  absolute UNC (if first char is '\\'),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *         else directory-relative (has form "z:foo")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *      3  absolute local pathname (begins with "z:\\")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
static int normalizePrefix(const char* path, int len, char* sb, int* sbLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    int src = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    while ((src < len) && isSlash(path[src])) src++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    if ((len - src >= 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        && isLetter(c = path[src])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        && path[src + 1] == ':') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        /* Remove leading slashes if followed by drive specifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
           This hack is necessary to support file URLs containing drive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
           specifiers (e.g., "file://c:/path").  As a side effect,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
           "/c:/path" can be used as an alternative to "c:/path". */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        sb[(*sbLen)++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        sb[(*sbLen)++] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        src += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        src = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if ((len >= 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            && isSlash(path[0])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            && isSlash(path[1])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            /* UNC pathname: Retain first slash; leave src pointed at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
               second slash so that further slashes will be collapsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
               into the second slash.  The result will be a pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
               beginning with "\\\\" followed (most likely) by a host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
               name. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            src = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            sb[(*sbLen)++] = slash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    return src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * Normalize the given pathname, whose length is len, starting at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * offset; everything before this offset is already normal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
static char* normalizePath(const char* path, int len, int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    int src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    char* sb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    int sbLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    if (len == 0) return (char*)path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    if (off < 3) off = 0;       /* Avoid fencepost cases with UNC pathnames */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    sb = (char*)malloc(len+1);
53313
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   143
    if (sb == NULL) {
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   144
        fprintf(stderr, "OOM error in native tmp buffer allocation");
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   145
        return NULL;
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   146
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    sbLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    if (off == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        /* Complete normalization, including prefix */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        src = normalizePrefix(path, len, sb, &sbLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        /* Partial normalization */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        src = off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        memcpy(sb+sbLen, path, off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        sbLen += off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /* Remove redundant slashes from the remainder of the path, forcing all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
       slashes into the preferred slash */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    while (src < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        char c = path[src++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (isSlash(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            while ((src < len) && isSlash(path[src])) src++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            if (src == len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                /* Check for trailing separator */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                if ((sbLen == 2) && (sb[1] == ':')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    /* "z:\\" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    sb[sbLen++] = slash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                if (sbLen == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    /* "\\" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    sb[sbLen++] = slash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                if ((sbLen == 1) && (isSlash(sb[0]))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    /* "\\\\" is not collapsed to "\\" because "\\\\" marks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                       the beginning of a UNC pathname.  Even though it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                       not, by itself, a valid UNC pathname, we leave it as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                       is in order to be consistent with the win32 APIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                       which treat this case as an invalid UNC pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                       rather than as an alias for the root directory of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                       the current drive. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    sb[sbLen++] = slash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                /* Path does not denote a root directory, so do not append
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                   trailing slash */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                sb[sbLen++] = slash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            sb[sbLen++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    sb[sbLen] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    return sb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * Check that the given pathname is normal.  If not, invoke the real
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 * normalizer on the part of the pathname that requires normalization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * This way we iterate through the whole pathname string only once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
char* normalize(char* path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    int n = (int)strlen(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    char c = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    int prev = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    for (i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        char c = path[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (c == altSlash)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            return normalizePath(path, n, (prev == slash) ? i - 1 : i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if ((c == slash) && (prev == slash) && (i > 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            return normalizePath(path, n, i - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if ((c == ':') && (i > 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            return normalizePath(path, n, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        prev = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    if (prev == slash)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return normalizePath(path, n, n - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    return path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
/* -- Resolution - src/windows/classes/java/io/Win32FileSystem.java */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
char* resolve(const char* parent, const char* child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    char* c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    char* theChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    int parentEnd, childStart, len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    int pn = (int)strlen(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    int cn = (int)strlen(child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    if (pn == 0) return (char*)child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    if (cn == 0) return (char*)parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    c = (char*)child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    childStart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    parentEnd = pn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    if ((cn > 1) && (c[0] == slash)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (c[1] == slash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            /* Drop prefix when child is a UNC pathname */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            childStart = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            /* Drop prefix when child is drive-relative */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            childStart = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (cn == childStart) { // Child is double slash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if (parent[pn - 1] == slash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                char* str = strdup(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                str[pn-1] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            return (char*)parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    if (parent[pn - 1] == slash)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        parentEnd--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    len = parentEnd + cn - childStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    if (child[childStart] == slash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        theChars = (char*)malloc(len+1);
53313
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   273
        if (theChars == NULL) {
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   274
            fprintf(stderr, "OOM error in native tmp buffer allocation");
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   275
            return NULL;
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   276
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        memcpy(theChars, parent, parentEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        memcpy(theChars+parentEnd, child+childStart, (cn-childStart));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        theChars[len] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        theChars = (char*)malloc(len+2);
53313
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   282
        if (theChars == NULL) {
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   283
            fprintf(stderr, "OOM error in native tmp buffer allocation");
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   284
            return NULL;
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   285
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        memcpy(theChars, parent, parentEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        theChars[parentEnd] = slash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        memcpy(theChars+parentEnd+1, child+childStart, (cn-childStart));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        theChars[len+1] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    return theChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
static int prefixLength(const char* path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    char c0, c1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    int n = (int)strlen(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    if (n == 0) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    c0 = path[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    c1 = (n > 1) ? path[1] : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    if (c0 == slash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (c1 == slash) return 2;      /* Absolute UNC pathname "\\\\foo" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return 1;                       /* Drive-relative "\\foo" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    if (isLetter(c0) && (c1 == ':')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if ((n > 2) && (path[2] == slash))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            return 3;           /* Absolute local pathname "z:\\foo" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return 2;                       /* Directory-relative "z:foo" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    return 0;                   /* Completely relative */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
int isAbsolute(const char* path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    int pl = prefixLength(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    return (((pl == 2) && (path[0] == slash)) || (pl == 3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
char* fromURIPath(const char* path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    int start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    int len = (int)strlen(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    if ((len > 2) && (path[2] == ':')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        // "/c:/foo" --> "c:/foo"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        start = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        // "c:/foo/" --> "c:/foo", but "c:/" --> "c:/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if ((len > 3) && path[len-1] == '/')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            len--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    } else if ((len > 1) && path[len-1] == '/') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        // "/foo/" --> "/foo"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        len--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    if (start == 0 && len == (int)strlen(path)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        return (char*)path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        char* p = (char*)malloc(len+1);
53313
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   340
        if (p == NULL) {
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   341
            fprintf(stderr, "OOM error in native tmp buffer allocation");
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   342
            return NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
53313
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   344
        memcpy(p, path+start, len);
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   345
        p[len] = '\0';
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
}