1 /*-------------------------------------------------------------------------
4 * Header for src/port/ compatibility functions.
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
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_html_path(const char *my_exec_path
, char *ret_path
);
46 extern void get_man_path(const char *my_exec_path
, char *ret_path
);
47 extern bool get_home_path(char *ret_path
);
48 extern void get_parent_directory(char *path
);
51 extern char **pgfnames(const char *path
);
52 extern void pgfnames_cleanup(char **filenames
);
57 * By making this a macro we avoid needing to include path.c in libpq.
60 #define is_absolute_path(filename) \
62 ((filename)[0] == '/') \
65 #define is_absolute_path(filename) \
67 ((filename)[0] == '/') || \
68 (filename)[0] == '\\' || \
69 (isalpha((unsigned char) ((filename)[0])) && (filename)[1] == ':' && \
70 ((filename)[2] == '\\' || (filename)[2] == '/')) \
74 /* Portable locale initialization (in exec.c) */
75 extern void set_pglocale_pgservice(const char *argv0
, const char *app
);
77 /* Portable way to find binaries (in exec.c) */
78 extern int find_my_exec(const char *argv0
, char *retpath
);
79 extern int find_other_exec(const char *argv0
, const char *target
,
80 const char *versionstr
, char *retpath
);
82 /* Windows security token manipulation (in exec.c) */
84 extern BOOL
AddUserToDacl(HANDLE hProcess
);
88 #if defined(WIN32) || defined(__CYGWIN__)
94 #if defined(WIN32) && !defined(__CYGWIN__)
96 /* "con" does not work from the Msys 1.0.10 console (part of MinGW). */
99 #define DEVNULL "/dev/null"
100 #define DEVTTY "/dev/tty"
104 * Win32 needs double quotes at the beginning and end of system()
105 * strings. If not, it gets confused with multiple quoted strings.
106 * It also requires double-quotes around the executable name and
107 * any files used for redirection. Other args can use single-quotes.
109 * Generated using Win32 "CMD /?":
111 * 1. If all of the following conditions are met, then quote characters
112 * on the command line are preserved:
115 * - exactly two quote characters
116 * - no special characters between the two quote characters, where special
117 * is one of: &<>()@^|
118 * - there are one or more whitespace characters between the the two quote
120 * - the string between the two quote characters is the name of an
123 * 2. Otherwise, old behavior is to see if the first character is a quote
124 * character and if so, strip the leading character and remove the last
125 * quote character on the command line, preserving any text after the last
128 #if defined(WIN32) && !defined(__CYGWIN__)
129 #define SYSTEMQUOTE "\""
131 #define SYSTEMQUOTE ""
134 /* Portable delay handling */
135 extern void pg_usleep(long microsec
);
137 /* Portable SQL-like case-independent comparisons and conversions */
138 extern int pg_strcasecmp(const char *s1
, const char *s2
);
139 extern int pg_strncasecmp(const char *s1
, const char *s2
, size_t n
);
140 extern unsigned char pg_toupper(unsigned char ch
);
141 extern unsigned char pg_tolower(unsigned char ch
);
143 #ifdef USE_REPL_SNPRINTF
146 * Versions of libintl >= 0.13 try to replace printf() and friends with
147 * macros to their own versions that understand the %$ format. We do the
148 * same, so disable their macros, if they exist.
169 extern int pg_vsnprintf(char *str
, size_t count
, const char *fmt
, va_list args
);
171 pg_snprintf(char *str
, size_t count
, const char *fmt
,...)
172 /* This extension allows gcc to check the format string */
173 __attribute__((format(printf
, 3, 4)));
175 pg_sprintf(char *str
, const char *fmt
,...)
176 /* This extension allows gcc to check the format string */
177 __attribute__((format(printf
, 2, 3)));
178 extern int pg_vfprintf(FILE *stream
, const char *fmt
, va_list args
);
180 pg_fprintf(FILE *stream
, const char *fmt
,...)
181 /* This extension allows gcc to check the format string */
182 __attribute__((format(printf
, 2, 3)));
184 pg_printf(const char *fmt
,...)
185 /* This extension allows gcc to check the format string */
186 __attribute__((format(printf
, 1, 2)));
189 * The GCC-specific code below prevents the __attribute__(... 'printf')
190 * above from being replaced, and this is required because gcc doesn't
191 * know anything about pg_printf.
194 #define vsnprintf(...) pg_vsnprintf(__VA_ARGS__)
195 #define snprintf(...) pg_snprintf(__VA_ARGS__)
196 #define sprintf(...) pg_sprintf(__VA_ARGS__)
197 #define vfprintf(...) pg_vfprintf(__VA_ARGS__)
198 #define fprintf(...) pg_fprintf(__VA_ARGS__)
199 #define printf(...) pg_printf(__VA_ARGS__)
201 #define vsnprintf pg_vsnprintf
202 #define snprintf pg_snprintf
203 #define sprintf pg_sprintf
204 #define vfprintf pg_vfprintf
205 #define fprintf pg_fprintf
206 #define printf pg_printf
208 #endif /* USE_REPL_SNPRINTF */
210 /* Portable prompt handling */
211 extern char *simple_prompt(const char *prompt
, int maxlen
, bool echo
);
214 * WIN32 doesn't allow descriptors returned by pipe() to be used in select(),
215 * so for that platform we use socket() instead of pipe().
216 * There is some inconsistency here because sometimes we require pg*, like
217 * pgpipe, but in other cases we define rename to pgrename just on Win32.
221 * The function prototypes are not supplied because every C file
222 * includes this file.
224 #define pgpipe(a) pipe(a)
225 #define piperead(a,b,c) read(a,b,c)
226 #define pipewrite(a,b,c) write(a,b,c)
228 extern int pgpipe(int handles
[2]);
229 extern int piperead(int s
, char *buf
, int len
);
231 #define pipewrite(a,b,c) send(a,b,c,0)
233 #define PG_SIGNAL_COUNT 32
234 #define kill(pid,sig) pgkill(pid,sig)
235 extern int pgkill(int pid
, int sig
);
238 extern int pclose_check(FILE *stream
);
240 /* Global variable holding time zone information. */
242 #define TIMEZONE_GLOBAL timezone
243 #define TZNAME_GLOBAL tzname
245 #define TIMEZONE_GLOBAL _timezone
246 #define TZNAME_GLOBAL _tzname
249 #if defined(WIN32) || defined(__CYGWIN__)
251 * Win32 doesn't have reliable rename/unlink during concurrent access.
253 extern int pgrename(const char *from
, const char *to
);
254 extern int pgunlink(const char *path
);
256 /* Include this first so later includes don't see these defines */
257 #ifdef WIN32_ONLY_COMPILER
261 #define rename(from, to) pgrename(from, to)
262 #define unlink(path) pgunlink(path)
263 #endif /* defined(WIN32) || defined(__CYGWIN__) */
266 * Win32 also doesn't have symlinks, but we can emulate them with
267 * junction points on newer Win32 versions.
269 * Cygwin has its own symlinks which work on Win95/98/ME where
270 * junction points don't, so use those instead. We have no way of
271 * knowing what type of system Cygwin binaries will be run on.
272 * Note: Some CYGWIN includes might #define WIN32.
274 #if defined(WIN32) && !defined(__CYGWIN__)
275 extern int pgsymlink(const char *oldpath
, const char *newpath
);
277 #define symlink(oldpath, newpath) pgsymlink(oldpath, newpath)
280 extern void copydir(char *fromdir
, char *todir
, bool recurse
);
282 extern bool rmtree(const char *path
, bool rmtopdir
);
285 * stat() is not guaranteed to set the st_size field on win32, so we
286 * redefine it to our own implementation that is.
288 * We must pull in sys/stat.h here so the system header definition
289 * goes in first, and we redefine that, and not the other way around.
291 * Some frontends don't need the size from stat, so if UNSAFE_STAT_OK
292 * is defined we don't bother with this.
294 #if defined(WIN32) && !defined(__CYGWIN__) && !defined(UNSAFE_STAT_OK)
295 #include <sys/stat.h>
296 extern int pgwin32_safestat(const char *path
, struct stat
* buf
);
298 #define stat(a,b) pgwin32_safestat(a,b)
301 #if defined(WIN32) && !defined(__CYGWIN__)
304 * open() and fopen() replacements to allow deletion of open files and
305 * passing of other special options.
307 #define O_DIRECT 0x80000000
308 extern int pgwin32_open(const char *, int,...);
309 extern FILE *pgwin32_fopen(const char *, const char *);
312 #define open(a,b,c) pgwin32_open(a,b,c)
313 #define fopen(a,b) pgwin32_fopen(a,b)
316 #define popen(a,b) _popen(a,b)
317 #define pclose(a) _pclose(a)
319 /* Missing rand functions */
320 extern long lrand48(void);
321 extern void srand48(long seed
);
323 /* New versions of MingW have gettimeofday, old mingw and msvc don't */
324 #ifndef HAVE_GETTIMEOFDAY
325 /* Last parameter not used */
326 extern int gettimeofday(struct timeval
* tp
, struct timezone
* tzp
);
331 * Win32 requires a special close for sockets and pipes, while on Unix
332 * close() does them all.
334 #define closesocket close
338 * Default "extern" declarations or macro substitutes for library routines.
339 * When necessary, these routines are provided by files in src/port/.
342 extern char *crypt(const char *key
, const char *setting
);
345 /* WIN32 handled in port/win32.h */
347 #define pgoff_t off_t
348 #if defined(bsdi) || defined(netbsd)
349 extern int fseeko(FILE *stream
, off_t offset
, int whence
);
350 extern off_t
ftello(FILE *stream
);
355 #define fseeko(a, b, c) fseek(a, b, c)
356 #define ftello(a) ftell(a)
360 extern int getopt(int nargc
, char *const * nargv
, const char *ostr
);
364 extern int isinf(double x
);
368 extern double rint(double x
);
371 #ifndef HAVE_INET_ATON
372 #include <netinet/in.h>
373 #include <arpa/inet.h>
374 extern int inet_aton(const char *cp
, struct in_addr
* addr
);
378 extern char *strdup(const char *str
);
381 #if !HAVE_DECL_STRLCAT
382 extern size_t strlcat(char *dst
, const char *src
, size_t siz
);
385 #if !HAVE_DECL_STRLCPY
386 extern size_t strlcpy(char *dst
, const char *src
, size_t siz
);
389 #if !defined(HAVE_RANDOM) && !defined(__BORLANDC__)
390 extern long random(void);
393 #ifndef HAVE_UNSETENV
394 extern void unsetenv(const char *name
);
398 extern void srandom(unsigned int seed
);
402 extern char *pqStrerror(int errnum
, char *strerrbuf
, size_t buflen
);
404 #if !defined(WIN32) || defined(__CYGWIN__)
405 extern int pqGetpwuid(uid_t uid
, struct passwd
* resultbuf
, char *buffer
,
406 size_t buflen
, struct passwd
** result
);
409 extern int pqGethostbyname(const char *name
,
410 struct hostent
* resultbuf
,
411 char *buffer
, size_t buflen
,
412 struct hostent
** result
,
415 extern void pg_qsort(void *base
, size_t nel
, size_t elsize
,
416 int (*cmp
) (const void *, const void *));
418 #define qsort(a,b,c,d) pg_qsort(a,b,c,d)
420 typedef int (*qsort_arg_comparator
) (const void *a
, const void *b
, void *arg
);
422 extern void qsort_arg(void *base
, size_t nel
, size_t elsize
,
423 qsort_arg_comparator cmp
, void *arg
);
425 /* port/chklocale.c */
426 extern int pg_get_encoding_from_locale(const char *ctype
);
428 #endif /* PG_PORT_H */