0N/A/*
2362N/A * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include <stdlib.h>
0N/A#include <string.h>
0N/A
0N/A#include "FileSystemSupport_md.h"
0N/A
0N/A/*
0N/A * Solaris/Linux implementation of the file system support functions.
0N/A */
0N/A
0N/A#define slash '/'
0N/A
0N/Achar pathSeparator() {
0N/A return ':';
0N/A}
0N/A
0N/A/* Filenames are case senstitive */
0N/Aint filenameStrcmp(const char* s1, const char* s2) {
0N/A return strcmp(s1, s2);
0N/A}
0N/A
0N/Achar* basePath(const char* path) {
0N/A char* last = strrchr(path, slash);
0N/A if (last == NULL) {
0N/A return (char*)path;
0N/A } else {
0N/A int len = last - path;
0N/A char* str = (char*)malloc(len+1);
0N/A if (len > 0) {
0N/A memcpy(str, path, len);
0N/A }
0N/A str[len] = '\0';
0N/A return str;
0N/A }
0N/A}
0N/A
0N/Aint isAbsolute(const char* path) {
0N/A return (path[0] == slash) ? 1 : 0;
0N/A}
0N/A
0N/A/* Ported from src/solaris/classes/java/io/UnixFileSystem.java */
0N/A
0N/A/* A normal Unix pathname contains no duplicate slashes and does not end
0N/A with a slash. It may be the empty string. */
0N/A
0N/A/* Normalize the given pathname, whose length is len, starting at the given
0N/A offset; everything before this offset is already normal. */
0N/Astatic char* normalizePath(const char* pathname, int len, int off) {
0N/A char* sb;
0N/A int sbLen, i, n;
0N/A char prevChar;
0N/A
0N/A if (len == 0) return (char*)pathname;
0N/A n = len;
0N/A while ((n > 0) && (pathname[n - 1] == slash)) n--;
0N/A if (n == 0) return strdup("/");
0N/A
0N/A sb = (char*)malloc(strlen(pathname)+1);
0N/A sbLen = 0;
0N/A
0N/A if (off > 0) {
0N/A memcpy(sb, pathname, off);
0N/A sbLen = off;
0N/A }
0N/A
0N/A prevChar = 0;
0N/A for (i = off; i < n; i++) {
0N/A char c = pathname[i];
0N/A if ((prevChar == slash) && (c == slash)) continue;
0N/A sb[sbLen++] = c;
0N/A prevChar = c;
0N/A }
0N/A return sb;
0N/A}
0N/A
0N/A/* Check that the given pathname is normal. If not, invoke the real
0N/A normalizer on the part of the pathname that requires normalization.
0N/A This way we iterate through the whole pathname string only once. */
0N/Achar* normalize(const char* pathname) {
0N/A int i;
0N/A int n = strlen(pathname);
0N/A char prevChar = 0;
0N/A for (i = 0; i < n; i++) {
0N/A char c = pathname[i];
0N/A if ((prevChar == slash) && (c == slash))
0N/A return normalizePath(pathname, n, i - 1);
0N/A prevChar = c;
0N/A }
0N/A if (prevChar == slash) return normalizePath(pathname, n, n - 1);
0N/A return (char*)pathname;
0N/A}
0N/A
0N/Achar* resolve(const char* parent, const char* child) {
0N/A int len;
0N/A char* theChars;
0N/A int pn = strlen(parent);
0N/A int cn = strlen(child);
0N/A int childStart = 0;
0N/A int parentEnd = pn;
0N/A
0N/A if (pn > 0 && parent[pn-1] == slash) {
0N/A parentEnd--;
0N/A }
0N/A len = parentEnd + cn - childStart;
0N/A if (child[0] == slash) {
0N/A theChars = (char*)malloc(len+1);
0N/A if (parentEnd > 0)
0N/A memcpy(theChars, parent, parentEnd);
0N/A if (cn > 0)
0N/A memcpy(theChars+parentEnd, child, cn);
0N/A theChars[len] = '\0';
0N/A } else {
0N/A theChars = (char*)malloc(len+2);
0N/A if (parentEnd > 0)
0N/A memcpy(theChars, parent, parentEnd);
0N/A theChars[parentEnd] = slash;
0N/A if (cn > 0)
0N/A memcpy(theChars+parentEnd+1, child, cn);
0N/A theChars[len+1] = '\0';
0N/A }
0N/A return theChars;
0N/A}
0N/A
0N/Achar* fromURIPath(const char* path) {
0N/A int len = strlen(path);
0N/A if (len > 1 && path[len-1] == slash) {
0N/A // "/foo/" --> "/foo", but "/" --> "/"
0N/A char* str = (char*)malloc(len);
0N/A if (str != NULL) {
0N/A memcpy(str, path, len-1);
0N/A str[len-1] = '\0';
0N/A }
0N/A return str;
0N/A } else {
0N/A return (char*)path;
0N/A }
0N/A}