1 /*-------------------------------------------------------------------------
4 * Header for src/port/ compatibility functions.
6 * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * $PostgreSQL: pgsql/src/include/port.h,v 1.108 2007/01/11 02:39:52 momjian Exp $
11 *-------------------------------------------------------------------------
21 extern bool pg_set_noblock(int sock);
22 extern bool pg_set_block(int sock);
24 /* Portable path handling for Unix/Win32 (in path.c) */
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);
52 * By making this a macro we avoid needing to include path.c in libpq.
55 #define is_absolute_path(filename) \
57 ((filename)[0] == '/') \
60 #define is_absolute_path(filename) \
62 ((filename)[0] == '/') || \
63 (filename)[0] == '\\' || \
64 (isalpha((unsigned char) ((filename)[0])) && (filename)[1] == ':' && \
65 ((filename)[2] == '\\' || (filename)[2] == '/')) \
69 /* Portable locale initialization (in exec.c) */
70 extern void set_pglocale_pgservice(const char *argv0, const char *app);
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);
77 #if defined(WIN32) || defined(__CYGWIN__)
83 #if defined(WIN32) && !defined(__CYGWIN__)
85 /* "con" does not work from the Msys 1.0.10 console (part of MinGW). */
88 #define DEVNULL "/dev/null"
89 #define DEVTTY "/dev/tty"
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.
98 * Generated using Win32 "CMD /?":
100 * 1. If all of the following conditions are met, then quote characters
101 * on the command line are preserved:
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
109 * - the string between the two quote characters is the name of an
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
117 #if defined(WIN32) && !defined(__CYGWIN__)
118 #define SYSTEMQUOTE "\""
120 #define SYSTEMQUOTE ""
123 /* Portable delay handling */
124 extern void pg_usleep(long microsec);
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);
132 #ifdef USE_REPL_SNPRINTF
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.
158 extern int pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args);
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)));
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);
169 pg_fprintf(FILE *stream, const char *fmt,...)
170 /* This extension allows gcc to check the format string */
171 __attribute__((format(printf, 2, 3)));
173 pg_printf(const char *fmt,...)
174 /* This extension allows gcc to check the format string */
175 __attribute__((format(printf, 1, 2)));
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.
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__)
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
197 #endif /* USE_REPL_SNPRINTF */
199 /* Portable prompt handling */
200 extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
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.
210 * The function prototypes are not supplied because every C file
211 * includes this file.
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)
217 extern int pgpipe(int handles[2]);
218 extern int piperead(int s, char *buf, int len);
220 #define pipewrite(a,b,c) send(a,b,c,0)
222 #define PG_SIGNAL_COUNT 32
223 #define kill(pid,sig) pgkill(pid,sig)
224 extern int pgkill(int pid, int sig);
227 extern int pclose_check(FILE *stream);
229 /* Global variable holding time zone information. */
231 #define TIMEZONE_GLOBAL timezone
232 #define TZNAME_GLOBAL tzname
234 #define TIMEZONE_GLOBAL _timezone
235 #define TZNAME_GLOBAL _tzname
238 #if defined(WIN32) || defined(__CYGWIN__)
240 * Win32 doesn't have reliable rename/unlink during concurrent access,
241 * and we need special code to do symlinks.
243 extern int pgrename(const char *from, const char *to);
244 extern int pgunlink(const char *path);
246 /* Include this first so later includes don't see these defines */
247 #ifdef WIN32_ONLY_COMPILER
251 #define rename(from, to) pgrename(from, to)
252 #define unlink(path) pgunlink(path)
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.
260 #if defined(WIN32) && !defined(__CYGWIN__)
261 extern int pgsymlink(const char *oldpath, const char *newpath);
263 #define symlink(oldpath, newpath) pgsymlink(oldpath, newpath)
265 #endif /* defined(WIN32) || defined(__CYGWIN__) */
267 extern void copydir(char *fromdir, char *todir, bool recurse);
269 extern bool rmtree(char *path, bool rmtopdir);
271 #if defined(WIN32) && !defined(__CYGWIN__)
273 /* open() and fopen() replacements to allow deletion of open files and
274 * passing of other special options.
276 extern int pgwin32_open(const char *, int,...);
277 extern FILE *pgwin32_fopen(const char *, const char *);
280 #define open(a,b,c) pgwin32_open(a,b,c)
281 #define fopen(a,b) pgwin32_fopen(a,b)
284 #define popen(a,b) _popen(a,b)
285 #define pclose(a) _pclose(a)
287 /* Missing rand functions */
288 extern long lrand48(void);
289 extern void srand48(long seed);
291 /* Last parameter not used */
292 extern int gettimeofday(struct timeval * tp, struct timezone * tzp);
296 * Win32 requires a special close for sockets and pipes, while on Unix
297 * close() does them all.
299 #define closesocket close
303 * Default "extern" declarations or macro substitutes for library routines.
304 * When necessary, these routines are provided by files in src/port/.
307 extern char *crypt(const char *key, const char *setting);
310 #if defined(bsdi) || defined(netbsd)
311 extern int fseeko(FILE *stream, off_t offset, int whence);
312 extern off_t ftello(FILE *stream);
316 #define fseeko(a, b, c) fseek(a, b, c)
317 #define ftello(a) ftell(a)
321 extern int getopt(int nargc, char *const * nargv, const char *ostr);
325 extern int isinf(double x);
329 extern double rint(double x);
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);
339 extern char *strdup(const char *str);
342 #if !HAVE_DECL_STRLCPY
343 extern size_t strlcpy(char *dst, const char *src, size_t siz);
346 #if !defined(HAVE_RANDOM) && !defined(__BORLANDC__)
347 extern long random(void);
350 #ifndef HAVE_UNSETENV
351 extern void unsetenv(const char *name);
355 extern void srandom(unsigned int seed);
359 extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen);
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);
366 extern int pqGethostbyname(const char *name,
367 struct hostent * resultbuf,
368 char *buffer, size_t buflen,
369 struct hostent ** result,
372 extern void pg_qsort(void *base, size_t nel, size_t elsize,
373 int (*cmp) (const void *, const void *));
375 #define qsort(a,b,c,d) pg_qsort(a,b,c,d)
377 typedef int (*qsort_arg_comparator) (const void *a, const void *b, void *arg);
379 extern void qsort_arg(void *base, size_t nel, size_t elsize,
380 qsort_arg_comparator cmp, void *arg);
382 #endif /* PG_PORT_H */