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