OSDN Git Service

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