Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/instrument/FileSystemSupport_md.c
32285 views
/*1* Copyright (c) 2004, 2018 Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <stdio.h>26#include <stdlib.h>27#include <string.h>2829#include "FileSystemSupport_md.h"3031/*32* Solaris/Linux implementation of the file system support functions.33*/3435#define slash '/'3637char pathSeparator() {38return ':';39}4041/* Filenames are case senstitive */42int filenameStrcmp(const char* s1, const char* s2) {43return strcmp(s1, s2);44}4546char* basePath(const char* path) {47char* last = strrchr(path, slash);48if (last == NULL) {49return (char*)path;50} else {51int len = last - path;52char* str = (char*)malloc(len+1);53if (str == NULL) {54fprintf(stderr, "OOM error in native tmp buffer allocation");55return NULL;56}57if (len > 0) {58memcpy(str, path, len);59}60str[len] = '\0';61return str;62}63}6465int isAbsolute(const char* path) {66return (path[0] == slash) ? 1 : 0;67}6869/* Ported from src/solaris/classes/java/io/UnixFileSystem.java */7071/* A normal Unix pathname contains no duplicate slashes and does not end72with a slash. It may be the empty string. */7374/* Normalize the given pathname, whose length is len, starting at the given75offset; everything before this offset is already normal. */76static char* normalizePath(const char* pathname, int len, int off) {77char* sb;78int sbLen, i, n;79char prevChar;8081if (len == 0) return (char*)pathname;82n = len;83while ((n > 0) && (pathname[n - 1] == slash)) n--;84if (n == 0) return strdup("/");8586sb = (char*)malloc(strlen(pathname)+1);87if (sb == NULL) {88fprintf(stderr, "OOM error in native tmp buffer allocation");89return NULL;90}91sbLen = 0;9293if (off > 0) {94memcpy(sb, pathname, off);95sbLen = off;96}9798prevChar = 0;99for (i = off; i < n; i++) {100char c = pathname[i];101if ((prevChar == slash) && (c == slash)) continue;102sb[sbLen++] = c;103prevChar = c;104}105return sb;106}107108/* Check that the given pathname is normal. If not, invoke the real109normalizer on the part of the pathname that requires normalization.110This way we iterate through the whole pathname string only once. */111char* normalize(const char* pathname) {112int i;113int n = strlen(pathname);114char prevChar = 0;115for (i = 0; i < n; i++) {116char c = pathname[i];117if ((prevChar == slash) && (c == slash))118return normalizePath(pathname, n, i - 1);119prevChar = c;120}121if (prevChar == slash) return normalizePath(pathname, n, n - 1);122return (char*)pathname;123}124125char* resolve(const char* parent, const char* child) {126int len;127char* theChars;128int pn = strlen(parent);129int cn = strlen(child);130int childStart = 0;131int parentEnd = pn;132133if (pn > 0 && parent[pn-1] == slash) {134parentEnd--;135}136len = parentEnd + cn - childStart;137if (child[0] == slash) {138theChars = (char*)malloc(len+1);139if (theChars == NULL) {140fprintf(stderr, "OOM error in native tmp buffer allocation");141return NULL;142}143if (parentEnd > 0)144memcpy(theChars, parent, parentEnd);145if (cn > 0)146memcpy(theChars+parentEnd, child, cn);147theChars[len] = '\0';148} else {149theChars = (char*)malloc(len+2);150if (theChars == NULL) {151fprintf(stderr, "OOM error in native tmp buffer allocation");152return NULL;153}154if (parentEnd > 0)155memcpy(theChars, parent, parentEnd);156theChars[parentEnd] = slash;157if (cn > 0)158memcpy(theChars+parentEnd+1, child, cn);159theChars[len+1] = '\0';160}161return theChars;162}163164char* fromURIPath(const char* path) {165int len = strlen(path);166if (len > 1 && path[len-1] == slash) {167// "/foo/" --> "/foo", but "/" --> "/"168char* str = (char*)malloc(len);169if (str == NULL)170{171fprintf(stderr, "OOM error in native tmp buffer allocation");172return NULL;173}174memcpy(str, path, len-1);175str[len-1] = '\0';176return str;177} else {178return (char*)path;179}180}181182183