Commit f607138b
f607138b4f61Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Make sp_parse_file_to_text portable on Windows (no open_memstream)
open_memstream is POSIX/glibc; MinGW lacks it, so the implicit decl made
FILE *out = open_memstream(...) a pointer-from-int error and the
windows-mingw CI build failed. On Windows, capture sp_parse_emit's
output through a uniquely-named temp file in TEMP/TMP (tmpfile() opens
in the often-unwritable drive root on MSVCRT) and read it back; POSIX
keeps the open_memstream fast path.
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- f607138b4f61
- Parent
- 4bc3f6aa743a
spinel_parse.cmodifiedView file
@@ -22,6 +22,9 @@
2222
#include <stdarg.h>2323
#include <limits.h>2424
#include <prism.h>25+
#if defined(_WIN32)26+
#include <process.h> /* _getpid -- for a unique temp-file name (no open_memstream on MinGW) */27+
#endif2528
2629
/* ---- Output buffer ---- */2730
static char **lines;@@ -2337,6 +2340,34 @@
23372340
frees), or NULL on error. `argv0` locates the stdlib for plain requires.23382341
Avoids any on-disk intermediate by writing to an in-memory stream. */23392342
char *sp_parse_file_to_text(const char *source_file, const char *argv0) {2343+
#if defined(_WIN32)2344+
/* MinGW/MSVCRT has no open_memstream, and tmpfile() opens in the (often2345+
unwritable) drive root on CI. Capture sp_parse_emit's output through a2346+
uniquely-named temp file in TEMP/TMP, then read it back. */2347+
const char *td = getenv("TEMP");2348+
if (!td) td = getenv("TMP");2349+
if (!td) td = ".";2350+
char path[4096];2351+
snprintf(path, sizeof path, "%s\\spinel_ast_%d.tmp", td, (int)_getpid());2352+
FILE *out = fopen(path, "wb+");2353+
if (!out) return NULL;2354+
int rc = sp_parse_emit(source_file, argv0, out);2355+
char *buf = NULL;2356+
if (rc == 0 && fflush(out) == 0 && fseek(out, 0, SEEK_END) == 0) {2357+
long sz = ftell(out);2358+
if (sz >= 0) {2359+
rewind(out);2360+
buf = (char *)malloc((size_t)sz + 1);2361+
if (buf) {2362+
size_t n = fread(buf, 1, (size_t)sz, out);2363+
buf[n] = '\0';2364+
}2365+
}2366+
}2367+
fclose(out);2368+
remove(path);2369+
return buf;2370+
#else23402371
char *buf = NULL;23412372
size_t sz = 0;23422373
FILE *out = open_memstream(&buf, &sz);@@ -2348,4 +2379,5 @@
23482379
return NULL;23492380
}23502381
return buf;2382+
#endif23512383
}