63 lines
1.9 KiB
C
63 lines
1.9 KiB
C
// posix_path.c - POSIX <-> native path translation
|
|
#include <stddef.h>
|
|
#include <string.h>
|
|
#include "posix_path.h"
|
|
|
|
// The drive letter used as the POSIX filesystem root.
|
|
// A future SYS_GET_BOOT_DRIVE could populate this; hardcoded for v1.
|
|
static const char gRootDrive = 'C';
|
|
|
|
static void FlipSlashes(char* s) {
|
|
for (; *s; s++) if (*s == '/') *s = '\\';
|
|
}
|
|
|
|
const char* PosixTranslatePath(const char* in, char* out, size_t outSize) {
|
|
if (!in || !out || outSize < 4) return out;
|
|
|
|
// Synthetic devices
|
|
if (strcmp(in, "/dev/null") == 0) return SYNTH_DEVNULL;
|
|
if (strcmp(in, "/dev/zero") == 0) return SYNTH_DEVZERO;
|
|
|
|
// /proc/self/* - read-only synthesized files; flag with a sentinel so
|
|
// posix_io.c can intercept them without opening a real file.
|
|
// (In v1 only /proc/self/pid is handled; others fall through to ENOENT.)
|
|
|
|
if (in[0] == '/') {
|
|
// Absolute POSIX path -> C:\...
|
|
// outSize must fit "C:\" + rest
|
|
out[0] = gRootDrive;
|
|
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 {
|
|
// Relative - pass through with slash flip only
|
|
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;
|
|
|
|
// Strip "X:\" prefix if present
|
|
const char* src = in;
|
|
if (src[0] != '\0' && src[1] == ':' && (src[2] == '\\' || src[2] == '/'))
|
|
src += 2; // skip "C:", leave the leading backslash
|
|
|
|
size_t len = strlen(src);
|
|
if (len + 1 > outSize) len = outSize - 1;
|
|
memcpy(out, src, len);
|
|
out[len] = '\0';
|
|
|
|
// Backslash -> forward slash
|
|
for (char* p = out; *p; p++) if (*p == '\\') *p = '/';
|
|
}
|