Files
dat-thang/extra/dat-dpi/inc/dat_paths.h
T

48 lines
1.3 KiB
C
Raw Normal View History

2026-10-19 00:12:36 -04:00
#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