OSDN Git Service

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