port.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*-------------------------------------------------------------------------
  2. *
  3. * port.h
  4. * Header for src/port/ compatibility functions.
  5. *
  6. * Portions Copyright (c) 1996-2016, 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. #include <ctype.h>
  16. #include <netdb.h>
  17. #include <pwd.h>
  18. /* socket has a different definition on WIN32 */
  19. #ifndef WIN32
  20. typedef int pgsocket;
  21. #define PGINVALID_SOCKET (-1)
  22. #else
  23. typedef SOCKET pgsocket;
  24. #define PGINVALID_SOCKET INVALID_SOCKET
  25. #endif
  26. /* non-blocking */
  27. extern bool pg_set_noblock(pgsocket sock);
  28. extern bool pg_set_block(pgsocket sock);
  29. /* Portable path handling for Unix/Win32 (in path.c) */
  30. extern bool has_drive_prefix(const char *filename);
  31. extern char *first_dir_separator(const char *filename);
  32. extern char *last_dir_separator(const char *filename);
  33. extern char *first_path_var_separator(const char *pathlist);
  34. extern void join_path_components(char *ret_path,
  35. const char *head, const char *tail);
  36. extern void canonicalize_path(char *path);
  37. extern void make_native_path(char *path);
  38. extern void cleanup_path(char *path);
  39. extern bool path_contains_parent_reference(const char *path);
  40. extern bool path_is_relative_and_below_cwd(const char *path);
  41. extern bool path_is_prefix_of_path(const char *path1, const char *path2);
  42. extern char *make_absolute_path(const char *path);
  43. extern const char *get_progname(const char *argv0);
  44. extern void get_share_path(const char *my_exec_path, char *ret_path);
  45. extern void get_etc_path(const char *my_exec_path, char *ret_path);
  46. extern void get_include_path(const char *my_exec_path, char *ret_path);
  47. extern void get_pkginclude_path(const char *my_exec_path, char *ret_path);
  48. extern void get_includeserver_path(const char *my_exec_path, char *ret_path);
  49. extern void get_lib_path(const char *my_exec_path, char *ret_path);
  50. extern void get_pkglib_path(const char *my_exec_path, char *ret_path);
  51. extern void get_locale_path(const char *my_exec_path, char *ret_path);
  52. extern void get_doc_path(const char *my_exec_path, char *ret_path);
  53. extern void get_html_path(const char *my_exec_path, char *ret_path);
  54. extern void get_man_path(const char *my_exec_path, char *ret_path);
  55. extern bool get_home_path(char *ret_path);
  56. extern void get_parent_directory(char *path);
  57. /* common/pgfnames.c */
  58. extern char **pgfnames(const char *path);
  59. extern void pgfnames_cleanup(char **filenames);
  60. /*
  61. * is_absolute_path
  62. *
  63. * By making this a macro we avoid needing to include path.c in libpq.
  64. */
  65. #ifndef WIN32
  66. #define IS_DIR_SEP(ch) ((ch) == '/')
  67. #define is_absolute_path(filename) \
  68. ( \
  69. IS_DIR_SEP((filename)[0]) \
  70. )
  71. #else
  72. #define IS_DIR_SEP(ch) ((ch) == '/' || (ch) == '\\')
  73. /* See path_is_relative_and_below_cwd() for how we handle 'E:abc'. */
  74. #define is_absolute_path(filename) \
  75. ( \
  76. IS_DIR_SEP((filename)[0]) || \
  77. (isalpha((unsigned char) ((filename)[0])) && (filename)[1] == ':' && \
  78. IS_DIR_SEP((filename)[2])) \
  79. )
  80. #endif
  81. /* Portable locale initialization (in exec.c) */
  82. extern void set_pglocale_pgservice(const char *argv0, const char *app);
  83. /* Portable way to find binaries (in exec.c) */
  84. extern int find_my_exec(const char *argv0, char *retpath);
  85. extern int find_other_exec(const char *argv0, const char *target,
  86. const char *versionstr, char *retpath);
  87. /* Windows security token manipulation (in exec.c) */
  88. #ifdef WIN32
  89. extern BOOL AddUserToTokenDacl(HANDLE hToken);
  90. #endif
  91. #if defined(WIN32) || defined(__CYGWIN__)
  92. #define EXE ".exe"
  93. #else
  94. #define EXE ""
  95. #endif
  96. #if defined(WIN32) && !defined(__CYGWIN__)
  97. #define DEVNULL "nul"
  98. #else
  99. #define DEVNULL "/dev/null"
  100. #endif
  101. /* Portable delay handling */
  102. extern void pg_usleep(long microsec);
  103. /* Portable SQL-like case-independent comparisons and conversions */
  104. extern int pg_strcasecmp(const char *s1, const char *s2);
  105. extern int pg_strncasecmp(const char *s1, const char *s2, size_t n);
  106. extern unsigned char pg_toupper(unsigned char ch);
  107. extern unsigned char pg_tolower(unsigned char ch);
  108. extern unsigned char pg_ascii_toupper(unsigned char ch);
  109. extern unsigned char pg_ascii_tolower(unsigned char ch);
  110. #ifdef USE_REPL_SNPRINTF
  111. /*
  112. * Versions of libintl >= 0.13 try to replace printf() and friends with
  113. * macros to their own versions that understand the %$ format. We do the
  114. * same, so disable their macros, if they exist.
  115. */
  116. #ifdef vsnprintf
  117. #undef vsnprintf
  118. #endif
  119. #ifdef snprintf
  120. #undef snprintf
  121. #endif
  122. #ifdef sprintf
  123. #undef sprintf
  124. #endif
  125. #ifdef vfprintf
  126. #undef vfprintf
  127. #endif
  128. #ifdef fprintf
  129. #undef fprintf
  130. #endif
  131. #ifdef printf
  132. #undef printf
  133. #endif
  134. extern int pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args);
  135. extern int pg_snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3, 4);
  136. extern int pg_sprintf(char *str, const char *fmt,...) pg_attribute_printf(2, 3);
  137. extern int pg_vfprintf(FILE *stream, const char *fmt, va_list args);
  138. extern int pg_fprintf(FILE *stream, const char *fmt,...) pg_attribute_printf(2, 3);
  139. extern int pg_printf(const char *fmt,...) pg_attribute_printf(1, 2);
  140. /*
  141. * The GCC-specific code below prevents the pg_attribute_printf above from
  142. * being replaced, and this is required because gcc doesn't know anything
  143. * about pg_printf.
  144. */
  145. #ifdef __GNUC__
  146. #define vsnprintf(...) pg_vsnprintf(__VA_ARGS__)
  147. #define snprintf(...) pg_snprintf(__VA_ARGS__)
  148. #define sprintf(...) pg_sprintf(__VA_ARGS__)
  149. #define vfprintf(...) pg_vfprintf(__VA_ARGS__)
  150. #define fprintf(...) pg_fprintf(__VA_ARGS__)
  151. #define printf(...) pg_printf(__VA_ARGS__)
  152. #else
  153. #define vsnprintf pg_vsnprintf
  154. #define snprintf pg_snprintf
  155. #define sprintf pg_sprintf
  156. #define vfprintf pg_vfprintf
  157. #define fprintf pg_fprintf
  158. #define printf pg_printf
  159. #endif
  160. #endif /* USE_REPL_SNPRINTF */
  161. #if defined(WIN32)
  162. /*
  163. * Versions of libintl >= 0.18? try to replace setlocale() with a macro
  164. * to their own versions. Remove the macro, if it exists, because it
  165. * ends up calling the wrong version when the backend and libintl use
  166. * different versions of msvcrt.
  167. */
  168. #if defined(setlocale)
  169. #undef setlocale
  170. #endif
  171. /*
  172. * Define our own wrapper macro around setlocale() to work around bugs in
  173. * Windows' native setlocale() function.
  174. */
  175. extern char *pgwin32_setlocale(int category, const char *locale);
  176. #define setlocale(a,b) pgwin32_setlocale(a,b)
  177. #endif /* WIN32 */
  178. /* Portable prompt handling */
  179. extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
  180. #ifdef WIN32
  181. #define PG_SIGNAL_COUNT 32
  182. #define kill(pid,sig) pgkill(pid,sig)
  183. extern int pgkill(int pid, int sig);
  184. #endif
  185. extern int pclose_check(FILE *stream);
  186. /* Global variable holding time zone information. */
  187. #if defined(WIN32) || defined(__CYGWIN__)
  188. #define TIMEZONE_GLOBAL _timezone
  189. #define TZNAME_GLOBAL _tzname
  190. #else
  191. #define TIMEZONE_GLOBAL timezone
  192. #define TZNAME_GLOBAL tzname
  193. #endif
  194. #if defined(WIN32) || defined(__CYGWIN__)
  195. /*
  196. * Win32 doesn't have reliable rename/unlink during concurrent access.
  197. */
  198. extern int pgrename(const char *from, const char *to);
  199. extern int pgunlink(const char *path);
  200. /* Include this first so later includes don't see these defines */
  201. #ifdef WIN32_ONLY_COMPILER
  202. #include <io.h>
  203. #endif
  204. #define rename(from, to) pgrename(from, to)
  205. #define unlink(path) pgunlink(path)
  206. #endif /* defined(WIN32) || defined(__CYGWIN__) */
  207. /*
  208. * Win32 also doesn't have symlinks, but we can emulate them with
  209. * junction points on newer Win32 versions.
  210. *
  211. * Cygwin has its own symlinks which work on Win95/98/ME where
  212. * junction points don't, so use those instead. We have no way of
  213. * knowing what type of system Cygwin binaries will be run on.
  214. * Note: Some CYGWIN includes might #define WIN32.
  215. */
  216. #if defined(WIN32) && !defined(__CYGWIN__)
  217. extern int pgsymlink(const char *oldpath, const char *newpath);
  218. extern int pgreadlink(const char *path, char *buf, size_t size);
  219. extern bool pgwin32_is_junction(char *path);
  220. #define symlink(oldpath, newpath) pgsymlink(oldpath, newpath)
  221. #define readlink(path, buf, size) pgreadlink(path, buf, size)
  222. #endif
  223. extern bool rmtree(const char *path, bool rmtopdir);
  224. /*
  225. * stat() is not guaranteed to set the st_size field on win32, so we
  226. * redefine it to our own implementation that is.
  227. *
  228. * We must pull in sys/stat.h here so the system header definition
  229. * goes in first, and we redefine that, and not the other way around.
  230. *
  231. * Some frontends don't need the size from stat, so if UNSAFE_STAT_OK
  232. * is defined we don't bother with this.
  233. */
  234. #if defined(WIN32) && !defined(__CYGWIN__) && !defined(UNSAFE_STAT_OK)
  235. #include <sys/stat.h>
  236. extern int pgwin32_safestat(const char *path, struct stat * buf);
  237. #define stat(a,b) pgwin32_safestat(a,b)
  238. #endif
  239. #if defined(WIN32) && !defined(__CYGWIN__)
  240. /*
  241. * open() and fopen() replacements to allow deletion of open files and
  242. * passing of other special options.
  243. */
  244. #define O_DIRECT 0x80000000
  245. extern int pgwin32_open(const char *, int,...);
  246. extern FILE *pgwin32_fopen(const char *, const char *);
  247. #ifndef FRONTEND
  248. #define open(a,b,c) pgwin32_open(a,b,c)
  249. #define fopen(a,b) pgwin32_fopen(a,b)
  250. #endif
  251. /*
  252. * Mingw-w64 headers #define popen and pclose to _popen and _pclose. We want
  253. * to use our popen wrapper, rather than plain _popen, so override that. For
  254. * consistency, use our version of pclose, too.
  255. */
  256. #ifdef popen
  257. #undef popen
  258. #endif
  259. #ifdef pclose
  260. #undef pclose
  261. #endif
  262. /*
  263. * system() and popen() replacements to enclose the command in an extra
  264. * pair of quotes.
  265. */
  266. extern int pgwin32_system(const char *command);
  267. extern FILE *pgwin32_popen(const char *command, const char *type);
  268. #define system(a) pgwin32_system(a)
  269. #define popen(a,b) pgwin32_popen(a,b)
  270. #define pclose(a) _pclose(a)
  271. /* New versions of MingW have gettimeofday, old mingw and msvc don't */
  272. #ifndef HAVE_GETTIMEOFDAY
  273. /* Last parameter not used */
  274. extern int gettimeofday(struct timeval * tp, struct timezone * tzp);
  275. #endif
  276. #else /* !WIN32 */
  277. /*
  278. * Win32 requires a special close for sockets and pipes, while on Unix
  279. * close() does them all.
  280. */
  281. #define closesocket close
  282. #endif /* WIN32 */
  283. /*
  284. * On Windows, setvbuf() does not support _IOLBF mode, and interprets that
  285. * as _IOFBF. To add insult to injury, setvbuf(file, NULL, _IOFBF, 0)
  286. * crashes outright if "parameter validation" is enabled. Therefore, in
  287. * places where we'd like to select line-buffered mode, we fall back to
  288. * unbuffered mode instead on Windows. Always use PG_IOLBF not _IOLBF
  289. * directly in order to implement this behavior.
  290. */
  291. #ifndef WIN32
  292. #define PG_IOLBF _IOLBF
  293. #else
  294. #define PG_IOLBF _IONBF
  295. #endif
  296. /*
  297. * Default "extern" declarations or macro substitutes for library routines.
  298. * When necessary, these routines are provided by files in src/port/.
  299. */
  300. #ifndef HAVE_CRYPT
  301. extern char *crypt(const char *key, const char *setting);
  302. #endif
  303. /* WIN32 handled in port/win32.h */
  304. #ifndef WIN32
  305. #define pgoff_t off_t
  306. #ifdef __NetBSD__
  307. extern int fseeko(FILE *stream, off_t offset, int whence);
  308. extern off_t ftello(FILE *stream);
  309. #endif
  310. #endif
  311. extern double pg_erand48(unsigned short xseed[3]);
  312. extern long pg_lrand48(void);
  313. extern void pg_srand48(long seed);
  314. #ifndef HAVE_FLS
  315. extern int fls(int mask);
  316. #endif
  317. #ifndef HAVE_FSEEKO
  318. #define fseeko(a, b, c) fseek(a, b, c)
  319. #define ftello(a) ftell(a)
  320. #endif
  321. #if !defined(HAVE_GETPEEREID) && !defined(WIN32)
  322. extern int getpeereid(int sock, uid_t *uid, gid_t *gid);
  323. #endif
  324. #ifndef HAVE_ISINF
  325. extern int isinf(double x);
  326. #endif
  327. #ifndef HAVE_MKDTEMP
  328. extern char *mkdtemp(char *path);
  329. #endif
  330. #ifndef HAVE_RINT
  331. extern double rint(double x);
  332. #endif
  333. #ifndef HAVE_INET_ATON
  334. #include <netinet/in.h>
  335. #include <arpa/inet.h>
  336. extern int inet_aton(const char *cp, struct in_addr * addr);
  337. #endif
  338. #if !HAVE_DECL_STRLCAT
  339. extern size_t strlcat(char *dst, const char *src, size_t siz);
  340. #endif
  341. #if !HAVE_DECL_STRLCPY
  342. extern size_t strlcpy(char *dst, const char *src, size_t siz);
  343. #endif
  344. #if !defined(HAVE_RANDOM) && !defined(__BORLANDC__)
  345. extern long random(void);
  346. #endif
  347. #ifndef HAVE_UNSETENV
  348. extern void unsetenv(const char *name);
  349. #endif
  350. #ifndef HAVE_SRANDOM
  351. extern void srandom(unsigned int seed);
  352. #endif
  353. #ifndef HAVE_SSL_GET_CURRENT_COMPRESSION
  354. #define SSL_get_current_compression(x) 0
  355. #endif
  356. /* thread.h */
  357. extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen);
  358. #ifndef WIN32
  359. extern int pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
  360. size_t buflen, struct passwd ** result);
  361. #endif
  362. extern int pqGethostbyname(const char *name,
  363. struct hostent * resultbuf,
  364. char *buffer, size_t buflen,
  365. struct hostent ** result,
  366. int *herrno);
  367. extern void pg_qsort(void *base, size_t nel, size_t elsize,
  368. int (*cmp) (const void *, const void *));
  369. extern int pg_qsort_strcmp(const void *a, const void *b);
  370. #define qsort(a,b,c,d) pg_qsort(a,b,c,d)
  371. typedef int (*qsort_arg_comparator) (const void *a, const void *b, void *arg);
  372. extern void qsort_arg(void *base, size_t nel, size_t elsize,
  373. qsort_arg_comparator cmp, void *arg);
  374. /* port/chklocale.c */
  375. extern int pg_get_encoding_from_locale(const char *ctype, bool write_message);
  376. #if defined(WIN32) && !defined(FRONTEND)
  377. extern int pg_codepage_to_encoding(UINT cp);
  378. #endif
  379. /* port/inet_net_ntop.c */
  380. extern char *inet_net_ntop(int af, const void *src, int bits,
  381. char *dst, size_t size);
  382. /* port/pgcheckdir.c */
  383. extern int pg_check_dir(const char *dir);
  384. /* port/pgmkdirp.c */
  385. extern int pg_mkdir_p(char *path, int omode);
  386. /* port/pqsignal.c */
  387. typedef void (*pqsigfunc) (int signo);
  388. extern pqsigfunc pqsignal(int signo, pqsigfunc func);
  389. #ifndef WIN32
  390. extern pqsigfunc pqsignal_no_restart(int signo, pqsigfunc func);
  391. #else
  392. #define pqsignal_no_restart(signo, func) pqsignal(signo, func)
  393. #endif
  394. /* port/quotes.c */
  395. extern char *escape_single_quotes_ascii(const char *src);
  396. /* port/wait_error.c */
  397. extern char *wait_result_to_str(int exit_status);
  398. #endif /* PG_PORT_H */