1 /*-------------------------------------------------------------------------
4 * Header for src/port/ compatibility functions.
6 * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 *-------------------------------------------------------------------------
20 /* socket has a different definition on WIN32 */
24 #define PGINVALID_SOCKET (-1)
26 typedef SOCKET pgsocket;
28 #define PGINVALID_SOCKET INVALID_SOCKET
32 extern bool pg_set_noblock(pgsocket sock);
33 extern bool pg_set_block(pgsocket sock);
35 /* Portable path handling for Unix/Win32 (in path.c) */
37 extern bool has_drive_prefix(const char *filename);
38 extern char *first_dir_separator(const char *filename);
39 extern char *last_dir_separator(const char *filename);
40 extern char *first_path_var_separator(const char *pathlist);
41 extern void join_path_components(char *ret_path,
42 const char *head, const char *tail);
43 extern void canonicalize_path(char *path);
44 extern void make_native_path(char *path);
45 extern void cleanup_path(char *path);
46 extern bool path_contains_parent_reference(const char *path);
47 extern bool path_is_relative_and_below_cwd(const char *path);
48 extern bool path_is_prefix_of_path(const char *path1, const char *path2);
49 extern char *make_absolute_path(const char *path);
50 extern const char *get_progname(const char *argv0);
51 extern void get_share_path(const char *my_exec_path, char *ret_path);
52 extern void get_etc_path(const char *my_exec_path, char *ret_path);
53 extern void get_include_path(const char *my_exec_path, char *ret_path);
54 extern void get_pkginclude_path(const char *my_exec_path, char *ret_path);
55 extern void get_includeserver_path(const char *my_exec_path, char *ret_path);
56 extern void get_lib_path(const char *my_exec_path, char *ret_path);
57 extern void get_pkglib_path(const char *my_exec_path, char *ret_path);
58 extern void get_locale_path(const char *my_exec_path, char *ret_path);
59 extern void get_doc_path(const char *my_exec_path, char *ret_path);
60 extern void get_html_path(const char *my_exec_path, char *ret_path);
61 extern void get_man_path(const char *my_exec_path, char *ret_path);
62 extern bool get_home_path(char *ret_path);
63 extern void get_parent_directory(char *path);
65 /* common/pgfnames.c */
66 extern char **pgfnames(const char *path);
67 extern void pgfnames_cleanup(char **filenames);
72 * By making this a macro we avoid needing to include path.c in libpq.
75 #define IS_DIR_SEP(ch) ((ch) == '/')
77 #define is_absolute_path(filename) \
79 IS_DIR_SEP((filename)[0]) \
82 #define IS_DIR_SEP(ch) ((ch) == '/' || (ch) == '\\')
84 /* See path_is_relative_and_below_cwd() for how we handle 'E:abc'. */
85 #define is_absolute_path(filename) \
87 IS_DIR_SEP((filename)[0]) || \
88 (isalpha((unsigned char) ((filename)[0])) && (filename)[1] == ':' && \
89 IS_DIR_SEP((filename)[2])) \
93 /* Portable locale initialization (in exec.c) */
94 extern void set_pglocale_pgservice(const char *argv0, const char *app);
96 /* Portable way to find binaries (in exec.c) */
97 extern int find_my_exec(const char *argv0, char *retpath);
98 extern int find_other_exec(const char *argv0, const char *target,
99 const char *versionstr, char *retpath);
101 /* Windows security token manipulation (in exec.c) */
103 extern BOOL AddUserToTokenDacl(HANDLE hToken);
107 #if defined(WIN32) || defined(__CYGWIN__)
113 #if defined(WIN32) && !defined(__CYGWIN__)
114 #define DEVNULL "nul"
116 #define DEVNULL "/dev/null"
119 /* Portable delay handling */
120 extern void pg_usleep(long microsec);
122 /* Portable SQL-like case-independent comparisons and conversions */
123 extern int pg_strcasecmp(const char *s1, const char *s2);
124 extern int pg_strncasecmp(const char *s1, const char *s2, size_t n);
125 extern unsigned char pg_toupper(unsigned char ch);
126 extern unsigned char pg_tolower(unsigned char ch);
127 extern unsigned char pg_ascii_toupper(unsigned char ch);
128 extern unsigned char pg_ascii_tolower(unsigned char ch);
130 #ifdef USE_REPL_SNPRINTF
133 * Versions of libintl >= 0.13 try to replace printf() and friends with
134 * macros to their own versions that understand the %$ format. We do the
135 * same, so disable their macros, if they exist.
156 extern int pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args);
157 extern int pg_snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3, 4);
158 extern int pg_sprintf(char *str, const char *fmt,...) pg_attribute_printf(2, 3);
159 extern int pg_vfprintf(FILE *stream, const char *fmt, va_list args);
160 extern int pg_fprintf(FILE *stream, const char *fmt,...) pg_attribute_printf(2, 3);
161 extern int pg_printf(const char *fmt,...) pg_attribute_printf(1, 2);
164 * The GCC-specific code below prevents the pg_attribute_printf above from
165 * being replaced, and this is required because gcc doesn't know anything
169 #define vsnprintf(...) pg_vsnprintf(__VA_ARGS__)
170 #define snprintf(...) pg_snprintf(__VA_ARGS__)
171 #define sprintf(...) pg_sprintf(__VA_ARGS__)
172 #define vfprintf(...) pg_vfprintf(__VA_ARGS__)
173 #define fprintf(...) pg_fprintf(__VA_ARGS__)
174 #define printf(...) pg_printf(__VA_ARGS__)
176 #define vsnprintf pg_vsnprintf
177 #define snprintf pg_snprintf
178 #define sprintf pg_sprintf
179 #define vfprintf pg_vfprintf
180 #define fprintf pg_fprintf
181 #define printf pg_printf
183 #endif /* USE_REPL_SNPRINTF */
187 * Versions of libintl >= 0.18? try to replace setlocale() with a macro
188 * to their own versions. Remove the macro, if it exists, because it
189 * ends up calling the wrong version when the backend and libintl use
190 * different versions of msvcrt.
192 #if defined(setlocale)
197 * Define our own wrapper macro around setlocale() to work around bugs in
198 * Windows' native setlocale() function.
200 extern char *pgwin32_setlocale(int category, const char *locale);
202 #define setlocale(a,b) pgwin32_setlocale(a,b)
205 /* Portable prompt handling */
206 extern void simple_prompt(const char *prompt, char *destination, size_t destlen,
210 #define PG_SIGNAL_COUNT 32
211 #define kill(pid,sig) pgkill(pid,sig)
212 extern int pgkill(int pid, int sig);
215 extern int pclose_check(FILE *stream);
217 /* Global variable holding time zone information. */
218 #if defined(WIN32) || defined(__CYGWIN__)
219 #define TIMEZONE_GLOBAL _timezone
220 #define TZNAME_GLOBAL _tzname
222 #define TIMEZONE_GLOBAL timezone
223 #define TZNAME_GLOBAL tzname
226 #if defined(WIN32) || defined(__CYGWIN__)
228 * Win32 doesn't have reliable rename/unlink during concurrent access.
230 extern int pgrename(const char *from, const char *to);
231 extern int pgunlink(const char *path);
233 /* Include this first so later includes don't see these defines */
238 #define rename(from, to) pgrename(from, to)
239 #define unlink(path) pgunlink(path)
240 #endif /* defined(WIN32) || defined(__CYGWIN__) */
243 * Win32 also doesn't have symlinks, but we can emulate them with
244 * junction points on newer Win32 versions.
246 * Cygwin has its own symlinks which work on Win95/98/ME where
247 * junction points don't, so use those instead. We have no way of
248 * knowing what type of system Cygwin binaries will be run on.
249 * Note: Some CYGWIN includes might #define WIN32.
251 #if defined(WIN32) && !defined(__CYGWIN__)
252 extern int pgsymlink(const char *oldpath, const char *newpath);
253 extern int pgreadlink(const char *path, char *buf, size_t size);
254 extern bool pgwin32_is_junction(const char *path);
256 #define symlink(oldpath, newpath) pgsymlink(oldpath, newpath)
257 #define readlink(path, buf, size) pgreadlink(path, buf, size)
260 extern bool rmtree(const char *path, bool rmtopdir);
263 * stat() is not guaranteed to set the st_size field on win32, so we
264 * redefine it to our own implementation that is.
266 * We must pull in sys/stat.h here so the system header definition
267 * goes in first, and we redefine that, and not the other way around.
269 * Some frontends don't need the size from stat, so if UNSAFE_STAT_OK
270 * is defined we don't bother with this.
272 #if defined(WIN32) && !defined(__CYGWIN__) && !defined(UNSAFE_STAT_OK)
273 #include <sys/stat.h>
274 extern int pgwin32_safestat(const char *path, struct stat *buf);
276 #define stat(a,b) pgwin32_safestat(a,b)
279 #if defined(WIN32) && !defined(__CYGWIN__)
282 * open() and fopen() replacements to allow deletion of open files and
283 * passing of other special options.
285 #define O_DIRECT 0x80000000
286 extern int pgwin32_open(const char *, int,...);
287 extern FILE *pgwin32_fopen(const char *, const char *);
290 #define open(a,b,c) pgwin32_open(a,b,c)
291 #define fopen(a,b) pgwin32_fopen(a,b)
295 * Mingw-w64 headers #define popen and pclose to _popen and _pclose. We want
296 * to use our popen wrapper, rather than plain _popen, so override that. For
297 * consistency, use our version of pclose, too.
307 * system() and popen() replacements to enclose the command in an extra
310 extern int pgwin32_system(const char *command);
311 extern FILE *pgwin32_popen(const char *command, const char *type);
313 #define system(a) pgwin32_system(a)
314 #define popen(a,b) pgwin32_popen(a,b)
315 #define pclose(a) _pclose(a)
317 /* New versions of MingW have gettimeofday, old mingw and msvc don't */
318 #ifndef HAVE_GETTIMEOFDAY
319 /* Last parameter not used */
320 extern int gettimeofday(struct timeval *tp, struct timezone *tzp);
325 * Win32 requires a special close for sockets and pipes, while on Unix
326 * close() does them all.
328 #define closesocket close
332 * On Windows, setvbuf() does not support _IOLBF mode, and interprets that
333 * as _IOFBF. To add insult to injury, setvbuf(file, NULL, _IOFBF, 0)
334 * crashes outright if "parameter validation" is enabled. Therefore, in
335 * places where we'd like to select line-buffered mode, we fall back to
336 * unbuffered mode instead on Windows. Always use PG_IOLBF not _IOLBF
337 * directly in order to implement this behavior.
340 #define PG_IOLBF _IOLBF
342 #define PG_IOLBF _IONBF
346 * Default "extern" declarations or macro substitutes for library routines.
347 * When necessary, these routines are provided by files in src/port/.
350 extern char *crypt(const char *key, const char *setting);
353 /* WIN32 handled in port/win32.h */
355 #define pgoff_t off_t
357 extern int fseeko(FILE *stream, off_t offset, int whence);
358 extern off_t ftello(FILE *stream);
362 extern double pg_erand48(unsigned short xseed[3]);
363 extern long pg_lrand48(void);
364 extern long pg_jrand48(unsigned short xseed[3]);
365 extern void pg_srand48(long seed);
368 extern int fls(int mask);
372 #define fseeko(a, b, c) fseek(a, b, c)
373 #define ftello(a) ftell(a)
376 #if !defined(HAVE_GETPEEREID) && !defined(WIN32)
377 extern int getpeereid(int sock, uid_t *uid, gid_t *gid);
381 extern int isinf(double x);
385 extern char *mkdtemp(char *path);
389 extern double rint(double x);
392 #ifndef HAVE_INET_ATON
393 #include <netinet/in.h>
394 #include <arpa/inet.h>
395 extern int inet_aton(const char *cp, struct in_addr *addr);
398 #if !HAVE_DECL_STRLCAT
399 extern size_t strlcat(char *dst, const char *src, size_t siz);
402 #if !HAVE_DECL_STRLCPY
403 extern size_t strlcpy(char *dst, const char *src, size_t siz);
406 #if !defined(HAVE_RANDOM)
407 extern long random(void);
410 #ifndef HAVE_UNSETENV
411 extern void unsetenv(const char *name);
415 extern void srandom(unsigned int seed);
418 #ifndef HAVE_SSL_GET_CURRENT_COMPRESSION
419 #define SSL_get_current_compression(x) 0
423 extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen);
426 extern int pqGetpwuid(uid_t uid, struct passwd *resultbuf, char *buffer,
427 size_t buflen, struct passwd **result);
430 extern int pqGethostbyname(const char *name,
431 struct hostent *resultbuf,
432 char *buffer, size_t buflen,
433 struct hostent **result,
436 extern void pg_qsort(void *base, size_t nel, size_t elsize,
437 int (*cmp) (const void *, const void *));
438 extern int pg_qsort_strcmp(const void *a, const void *b);
440 #define qsort(a,b,c,d) pg_qsort(a,b,c,d)
442 typedef int (*qsort_arg_comparator) (const void *a, const void *b, void *arg);
444 extern void qsort_arg(void *base, size_t nel, size_t elsize,
445 qsort_arg_comparator cmp, void *arg);
447 /* port/chklocale.c */
448 extern int pg_get_encoding_from_locale(const char *ctype, bool write_message);
450 #if defined(WIN32) && !defined(FRONTEND)
451 extern int pg_codepage_to_encoding(UINT cp);
454 /* port/inet_net_ntop.c */
455 extern char *inet_net_ntop(int af, const void *src, int bits,
456 char *dst, size_t size);
458 /* port/pg_strong_random.c */
459 #ifdef HAVE_STRONG_RANDOM
460 extern bool pg_strong_random(void *buf, size_t len);
463 /* port/pgcheckdir.c */
464 extern int pg_check_dir(const char *dir);
466 /* port/pgmkdirp.c */
467 extern int pg_mkdir_p(char *path, int omode);
469 /* port/pqsignal.c */
470 typedef void (*pqsigfunc) (int signo);
471 extern pqsigfunc pqsignal(int signo, pqsigfunc func);
473 extern pqsigfunc pqsignal_no_restart(int signo, pqsigfunc func);
475 #define pqsignal_no_restart(signo, func) pqsignal(signo, func)
479 extern char *escape_single_quotes_ascii(const char *src);
481 /* port/wait_error.c */
482 extern char *wait_result_to_str(int exit_status);
484 #endif /* PG_PORT_H */