]> granicus.if.org Git - postgresql/blob - src/include/port.h
Use correct output device for Windows prompts.
[postgresql] / src / include / port.h
1 /*-------------------------------------------------------------------------
2  *
3  * port.h
4  *        Header for src/port/ compatibility functions.
5  *
6  * Portions Copyright (c) 1996-2013, 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 const char *get_progname(const char *argv0);
49 extern void get_share_path(const char *my_exec_path, char *ret_path);
50 extern void get_etc_path(const char *my_exec_path, char *ret_path);
51 extern void get_include_path(const char *my_exec_path, char *ret_path);
52 extern void get_pkginclude_path(const char *my_exec_path, char *ret_path);
53 extern void get_includeserver_path(const char *my_exec_path, char *ret_path);
54 extern void get_lib_path(const char *my_exec_path, char *ret_path);
55 extern void get_pkglib_path(const char *my_exec_path, char *ret_path);
56 extern void get_locale_path(const char *my_exec_path, char *ret_path);
57 extern void get_doc_path(const char *my_exec_path, char *ret_path);
58 extern void get_html_path(const char *my_exec_path, char *ret_path);
59 extern void get_man_path(const char *my_exec_path, char *ret_path);
60 extern bool get_home_path(char *ret_path);
61 extern void get_parent_directory(char *path);
62
63 /* port/dirmod.c */
64 extern char **pgfnames(const char *path);
65 extern void pgfnames_cleanup(char **filenames);
66
67 /*
68  *      is_absolute_path
69  *
70  *      By making this a macro we avoid needing to include path.c in libpq.
71  */
72 #ifndef WIN32
73 #define IS_DIR_SEP(ch)  ((ch) == '/')
74
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
82 /* See path_is_relative_and_below_cwd() for how we handle 'E:abc'. */
83 #define is_absolute_path(filename) \
84 ( \
85         IS_DIR_SEP((filename)[0]) || \
86         (isalpha((unsigned char) ((filename)[0])) && (filename)[1] == ':' && \
87          IS_DIR_SEP((filename)[2])) \
88 )
89 #endif
90
91 /* Portable locale initialization (in exec.c) */
92 extern void set_pglocale_pgservice(const char *argv0, const char *app);
93
94 /* Portable way to find binaries (in exec.c) */
95 extern int      find_my_exec(const char *argv0, char *retpath);
96 extern int find_other_exec(const char *argv0, const char *target,
97                                 const char *versionstr, char *retpath);
98
99 /* Windows security token manipulation (in exec.c) */
100 #ifdef WIN32
101 extern BOOL AddUserToTokenDacl(HANDLE hToken);
102 #endif
103
104
105 #if defined(WIN32) || defined(__CYGWIN__)
106 #define EXE ".exe"
107 #else
108 #define EXE ""
109 #endif
110
111 #if defined(WIN32) && !defined(__CYGWIN__)
112 #define DEVNULL "nul"
113 #else
114 #define DEVNULL "/dev/null"
115 #endif
116
117 /*
118  *      Win32 needs double quotes at the beginning and end of system()
119  *      strings.  If not, it gets confused with multiple quoted strings.
120  *      It also requires double-quotes around the executable name and
121  *      any files used for redirection.  Other args can use single-quotes.
122  *
123  *      Generated using Win32 "CMD /?":
124  *
125  *      1. If all of the following conditions are met, then quote characters
126  *      on the command line are preserved:
127  *
128  *       - no /S switch
129  *       - exactly two quote characters
130  *       - no special characters between the two quote characters, where special
131  *         is one of: &<>()@^|
132  *       - there are one or more whitespace characters between the two quote
133  *         characters
134  *       - the string between the two quote characters is the name of an
135  *         executable file.
136  *
137  *       2. Otherwise, old behavior is to see if the first character is a quote
138  *       character and if so, strip the leading character and remove the last
139  *       quote character on the command line, preserving any text after the last
140  *       quote character.
141  */
142 #if defined(WIN32) && !defined(__CYGWIN__)
143 #define SYSTEMQUOTE "\""
144 #else
145 #define SYSTEMQUOTE ""
146 #endif
147
148 /* Portable delay handling */
149 extern void pg_usleep(long microsec);
150
151 /* Portable SQL-like case-independent comparisons and conversions */
152 extern int      pg_strcasecmp(const char *s1, const char *s2);
153 extern int      pg_strncasecmp(const char *s1, const char *s2, size_t n);
154 extern unsigned char pg_toupper(unsigned char ch);
155 extern unsigned char pg_tolower(unsigned char ch);
156 extern unsigned char pg_ascii_toupper(unsigned char ch);
157 extern unsigned char pg_ascii_tolower(unsigned char ch);
158
159 #ifdef USE_REPL_SNPRINTF
160
161 /*
162  * Versions of libintl >= 0.13 try to replace printf() and friends with
163  * macros to their own versions that understand the %$ format.  We do the
164  * same, so disable their macros, if they exist.
165  */
166 #ifdef vsnprintf
167 #undef vsnprintf
168 #endif
169 #ifdef snprintf
170 #undef snprintf
171 #endif
172 #ifdef sprintf
173 #undef sprintf
174 #endif
175 #ifdef vfprintf
176 #undef vfprintf
177 #endif
178 #ifdef fprintf
179 #undef fprintf
180 #endif
181 #ifdef printf
182 #undef printf
183 #endif
184
185 extern int      pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args);
186 extern int
187 pg_snprintf(char *str, size_t count, const char *fmt,...)
188 /* This extension allows gcc to check the format string */
189 __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
190 extern int
191 pg_sprintf(char *str, const char *fmt,...)
192 /* This extension allows gcc to check the format string */
193 __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
194 extern int      pg_vfprintf(FILE *stream, const char *fmt, va_list args);
195 extern int
196 pg_fprintf(FILE *stream, const char *fmt,...)
197 /* This extension allows gcc to check the format string */
198 __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
199 extern int
200 pg_printf(const char *fmt,...)
201 /* This extension allows gcc to check the format string */
202 __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
203
204 /*
205  *      The GCC-specific code below prevents the __attribute__(... 'printf')
206  *      above from being replaced, and this is required because gcc doesn't
207  *      know anything about pg_printf.
208  */
209 #ifdef __GNUC__
210 #define vsnprintf(...)  pg_vsnprintf(__VA_ARGS__)
211 #define snprintf(...)   pg_snprintf(__VA_ARGS__)
212 #define sprintf(...)    pg_sprintf(__VA_ARGS__)
213 #define vfprintf(...)   pg_vfprintf(__VA_ARGS__)
214 #define fprintf(...)    pg_fprintf(__VA_ARGS__)
215 #define printf(...)             pg_printf(__VA_ARGS__)
216 #else
217 #define vsnprintf               pg_vsnprintf
218 #define snprintf                pg_snprintf
219 #define sprintf                 pg_sprintf
220 #define vfprintf                pg_vfprintf
221 #define fprintf                 pg_fprintf
222 #define printf                  pg_printf
223 #endif
224 #endif   /* USE_REPL_SNPRINTF */
225
226 #if defined(WIN32)
227 /*
228  * Versions of libintl >= 0.18? try to replace setlocale() with a macro
229  * to their own versions.  Remove the macro, if it exists, because it
230  * ends up calling the wrong version when the backend and libintl use
231  * different versions of msvcrt.
232  */
233 #if defined(setlocale)
234 #undef setlocale
235 #endif
236
237 /*
238  * Define our own wrapper macro around setlocale() to work around bugs in
239  * Windows' native setlocale() function.
240  */
241 extern char *pgwin32_setlocale(int category, const char *locale);
242
243 #define setlocale(a,b) pgwin32_setlocale(a,b)
244 #endif   /* WIN32 */
245
246 /* Portable prompt handling */
247 extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
248
249 #ifdef WIN32
250 #define PG_SIGNAL_COUNT 32
251 #define kill(pid,sig)   pgkill(pid,sig)
252 extern int      pgkill(int pid, int sig);
253 #endif
254
255 extern int      pclose_check(FILE *stream);
256
257 /* Global variable holding time zone information. */
258 #ifndef __CYGWIN__
259 #define TIMEZONE_GLOBAL timezone
260 #define TZNAME_GLOBAL tzname
261 #else
262 #define TIMEZONE_GLOBAL _timezone
263 #define TZNAME_GLOBAL _tzname
264 #endif
265
266 #if defined(WIN32) || defined(__CYGWIN__)
267 /*
268  *      Win32 doesn't have reliable rename/unlink during concurrent access.
269  */
270 extern int      pgrename(const char *from, const char *to);
271 extern int      pgunlink(const char *path);
272
273 /* Include this first so later includes don't see these defines */
274 #ifdef WIN32_ONLY_COMPILER
275 #include <io.h>
276 #endif
277
278 #define rename(from, to)                pgrename(from, to)
279 #define unlink(path)                    pgunlink(path)
280 #endif   /* defined(WIN32) || defined(__CYGWIN__) */
281
282 /*
283  *      Win32 also doesn't have symlinks, but we can emulate them with
284  *      junction points on newer Win32 versions.
285  *
286  *      Cygwin has its own symlinks which work on Win95/98/ME where
287  *      junction points don't, so use those instead.  We have no way of
288  *      knowing what type of system Cygwin binaries will be run on.
289  *              Note: Some CYGWIN includes might #define WIN32.
290  */
291 #if defined(WIN32) && !defined(__CYGWIN__)
292 extern int      pgsymlink(const char *oldpath, const char *newpath);
293 extern int      pgreadlink(const char *path, char *buf, size_t size);
294 extern bool pgwin32_is_junction(char *path);
295
296 #define symlink(oldpath, newpath)       pgsymlink(oldpath, newpath)
297 #define readlink(path, buf, size)       pgreadlink(path, buf, size)
298 #endif
299
300 extern bool rmtree(const char *path, bool rmtopdir);
301
302 /*
303  * stat() is not guaranteed to set the st_size field on win32, so we
304  * redefine it to our own implementation that is.
305  *
306  * We must pull in sys/stat.h here so the system header definition
307  * goes in first, and we redefine that, and not the other way around.
308  *
309  * Some frontends don't need the size from stat, so if UNSAFE_STAT_OK
310  * is defined we don't bother with this.
311  */
312 #if defined(WIN32) && !defined(__CYGWIN__) && !defined(UNSAFE_STAT_OK)
313 #include <sys/stat.h>
314 extern int      pgwin32_safestat(const char *path, struct stat * buf);
315
316 #define stat(a,b) pgwin32_safestat(a,b)
317 #endif
318
319 #if defined(WIN32) && !defined(__CYGWIN__)
320
321 /*
322  * open() and fopen() replacements to allow deletion of open files and
323  * passing of other special options.
324  */
325 #define         O_DIRECT        0x80000000
326 extern int      pgwin32_open(const char *, int,...);
327 extern FILE *pgwin32_fopen(const char *, const char *);
328
329 #ifndef FRONTEND
330 #define         open(a,b,c) pgwin32_open(a,b,c)
331 #define         fopen(a,b) pgwin32_fopen(a,b)
332 #endif
333
334 #ifndef popen
335 #define popen(a,b) _popen(a,b)
336 #endif
337 #ifndef pclose
338 #define pclose(a) _pclose(a)
339 #endif
340
341 /* New versions of MingW have gettimeofday, old mingw and msvc don't */
342 #ifndef HAVE_GETTIMEOFDAY
343 /* Last parameter not used */
344 extern int      gettimeofday(struct timeval * tp, struct timezone * tzp);
345 #endif
346 #else                                                   /* !WIN32 */
347
348 /*
349  *      Win32 requires a special close for sockets and pipes, while on Unix
350  *      close() does them all.
351  */
352 #define closesocket close
353 #endif   /* WIN32 */
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 #ifndef HAVE_GETOPT
386 extern int      getopt(int nargc, char *const * nargv, const char *ostr);
387 #endif
388
389 #if !defined(HAVE_GETPEEREID) && !defined(WIN32)
390 extern int      getpeereid(int sock, uid_t *uid, gid_t *gid);
391 #endif
392
393 #ifndef HAVE_ISINF
394 extern int      isinf(double x);
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 /* thread.h */
428 extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen);
429
430 #if !defined(WIN32) || defined(__CYGWIN__)
431 extern int pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
432                    size_t buflen, struct passwd ** result);
433 #endif
434
435 extern int pqGethostbyname(const char *name,
436                                 struct hostent * resultbuf,
437                                 char *buffer, size_t buflen,
438                                 struct hostent ** result,
439                                 int *herrno);
440
441 extern void pg_qsort(void *base, size_t nel, size_t elsize,
442                  int (*cmp) (const void *, const void *));
443 extern int pg_qsort_strcmp(const void *a, const void *b);
444
445 #define qsort(a,b,c,d) pg_qsort(a,b,c,d)
446
447 typedef int (*qsort_arg_comparator) (const void *a, const void *b, void *arg);
448
449 extern void qsort_arg(void *base, size_t nel, size_t elsize,
450                   qsort_arg_comparator cmp, void *arg);
451
452 /* port/chklocale.c */
453 extern int      pg_get_encoding_from_locale(const char *ctype, bool write_message);
454
455 /* port/inet_net_ntop.c */
456 extern char *inet_net_ntop(int af, const void *src, int bits,
457                           char *dst, size_t size);
458
459 /* port/pgcheckdir.c */
460 extern int      pg_check_dir(const char *dir);
461
462 /* port/pgmkdirp.c */
463 extern int      pg_mkdir_p(char *path, int omode);
464
465 /* port/quotes.c */
466 extern char *escape_single_quotes_ascii(const char *src);
467
468 #endif   /* PG_PORT_H */