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