1/*
2 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26#include <stdlib.h>
27#include <string.h>
28
29#include "FileSystemSupport_md.h"
30
31/*
32 * Solaris/Linux implementation of the file system support functions.
33 */
34
35#define slash           '/'
36
37char pathSeparator() {
38    return ':';
39}
40
41/* Filenames are case senstitive */
42int filenameStrcmp(const char* s1, const char* s2) {
43  return strcmp(s1, s2);
44}
45
46char* basePath(const char* path) {
47    char* last = strrchr(path, slash);
48    if (last == NULL) {
49        return (char*)path;
50    } else {
51        int len = last - path;
52        char* str = (char*)malloc(len+1);
53        if (len > 0) {
54            memcpy(str, path, len);
55        }
56        str[len] = '\0';
57        return str;
58    }
59}
60
61int isAbsolute(const char* path) {
62    return (path[0] == slash) ? 1 : 0;
63}
64
65/* Ported from src/solaris/classes/java/io/UnixFileSystem.java */
66
67/* A normal Unix pathname contains no duplicate slashes and does not end
68   with a slash.  It may be the empty string. */
69
70/* Normalize the given pathname, whose length is len, starting at the given
71   offset; everything before this offset is already normal. */
72static char* normalizePath(const char* pathname, int len, int off) {
73    char* sb;
74    int sbLen, i, n;
75    char prevChar;
76
77    if (len == 0) return (char*)pathname;
78    n = len;
79    while ((n > 0) && (pathname[n - 1] == slash)) n--;
80    if (n == 0) return strdup("/");
81
82    sb = (char*)malloc(strlen(pathname)+1);
83    sbLen = 0;
84
85    if (off > 0) {
86        memcpy(sb, pathname, off);
87        sbLen = off;
88    }
89
90    prevChar = 0;
91    for (i = off; i < n; i++) {
92        char c = pathname[i];
93        if ((prevChar == slash) && (c == slash)) continue;
94        sb[sbLen++] = c;
95        prevChar = c;
96    }
97    return sb;
98}
99
100/* Check that the given pathname is normal.  If not, invoke the real
101   normalizer on the part of the pathname that requires normalization.
102   This way we iterate through the whole pathname string only once. */
103char* normalize(const char* pathname) {
104    int i;
105    int n = strlen(pathname);
106    char prevChar = 0;
107    for (i = 0; i < n; i++) {
108        char c = pathname[i];
109        if ((prevChar == slash) && (c == slash))
110            return normalizePath(pathname, n, i - 1);
111        prevChar = c;
112    }
113    if (prevChar == slash) return normalizePath(pathname, n, n - 1);
114    return (char*)pathname;
115}
116
117char* resolve(const char* parent, const char* child) {
118    int len;
119    char* theChars;
120    int pn = strlen(parent);
121    int cn = strlen(child);
122    int childStart = 0;
123    int parentEnd = pn;
124
125    if (pn > 0 && parent[pn-1] == slash) {
126        parentEnd--;
127    }
128    len = parentEnd + cn - childStart;
129    if (child[0] == slash) {
130        theChars = (char*)malloc(len+1);
131        if (parentEnd > 0)
132            memcpy(theChars, parent, parentEnd);
133        if (cn > 0)
134            memcpy(theChars+parentEnd, child, cn);
135        theChars[len] = '\0';
136    } else {
137        theChars = (char*)malloc(len+2);
138        if (parentEnd > 0)
139            memcpy(theChars, parent, parentEnd);
140        theChars[parentEnd] = slash;
141        if (cn > 0)
142            memcpy(theChars+parentEnd+1, child, cn);
143        theChars[len+1] = '\0';
144    }
145    return theChars;
146}
147
148char* fromURIPath(const char* path) {
149    int len = strlen(path);
150    if (len > 1 && path[len-1] == slash) {
151        // "/foo/" --> "/foo", but "/" --> "/"
152        char* str = (char*)malloc(len);
153        if (str != NULL) {
154            memcpy(str, path, len-1);
155            str[len-1] = '\0';
156        }
157        return str;
158    } else {
159        return (char*)path;
160    }
161}
162