A build de 32 bits normal nao abre no Windows 2000: o SDL2_ttf do MSYS2 puxa harfbuzz -> glib (threadpool do Vista, funcoes _s do CRT) e ate a dwrite.dll; o msvcrt.dll antigo nao entende %zu/%lld no printf; e um programa de janela pode nao ter stdout. - FreeType e SDL2_ttf do codigo-fonte, estaticas e sem harfbuzz (packaging/windows/w2k_ttf.sh). - O .exe e as DLLs tem os imports trocados pelos shims do uniwin, que vem junto no pacote; w2k_check.py confere cada funcao contra as DLLs reais do Windows 2000 e para a build se faltar alguma. - -D__USE_MINGW_ANSI_STDIO=1 para o printf do proprio mingw. - main.c reabre stdout/stderr em NUL quando nao existem; o editor captura o escreva por fileno(stdout), nao STDOUT_FILENO. - compat: tmpfile na pasta temporaria do usuario e gethostname por GetComputerNameA. Testado na VM com Windows 2000 pelo MCP: o programa roda e o escreva chega na saida.
191 lines
6.2 KiB
C
191 lines
6.2 KiB
C
/* tb_win_compat.c - implementacoes do tb_win_compat.h, dlfcn.h, pwd.h e
|
|
sys/utsname.h desta pasta (build Windows). */
|
|
#define WIN32_LEAN_AND_MEAN
|
|
#include <windows.h>
|
|
#include <direct.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "dlfcn.h"
|
|
#include "pwd.h"
|
|
#include "sys/utsname.h"
|
|
|
|
int tb_win_mkdir(const char* p, int mode) { (void)mode; return _mkdir(p); }
|
|
|
|
long tb_win_sysconf(int name) {
|
|
SYSTEM_INFO si;
|
|
GetSystemInfo(&si);
|
|
if (name == 84) return (long)si.dwNumberOfProcessors; /* _SC_NPROCESSORS_ONLN */
|
|
if (name == 30) return (long)si.dwPageSize; /* _SC_PAGESIZE */
|
|
if (name == 85) { /* _SC_PHYS_PAGES */
|
|
MEMORYSTATUSEX m;
|
|
m.dwLength = sizeof(m);
|
|
return GlobalMemoryStatusEx(&m) ? (long)(m.ullTotalPhys / si.dwPageSize) : -1;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int tb_win_setenv(const char* k, const char* v, int over) {
|
|
if (!over && getenv(k)) return 0;
|
|
return _putenv_s(k, v) == 0 ? 0 : -1;
|
|
}
|
|
|
|
int tb_win_unsetenv(const char* k) { return _putenv_s(k, "") == 0 ? 0 : -1; }
|
|
|
|
ssize_t tb_win_readlink(const char* path, char* buf, size_t cap) {
|
|
if (strcmp(path, "/proc/self/exe") != 0 || cap == 0) return -1;
|
|
DWORD n = GetModuleFileNameA(NULL, buf, (DWORD)cap);
|
|
if (n == 0 || n >= cap) return -1;
|
|
for (DWORD i = 0; i < n; i++) if (buf[i] == '\\') buf[i] = '/';
|
|
return (ssize_t)n; /* como readlink: sem o '\0' */
|
|
}
|
|
|
|
char* tb_win_strndup(const char* s, size_t n) {
|
|
size_t len = 0;
|
|
while (len < n && s[len]) len++;
|
|
char* r = malloc(len + 1);
|
|
if (r) { memcpy(r, s, len); r[len] = 0; }
|
|
return r;
|
|
}
|
|
|
|
ssize_t tb_win_getline(char** line, size_t* cap, FILE* f) {
|
|
if (!line || !cap || !f) return -1;
|
|
size_t n = 0;
|
|
int c;
|
|
while ((c = fgetc(f)) != EOF) {
|
|
if (n + 2 > *cap) {
|
|
size_t nc = *cap ? *cap * 2 : 128;
|
|
char* p = realloc(*line, nc);
|
|
if (!p) return -1;
|
|
*line = p; *cap = nc;
|
|
}
|
|
(*line)[n++] = (char)c;
|
|
if (c == '\n') break;
|
|
}
|
|
if (n == 0) return -1;
|
|
(*line)[n] = 0;
|
|
return (ssize_t)n;
|
|
}
|
|
|
|
/* -- open_memstream: um arquivo temporario lembrado aqui; o fclose dele le
|
|
o conteudo para o buffer do chamador (como no POSIX) e apaga o arquivo */
|
|
#undef fclose
|
|
typedef struct { FILE* f; char** buf; size_t* len; char path[MAX_PATH]; } MemStream;
|
|
static MemStream s_ms[8];
|
|
|
|
FILE* tb_win_open_memstream(char** buf, size_t* len) {
|
|
for (int i = 0; i < 8; i++) {
|
|
if (s_ms[i].f) continue;
|
|
char dir[MAX_PATH], path[MAX_PATH];
|
|
if (!GetTempPathA(sizeof(dir), dir) || !GetTempFileNameA(dir, "tbm", 0, path)) return NULL;
|
|
FILE* f = fopen(path, "w+b");
|
|
if (!f) { DeleteFileA(path); return NULL; }
|
|
s_ms[i].f = f;
|
|
s_ms[i].buf = buf;
|
|
s_ms[i].len = len;
|
|
memcpy(s_ms[i].path, path, sizeof(path));
|
|
return f;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
int tb_win_fclose(FILE* f) {
|
|
for (int i = 0; f && i < 8; i++) {
|
|
if (s_ms[i].f != f) continue;
|
|
fflush(f);
|
|
long n = ftell(f);
|
|
if (n < 0) n = 0;
|
|
char* b = malloc((size_t)n + 1);
|
|
if (b) { rewind(f); n = (long)fread(b, 1, (size_t)n, f); b[n] = 0; }
|
|
*s_ms[i].buf = b;
|
|
if (s_ms[i].len) *s_ms[i].len = b ? (size_t)n : 0;
|
|
int r = fclose(f);
|
|
DeleteFileA(s_ms[i].path);
|
|
s_ms[i].f = NULL;
|
|
return r;
|
|
}
|
|
return fclose(f);
|
|
}
|
|
|
|
#undef gethostname
|
|
int tb_win_gethostname(char* name, size_t len) {
|
|
DWORD n = (DWORD)len;
|
|
return GetComputerNameA(name, &n) ? 0 : -1;
|
|
}
|
|
|
|
/* tmpfile na pasta temporaria do usuario; "D" = o msvcrt apaga ao fechar */
|
|
FILE* tb_win_tmpfile(void) {
|
|
char dir[MAX_PATH], path[MAX_PATH];
|
|
if (!GetTempPathA(sizeof(dir), dir) || !GetTempFileNameA(dir, "tbt", 0, path)) return NULL;
|
|
FILE* f = fopen(path, "w+bD");
|
|
if (!f) DeleteFileA(path);
|
|
return f;
|
|
}
|
|
|
|
/* -- dlfcn ------------------------------------------------------------- */
|
|
static char s_dl_err[256];
|
|
|
|
void* dlopen(const char* path, int flags) {
|
|
(void)flags;
|
|
HMODULE h = path ? LoadLibraryA(path) : GetModuleHandleA(NULL);
|
|
if (!h) snprintf(s_dl_err, sizeof(s_dl_err), "LoadLibrary('%s') falhou (erro %lu)", path, GetLastError());
|
|
return (void*)h;
|
|
}
|
|
|
|
void* dlsym(void* h, const char* name) {
|
|
FARPROC p = GetProcAddress((HMODULE)h, name);
|
|
if (!p) snprintf(s_dl_err, sizeof(s_dl_err), "simbolo '%s' nao encontrado", name);
|
|
void* r;
|
|
memcpy(&r, &p, sizeof(r));
|
|
return r;
|
|
}
|
|
|
|
int dlclose(void* h) { return FreeLibrary((HMODULE)h) ? 0 : -1; }
|
|
|
|
char* dlerror(void) {
|
|
static char out[256];
|
|
if (!s_dl_err[0]) return NULL;
|
|
snprintf(out, sizeof(out), "%s", s_dl_err);
|
|
s_dl_err[0] = 0;
|
|
return out;
|
|
}
|
|
|
|
/* -- pwd --------------------------------------------------------------- */
|
|
int getuid(void) { return 0; }
|
|
|
|
struct passwd* getpwuid(int uid) {
|
|
static char name[256], dir[512];
|
|
static struct passwd pw;
|
|
(void)uid;
|
|
DWORD n = sizeof(name);
|
|
if (!GetUserNameA(name, &n)) snprintf(name, sizeof(name), "%s", getenv("USERNAME") ? getenv("USERNAME") : "");
|
|
const char* h = getenv("USERPROFILE");
|
|
snprintf(dir, sizeof(dir), "%s", h ? h : "");
|
|
pw.pw_name = name;
|
|
pw.pw_dir = dir;
|
|
return &pw;
|
|
}
|
|
|
|
/* -- uname ------------------------------------------------------------- */
|
|
int uname(struct utsname* u) {
|
|
OSVERSIONINFOA v;
|
|
memset(&v, 0, sizeof(v));
|
|
v.dwOSVersionInfoSize = sizeof(v);
|
|
#pragma GCC diagnostic push
|
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
|
GetVersionExA(&v);
|
|
#pragma GCC diagnostic pop
|
|
snprintf(u->sysname, sizeof(u->sysname), "Windows");
|
|
snprintf(u->release, sizeof(u->release), "%lu.%lu.%lu", v.dwMajorVersion, v.dwMinorVersion, v.dwBuildNumber);
|
|
snprintf(u->version, sizeof(u->version), "%s", v.szCSDVersion);
|
|
DWORD n = sizeof(u->nodename);
|
|
if (!GetComputerNameA(u->nodename, &n)) u->nodename[0] = 0;
|
|
SYSTEM_INFO si;
|
|
GetNativeSystemInfo(&si);
|
|
snprintf(u->machine, sizeof(u->machine), "%s",
|
|
si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ? "x86_64" :
|
|
si.wProcessorArchitecture == 12 /* ARM64 */ ? "aarch64" : "i686");
|
|
return 0;
|
|
}
|