1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
| | /*
* Copyright (C) 2012-2020 all contributors <cmogstored-public@yhbt.net>
* License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
*/
#ifdef HAVE_OPEN_MEMSTREAM
# define my_memstream_close(fp,dst,bytes) fclose((fp))
# define my_memstream_errclose(fp) ((void)fclose((fp)))
#else
static FILE * my_open_memstream(char **ptr, size_t *sizeloc)
{
FILE *fp;
do {
errno = 0;
fp = tmpfile();
if (fp != NULL)
return fp;
} while (errno == EINTR);
return NULL;
}
#define open_memstream(ptr,sizeloc) my_open_memstream((ptr),(sizeloc))
/* EBADF is fatal in MT applications like ours */
static void my_memstream_errclose(FILE *fp)
{
errno = 0;
if (fclose(fp) != 0)
assert(errno != EBADF
&& "EBADF in stdio/fclose(memstream) replacement");
}
static int my_memstream_close(FILE *fp, char **dst, size_t *bytes)
{
long pos;
errno = 0;
pos = ftell(fp);
if (pos >= 0) {
rewind(fp);
*bytes = (size_t)pos;
*dst = xmalloc(*bytes);
if (fread(*dst, 1, *bytes, fp) == *bytes)
goto out;
} else {
assert(errno != EBADF
&& "EBADF in stdio/open_memstream replacement");
}
*bytes = 0;
syslog(LOG_ERR, "stdio/open_memstream replacement failed: %m");
out:
/*
* if ftell() fails, fclose() may fail due to an I/O error,
* too, but at least hope we release memory..
*/
my_memstream_errclose(fp);
return 0;
}
#endif /* !HAVE_OPEN_MEMSTREAM */
|