Skip to content

Commit ee31896

Browse files
committed
Fix handling of .. in jepath()
1 parent 3be8a78 commit ee31896

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
static char jdllver[20]; // Not sure why this is being added, but keeping it for now.
3637
static int FHS = 0; // Not sure what this is
3738

@@ -104,7 +105,6 @@ jepath(char* arg, char* lib) -> void {
104105
// C strings need to be used for POSIX APIs and macOS APIs
105106
auto arg2 = new char[sz];
106107
auto arg3 = new char[sz];
107-
auto path_temp = new char[sz];
108108
// Return for readlinks
109109
int n;
110110

@@ -129,44 +129,19 @@ jepath(char* arg, char* lib) -> void {
129129
arg3[n] = 0;
130130

131131
if ('/' == *arg3)
132-
strcpy(path_temp, arg3);
133-
else {
134-
if (!getcwd(path_temp, sizeof(path_temp))) path_temp[0] = 0;
135-
strcat(path_temp, "/");
136-
strcat(path_temp, arg3);
137-
}
132+
path = arg3;
133+
else
134+
path = std::filesystem::current_path() / arg3;
138135
// Now append path_temp to path, as all POSIX and macOS API calls are done, and free up arg2, arg3, path_temp.
139-
path.append(path_temp);
140-
delete[] path_temp;
141136
delete[] arg2;
142137
delete[] arg3;
143138

144-
// Remove everything after the last / as that would be the current executables name
145-
path.erase(std::next(path.begin(), path.rfind('/')), path.end());
139+
path.remove_filename();
146140

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

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

172147
// 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)