/Users/deen/code/yugabyte-db/src/postgres/src/include/port.h
Line | Count | Source (jump to first uncovered line) |
1 | | /*------------------------------------------------------------------------- |
2 | | * |
3 | | * port.h |
4 | | * Header for src/port/ compatibility functions. |
5 | | * |
6 | | * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group |
7 | | * Portions Copyright (c) 1994, Regents of the University of California |
8 | | * |
9 | | * src/include/port.h |
10 | | * |
11 | | *------------------------------------------------------------------------- |
12 | | */ |
13 | | #ifndef PG_PORT_H |
14 | | #define PG_PORT_H |
15 | | |
16 | | #include <ctype.h> |
17 | | #include <netdb.h> |
18 | | #include <pwd.h> |
19 | | |
20 | | /* |
21 | | * Windows has enough specialized port stuff that we push most of it off |
22 | | * into another file. |
23 | | * Note: Some CYGWIN includes might #define WIN32. |
24 | | */ |
25 | | #if defined(WIN32) && !defined(__CYGWIN__) |
26 | | #include "port/win32_port.h" |
27 | | #endif |
28 | | |
29 | | /* socket has a different definition on WIN32 */ |
30 | | #ifndef WIN32 |
31 | | typedef int pgsocket; |
32 | | |
33 | 16.9M | #define PGINVALID_SOCKET (-1) |
34 | | #else |
35 | | typedef SOCKET pgsocket; |
36 | | |
37 | | #define PGINVALID_SOCKET INVALID_SOCKET |
38 | | #endif |
39 | | |
40 | | /* non-blocking */ |
41 | | extern bool pg_set_noblock(pgsocket sock); |
42 | | extern bool pg_set_block(pgsocket sock); |
43 | | |
44 | | /* Portable path handling for Unix/Win32 (in path.c) */ |
45 | | |
46 | | extern bool has_drive_prefix(const char *filename); |
47 | | extern char *first_dir_separator(const char *filename); |
48 | | extern char *last_dir_separator(const char *filename); |
49 | | extern char *first_path_var_separator(const char *pathlist); |
50 | | extern void join_path_components(char *ret_path, |
51 | | const char *head, const char *tail); |
52 | | extern void canonicalize_path(char *path); |
53 | | extern void make_native_path(char *path); |
54 | | extern void cleanup_path(char *path); |
55 | | extern bool path_contains_parent_reference(const char *path); |
56 | | extern bool path_is_relative_and_below_cwd(const char *path); |
57 | | extern bool path_is_prefix_of_path(const char *path1, const char *path2); |
58 | | extern char *make_absolute_path(const char *path); |
59 | | extern const char *get_progname(const char *argv0); |
60 | | extern void get_share_path(const char *my_exec_path, char *ret_path); |
61 | | extern void get_etc_path(const char *my_exec_path, char *ret_path); |
62 | | extern void get_include_path(const char *my_exec_path, char *ret_path); |
63 | | extern void get_pkginclude_path(const char *my_exec_path, char *ret_path); |
64 | | extern void get_includeserver_path(const char *my_exec_path, char *ret_path); |
65 | | extern void get_lib_path(const char *my_exec_path, char *ret_path); |
66 | | extern void get_pkglib_path(const char *my_exec_path, char *ret_path); |
67 | | extern void get_locale_path(const char *my_exec_path, char *ret_path); |
68 | | extern void get_doc_path(const char *my_exec_path, char *ret_path); |
69 | | extern void get_html_path(const char *my_exec_path, char *ret_path); |
70 | | extern void get_man_path(const char *my_exec_path, char *ret_path); |
71 | | extern bool get_home_path(char *ret_path); |
72 | | extern void get_parent_directory(char *path); |
73 | | |
74 | | /* common/pgfnames.c */ |
75 | | extern char **pgfnames(const char *path); |
76 | | extern void pgfnames_cleanup(char **filenames); |
77 | | |
78 | | /* |
79 | | * is_absolute_path |
80 | | * |
81 | | * By making this a macro we avoid needing to include path.c in libpq. |
82 | | */ |
83 | | #ifndef WIN32 |
84 | 19.9M | #define IS_DIR_SEP(ch) ((ch) == '/') |
85 | | |
86 | 370k | #define is_absolute_path(filename) \ |
87 | 370k | ( \ |
88 | 370k | IS_DIR_SEP370k ((filename)[0]) \ |
89 | 370k | ) |
90 | | #else |
91 | | #define IS_DIR_SEP(ch) ((ch) == '/' || (ch) == '\\') |
92 | | |
93 | | /* See path_is_relative_and_below_cwd() for how we handle 'E:abc'. */ |
94 | | #define is_absolute_path(filename) \ |
95 | | ( \ |
96 | | IS_DIR_SEP((filename)[0]) || \ |
97 | | (isalpha((unsigned char) ((filename)[0])) && (filename)[1] == ':' && \ |
98 | | IS_DIR_SEP((filename)[2])) \ |
99 | | ) |
100 | | #endif |
101 | | |
102 | | /* Portable locale initialization (in exec.c) */ |
103 | | extern void set_pglocale_pgservice(const char *argv0, const char *app); |
104 | | |
105 | | /* Portable way to find binaries (in exec.c) */ |
106 | | extern int find_my_exec(const char *argv0, char *retpath); |
107 | | extern int find_other_exec(const char *argv0, const char *target, |
108 | | const char *versionstr, char *retpath); |
109 | | |
110 | | /* |
111 | | * YugaByte needed functionality of portable way to run system command |
112 | | * and read first line of output (in exec.c) |
113 | | */ |
114 | | extern char *exec_pipe_read_line(char *cmd, char *line, int maxsize); |
115 | | |
116 | | /* Doesn't belong here, but this is used with find_other_exec(), so... */ |
117 | 4.01k | #define PG_BACKEND_VERSIONSTR "postgres (PostgreSQL) " PG_VERSION "\n" |
118 | | |
119 | | |
120 | | #if defined(WIN32) || defined(__CYGWIN__) |
121 | | #define EXE ".exe" |
122 | | #else |
123 | | #define EXE "" |
124 | | #endif |
125 | | |
126 | | #if defined(WIN32) && !defined(__CYGWIN__) |
127 | | #define DEVNULL "nul" |
128 | | #else |
129 | 0 | #define DEVNULL "/dev/null" |
130 | | #endif |
131 | | |
132 | | /* Portable delay handling */ |
133 | | extern void pg_usleep(long microsec); |
134 | | |
135 | | /* Portable SQL-like case-independent comparisons and conversions */ |
136 | | extern int pg_strcasecmp(const char *s1, const char *s2); |
137 | | extern int pg_strncasecmp(const char *s1, const char *s2, size_t n); |
138 | | extern unsigned char pg_toupper(unsigned char ch); |
139 | | extern unsigned char pg_tolower(unsigned char ch); |
140 | | extern unsigned char pg_ascii_toupper(unsigned char ch); |
141 | | extern unsigned char pg_ascii_tolower(unsigned char ch); |
142 | | |
143 | | #ifdef USE_REPL_SNPRINTF |
144 | | |
145 | | /* |
146 | | * Versions of libintl >= 0.13 try to replace printf() and friends with |
147 | | * macros to their own versions that understand the %$ format. We do the |
148 | | * same, so disable their macros, if they exist. |
149 | | */ |
150 | | #ifdef vsnprintf |
151 | | #undef vsnprintf |
152 | | #endif |
153 | | #ifdef snprintf |
154 | | #undef snprintf |
155 | | #endif |
156 | | #ifdef sprintf |
157 | | #undef sprintf |
158 | | #endif |
159 | | #ifdef vfprintf |
160 | | #undef vfprintf |
161 | | #endif |
162 | | #ifdef fprintf |
163 | | #undef fprintf |
164 | | #endif |
165 | | #ifdef printf |
166 | | #undef printf |
167 | | #endif |
168 | | |
169 | | extern int pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args); |
170 | | extern int pg_snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3, 4); |
171 | | extern int pg_sprintf(char *str, const char *fmt,...) pg_attribute_printf(2, 3); |
172 | | extern int pg_vfprintf(FILE *stream, const char *fmt, va_list args); |
173 | | extern int pg_fprintf(FILE *stream, const char *fmt,...) pg_attribute_printf(2, 3); |
174 | | extern int pg_printf(const char *fmt,...) pg_attribute_printf(1, 2); |
175 | | |
176 | | /* |
177 | | * The GCC-specific code below prevents the pg_attribute_printf above from |
178 | | * being replaced, and this is required because gcc doesn't know anything |
179 | | * about pg_printf. |
180 | | */ |
181 | | #ifdef __GNUC__ |
182 | | #define vsnprintf(...) pg_vsnprintf(__VA_ARGS__) |
183 | | #define snprintf(...) pg_snprintf(__VA_ARGS__) |
184 | | #define sprintf(...) pg_sprintf(__VA_ARGS__) |
185 | | #define vfprintf(...) pg_vfprintf(__VA_ARGS__) |
186 | | #define fprintf(...) pg_fprintf(__VA_ARGS__) |
187 | | #define printf(...) pg_printf(__VA_ARGS__) |
188 | | #else |
189 | | #define vsnprintf pg_vsnprintf |
190 | | #define snprintf pg_snprintf |
191 | | #define sprintf pg_sprintf |
192 | | #define vfprintf pg_vfprintf |
193 | | #define fprintf pg_fprintf |
194 | | #define printf pg_printf |
195 | | #endif |
196 | | #endif /* USE_REPL_SNPRINTF */ |
197 | | |
198 | | /* Portable prompt handling */ |
199 | | extern void simple_prompt(const char *prompt, char *destination, size_t destlen, |
200 | | bool echo); |
201 | | |
202 | | extern int pclose_check(FILE *stream); |
203 | | |
204 | | /* Global variable holding time zone information. */ |
205 | | #if defined(WIN32) || defined(__CYGWIN__) |
206 | | #define TIMEZONE_GLOBAL _timezone |
207 | | #define TZNAME_GLOBAL _tzname |
208 | | #else |
209 | | #define TIMEZONE_GLOBAL timezone |
210 | | #define TZNAME_GLOBAL tzname |
211 | | #endif |
212 | | |
213 | | #if defined(WIN32) || defined(__CYGWIN__) |
214 | | /* |
215 | | * Win32 doesn't have reliable rename/unlink during concurrent access. |
216 | | */ |
217 | | extern int pgrename(const char *from, const char *to); |
218 | | extern int pgunlink(const char *path); |
219 | | |
220 | | /* Include this first so later includes don't see these defines */ |
221 | | #ifdef _MSC_VER |
222 | | #include <io.h> |
223 | | #endif |
224 | | |
225 | | #define rename(from, to) pgrename(from, to) |
226 | | #define unlink(path) pgunlink(path) |
227 | | #endif /* defined(WIN32) || defined(__CYGWIN__) */ |
228 | | |
229 | | /* |
230 | | * Win32 also doesn't have symlinks, but we can emulate them with |
231 | | * junction points on newer Win32 versions. |
232 | | * |
233 | | * Cygwin has its own symlinks which work on Win95/98/ME where |
234 | | * junction points don't, so use those instead. We have no way of |
235 | | * knowing what type of system Cygwin binaries will be run on. |
236 | | * Note: Some CYGWIN includes might #define WIN32. |
237 | | */ |
238 | | #if defined(WIN32) && !defined(__CYGWIN__) |
239 | | extern int pgsymlink(const char *oldpath, const char *newpath); |
240 | | extern int pgreadlink(const char *path, char *buf, size_t size); |
241 | | extern bool pgwin32_is_junction(const char *path); |
242 | | |
243 | | #define symlink(oldpath, newpath) pgsymlink(oldpath, newpath) |
244 | | #define readlink(path, buf, size) pgreadlink(path, buf, size) |
245 | | #endif |
246 | | |
247 | | extern bool rmtree(const char *path, bool rmtopdir); |
248 | | |
249 | | #if defined(WIN32) && !defined(__CYGWIN__) |
250 | | |
251 | | /* |
252 | | * open() and fopen() replacements to allow deletion of open files and |
253 | | * passing of other special options. |
254 | | */ |
255 | | #define O_DIRECT 0x80000000 |
256 | | extern int pgwin32_open(const char *, int,...); |
257 | | extern FILE *pgwin32_fopen(const char *, const char *); |
258 | | |
259 | | #ifndef FRONTEND |
260 | | #define open(a,b,c) pgwin32_open(a,b,c) |
261 | | #define fopen(a,b) pgwin32_fopen(a,b) |
262 | | #endif |
263 | | |
264 | | /* |
265 | | * Mingw-w64 headers #define popen and pclose to _popen and _pclose. We want |
266 | | * to use our popen wrapper, rather than plain _popen, so override that. For |
267 | | * consistency, use our version of pclose, too. |
268 | | */ |
269 | | #ifdef popen |
270 | | #undef popen |
271 | | #endif |
272 | | #ifdef pclose |
273 | | #undef pclose |
274 | | #endif |
275 | | |
276 | | /* |
277 | | * system() and popen() replacements to enclose the command in an extra |
278 | | * pair of quotes. |
279 | | */ |
280 | | extern int pgwin32_system(const char *command); |
281 | | extern FILE *pgwin32_popen(const char *command, const char *type); |
282 | | |
283 | | #define system(a) pgwin32_system(a) |
284 | | #define popen(a,b) pgwin32_popen(a,b) |
285 | | #define pclose(a) _pclose(a) |
286 | | |
287 | | /* New versions of MingW have gettimeofday, old mingw and msvc don't */ |
288 | | #ifndef HAVE_GETTIMEOFDAY |
289 | | /* Last parameter not used */ |
290 | | extern int gettimeofday(struct timeval *tp, struct timezone *tzp); |
291 | | #endif |
292 | | #else /* !WIN32 */ |
293 | | |
294 | | /* |
295 | | * Win32 requires a special close for sockets and pipes, while on Unix |
296 | | * close() does them all. |
297 | | */ |
298 | 181k | #define closesocket close |
299 | | #endif /* WIN32 */ |
300 | | |
301 | | /* |
302 | | * On Windows, setvbuf() does not support _IOLBF mode, and interprets that |
303 | | * as _IOFBF. To add insult to injury, setvbuf(file, NULL, _IOFBF, 0) |
304 | | * crashes outright if "parameter validation" is enabled. Therefore, in |
305 | | * places where we'd like to select line-buffered mode, we fall back to |
306 | | * unbuffered mode instead on Windows. Always use PG_IOLBF not _IOLBF |
307 | | * directly in order to implement this behavior. |
308 | | */ |
309 | | #ifndef WIN32 |
310 | 2.00k | #define PG_IOLBF _IOLBF |
311 | | #else |
312 | | #define PG_IOLBF _IONBF |
313 | | #endif |
314 | | |
315 | | /* |
316 | | * Default "extern" declarations or macro substitutes for library routines. |
317 | | * When necessary, these routines are provided by files in src/port/. |
318 | | */ |
319 | | #ifndef HAVE_CRYPT |
320 | | extern char *crypt(const char *key, const char *setting); |
321 | | #endif |
322 | | |
323 | | /* WIN32 handled in port/win32_port.h */ |
324 | | #ifndef WIN32 |
325 | 0 | #define pgoff_t off_t |
326 | | #ifdef __NetBSD__ |
327 | | extern int fseeko(FILE *stream, off_t offset, int whence); |
328 | | extern off_t ftello(FILE *stream); |
329 | | #endif |
330 | | #endif |
331 | | |
332 | | extern double pg_erand48(unsigned short xseed[3]); |
333 | | extern long pg_lrand48(void); |
334 | | extern long pg_jrand48(unsigned short xseed[3]); |
335 | | extern void pg_srand48(long seed); |
336 | | |
337 | | #ifndef HAVE_FLS |
338 | | extern int fls(int mask); |
339 | | #endif |
340 | | |
341 | | #ifndef HAVE_FSEEKO |
342 | | #define fseeko(a, b, c) fseek(a, b, c) |
343 | | #define ftello(a) ftell(a) |
344 | | #endif |
345 | | |
346 | | #if !defined(HAVE_GETPEEREID) && !defined(WIN32) |
347 | | extern int getpeereid(int sock, uid_t *uid, gid_t *gid); |
348 | | #endif |
349 | | |
350 | | #ifndef HAVE_ISINF |
351 | | extern int isinf(double x); |
352 | | #else |
353 | | /* |
354 | | * Glibc doesn't use the builtin for clang due to a *gcc* bug in a version |
355 | | * newer than the gcc compatibility clang claims to have. This would cause a |
356 | | * *lot* of superfluous function calls, therefore revert when using clang. In |
357 | | * C++ there's issues with libc++ (not libstdc++), so disable as well. |
358 | | */ |
359 | | #if defined(__clang__) && !defined(__cplusplus) |
360 | | /* needs to be separate to not confuse other compilers */ |
361 | | #if __has_builtin(__builtin_isinf) |
362 | | /* need to include before, to avoid getting overwritten */ |
363 | | #include <math.h> |
364 | | #undef isinf |
365 | 4.78M | #define isinf __builtin_isinf |
366 | | #endif /* __has_builtin(isinf) */ |
367 | | #endif /* __clang__ && !__cplusplus*/ |
368 | | #endif /* !HAVE_ISINF */ |
369 | | |
370 | | #ifndef HAVE_MKDTEMP |
371 | | extern char *mkdtemp(char *path); |
372 | | #endif |
373 | | |
374 | | #ifndef HAVE_RINT |
375 | | extern double rint(double x); |
376 | | #endif |
377 | | |
378 | | #ifndef HAVE_INET_ATON |
379 | | #include <netinet/in.h> |
380 | | #include <arpa/inet.h> |
381 | | extern int inet_aton(const char *cp, struct in_addr *addr); |
382 | | #endif |
383 | | |
384 | | #if !HAVE_DECL_STRLCAT |
385 | | extern size_t strlcat(char *dst, const char *src, size_t siz); |
386 | | #endif |
387 | | |
388 | | #if !HAVE_DECL_STRLCPY |
389 | | extern size_t strlcpy(char *dst, const char *src, size_t siz); |
390 | | #endif |
391 | | |
392 | | #if !HAVE_DECL_STRNLEN |
393 | | extern size_t strnlen(const char *str, size_t maxlen); |
394 | | #endif |
395 | | |
396 | | #if !defined(HAVE_RANDOM) |
397 | | extern long random(void); |
398 | | #endif |
399 | | |
400 | | #ifndef HAVE_UNSETENV |
401 | | extern void unsetenv(const char *name); |
402 | | #endif |
403 | | |
404 | | #ifndef HAVE_SRANDOM |
405 | | extern void srandom(unsigned int seed); |
406 | | #endif |
407 | | |
408 | | #ifndef HAVE_SSL_GET_CURRENT_COMPRESSION |
409 | | #define SSL_get_current_compression(x) 0 |
410 | | #endif |
411 | | |
412 | | /* thread.h */ |
413 | | extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen); |
414 | | |
415 | | #ifndef WIN32 |
416 | | extern int pqGetpwuid(uid_t uid, struct passwd *resultbuf, char *buffer, |
417 | | size_t buflen, struct passwd **result); |
418 | | #endif |
419 | | |
420 | | extern int pqGethostbyname(const char *name, |
421 | | struct hostent *resultbuf, |
422 | | char *buffer, size_t buflen, |
423 | | struct hostent **result, |
424 | | int *herrno); |
425 | | |
426 | | extern void pg_qsort(void *base, size_t nel, size_t elsize, |
427 | | int (*cmp) (const void *, const void *)); |
428 | | extern int pg_qsort_strcmp(const void *a, const void *b); |
429 | | |
430 | 353k | #define qsort(a,b,c,d) pg_qsort(a76 ,b,c,d0 ) |
431 | | |
432 | | typedef int (*qsort_arg_comparator) (const void *a, const void *b, void *arg); |
433 | | |
434 | | extern void qsort_arg(void *base, size_t nel, size_t elsize, |
435 | | qsort_arg_comparator cmp, void *arg); |
436 | | |
437 | | /* port/chklocale.c */ |
438 | | extern int pg_get_encoding_from_locale(const char *ctype, bool write_message); |
439 | | |
440 | | #if defined(WIN32) && !defined(FRONTEND) |
441 | | extern int pg_codepage_to_encoding(UINT cp); |
442 | | #endif |
443 | | |
444 | | /* port/inet_net_ntop.c */ |
445 | | extern char *inet_net_ntop(int af, const void *src, int bits, |
446 | | char *dst, size_t size); |
447 | | |
448 | | /* port/pg_strong_random.c */ |
449 | | #ifdef HAVE_STRONG_RANDOM |
450 | | extern bool pg_strong_random(void *buf, size_t len); |
451 | | #endif |
452 | | |
453 | | /* port/pgcheckdir.c */ |
454 | | extern int pg_check_dir(const char *dir); |
455 | | |
456 | | /* port/pgmkdirp.c */ |
457 | | extern int pg_mkdir_p(char *path, int omode); |
458 | | |
459 | | /* port/pqsignal.c */ |
460 | | typedef void (*pqsigfunc) (int signo); |
461 | | extern pqsigfunc pqsignal(int signo, pqsigfunc func); |
462 | | #ifndef WIN32 |
463 | | extern pqsigfunc pqsignal_no_restart(int signo, pqsigfunc func); |
464 | | #else |
465 | | #define pqsignal_no_restart(signo, func) pqsignal(signo, func) |
466 | | #endif |
467 | | |
468 | | /* port/quotes.c */ |
469 | | extern char *escape_single_quotes_ascii(const char *src); |
470 | | |
471 | | /* common/wait_error.c */ |
472 | | extern char *wait_result_to_str(int exit_status); |
473 | | extern bool wait_result_is_signal(int exit_status, int signum); |
474 | | extern bool wait_result_is_any_signal(int exit_status, bool include_command_not_found); |
475 | | |
476 | | #endif /* PG_PORT_H */ |