92 lines
3.0 KiB
C
92 lines
3.0 KiB
C
// POSIX compatibility support for path translation.
|
|
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <string.h>
|
|
#include <ntlite/status.h>
|
|
#include "posix_path.h"
|
|
|
|
#define POSIX_SYSTEM_ROOT "/SystemRoot"
|
|
|
|
// Translates POSIX filesystem syntax into the native resource namespace.
|
|
const char* PosixTranslatePath(const char* in, char* out, size_t outSize) {
|
|
if (!in || !out || outSize == 0) return out;
|
|
|
|
if (strcmp(in, "/dev/null") == 0) return SYNTH_DEVNULL;
|
|
if (strcmp(in, "/dev/zero") == 0) return SYNTH_DEVZERO;
|
|
|
|
if (in[0] == '/') {
|
|
size_t prefixLength = sizeof(POSIX_SYSTEM_ROOT) - 1u;
|
|
size_t restLength = strlen(in);
|
|
if (prefixLength + restLength + 1u > outSize) {
|
|
out[0] = '\0';
|
|
return out;
|
|
}
|
|
memcpy(out, POSIX_SYSTEM_ROOT, prefixLength);
|
|
memcpy(out + prefixLength, in, restLength + 1u);
|
|
return out;
|
|
}
|
|
|
|
size_t length = strlen(in);
|
|
if (length + 1u > outSize) {
|
|
out[0] = '\0';
|
|
return out;
|
|
}
|
|
memcpy(out, in, length + 1u);
|
|
return out;
|
|
}
|
|
|
|
// Translates a canonical native SystemRoot path back to POSIX syntax.
|
|
void NativeToPosix(const char* in, char* out, size_t outSize) {
|
|
if (!in || !out || outSize == 0) return;
|
|
|
|
size_t prefixLength = sizeof(POSIX_SYSTEM_ROOT) - 1u;
|
|
const char* src = in;
|
|
if (strncmp(src, POSIX_SYSTEM_ROOT, prefixLength) == 0 &&
|
|
(src[prefixLength] == '\0' || src[prefixLength] == '/')) {
|
|
src += prefixLength;
|
|
if (*src == '\0') src = "/";
|
|
}
|
|
|
|
size_t length = strlen(src);
|
|
if (length + 1u > outSize) {
|
|
out[0] = '\0';
|
|
return;
|
|
}
|
|
memcpy(out, src, length + 1u);
|
|
}
|
|
|
|
// Maps POSIX executable syntax into native resource/CWD syntax.
|
|
KSTATUS PosixProcessPath(const char* in, char* out, size_t outSize) {
|
|
if (!in || !out || outSize == 0) return NTLSTATUS_INVALID_PARAMETER;
|
|
const char* translated = PosixTranslatePath(in, out, outSize);
|
|
if (translated != out || out[0] == '\0') return NTLSTATUS_INVALID_PARAMETER;
|
|
return NTLSTATUS_SUCCESS;
|
|
}
|
|
|
|
static bool PosixViewEqualsI(SYS_STRING_VIEW view, const char* text) {
|
|
size_t length = strlen(text);
|
|
if (view.Length != length) return false;
|
|
for (size_t i = 0; i < length; ++i) {
|
|
char a = view.Data[i];
|
|
char b = text[i];
|
|
if (a >= 'A' && a <= 'Z') a = (char)(a - 'A' + 'a');
|
|
if (b >= 'A' && b <= 'Z') b = (char)(b - 'A' + 'a');
|
|
if (a != b) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// POSIX owns the msvcrt compatibility alias; all other import names are native.
|
|
KSTATUS PosixResolveModuleName(void* context, SYS_STRING_VIEW requested, SYS_STRING_VIEW* canonicalOut) {
|
|
(void)context;
|
|
if (!canonicalOut || !SysSvIsValid(requested) || requested.Length == 0)
|
|
return NTLSTATUS_INVALID_PARAMETER;
|
|
if (PosixViewEqualsI(requested, "msvcrt.dll")) {
|
|
*canonicalOut = SYS_SV_LITERAL("posix.dll");
|
|
return NTLSTATUS_SUCCESS;
|
|
}
|
|
*canonicalOut = requested;
|
|
return NTLSTATUS_SUCCESS;
|
|
}
|