src/java.instrument/unix/native/libinstrument/FileSystemSupport_md.c
author ihse
Sat, 03 Mar 2018 08:21:47 +0100
branchihse-warnings-cflags-branch
changeset 56230 489867818774
parent 47216 71c04702a3d5
permissions -rw-r--r--
No longer disable E_OLD_STYLE_FUNC_DEF.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "FileSystemSupport_md.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Solaris/Linux implementation of the file system support functions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#define slash           '/'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
56230
489867818774 No longer disable E_OLD_STYLE_FUNC_DEF.
ihse
parents: 47216
diff changeset
    37
char pathSeparator(void) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    return ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/* Filenames are case senstitive */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
int filenameStrcmp(const char* s1, const char* s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
  return strcmp(s1, s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
char* basePath(const char* path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    char* last = strrchr(path, slash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    if (last == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        return (char*)path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        int len = last - path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        char* str = (char*)malloc(len+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            memcpy(str, path, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        str[len] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
int isAbsolute(const char* path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    return (path[0] == slash) ? 1 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
/* Ported from src/solaris/classes/java/io/UnixFileSystem.java */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
/* A normal Unix pathname contains no duplicate slashes and does not end
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
   with a slash.  It may be the empty string. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
/* Normalize the given pathname, whose length is len, starting at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
   offset; everything before this offset is already normal. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
static char* normalizePath(const char* pathname, int len, int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    char* sb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    int sbLen, i, n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    char prevChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    if (len == 0) return (char*)pathname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    n = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    while ((n > 0) && (pathname[n - 1] == slash)) n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    if (n == 0) return strdup("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    sb = (char*)malloc(strlen(pathname)+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    sbLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    if (off > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        memcpy(sb, pathname, off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        sbLen = off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    prevChar = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    for (i = off; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        char c = pathname[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if ((prevChar == slash) && (c == slash)) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        sb[sbLen++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        prevChar = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    return sb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
/* Check that the given pathname is normal.  If not, invoke the real
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
   normalizer on the part of the pathname that requires normalization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
   This way we iterate through the whole pathname string only once. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
char* normalize(const char* pathname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    int n = strlen(pathname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    char prevChar = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    for (i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        char c = pathname[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if ((prevChar == slash) && (c == slash))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            return normalizePath(pathname, n, i - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        prevChar = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    if (prevChar == slash) return normalizePath(pathname, n, n - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    return (char*)pathname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
char* resolve(const char* parent, const char* child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    char* theChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    int pn = strlen(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    int cn = strlen(child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    int childStart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    int parentEnd = pn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    if (pn > 0 && parent[pn-1] == slash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        parentEnd--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    len = parentEnd + cn - childStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    if (child[0] == slash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        theChars = (char*)malloc(len+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (parentEnd > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            memcpy(theChars, parent, parentEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (cn > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            memcpy(theChars+parentEnd, child, cn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        theChars[len] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        theChars = (char*)malloc(len+2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (parentEnd > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            memcpy(theChars, parent, parentEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        theChars[parentEnd] = slash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (cn > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            memcpy(theChars+parentEnd+1, child, cn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        theChars[len+1] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    return theChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
char* fromURIPath(const char* path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    int len = strlen(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    if (len > 1 && path[len-1] == slash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        // "/foo/" --> "/foo", but "/" --> "/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        char* str = (char*)malloc(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (str != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            memcpy(str, path, len-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            str[len-1] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return (char*)path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
}