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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
/*-------------------------------------------------------------------------
*
* syswrap.c
* error-throwing wrappers around POSIX functions that rarely fail
*
* Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
*
*
* IDENTIFICATION
* src/port/syswrap.c
*
*-------------------------------------------------------------------------
*/
#ifndef FRONTEND
#include "postgres.h"
#else
#include "postgres_fe.h"
#endif
/* Prevent recursion */
#undef vsnprintf
#undef snprintf
#undef vsprintf
#undef sprintf
#undef vfprintf
#undef fprintf
#undef printf
/* When the libc primitives are lacking, use our own. */
#ifdef USE_REPL_SNPRINTF
#ifdef __GNUC__
#define vsnprintf(...) pg_vsnprintf(__VA_ARGS__)
#define snprintf(...) pg_snprintf(__VA_ARGS__)
#define vsprintf(...) pg_vsprintf(__VA_ARGS__)
#define sprintf(...) pg_sprintf(__VA_ARGS__)
#define vfprintf(...) pg_vfprintf(__VA_ARGS__)
#define fprintf(...) pg_fprintf(__VA_ARGS__)
#define printf(...) pg_printf(__VA_ARGS__)
#else
#define vsnprintf pg_vsnprintf
#define snprintf pg_snprintf
#define vsprintf pg_vsprintf
#define sprintf pg_sprintf
#define vfprintf pg_vfprintf
#define fprintf pg_fprintf
#define printf pg_printf
#endif
#endif /* USE_REPL_SNPRINTF */
/*
* We abort() in the frontend, rather than exit(), because libpq in particular
* has no business calling exit(). These failures had better be rare.
*/
#ifdef FRONTEND
#define LIB_ERR(func) \
do { \
int discard = fprintf(stderr, "%s failed: %s\n", func, strerror(errno)); \
(void) discard; \
abort(); \
} while (0)
#else
#define LIB_ERR(func) elog(ERROR, "%s failed: %m", func)
#endif
int
vsnprintf_throw_on_fail(char *str, size_t count, const char *fmt, va_list args)
{
int save_errno;
int ret;
/*
* On HP-UX B.11.31, a call that truncates output returns -1 without
* setting errno. (SUSv2 allowed this until the approval of Base Working
* Group Resolution BWG98-006.) We could avoid the save and restore of
* errno on most platforms.
*/
save_errno = errno;
errno = 0;
ret = vsnprintf(str, count, fmt, args);
if (ret < 0 && errno != 0)
LIB_ERR("vsnprintf");
errno = save_errno;
return ret;
}
int
snprintf_throw_on_fail(char *str, size_t count, const char *fmt,...)
{
int ret;
va_list args;
va_start(args, fmt);
ret = vsnprintf_throw_on_fail(str, count, fmt, args);
va_end(args);
return ret;
}
int
vsprintf_throw_on_fail(char *str, const char *fmt, va_list args)
{
int ret;
ret = vsprintf(str, fmt, args);
if (ret < 0)
LIB_ERR("vsprintf");
return ret;
}
int
sprintf_throw_on_fail(char *str, const char *fmt,...)
{
int ret;
va_list args;
va_start(args, fmt);
ret = vsprintf_throw_on_fail(str, fmt, args);
va_end(args);
return ret;
}
int
vfprintf_throw_on_fail(FILE *stream, const char *fmt, va_list args)
{
int ret;
ret = vfprintf(stream, fmt, args);
if (ret < 0)
LIB_ERR("vfprintf");
return ret;
}
int
fprintf_throw_on_fail(FILE *stream, const char *fmt,...)
{
int ret;
va_list args;
va_start(args, fmt);
ret = vfprintf_throw_on_fail(stream, fmt, args);
va_end(args);
return ret;
}
int
printf_throw_on_fail(const char *fmt,...)
{
int ret;
va_list args;
va_start(args, fmt);
ret = vfprintf_throw_on_fail(stdout, fmt, args);
va_end(args);
return ret;
}
|