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