gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

542 lines
16KB

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