#ifndef DAT_PATHS_H #define DAT_PATHS_H #include #include #include #ifdef _WIN32 #include #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