]> granicus.if.org Git - postgresql/blob - src/include/port.h
Remove BSD/OS (BSDi) port. There are no known users upgrading to
[postgresql] / src / include / port.h
1 /*-------------------------------------------------------------------------
2  *
3  * port.h
4  *        Header for src/port/ compatibility functions.
5  *
6  * Portions Copyright (c) 1996-2012, 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 /* "con" does not work from the Msys 1.0.10 console (part of MinGW). */
114 #define DEVTTY  "con"
115 #else
116 #define DEVNULL "/dev/null"
117 #define DEVTTY "/dev/tty"
118 #endif
119
120 /*
121  *      Win32 needs double quotes at the beginning and end of system()
122  *      strings.  If not, it gets confused with multiple quoted strings.
123  *      It also requires double-quotes around the executable name and
124  *      any files used for redirection.  Other args can use single-quotes.
125  *
126  *      Generated using Win32 "CMD /?":
127  *
128  *      1. If all of the following conditions are met, then quote characters
129  *      on the command line are preserved:
130  *
131  *       - no /S switch
132  *       - exactly two quote characters
133  *       - no special characters between the two quote characters, where special
134  *         is one of: &<>()@^|
135  *       - there are one or more whitespace characters between the two quote
136  *         characters
137  *       - the string between the two quote characters is the name of an
138  *         executable file.
139  *
140  *       2. Otherwise, old behavior is to see if the first character is a quote
141  *       character and if so, strip the leading character and remove the last
142  *       quote character on the command line, preserving any text after the last
143  *       quote character.
144  */
145 #if defined(WIN32) && !defined(__CYGWIN__)
146 #define SYSTEMQUOTE "\""
147 #else
148 #define SYSTEMQUOTE ""
149 #endif
150
151 /* Portable delay handling */
152 extern void pg_usleep(long microsec);
153
154 /* Portable SQL-like case-independent comparisons and conversions */
155 extern int      pg_strcasecmp(const char *s1, const char *s2);
156 extern int      pg_strncasecmp(const char *s1, const char *s2, size_t n);
157 extern unsigned char pg_toupper(unsigned char ch);
158 extern unsigned char pg_tolower(unsigned char ch);
159 extern unsigned char pg_ascii_toupper(unsigned char ch);
160 extern unsigned char pg_ascii_tolower(unsigned char ch);
161
162 #ifdef USE_REPL_SNPRINTF
163
164 /*
165  * Versions of libintl >= 0.13 try to replace printf() and friends with
166  * macros to their own versions that understand the %$ format.  We do the
167  * same, so disable their macros, if they exist.
168  */
169 #ifdef vsnprintf
170 #undef vsnprintf
171 #endif
172 #ifdef snprintf
173 #undef snprintf
174 #endif
175 #ifdef sprintf
176 #undef sprintf
177 #endif
178 #ifdef vfprintf
179 #undef vfprintf
180 #endif
181 #ifdef fprintf
182 #undef fprintf
183 #endif
184 #ifdef printf
185 #undef printf
186 #endif
187
188 extern int      pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args);
189 extern int
190 pg_snprintf(char *str, size_t count, const char *fmt,...)
191 /* This extension allows gcc to check the format string */
192 __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
193 extern int
194 pg_sprintf(char *str, const char *fmt,...)
195 /* This extension allows gcc to check the format string */
196 __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
197 extern int      pg_vfprintf(FILE *stream, const char *fmt, va_list args);
198 extern int
199 pg_fprintf(FILE *stream, const char *fmt,...)
200 /* This extension allows gcc to check the format string */
201 __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
202 extern int
203 pg_printf(const char *fmt,...)
204 /* This extension allows gcc to check the format string */
205 __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
206
207 /*
208  *      The GCC-specific code below prevents the __attribute__(... 'printf')
209  *      above from being replaced, and this is required because gcc doesn't
210  *      know anything about pg_printf.
211  */
212 #ifdef __GNUC__
213 #define vsnprintf(...)  pg_vsnprintf(__VA_ARGS__)
214 #define snprintf(...)   pg_snprintf(__VA_ARGS__)
215 #define sprintf(...)    pg_sprintf(__VA_ARGS__)
216 #define vfprintf(...)   pg_vfprintf(__VA_ARGS__)
217 #define fprintf(...)    pg_fprintf(__VA_ARGS__)
218 #define printf(...)             pg_printf(__VA_ARGS__)
219 #else
220 #define vsnprintf               pg_vsnprintf
221 #define snprintf                pg_snprintf
222 #define sprintf                 pg_sprintf
223 #define vfprintf                pg_vfprintf
224 #define fprintf                 pg_fprintf
225 #define printf                  pg_printf
226 #endif
227 #endif   /* USE_REPL_SNPRINTF */
228
229 #if defined(WIN32)
230 /*
231  * Versions of libintl >= 0.18? try to replace setlocale() with a macro
232  * to their own versions.  Remove the macro, if it exists, because it
233  * ends up calling the wrong version when the backend and libintl use
234  * different versions of msvcrt.
235  */
236 #if defined(setlocale)
237 #undef setlocale
238 #endif
239
240 /*
241  * Define our own wrapper macro around setlocale() to work around bugs in
242  * Windows' native setlocale() function.
243  */
244 extern char *pgwin32_setlocale(int category, const char *locale);
245
246 #define setlocale(a,b) pgwin32_setlocale(a,b)
247
248 #endif   /* WIN32 */
249
250 /* Portable prompt handling */
251 extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
252
253 #ifdef WIN32
254 #define PG_SIGNAL_COUNT 32
255 #define kill(pid,sig)   pgkill(pid,sig)
256 extern int      pgkill(int pid, int sig);
257 #endif
258
259 extern int      pclose_check(FILE *stream);
260
261 /* Global variable holding time zone information. */
262 #ifndef __CYGWIN__
263 #define TIMEZONE_GLOBAL timezone
264 #define TZNAME_GLOBAL tzname
265 #else
266 #define TIMEZONE_GLOBAL _timezone
267 #define TZNAME_GLOBAL _tzname
268 #endif
269
270 #if defined(WIN32) || defined(__CYGWIN__)
271 /*
272  *      Win32 doesn't have reliable rename/unlink during concurrent access.
273  */
274 extern int      pgrename(const char *from, const char *to);
275 extern int      pgunlink(const char *path);
276
277 /* Include this first so later includes don't see these defines */
278 #ifdef WIN32_ONLY_COMPILER
279 #include <io.h>
280 #endif
281
282 #define rename(from, to)                pgrename(from, to)
283 #define unlink(path)                    pgunlink(path)
284 #endif   /* defined(WIN32) || defined(__CYGWIN__) */
285
286 /*
287  *      Win32 also doesn't have symlinks, but we can emulate them with
288  *      junction points on newer Win32 versions.
289  *
290  *      Cygwin has its own symlinks which work on Win95/98/ME where
291  *      junction points don't, so use those instead.  We have no way of
292  *      knowing what type of system Cygwin binaries will be run on.
293  *              Note: Some CYGWIN includes might #define WIN32.
294  */
295 #if defined(WIN32) && !defined(__CYGWIN__)
296 extern int      pgsymlink(const char *oldpath, const char *newpath);
297 extern int      pgreadlink(const char *path, char *buf, size_t size);
298 extern bool pgwin32_is_junction(char *path);
299
300 #define symlink(oldpath, newpath)       pgsymlink(oldpath, newpath)
301 #define readlink(path, buf, size)       pgreadlink(path, buf, size)
302 #endif
303
304 extern bool rmtree(const char *path, bool rmtopdir);
305
306 /*
307  * stat() is not guaranteed to set the st_size field on win32, so we
308  * redefine it to our own implementation that is.
309  *
310  * We must pull in sys/stat.h here so the system header definition
311  * goes in first, and we redefine that, and not the other way around.
312  *
313  * Some frontends don't need the size from stat, so if UNSAFE_STAT_OK
314  * is defined we don't bother with this.
315  */
316 #if defined(WIN32) && !defined(__CYGWIN__) && !defined(UNSAFE_STAT_OK)
317 #include <sys/stat.h>
318 extern int      pgwin32_safestat(const char *path, struct stat * buf);
319
320 #define stat(a,b) pgwin32_safestat(a,b)
321 #endif
322
323 #if defined(WIN32) && !defined(__CYGWIN__)
324
325 /*
326  * open() and fopen() replacements to allow deletion of open files and
327  * passing of other special options.
328  */
329 #define         O_DIRECT        0x80000000
330 extern int      pgwin32_open(const char *, int,...);
331 extern FILE *pgwin32_fopen(const char *, const char *);
332
333 #ifndef FRONTEND
334 #define         open(a,b,c) pgwin32_open(a,b,c)
335 #define         fopen(a,b) pgwin32_fopen(a,b)
336 #endif
337
338 #ifndef popen
339 #define popen(a,b) _popen(a,b)
340 #endif
341 #ifndef pclose
342 #define pclose(a) _pclose(a)
343 #endif
344
345 /* New versions of MingW have gettimeofday, old mingw and msvc don't */
346 #ifndef HAVE_GETTIMEOFDAY
347 /* Last parameter not used */
348 extern int      gettimeofday(struct timeval * tp, struct timezone * tzp);
349 #endif
350 #else                                                   /* !WIN32 */
351
352 /*
353  *      Win32 requires a special close for sockets and pipes, while on Unix
354  *      close() does them all.
355  */
356 #define closesocket close
357 #endif   /* WIN32 */
358
359 /*
360  * Default "extern" declarations or macro substitutes for library routines.
361  * When necessary, these routines are provided by files in src/port/.
362  */
363 #ifndef HAVE_CRYPT
364 extern char *crypt(const char *key, const char *setting);
365 #endif
366
367 /* WIN32 handled in port/win32.h */
368 #ifndef WIN32
369 #define pgoff_t off_t
370 #ifdef __NetBSD__
371 extern int      fseeko(FILE *stream, off_t offset, int whence);
372 extern off_t ftello(FILE *stream);
373 #endif
374 #endif
375
376 extern double pg_erand48(unsigned short xseed[3]);
377 extern long pg_lrand48(void);
378 extern void pg_srand48(long seed);
379
380 #ifndef HAVE_FLS
381 extern int fls(int mask);
382 #endif
383
384 #ifndef HAVE_FSEEKO
385 #define fseeko(a, b, c) fseek(a, b, c)
386 #define ftello(a)               ftell(a)
387 #endif
388
389 #ifndef HAVE_GETOPT
390 extern int      getopt(int nargc, char *const * nargv, const char *ostr);
391 #endif
392
393 #if !defined(HAVE_GETPEEREID) && !defined(WIN32)
394 extern int      getpeereid(int sock, uid_t *uid, gid_t *gid);
395 #endif
396
397 #ifndef HAVE_ISINF
398 extern int      isinf(double x);
399 #endif
400
401 #ifndef HAVE_RINT
402 extern double rint(double x);
403 #endif
404
405 #ifndef HAVE_INET_ATON
406 #include <netinet/in.h>
407 #include <arpa/inet.h>
408 extern int      inet_aton(const char *cp, struct in_addr * addr);
409 #endif
410
411 #ifndef HAVE_STRDUP
412 extern char *strdup(const char *str);
413 #endif
414
415 #if !HAVE_DECL_STRLCAT
416 extern size_t strlcat(char *dst, const char *src, size_t siz);
417 #endif
418
419 #if !HAVE_DECL_STRLCPY
420 extern size_t strlcpy(char *dst, const char *src, size_t siz);
421 #endif
422
423 #if !defined(HAVE_RANDOM) && !defined(__BORLANDC__)
424 extern long random(void);
425 #endif
426
427 #ifndef HAVE_UNSETENV
428 extern void unsetenv(const char *name);
429 #endif
430
431 #ifndef HAVE_SRANDOM
432 extern void srandom(unsigned int seed);
433 #endif
434
435 /* thread.h */
436 extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen);
437
438 #if !defined(WIN32) || defined(__CYGWIN__)
439 extern int pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
440                    size_t buflen, struct passwd ** result);
441 #endif
442
443 extern int pqGethostbyname(const char *name,
444                                 struct hostent * resultbuf,
445                                 char *buffer, size_t buflen,
446                                 struct hostent ** result,
447                                 int *herrno);
448
449 extern void pg_qsort(void *base, size_t nel, size_t elsize,
450                  int (*cmp) (const void *, const void *));
451
452 #define qsort(a,b,c,d) pg_qsort(a,b,c,d)
453
454 typedef int (*qsort_arg_comparator) (const void *a, const void *b, void *arg);
455
456 extern void qsort_arg(void *base, size_t nel, size_t elsize,
457                   qsort_arg_comparator cmp, void *arg);
458
459 /* port/chklocale.c */
460 extern int      pg_get_encoding_from_locale(const char *ctype, bool write_message);
461
462 /* port/inet_net_ntop.c */
463 extern char *inet_net_ntop(int af, const void *src, int bits,
464                           char *dst, size_t size);
465
466 /* port/pgcheckdir.c */
467 extern int      pg_check_dir(const char *dir);
468
469 /* port/pgmkdirp.c */
470 extern int      pg_mkdir_p(char *path, int omode);
471
472 #endif   /* PG_PORT_H */