1 /*-------------------------------------------------------------------------
4 * Header for src/port/ compatibility functions.
6 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * $PostgreSQL: pgsql/src/include/port.h,v 1.98 2006/09/11 20:10:30 tgl Exp $
11 *-------------------------------------------------------------------------
19 extern bool pg_set_noblock(int sock);
20 extern bool pg_set_block(int sock);
22 /* Portable path handling for Unix/Win32 (in path.c) */
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);
50 * By making this a macro we avoid needing to include path.c in libpq.
53 #define is_absolute_path(filename) \
55 ((filename)[0] == '/') \
58 #define is_absolute_path(filename) \
60 ((filename)[0] == '/') || \
61 (filename)[0] == '\\' || \
62 (isalpha((filename)[0]) && (filename)[1] == ':' && \
63 ((filename)[2] == '\\' || (filename)[2] == '/')) \
67 /* Portable locale initialization (in exec.c) */
68 extern void set_pglocale_pgservice(const char *argv0, const char *app);
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);
75 #if defined(WIN32) || defined(__CYGWIN__)
81 #if defined(WIN32) && !defined(__CYGWIN__)
83 /* "con" does not work from the Msys 1.0.10 console (part of MinGW). */
86 #define DEVNULL "/dev/null"
87 #define DEVTTY "/dev/tty"
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.
96 * Generated using Win32 "CMD /?":
98 * 1. If all of the following conditions are met, then quote characters
99 * on the command line are preserved:
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
107 * - the string between the two quote characters is the name of an
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
115 #if defined(WIN32) && !defined(__CYGWIN__)
116 #define SYSTEMQUOTE "\""
118 #define SYSTEMQUOTE ""
121 /* Portable delay handling */
122 extern void pg_usleep(long microsec);
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);
130 #ifdef USE_REPL_SNPRINTF
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.
153 extern int pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args);
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)));
159 pg_sprintf(char *str, const char *fmt,...)
160 /* This extension allows gcc to check the format string */
161 __attribute__((format(printf, 2, 3)));
163 pg_fprintf(FILE *stream, const char *fmt,...)
164 /* This extension allows gcc to check the format string */
165 __attribute__((format(printf, 2, 3)));
167 pg_printf(const char *fmt,...)
168 /* This extension allows gcc to check the format string */
169 __attribute__((format(printf, 1, 2)));
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.
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__)
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
190 #endif /* USE_REPL_SNPRINTF */
192 /* Portable prompt handling */
193 extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
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.
203 * The function prototypes are not supplied because every C file
204 * includes this file.
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)
210 extern int pgpipe(int handles[2]);
211 extern int piperead(int s, char *buf, int len);
213 #define pipewrite(a,b,c) send(a,b,c,0)
215 #define PG_SIGNAL_COUNT 32
216 #define kill(pid,sig) pgkill(pid,sig)
217 extern int pgkill(int pid, int sig);
220 extern int pclose_check(FILE *stream);
222 /* Global variable holding time zone information. */
224 #define TIMEZONE_GLOBAL timezone
225 #define TZNAME_GLOBAL tzname
227 #define TIMEZONE_GLOBAL _timezone
228 #define TZNAME_GLOBAL _tzname
231 #if defined(WIN32) || defined(__CYGWIN__)
233 * Win32 doesn't have reliable rename/unlink during concurrent access,
234 * and we need special code to do symlinks.
236 extern int pgrename(const char *from, const char *to);
237 extern int pgunlink(const char *path);
239 /* Include this first so later includes don't see these defines */
240 #ifdef WIN32_ONLY_COMPILER
244 #define rename(from, to) pgrename(from, to)
245 #define unlink(path) pgunlink(path)
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.
253 #if defined(WIN32) && !defined(__CYGWIN__)
254 extern int pgsymlink(const char *oldpath, const char *newpath);
256 #define symlink(oldpath, newpath) pgsymlink(oldpath, newpath)
258 #endif /* defined(WIN32) || defined(__CYGWIN__) */
260 extern void copydir(char *fromdir, char *todir, bool recurse);
262 extern bool rmtree(char *path, bool rmtopdir);
264 #if defined(WIN32) && !defined(__CYGWIN__)
266 /* open() and fopen() replacements to allow deletion of open files and
267 * passing of other special options.
269 extern int pgwin32_open(const char *, int,...);
270 extern FILE *pgwin32_fopen(const char *, const char *);
273 #define open(a,b,c) pgwin32_open(a,b,c)
274 #define fopen(a,b) pgwin32_fopen(a,b)
277 #define popen(a,b) _popen(a,b)
278 #define pclose(a) _pclose(a)
280 /* Missing rand functions */
281 extern long lrand48(void);
282 extern void srand48(long seed);
284 /* Last parameter not used */
285 extern int gettimeofday(struct timeval * tp, struct timezone * tzp);
289 * Win32 requires a special close for sockets and pipes, while on Unix
290 * close() does them all.
292 #define closesocket close
296 * Default "extern" declarations or macro substitutes for library routines.
297 * When necessary, these routines are provided by files in src/port/.
300 extern char *crypt(const char *key, const char *setting);
303 #if defined(bsdi) || defined(netbsd)
304 extern int fseeko(FILE *stream, off_t offset, int whence);
305 extern off_t ftello(FILE *stream);
309 #define fseeko(a, b, c) fseek(a, b, c)
310 #define ftello(a) ftell(a)
314 extern int getopt(int nargc, char *const * nargv, const char *ostr);
318 extern int isinf(double x);
322 extern double rint(double x);
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);
332 extern char *strdup(char const *);
336 extern long random(void);
339 #ifndef HAVE_UNSETENV
340 extern void unsetenv(const char *name);
344 extern void srandom(unsigned int seed);
348 extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen);
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);
355 extern int pqGethostbyname(const char *name,
356 struct hostent * resultbuf,
357 char *buffer, size_t buflen,
358 struct hostent ** result,