1 /*-------------------------------------------------------------------------
4 * Header for src/port/ compatibility functions.
6 * Portions Copyright (c) 1996-2014, 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 bool path_contains_parent_reference(const char *path);
46 extern bool path_is_relative_and_below_cwd(const char *path);
47 extern bool path_is_prefix_of_path(const char *path1, const char *path2);
48 extern char *make_absolute_path(const char *path);
49 extern const char *get_progname(const char *argv0);
50 extern void get_share_path(const char *my_exec_path, char *ret_path);
51 extern void get_etc_path(const char *my_exec_path, char *ret_path);
52 extern void get_include_path(const char *my_exec_path, char *ret_path);
53 extern void get_pkginclude_path(const char *my_exec_path, char *ret_path);
54 extern void get_includeserver_path(const char *my_exec_path, char *ret_path);
55 extern void get_lib_path(const char *my_exec_path, char *ret_path);
56 extern void get_pkglib_path(const char *my_exec_path, char *ret_path);
57 extern void get_locale_path(const char *my_exec_path, char *ret_path);
58 extern void get_doc_path(const char *my_exec_path, char *ret_path);
59 extern void get_html_path(const char *my_exec_path, char *ret_path);
60 extern void get_man_path(const char *my_exec_path, char *ret_path);
61 extern bool get_home_path(char *ret_path);
62 extern void get_parent_directory(char *path);
65 extern char **pgfnames(const char *path);
66 extern void pgfnames_cleanup(char **filenames);
71 * By making this a macro we avoid needing to include path.c in libpq.
74 #define IS_DIR_SEP(ch) ((ch) == '/')
76 #define is_absolute_path(filename) \
78 IS_DIR_SEP((filename)[0]) \
81 #define IS_DIR_SEP(ch) ((ch) == '/' || (ch) == '\\')
83 /* See path_is_relative_and_below_cwd() for how we handle 'E:abc'. */
84 #define is_absolute_path(filename) \
86 IS_DIR_SEP((filename)[0]) || \
87 (isalpha((unsigned char) ((filename)[0])) && (filename)[1] == ':' && \
88 IS_DIR_SEP((filename)[2])) \
92 /* Portable locale initialization (in exec.c) */
93 extern void set_pglocale_pgservice(const char *argv0, const char *app);
95 /* Portable way to find binaries (in exec.c) */
96 extern int find_my_exec(const char *argv0, char *retpath);
97 extern int find_other_exec(const char *argv0, const char *target,
98 const char *versionstr, char *retpath);
100 /* Windows security token manipulation (in exec.c) */
102 extern BOOL AddUserToTokenDacl(HANDLE hToken);
106 #if defined(WIN32) || defined(__CYGWIN__)
112 #if defined(WIN32) && !defined(__CYGWIN__)
113 #define DEVNULL "nul"
115 #define DEVNULL "/dev/null"
119 * Win32 needs double quotes at the beginning and end of system()
120 * strings. If not, it gets confused with multiple quoted strings.
121 * It also requires double-quotes around the executable name and
122 * any files used for redirection. Other args can use single-quotes.
124 * Generated using Win32 "CMD /?":
126 * 1. If all of the following conditions are met, then quote characters
127 * on the command line are preserved:
130 * - exactly two quote characters
131 * - no special characters between the two quote characters, where special
132 * is one of: &<>()@^|
133 * - there are one or more whitespace characters between the two quote
135 * - the string between the two quote characters is the name of an
138 * 2. Otherwise, old behavior is to see if the first character is a quote
139 * character and if so, strip the leading character and remove the last
140 * quote character on the command line, preserving any text after the last
143 #if defined(WIN32) && !defined(__CYGWIN__)
144 #define SYSTEMQUOTE "\""
146 #define SYSTEMQUOTE ""
149 /* Portable delay handling */
150 extern void pg_usleep(long microsec);
152 /* Portable SQL-like case-independent comparisons and conversions */
153 extern int pg_strcasecmp(const char *s1, const char *s2);
154 extern int pg_strncasecmp(const char *s1, const char *s2, size_t n);
155 extern unsigned char pg_toupper(unsigned char ch);
156 extern unsigned char pg_tolower(unsigned char ch);
157 extern unsigned char pg_ascii_toupper(unsigned char ch);
158 extern unsigned char pg_ascii_tolower(unsigned char ch);
160 #ifdef USE_REPL_SNPRINTF
163 * Versions of libintl >= 0.13 try to replace printf() and friends with
164 * macros to their own versions that understand the %$ format. We do the
165 * same, so disable their macros, if they exist.
186 extern int pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args);
188 pg_snprintf(char *str, size_t count, const char *fmt,...)
189 /* This extension allows gcc to check the format string */
190 __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
192 pg_sprintf(char *str, const char *fmt,...)
193 /* This extension allows gcc to check the format string */
194 __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
195 extern int pg_vfprintf(FILE *stream, const char *fmt, va_list args);
197 pg_fprintf(FILE *stream, const char *fmt,...)
198 /* This extension allows gcc to check the format string */
199 __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
201 pg_printf(const char *fmt,...)
202 /* This extension allows gcc to check the format string */
203 __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
206 * The GCC-specific code below prevents the __attribute__(... 'printf')
207 * above from being replaced, and this is required because gcc doesn't
208 * know anything about pg_printf.
211 #define vsnprintf(...) pg_vsnprintf(__VA_ARGS__)
212 #define snprintf(...) pg_snprintf(__VA_ARGS__)
213 #define sprintf(...) pg_sprintf(__VA_ARGS__)
214 #define vfprintf(...) pg_vfprintf(__VA_ARGS__)
215 #define fprintf(...) pg_fprintf(__VA_ARGS__)
216 #define printf(...) pg_printf(__VA_ARGS__)
218 #define vsnprintf pg_vsnprintf
219 #define snprintf pg_snprintf
220 #define sprintf pg_sprintf
221 #define vfprintf pg_vfprintf
222 #define fprintf pg_fprintf
223 #define printf pg_printf
225 #endif /* USE_REPL_SNPRINTF */
229 * Versions of libintl >= 0.18? try to replace setlocale() with a macro
230 * to their own versions. Remove the macro, if it exists, because it
231 * ends up calling the wrong version when the backend and libintl use
232 * different versions of msvcrt.
234 #if defined(setlocale)
239 * Define our own wrapper macro around setlocale() to work around bugs in
240 * Windows' native setlocale() function.
242 extern char *pgwin32_setlocale(int category, const char *locale);
244 #define setlocale(a,b) pgwin32_setlocale(a,b)
247 /* Portable prompt handling */
248 extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
251 #define PG_SIGNAL_COUNT 32
252 #define kill(pid,sig) pgkill(pid,sig)
253 extern int pgkill(int pid, int sig);
256 extern int pclose_check(FILE *stream);
258 /* Global variable holding time zone information. */
260 #define TIMEZONE_GLOBAL timezone
261 #define TZNAME_GLOBAL tzname
263 #define TIMEZONE_GLOBAL _timezone
264 #define TZNAME_GLOBAL _tzname
267 #if defined(WIN32) || defined(__CYGWIN__)
269 * Win32 doesn't have reliable rename/unlink during concurrent access.
271 extern int pgrename(const char *from, const char *to);
272 extern int pgunlink(const char *path);
274 /* Include this first so later includes don't see these defines */
275 #ifdef WIN32_ONLY_COMPILER
279 #define rename(from, to) pgrename(from, to)
280 #define unlink(path) pgunlink(path)
281 #endif /* defined(WIN32) || defined(__CYGWIN__) */
284 * Win32 also doesn't have symlinks, but we can emulate them with
285 * junction points on newer Win32 versions.
287 * Cygwin has its own symlinks which work on Win95/98/ME where
288 * junction points don't, so use those instead. We have no way of
289 * knowing what type of system Cygwin binaries will be run on.
290 * Note: Some CYGWIN includes might #define WIN32.
292 #if defined(WIN32) && !defined(__CYGWIN__)
293 extern int pgsymlink(const char *oldpath, const char *newpath);
294 extern int pgreadlink(const char *path, char *buf, size_t size);
295 extern bool pgwin32_is_junction(char *path);
297 #define symlink(oldpath, newpath) pgsymlink(oldpath, newpath)
298 #define readlink(path, buf, size) pgreadlink(path, buf, size)
301 extern bool rmtree(const char *path, bool rmtopdir);
304 * stat() is not guaranteed to set the st_size field on win32, so we
305 * redefine it to our own implementation that is.
307 * We must pull in sys/stat.h here so the system header definition
308 * goes in first, and we redefine that, and not the other way around.
310 * Some frontends don't need the size from stat, so if UNSAFE_STAT_OK
311 * is defined we don't bother with this.
313 #if defined(WIN32) && !defined(__CYGWIN__) && !defined(UNSAFE_STAT_OK)
314 #include <sys/stat.h>
315 extern int pgwin32_safestat(const char *path, struct stat * buf);
317 #define stat(a,b) pgwin32_safestat(a,b)
320 #if defined(WIN32) && !defined(__CYGWIN__)
323 * open() and fopen() replacements to allow deletion of open files and
324 * passing of other special options.
326 #define O_DIRECT 0x80000000
327 extern int pgwin32_open(const char *, int,...);
328 extern FILE *pgwin32_fopen(const char *, const char *);
331 #define open(a,b,c) pgwin32_open(a,b,c)
332 #define fopen(a,b) pgwin32_fopen(a,b)
336 #define popen(a,b) _popen(a,b)
339 #define pclose(a) _pclose(a)
342 /* New versions of MingW have gettimeofday, old mingw and msvc don't */
343 #ifndef HAVE_GETTIMEOFDAY
344 /* Last parameter not used */
345 extern int gettimeofday(struct timeval * tp, struct timezone * tzp);
350 * Win32 requires a special close for sockets and pipes, while on Unix
351 * close() does them all.
353 #define closesocket close
357 * Default "extern" declarations or macro substitutes for library routines.
358 * When necessary, these routines are provided by files in src/port/.
361 extern char *crypt(const char *key, const char *setting);
364 /* WIN32 handled in port/win32.h */
366 #define pgoff_t off_t
368 extern int fseeko(FILE *stream, off_t offset, int whence);
369 extern off_t ftello(FILE *stream);
373 extern double pg_erand48(unsigned short xseed[3]);
374 extern long pg_lrand48(void);
375 extern void pg_srand48(long seed);
378 extern int fls(int mask);
382 #define fseeko(a, b, c) fseek(a, b, c)
383 #define ftello(a) ftell(a)
386 #if !defined(HAVE_GETPEEREID) && !defined(WIN32)
387 extern int getpeereid(int sock, uid_t *uid, gid_t *gid);
391 extern int isinf(double x);
395 extern double rint(double x);
398 #ifndef HAVE_INET_ATON
399 #include <netinet/in.h>
400 #include <arpa/inet.h>
401 extern int inet_aton(const char *cp, struct in_addr * addr);
404 #if !HAVE_DECL_STRLCAT
405 extern size_t strlcat(char *dst, const char *src, size_t siz);
408 #if !HAVE_DECL_STRLCPY
409 extern size_t strlcpy(char *dst, const char *src, size_t siz);
412 #if !defined(HAVE_RANDOM) && !defined(__BORLANDC__)
413 extern long random(void);
416 #ifndef HAVE_UNSETENV
417 extern void unsetenv(const char *name);
421 extern void srandom(unsigned int seed);
425 extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen);
427 #if !defined(WIN32) || defined(__CYGWIN__)
428 extern int pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
429 size_t buflen, struct passwd ** result);
432 extern int pqGethostbyname(const char *name,
433 struct hostent * resultbuf,
434 char *buffer, size_t buflen,
435 struct hostent ** result,
438 extern void pg_qsort(void *base, size_t nel, size_t elsize,
439 int (*cmp) (const void *, const void *));
440 extern int pg_qsort_strcmp(const void *a, const void *b);
442 #define qsort(a,b,c,d) pg_qsort(a,b,c,d)
444 typedef int (*qsort_arg_comparator) (const void *a, const void *b, void *arg);
446 extern void qsort_arg(void *base, size_t nel, size_t elsize,
447 qsort_arg_comparator cmp, void *arg);
449 /* port/chklocale.c */
450 extern int pg_get_encoding_from_locale(const char *ctype, bool write_message);
452 #if defined(WIN32) && !defined(FRONTEND)
453 extern int pg_codepage_to_encoding(UINT cp);
456 /* port/inet_net_ntop.c */
457 extern char *inet_net_ntop(int af, const void *src, int bits,
458 char *dst, size_t size);
460 /* port/pgcheckdir.c */
461 extern int pg_check_dir(const char *dir);
463 /* port/pgmkdirp.c */
464 extern int pg_mkdir_p(char *path, int omode);
466 /* port/pqsignal.c */
467 typedef void (*pqsigfunc) (int signo);
468 extern pqsigfunc pqsignal(int signo, pqsigfunc func);
471 extern char *escape_single_quotes_ascii(const char *src);
473 /* port/wait_error.c */
474 extern char *wait_result_to_str(int exit_status);
476 #endif /* PG_PORT_H */