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