Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.instrument/unix/native/libinstrument/FileSystemSupport_md.c
41149 views
1
/*
2
* Copyright (c) 2004, 2020, 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 <stdio.h>
27
#include <stdlib.h>
28
#include <string.h>
29
30
#include "FileSystemSupport_md.h"
31
32
/*
33
* Solaris/Linux implementation of the file system support functions.
34
*/
35
36
#define slash '/'
37
38
char* basePath(const char* path) {
39
char* last = strrchr(path, slash);
40
if (last == NULL) {
41
return (char*)path;
42
} else {
43
int len = last - path;
44
char* str = (char*)malloc(len+1);
45
if (str == NULL) {
46
fprintf(stderr, "OOM error in native tmp buffer allocation");
47
return NULL;
48
}
49
if (len > 0) {
50
memcpy(str, path, len);
51
}
52
str[len] = '\0';
53
return str;
54
}
55
}
56
57
int isAbsolute(const char* path) {
58
return (path[0] == slash) ? 1 : 0;
59
}
60
61
/* Ported from src/solaris/classes/java/io/UnixFileSystem.java */
62
63
/* A normal Unix pathname contains no duplicate slashes and does not end
64
with a slash. It may be the empty string. */
65
66
/* Normalize the given pathname, whose length is len, starting at the given
67
offset; everything before this offset is already normal. */
68
static char* normalizePath(const char* pathname, int len, int off) {
69
char* sb;
70
int sbLen, i, n;
71
char prevChar;
72
73
if (len == 0) return (char*)pathname;
74
n = len;
75
while ((n > 0) && (pathname[n - 1] == slash)) n--;
76
if (n == 0) return strdup("/");
77
78
sb = (char*)malloc(strlen(pathname)+1);
79
if (sb == NULL) {
80
fprintf(stderr, "OOM error in native tmp buffer allocation");
81
return NULL;
82
}
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. */
103
char* 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
117
char* 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 (theChars == NULL) {
132
fprintf(stderr, "OOM error in native tmp buffer allocation");
133
return NULL;
134
}
135
if (parentEnd > 0)
136
memcpy(theChars, parent, parentEnd);
137
if (cn > 0)
138
memcpy(theChars+parentEnd, child, cn);
139
theChars[len] = '\0';
140
} else {
141
theChars = (char*)malloc(len+2);
142
if (theChars == NULL) {
143
fprintf(stderr, "OOM error in native tmp buffer allocation");
144
return NULL;
145
}
146
if (parentEnd > 0)
147
memcpy(theChars, parent, parentEnd);
148
theChars[parentEnd] = slash;
149
if (cn > 0)
150
memcpy(theChars+parentEnd+1, child, cn);
151
theChars[len+1] = '\0';
152
}
153
return theChars;
154
}
155
156
char* fromURIPath(const char* path) {
157
int len = strlen(path);
158
if (len > 1 && path[len-1] == slash) {
159
// "/foo/" --> "/foo", but "/" --> "/"
160
char* str = (char*)malloc(len);
161
if (str == NULL)
162
{
163
fprintf(stderr, "OOM error in native tmp buffer allocation");
164
return NULL;
165
}
166
memcpy(str, path, len-1);
167
str[len-1] = '\0';
168
return str;
169
} else {
170
return (char*)path;
171
}
172
}
173
174