test: Use open_memstream() only if available

On Solaris this function is not yet available.
This commit is contained in:
Guillem Jover 2023-04-10 23:11:33 +02:00
parent 7c652a94ea
commit fe16f3863e
2 changed files with 21 additions and 11 deletions

View File

@ -296,6 +296,7 @@ AC_CHECK_FUNCS([\
getentropy \ getentropy \
getexecname \ getexecname \
getline \ getline \
open_memstream \
pstat_getproc \ pstat_getproc \
sysconf \ sysconf \
]) ])

View File

@ -29,23 +29,36 @@
#include <string.h> #include <string.h>
static int static int
test_memstream(FILE *fp, size_t bufsz) test_memstream(void)
{ {
int rc = 0;
#if HAVE_OPEN_MEMSTREAM
FILE *fp;
char *buf = NULL;
size_t bufsz = 0;
fp = open_memstream(&buf, &bufsz);
if (fp == NULL)
return 1;
fputs("World", fp); fputs("World", fp);
if (fpurge(fp) < 0) if (fpurge(fp) < 0)
return 1; rc = 1;
fflush(fp); fflush(fp);
if (bufsz != 0) if (bufsz != 0)
return 1; rc = 1;
return 0;
fclose(fp);
free(buf);
#endif
return rc;
} }
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
FILE *fp; FILE *fp;
char *buf = NULL;
size_t bufsz = 0;
int rc; int rc;
if (fpurge(NULL) == 0) if (fpurge(NULL) == 0)
@ -54,13 +67,9 @@ main(int argc, char *argv[])
fp = fopen("/dev/zero", "r"); fp = fopen("/dev/zero", "r");
if (fpurge(fp) < 0) if (fpurge(fp) < 0)
return 1; return 1;
fclose(fp); fclose(fp);
fp = open_memstream(&buf, &bufsz); rc = test_memstream();
rc = test_memstream(fp, bufsz);
fclose(fp);
free(buf);
return rc; return rc;
} }