From a1e20270ad76a38d9098fe404acfac472820e67c Mon Sep 17 00:00:00 2001 From: Carter Li Date: Fri, 20 Feb 2026 23:59:03 +0800 Subject: [PATCH] Platform: add current working directory (cwd) to FFPlatform structure and initialization --- src/common/FFPlatform.h | 1 + src/common/impl/FFPlatform.c | 2 ++ src/common/impl/FFPlatform_unix.c | 11 +++++++++++ src/common/impl/FFPlatform_windows.c | 12 ++++++++++++ src/common/windows/nt.h | 6 ++++++ src/modules/title/title.c | 3 +++ 6 files changed, 35 insertions(+) diff --git a/src/common/FFPlatform.h b/src/common/FFPlatform.h index c714b5dda..75e81713c 100644 --- a/src/common/FFPlatform.h +++ b/src/common/FFPlatform.h @@ -19,6 +19,7 @@ typedef struct FFPlatform FFlist configDirs; // List of FFstrbuf, trailing slash included FFlist dataDirs; // List of FFstrbuf, trailing slash included FFstrbuf exePath; // The real path of current exe (empty if unavailable) + FFstrbuf cwd; // Trailing slash included uint32_t pid; #ifndef _WIN32 diff --git a/src/common/impl/FFPlatform.c b/src/common/impl/FFPlatform.c index 0c42a649f..f84ab5878 100644 --- a/src/common/impl/FFPlatform.c +++ b/src/common/impl/FFPlatform.c @@ -10,6 +10,7 @@ void ffPlatformInit(FFPlatform* platform) ffListInit(&platform->configDirs, sizeof(FFstrbuf)); ffListInit(&platform->dataDirs, sizeof(FFstrbuf)); ffStrbufInit(&platform->exePath); + ffStrbufInit(&platform->cwd); ffStrbufInit(&platform->userName); ffStrbufInit(&platform->fullUserName); @@ -49,6 +50,7 @@ void ffPlatformDestroy(FFPlatform* platform) ffStrbufDestroy(dir); ffListDestroy(&platform->dataDirs); ffStrbufDestroy(&platform->exePath); + ffStrbufDestroy(&platform->cwd); ffStrbufDestroy(&platform->userName); ffStrbufDestroy(&platform->hostName); diff --git a/src/common/impl/FFPlatform_unix.c b/src/common/impl/FFPlatform_unix.c index 985494bf9..c26a625e5 100644 --- a/src/common/impl/FFPlatform_unix.c +++ b/src/common/impl/FFPlatform_unix.c @@ -224,6 +224,16 @@ static void getSysinfo(FFPlatformSysinfo* info, const struct utsname* uts) #endif } +static void getCwd(FFPlatform* platform) +{ + char cwd[PATH_MAX]; + if (getcwd(cwd, sizeof(cwd)) != NULL) + { + ffStrbufSetS(&platform->cwd, cwd); + ffStrbufEnsureEndsWithC(&platform->cwd, '/'); + } +} + void ffPlatformInitImpl(FFPlatform* platform) { platform->pid = (uint32_t) getpid(); @@ -235,6 +245,7 @@ void ffPlatformInitImpl(FFPlatform* platform) memset(&uts, 0, sizeof(uts)); getExePath(platform); + getCwd(platform); getHomeDir(platform, pwd); getCacheDir(platform); getConfigDirs(platform); diff --git a/src/common/impl/FFPlatform_windows.c b/src/common/impl/FFPlatform_windows.c index d7c256aed..e20271d37 100644 --- a/src/common/impl/FFPlatform_windows.c +++ b/src/common/impl/FFPlatform_windows.c @@ -313,10 +313,22 @@ static void getSystemArchitecture(FFPlatformSysinfo* info) } } +static void getCwd(FFPlatform* platform) +{ + static_assert( + offsetof(RTL_USER_PROCESS_PARAMETERS, Reserved2[5]) == 0x38, + "CurrentDirectory should be at offset 0x38 in RTL_USER_PROCESS_PARAMETERS. Structure layout mismatch detected."); + PCURDIR cwd = (PCURDIR) &NtCurrentTeb()->ProcessEnvironmentBlock->ProcessParameters->Reserved2[5]; + ffStrbufSetNWS(&platform->cwd, cwd->DosPath.Length / sizeof(WCHAR), cwd->DosPath.Buffer); + ffStrbufReplaceAllC(&platform->cwd, '\\', '/'); + ffStrbufEnsureEndsWithC(&platform->cwd, '/'); +} + void ffPlatformInitImpl(FFPlatform* platform) { platform->pid = (uint32_t) GetCurrentProcessId(); getExePath(platform); + getCwd(platform); getHomeDir(platform); getCacheDir(platform); getConfigDirs(platform); diff --git a/src/common/windows/nt.h b/src/common/windows/nt.h index b4d83d66a..197aeab26 100644 --- a/src/common/windows/nt.h +++ b/src/common/windows/nt.h @@ -270,3 +270,9 @@ typedef struct _PROCESS_DEVICEMAP_INFORMATION_EX #ifndef NtCurrentProcess #define NtCurrentProcess() ((HANDLE)(LONG_PTR)-1) #endif + +typedef struct _CURDIR +{ + UNICODE_STRING DosPath; + HANDLE Handle; +} CURDIR, *PCURDIR; diff --git a/src/modules/title/title.c b/src/modules/title/title.c index b3948429a..6d9fa3290 100644 --- a/src/modules/title/title.c +++ b/src/modules/title/title.c @@ -70,6 +70,7 @@ bool ffPrintTitle(FFTitleOptions* options) FF_FORMAT_ARG(instance.state.platform.sid, "user-id"), #endif FF_FORMAT_ARG(instance.state.platform.pid, "pid"), + FF_FORMAT_ARG(instance.state.platform.cwd, "cwd"), })); } @@ -139,6 +140,7 @@ bool ffGenerateTitleJsonResult(FF_MAYBE_UNUSED FFTitleOptions* options, yyjson_m yyjson_mut_obj_add_strbuf(doc, obj, "exePath", &instance.state.platform.exePath); yyjson_mut_obj_add_strbuf(doc, obj, "userShell", &instance.state.platform.userShell); yyjson_mut_obj_add_uint(doc, obj, "pid", instance.state.platform.pid); + yyjson_mut_obj_add_strbuf(doc, obj, "cwd", &instance.state.platform.cwd); return true; } @@ -183,5 +185,6 @@ FFModuleBaseInfo ffTitleModuleInfo = { {"Full user name", "full-user-name"}, {"UID (*nix) / SID (Windows)", "user-id"}, {"PID of current process", "pid"}, + {"Current working directory", "cwd"}, })) };