major restructuring, prepare for smp
This commit is contained in:
1 parent
b3569a2b90
commit
6bff37746d
229 files changed
+1617
-1489
No files matched your search
@@ -0,0 +1,79 @@
|
||||
// posix_path.c - POSIX <-> native path translation
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include "posix_path.h"
|
||||
|
||||
static void FlipSlashes(char* s) {
|
||||
for (; *s; s++) if (*s == '/') *s = '\\';
|
||||
}
|
||||
|
||||
static int IsDriveMount(const char* p) {
|
||||
return p && p[0] == '/' && p[1] >= '0' && p[1] <= '7' &&
|
||||
(p[2] == '\0' || p[2] == '/');
|
||||
}
|
||||
|
||||
const char* PosixTranslatePath(const char* in, char* out, size_t outSize) {
|
||||
if (!in || !out || outSize < 4) return out;
|
||||
|
||||
if (strcmp(in, "/dev/null") == 0) return SYNTH_DEVNULL;
|
||||
if (strcmp(in, "/dev/zero") == 0) return SYNTH_DEVZERO;
|
||||
|
||||
if (IsDriveMount(in)) {
|
||||
out[0] = in[1];
|
||||
out[1] = ':';
|
||||
out[2] = '/';
|
||||
const char* rest = (in[2] == '/') ? in + 3 : "";
|
||||
size_t restLen = strlen(rest);
|
||||
if (restLen + 4 > outSize) restLen = outSize - 4;
|
||||
memcpy(out + 3, rest, restLen);
|
||||
out[3 + restLen] = '\0';
|
||||
FlipSlashes(out);
|
||||
return out;
|
||||
}
|
||||
|
||||
if (in[0] == '/') {
|
||||
out[0] = '0';
|
||||
out[1] = ':';
|
||||
out[2] = '/';
|
||||
size_t rest = strlen(in + 1);
|
||||
if (rest + 4 > outSize) rest = outSize - 4;
|
||||
memcpy(out + 3, in + 1, rest);
|
||||
out[3 + rest] = '\0';
|
||||
} else {
|
||||
size_t len = strlen(in);
|
||||
if (len + 1 > outSize) len = outSize - 1;
|
||||
memcpy(out, in, len);
|
||||
out[len] = '\0';
|
||||
}
|
||||
|
||||
FlipSlashes(out);
|
||||
return out;
|
||||
}
|
||||
|
||||
void NativeToPosix(const char* in, char* out, size_t outSize) {
|
||||
if (!in || !out || outSize == 0) return;
|
||||
|
||||
const char* src = in;
|
||||
char drive = 0;
|
||||
if (src[0] >= '0' && src[0] <= '7' && src[1] == ':' &&
|
||||
(src[2] == '\\' || src[2] == '/')) {
|
||||
drive = src[0];
|
||||
src += 3;
|
||||
}
|
||||
|
||||
size_t pos = 0;
|
||||
if (drive && drive != '0') {
|
||||
if (outSize < 4) { out[0] = '\0'; return; }
|
||||
out[pos++] = '/';
|
||||
out[pos++] = drive;
|
||||
if (*src) out[pos++] = '/';
|
||||
} else {
|
||||
out[pos++] = '/';
|
||||
}
|
||||
|
||||
while (*src && pos + 1 < outSize) {
|
||||
out[pos++] = (*src == '\\') ? '/' : *src;
|
||||
src++;
|
||||
}
|
||||
out[pos] = '\0';
|
||||
}
|
||||
Reference in new issue
Block a user