77 lines
2.0 KiB
C
77 lines
2.0 KiB
C
// POSIX compatibility support for posix path.
|
|
|
|
#include <stddef.h>
|
|
#include <string.h>
|
|
#include "posix_path.h"
|
|
|
|
// Checks whether drive Mount.
|
|
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';
|
|
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';
|
|
}
|
|
|
|
return out;
|
|
}
|
|
|
|
// Implements the native To Posix operation.
|
|
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';
|
|
}
|