PageRenderTime 28ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/RPostgreSQL/src/libpq/port.h

http://rpostgresql.googlecode.com/
C Header | 489 lines | 281 code | 75 blank | 133 comment | 20 complexity | e7629acdfe627335381defd82d6c6d36 MD5 | raw file
  1. /*-------------------------------------------------------------------------
  2. *
  3. * port.h
  4. * Header for src/port/ compatibility functions.
  5. *
  6. * Portions Copyright (c) 1996-2011, 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 char *first_dir_separator(const char *filename);
  31. extern char *last_dir_separator(const char *filename);
  32. extern char *first_path_var_separator(const char *pathlist);
  33. extern void join_path_components(char *ret_path,
  34. const char *head, const char *tail);
  35. extern void canonicalize_path(char *path);
  36. extern void make_native_path(char *path);
  37. extern bool path_contains_parent_reference(const char *path);
  38. extern bool path_is_relative_and_below_cwd(const char *path);
  39. extern bool path_is_prefix_of_path(const char *path1, const char *path2);
  40. extern const char *get_progname(const char *argv0);
  41. extern void get_share_path(const char *my_exec_path, char *ret_path);
  42. extern void get_etc_path(const char *my_exec_path, char *ret_path);
  43. extern void get_include_path(const char *my_exec_path, char *ret_path);
  44. extern void get_pkginclude_path(const char *my_exec_path, char *ret_path);
  45. extern void get_includeserver_path(const char *my_exec_path, char *ret_path);
  46. extern void get_lib_path(const char *my_exec_path, char *ret_path);
  47. extern void get_pkglib_path(const char *my_exec_path, char *ret_path);
  48. extern void get_locale_path(const char *my_exec_path, char *ret_path);
  49. extern void get_doc_path(const char *my_exec_path, char *ret_path);
  50. extern void get_html_path(const char *my_exec_path, char *ret_path);
  51. extern void get_man_path(const char *my_exec_path, char *ret_path);
  52. extern bool get_home_path(char *ret_path);
  53. extern void get_parent_directory(char *path);
  54. /* port/dirmod.c */
  55. extern char **pgfnames(const char *path);
  56. extern void pgfnames_cleanup(char **filenames);
  57. /*
  58. * is_absolute_path
  59. *
  60. * By making this a macro we avoid needing to include path.c in libpq.
  61. */
  62. #ifndef WIN32
  63. #define IS_DIR_SEP(ch) ((ch) == '/')
  64. #define is_absolute_path(filename) \
  65. ( \
  66. IS_DIR_SEP((filename)[0]) \
  67. )
  68. #else
  69. #define IS_DIR_SEP(ch) ((ch) == '/' || (ch) == '\\')
  70. /* See path_is_relative_and_below_cwd() for how we handle 'E:abc'. */
  71. #define is_absolute_path(filename) \
  72. ( \
  73. IS_DIR_SEP((filename)[0]) || \
  74. (isalpha((unsigned char) ((filename)[0])) && (filename)[1] == ':' && \
  75. IS_DIR_SEP((filename)[2])) \
  76. )
  77. #endif
  78. /* Portable locale initialization (in exec.c) */
  79. extern void set_pglocale_pgservice(const char *argv0, const char *app);
  80. /* Portable way to find binaries (in exec.c) */
  81. extern int find_my_exec(const char *argv0, char *retpath);
  82. extern int find_other_exec(const char *argv0, const char *target,
  83. const char *versionstr, char *retpath);
  84. /* Windows security token manipulation (in exec.c) */
  85. #ifdef WIN32
  86. extern BOOL AddUserToTokenDacl(HANDLE hToken);
  87. #endif
  88. #if defined(WIN32) || defined(__CYGWIN__)
  89. #define EXE ".exe"
  90. #else
  91. #define EXE ""
  92. #endif
  93. #if defined(WIN32) && !defined(__CYGWIN__)
  94. #define DEVNULL "nul"
  95. /* "con" does not work from the Msys 1.0.10 console (part of MinGW). */
  96. #define DEVTTY "con"
  97. #else
  98. #define DEVNULL "/dev/null"
  99. #define DEVTTY "/dev/tty"
  100. #endif
  101. /*
  102. * Win32 needs double quotes at the beginning and end of system()
  103. * strings. If not, it gets confused with multiple quoted strings.
  104. * It also requires double-quotes around the executable name and
  105. * any files used for redirection. Other args can use single-quotes.
  106. *
  107. * Generated using Win32 "CMD /?":
  108. *
  109. * 1. If all of the following conditions are met, then quote characters
  110. * on the command line are preserved:
  111. *
  112. * - no /S switch
  113. * - exactly two quote characters
  114. * - no special characters between the two quote characters, where special
  115. * is one of: &<>()@^|
  116. * - there are one or more whitespace characters between the two quote
  117. * characters
  118. * - the string between the two quote characters is the name of an
  119. * executable file.
  120. *
  121. * 2. Otherwise, old behavior is to see if the first character is a quote
  122. * character and if so, strip the leading character and remove the last
  123. * quote character on the command line, preserving any text after the last
  124. * quote character.
  125. */
  126. #if defined(WIN32) && !defined(__CYGWIN__)
  127. #define SYSTEMQUOTE "\""
  128. #else
  129. #define SYSTEMQUOTE ""
  130. #endif
  131. /* Portable delay handling */
  132. extern void pg_usleep(long microsec);
  133. /* Portable SQL-like case-independent comparisons and conversions */
  134. extern int pg_strcasecmp(const char *s1, const char *s2);
  135. extern int pg_strncasecmp(const char *s1, const char *s2, size_t n);
  136. extern unsigned char pg_toupper(unsigned char ch);
  137. extern unsigned char pg_tolower(unsigned char ch);
  138. extern unsigned char pg_ascii_toupper(unsigned char ch);
  139. extern unsigned char pg_ascii_tolower(unsigned char ch);
  140. #ifdef USE_REPL_SNPRINTF
  141. /*
  142. * Versions of libintl >= 0.13 try to replace printf() and friends with
  143. * macros to their own versions that understand the %$ format. We do the
  144. * same, so disable their macros, if they exist.
  145. */
  146. #ifdef vsnprintf
  147. #undef vsnprintf
  148. #endif
  149. #ifdef snprintf
  150. #undef snprintf
  151. #endif
  152. #ifdef sprintf
  153. #undef sprintf
  154. #endif
  155. #ifdef vfprintf
  156. #undef vfprintf
  157. #endif
  158. #ifdef fprintf
  159. #undef fprintf
  160. #endif
  161. #ifdef printf
  162. #undef printf
  163. #endif
  164. extern int pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args);
  165. extern int
  166. pg_snprintf(char *str, size_t count, const char *fmt,...)
  167. /* This extension allows gcc to check the format string */
  168. __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
  169. extern int
  170. pg_sprintf(char *str, const char *fmt,...)
  171. /* This extension allows gcc to check the format string */
  172. __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
  173. extern int pg_vfprintf(FILE *stream, const char *fmt, va_list args);
  174. extern int
  175. pg_fprintf(FILE *stream, const char *fmt,...)
  176. /* This extension allows gcc to check the format string */
  177. __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
  178. extern int
  179. pg_printf(const char *fmt,...)
  180. /* This extension allows gcc to check the format string */
  181. __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
  182. /*
  183. * The GCC-specific code below prevents the __attribute__(... 'printf')
  184. * above from being replaced, and this is required because gcc doesn't
  185. * know anything about pg_printf.
  186. */
  187. #ifdef __GNUC__
  188. #define vsnprintf(...) pg_vsnprintf(__VA_ARGS__)
  189. #define snprintf(...) pg_snprintf(__VA_ARGS__)
  190. #define sprintf(...) pg_sprintf(__VA_ARGS__)
  191. #define vfprintf(...) pg_vfprintf(__VA_ARGS__)
  192. #define fprintf(...) pg_fprintf(__VA_ARGS__)
  193. #define printf(...) pg_printf(__VA_ARGS__)
  194. #else
  195. #define vsnprintf pg_vsnprintf
  196. #define snprintf pg_snprintf
  197. #define sprintf pg_sprintf
  198. #define vfprintf pg_vfprintf
  199. #define fprintf pg_fprintf
  200. #define printf pg_printf
  201. #endif
  202. #endif /* USE_REPL_SNPRINTF */
  203. #if defined(WIN32)
  204. /*
  205. * Versions of libintl >= 0.18? try to replace setlocale() with a macro
  206. * to their own versions. Remove the macro, if it exists, because it
  207. * ends up calling the wrong version when the backend and libintl use
  208. * different versions of msvcrt.
  209. */
  210. #if defined(setlocale)
  211. #undef setlocale
  212. #endif
  213. /*
  214. * Define our own wrapper macro around setlocale() to work around bugs in
  215. * Windows' native setlocale() function.
  216. */
  217. extern char *pgwin32_setlocale(int category, const char *locale);
  218. #define setlocale(a,b) pgwin32_setlocale(a,b)
  219. #endif /* WIN32 */
  220. /* Portable prompt handling */
  221. extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
  222. /*
  223. * WIN32 doesn't allow descriptors returned by pipe() to be used in select(),
  224. * so for that platform we use socket() instead of pipe().
  225. * There is some inconsistency here because sometimes we require pg*, like
  226. * pgpipe, but in other cases we define rename to pgrename just on Win32.
  227. */
  228. #ifndef WIN32
  229. /*
  230. * The function prototypes are not supplied because every C file
  231. * includes this file.
  232. */
  233. #define pgpipe(a) pipe(a)
  234. #define piperead(a,b,c) read(a,b,c)
  235. #define pipewrite(a,b,c) write(a,b,c)
  236. #else
  237. extern int pgpipe(int handles[2]);
  238. extern int piperead(int s, char *buf, int len);
  239. #define pipewrite(a,b,c) send(a,b,c,0)
  240. #define PG_SIGNAL_COUNT 32
  241. #define kill(pid,sig) pgkill(pid,sig)
  242. extern int pgkill(int pid, int sig);
  243. #endif
  244. extern int pclose_check(FILE *stream);
  245. /* Global variable holding time zone information. */
  246. #ifndef __CYGWIN__
  247. #define TIMEZONE_GLOBAL timezone
  248. #define TZNAME_GLOBAL tzname
  249. #else
  250. #define TIMEZONE_GLOBAL _timezone
  251. #define TZNAME_GLOBAL _tzname
  252. #endif
  253. #if defined(WIN32) || defined(__CYGWIN__)
  254. /*
  255. * Win32 doesn't have reliable rename/unlink during concurrent access.
  256. */
  257. extern int pgrename(const char *from, const char *to);
  258. extern int pgunlink(const char *path);
  259. /* Include this first so later includes don't see these defines */
  260. #ifdef WIN32_ONLY_COMPILER
  261. #include <io.h>
  262. #endif
  263. #define rename(from, to) pgrename(from, to)
  264. #define unlink(path) pgunlink(path)
  265. #endif /* defined(WIN32) || defined(__CYGWIN__) */
  266. /*
  267. * Win32 also doesn't have symlinks, but we can emulate them with
  268. * junction points on newer Win32 versions.
  269. *
  270. * Cygwin has its own symlinks which work on Win95/98/ME where
  271. * junction points don't, so use those instead. We have no way of
  272. * knowing what type of system Cygwin binaries will be run on.
  273. * Note: Some CYGWIN includes might #define WIN32.
  274. */
  275. #if defined(WIN32) && !defined(__CYGWIN__)
  276. extern int pgsymlink(const char *oldpath, const char *newpath);
  277. extern int pgreadlink(const char *path, char *buf, size_t size);
  278. extern bool pgwin32_is_junction(char *path);
  279. #define symlink(oldpath, newpath) pgsymlink(oldpath, newpath)
  280. #define readlink(path, buf, size) pgreadlink(path, buf, size)
  281. #endif
  282. extern bool rmtree(const char *path, bool rmtopdir);
  283. /*
  284. * stat() is not guaranteed to set the st_size field on win32, so we
  285. * redefine it to our own implementation that is.
  286. *
  287. * We must pull in sys/stat.h here so the system header definition
  288. * goes in first, and we redefine that, and not the other way around.
  289. *
  290. * Some frontends don't need the size from stat, so if UNSAFE_STAT_OK
  291. * is defined we don't bother with this.
  292. */
  293. #if defined(WIN32) && !defined(__CYGWIN__) && !defined(UNSAFE_STAT_OK)
  294. #include <sys/stat.h>
  295. extern int pgwin32_safestat(const char *path, struct stat * buf);
  296. #define stat(a,b) pgwin32_safestat(a,b)
  297. #endif
  298. #if defined(WIN32) && !defined(__CYGWIN__)
  299. /*
  300. * open() and fopen() replacements to allow deletion of open files and
  301. * passing of other special options.
  302. */
  303. #define O_DIRECT 0x80000000
  304. extern int pgwin32_open(const char *, int,...);
  305. extern FILE *pgwin32_fopen(const char *, const char *);
  306. #ifndef FRONTEND
  307. #define open(a,b,c) pgwin32_open(a,b,c)
  308. #define fopen(a,b) pgwin32_fopen(a,b)
  309. #endif
  310. #ifndef popen
  311. #define popen(a,b) _popen(a,b)
  312. #endif
  313. #ifndef pclose
  314. #define pclose(a) _pclose(a)
  315. #endif
  316. /* New versions of MingW have gettimeofday, old mingw and msvc don't */
  317. #ifndef HAVE_GETTIMEOFDAY
  318. /* Last parameter not used */
  319. extern int gettimeofday(struct timeval * tp, struct timezone * tzp);
  320. #endif
  321. #else /* !WIN32 */
  322. /*
  323. * Win32 requires a special close for sockets and pipes, while on Unix
  324. * close() does them all.
  325. */
  326. #define closesocket close
  327. #endif /* WIN32 */
  328. /*
  329. * Default "extern" declarations or macro substitutes for library routines.
  330. * When necessary, these routines are provided by files in src/port/.
  331. */
  332. #ifndef HAVE_CRYPT
  333. extern char *crypt(const char *key, const char *setting);
  334. #endif
  335. /* WIN32 handled in port/win32.h */
  336. #ifndef WIN32
  337. #define pgoff_t off_t
  338. #if defined(__bsdi__) || defined(__NetBSD__)
  339. extern int fseeko(FILE *stream, off_t offset, int whence);
  340. extern off_t ftello(FILE *stream);
  341. #endif
  342. #endif
  343. #ifndef HAVE_ERAND48
  344. /* we assume all of these are present or missing together */
  345. extern double erand48(unsigned short xseed[3]);
  346. extern long lrand48(void);
  347. extern void srand48(long seed);
  348. #endif
  349. #ifndef HAVE_FSEEKO
  350. #define fseeko(a, b, c) fseek(a, b, c)
  351. #define ftello(a) ftell(a)
  352. #endif
  353. #ifndef HAVE_GETOPT
  354. extern int getopt(int nargc, char *const * nargv, const char *ostr);
  355. #endif
  356. #if !defined(HAVE_GETPEEREID) && !defined(WIN32)
  357. extern int getpeereid(int sock, uid_t *uid, gid_t *gid);
  358. #endif
  359. #ifndef HAVE_ISINF
  360. extern int isinf(double x);
  361. #endif
  362. #ifndef HAVE_RINT
  363. extern double rint(double x);
  364. #endif
  365. #ifndef HAVE_INET_ATON
  366. #include <netinet/in.h>
  367. #include <arpa/inet.h>
  368. extern int inet_aton(const char *cp, struct in_addr * addr);
  369. #endif
  370. #ifndef HAVE_STRDUP
  371. extern char *strdup(const char *str);
  372. #endif
  373. #if !HAVE_DECL_STRLCAT
  374. extern size_t strlcat(char *dst, const char *src, size_t siz);
  375. #endif
  376. #if !HAVE_DECL_STRLCPY
  377. extern size_t strlcpy(char *dst, const char *src, size_t siz);
  378. #endif
  379. #if !defined(HAVE_RANDOM) && !defined(__BORLANDC__)
  380. extern long random(void);
  381. #endif
  382. #ifndef HAVE_UNSETENV
  383. extern void unsetenv(const char *name);
  384. #endif
  385. #ifndef HAVE_SRANDOM
  386. extern void srandom(unsigned int seed);
  387. #endif
  388. /* thread.h */
  389. extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen);
  390. #if !defined(WIN32) || defined(__CYGWIN__)
  391. extern int pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
  392. size_t buflen, struct passwd ** result);
  393. #endif
  394. extern int pqGethostbyname(const char *name,
  395. struct hostent * resultbuf,
  396. char *buffer, size_t buflen,
  397. struct hostent ** result,
  398. int *herrno);
  399. extern void pg_qsort(void *base, size_t nel, size_t elsize,
  400. int (*cmp) (const void *, const void *));
  401. #define qsort(a,b,c,d) pg_qsort(a,b,c,d)
  402. typedef int (*qsort_arg_comparator) (const void *a, const void *b, void *arg);
  403. extern void qsort_arg(void *base, size_t nel, size_t elsize,
  404. qsort_arg_comparator cmp, void *arg);
  405. /* port/chklocale.c */
  406. extern int pg_get_encoding_from_locale(const char *ctype, bool write_message);
  407. /* port/inet_net_ntop.c */
  408. extern char *inet_net_ntop(int af, const void *src, int bits,
  409. char *dst, size_t size);
  410. /* port/pgcheckdir.c */
  411. extern int pg_check_dir(const char *dir);
  412. /* port/pgmkdirp.c */
  413. extern int pg_mkdir_p(char *path, int omode);
  414. #endif /* PG_PORT_H */