mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
Platform: add current working directory (cwd) to FFPlatform structure and initialization
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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"},
|
||||
}))
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user