Skip to content

Commit 177ca07

Browse files
committed
Fix handling of .. in jepath()
1 parent f0b8466 commit 177ca07

File tree

2 files changed

+9
-35
lines changed

2 files changed

+9
-35
lines changed

jsrc/jeload.cpp

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// utilities for JFE to load JE, initiallize, and run profile sentence
44
// JFEs are jconsole, jwdw, and jwdp
55
// TODO: Remove all void* where applicable when other parts of the code start being refactored
6+
#include <filesystem>
67
#include <string>
78
#include <stdexcept>
89
#include <cstdint>
@@ -30,8 +31,8 @@ static JgaType jga;
3031
static JGetLocaleType jgetlocale;
3132
static JGetAType jgeta;
3233
static JSetAType jseta;
33-
std::string path;
34-
std::string pathdll;
34+
std::filesystem::path path;
35+
std::filesystem::path pathdll;
3536

3637
auto
3738
jedo(char const* sentence) -> int {
@@ -101,7 +102,6 @@ jepath(char* arg, char* lib) -> void {
101102
// C strings need to be used for POSIX APIs and macOS APIs
102103
auto arg2 = new char[sz];
103104
auto arg3 = new char[sz];
104-
auto path_temp = new char[sz];
105105
// Return for readlinks
106106
int n;
107107

@@ -127,44 +127,19 @@ jepath(char* arg, char* lib) -> void {
127127
arg3[n] = 0;
128128

129129
if ('/' == *arg3)
130-
strcpy(path_temp, arg3);
131-
else {
132-
if (!getcwd(path_temp, sizeof(path_temp))) path_temp[0] = 0;
133-
strcat(path_temp, "/");
134-
strcat(path_temp, arg3);
135-
}
130+
path = arg3;
131+
else
132+
path = std::filesystem::current_path() / arg3;
136133
// Now append path_temp to path, as all POSIX and macOS API calls are done, and free up arg2, arg3, path_temp.
137-
path.append(path_temp);
138-
delete[] path_temp;
139134
delete[] arg2;
140135
delete[] arg3;
141136

142-
// Remove everything after the last / as that would be the current executables name
143-
path.erase(std::next(path.begin(), path.rfind('/')), path.end());
137+
path.remove_filename();
144138

145139
// remove ./ and backoff ../
146-
util::remove_all_occurrences(path, "../");
147-
util::remove_all_occurrences(path, "./");
148-
149-
pathdll.append(path);
150-
pathdll.append("/");
151-
pathdll.append(JDLLNAME);
152-
153-
if (*lib) {
154-
if ('/' != *lib) {
155-
pathdll.append(path);
156-
pathdll.append("/");
157-
}
158-
pathdll.append(lib);
159-
}
160-
}
140+
path = path.lexically_normal();
161141

162-
// called by jwdp (java jnative.c) to set path
163-
auto
164-
jesetpath(char* arg) -> void {
165-
pathdll.append(arg);
166-
path.append(arg);
167-
path.erase(std::next(path.begin(), path.rfind('/')), path.end());
142+
pathdll = path / (*lib ? lib : JDLLNAME);
168143
}
169144

170145
// build and run first sentence to set BINPATH, ARGV, and run profile

jsrc/jeload.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#pragma once
55

66
void jepath(char*, char*);
7-
void jesetpath(char*);
87
void* jeload(void* callbacks); // returns J
98
A jegeta(int, char*);
109
I jeseta(I n, char* name, I x, char* d);

0 commit comments

Comments
 (0)