Files
dat-thang/extra/dat-dpi/inc/dat_paths.h
T
divorce b735900f2e 0.0.002
ddat interop with dat js
cable interop with cabal js
ddat's frontend = dat-thang
cable fe = cable-thang
dillo support based on cel's dillo-dat
bootstrap node
xplatform support

Signed-off-by: divo <418@cryspace.space>
2026-10-19 00:17:02 -04:00

49 lines
1.3 KiB
C

#ifndef DAT_PATHS_H
#define DAT_PATHS_H
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
#include <windows.h>
#endif
/* Return the user's home directory across POSIX shells and native Windows
* launches. Windows GUI starts commonly have USERPROFILE but no HOME. */
static inline const char *dat_home_directory(void)
{
const char *home = getenv("HOME");
if (home && *home) return home;
#ifdef _WIN32
home = getenv("USERPROFILE");
if (home && *home) return home;
{
const char *drive = getenv("HOMEDRIVE");
const char *path = getenv("HOMEPATH");
static char combined[1024];
int n;
if (!drive || !*drive || !path || !*path) return NULL;
n = snprintf(combined, sizeof(combined), "%s%s", drive, path);
return n >= 0 && (size_t)n < sizeof(combined) ? combined : NULL;
}
#else
return NULL;
#endif
}
/* POSIX rename replaces an existing file; Windows rename does not. These
* state files are written to a sibling temporary file before replacement. */
static inline int dat_replace_file(const char *temporary, const char *destination)
{
#ifdef _WIN32
if (MoveFileExA(temporary, destination,
MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) return 0;
errno = EIO;
return -1;
#else
return rename(temporary, destination);
#endif
}
#endif