Header And Logo

PostgreSQL
| The world's most advanced open source database.

port.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * port.h
00004  *    Header for src/port/ compatibility functions.
00005  *
00006  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00007  * Portions Copyright (c) 1994, Regents of the University of California
00008  *
00009  * src/include/port.h
00010  *
00011  *-------------------------------------------------------------------------
00012  */
00013 #ifndef PG_PORT_H
00014 #define PG_PORT_H
00015 
00016 #include <ctype.h>
00017 #include <netdb.h>
00018 #include <pwd.h>
00019 
00020 /* socket has a different definition on WIN32 */
00021 #ifndef WIN32
00022 typedef int pgsocket;
00023 
00024 #define PGINVALID_SOCKET (-1)
00025 #else
00026 typedef SOCKET pgsocket;
00027 
00028 #define PGINVALID_SOCKET INVALID_SOCKET
00029 #endif
00030 
00031 /* non-blocking */
00032 extern bool pg_set_noblock(pgsocket sock);
00033 extern bool pg_set_block(pgsocket sock);
00034 
00035 /* Portable path handling for Unix/Win32 (in path.c) */
00036 
00037 extern bool has_drive_prefix(const char *filename);
00038 extern char *first_dir_separator(const char *filename);
00039 extern char *last_dir_separator(const char *filename);
00040 extern char *first_path_var_separator(const char *pathlist);
00041 extern void join_path_components(char *ret_path,
00042                      const char *head, const char *tail);
00043 extern void canonicalize_path(char *path);
00044 extern void make_native_path(char *path);
00045 extern bool path_contains_parent_reference(const char *path);
00046 extern bool path_is_relative_and_below_cwd(const char *path);
00047 extern bool path_is_prefix_of_path(const char *path1, const char *path2);
00048 extern const char *get_progname(const char *argv0);
00049 extern void get_share_path(const char *my_exec_path, char *ret_path);
00050 extern void get_etc_path(const char *my_exec_path, char *ret_path);
00051 extern void get_include_path(const char *my_exec_path, char *ret_path);
00052 extern void get_pkginclude_path(const char *my_exec_path, char *ret_path);
00053 extern void get_includeserver_path(const char *my_exec_path, char *ret_path);
00054 extern void get_lib_path(const char *my_exec_path, char *ret_path);
00055 extern void get_pkglib_path(const char *my_exec_path, char *ret_path);
00056 extern void get_locale_path(const char *my_exec_path, char *ret_path);
00057 extern void get_doc_path(const char *my_exec_path, char *ret_path);
00058 extern void get_html_path(const char *my_exec_path, char *ret_path);
00059 extern void get_man_path(const char *my_exec_path, char *ret_path);
00060 extern bool get_home_path(char *ret_path);
00061 extern void get_parent_directory(char *path);
00062 
00063 /* port/dirmod.c */
00064 extern char **pgfnames(const char *path);
00065 extern void pgfnames_cleanup(char **filenames);
00066 
00067 /*
00068  *  is_absolute_path
00069  *
00070  *  By making this a macro we avoid needing to include path.c in libpq.
00071  */
00072 #ifndef WIN32
00073 #define IS_DIR_SEP(ch)  ((ch) == '/')
00074 
00075 #define is_absolute_path(filename) \
00076 ( \
00077     IS_DIR_SEP((filename)[0]) \
00078 )
00079 #else
00080 #define IS_DIR_SEP(ch)  ((ch) == '/' || (ch) == '\\')
00081 
00082 /* See path_is_relative_and_below_cwd() for how we handle 'E:abc'. */
00083 #define is_absolute_path(filename) \
00084 ( \
00085     IS_DIR_SEP((filename)[0]) || \
00086     (isalpha((unsigned char) ((filename)[0])) && (filename)[1] == ':' && \
00087      IS_DIR_SEP((filename)[2])) \
00088 )
00089 #endif
00090 
00091 /* Portable locale initialization (in exec.c) */
00092 extern void set_pglocale_pgservice(const char *argv0, const char *app);
00093 
00094 /* Portable way to find binaries (in exec.c) */
00095 extern int  find_my_exec(const char *argv0, char *retpath);
00096 extern int find_other_exec(const char *argv0, const char *target,
00097                 const char *versionstr, char *retpath);
00098 
00099 /* Windows security token manipulation (in exec.c) */
00100 #ifdef WIN32
00101 extern BOOL AddUserToTokenDacl(HANDLE hToken);
00102 #endif
00103 
00104 
00105 #if defined(WIN32) || defined(__CYGWIN__)
00106 #define EXE ".exe"
00107 #else
00108 #define EXE ""
00109 #endif
00110 
00111 #if defined(WIN32) && !defined(__CYGWIN__)
00112 #define DEVNULL "nul"
00113 #else
00114 #define DEVNULL "/dev/null"
00115 #endif
00116 
00117 /*
00118  *  Win32 needs double quotes at the beginning and end of system()
00119  *  strings.  If not, it gets confused with multiple quoted strings.
00120  *  It also requires double-quotes around the executable name and
00121  *  any files used for redirection.  Other args can use single-quotes.
00122  *
00123  *  Generated using Win32 "CMD /?":
00124  *
00125  *  1. If all of the following conditions are met, then quote characters
00126  *  on the command line are preserved:
00127  *
00128  *   - no /S switch
00129  *   - exactly two quote characters
00130  *   - no special characters between the two quote characters, where special
00131  *     is one of: &<>()@^|
00132  *   - there are one or more whitespace characters between the two quote
00133  *     characters
00134  *   - the string between the two quote characters is the name of an
00135  *     executable file.
00136  *
00137  *   2. Otherwise, old behavior is to see if the first character is a quote
00138  *   character and if so, strip the leading character and remove the last
00139  *   quote character on the command line, preserving any text after the last
00140  *   quote character.
00141  */
00142 #if defined(WIN32) && !defined(__CYGWIN__)
00143 #define SYSTEMQUOTE "\""
00144 #else
00145 #define SYSTEMQUOTE ""
00146 #endif
00147 
00148 /* Portable delay handling */
00149 extern void pg_usleep(long microsec);
00150 
00151 /* Portable SQL-like case-independent comparisons and conversions */
00152 extern int  pg_strcasecmp(const char *s1, const char *s2);
00153 extern int  pg_strncasecmp(const char *s1, const char *s2, size_t n);
00154 extern unsigned char pg_toupper(unsigned char ch);
00155 extern unsigned char pg_tolower(unsigned char ch);
00156 extern unsigned char pg_ascii_toupper(unsigned char ch);
00157 extern unsigned char pg_ascii_tolower(unsigned char ch);
00158 
00159 #ifdef USE_REPL_SNPRINTF
00160 
00161 /*
00162  * Versions of libintl >= 0.13 try to replace printf() and friends with
00163  * macros to their own versions that understand the %$ format.  We do the
00164  * same, so disable their macros, if they exist.
00165  */
00166 #ifdef vsnprintf
00167 #undef vsnprintf
00168 #endif
00169 #ifdef snprintf
00170 #undef snprintf
00171 #endif
00172 #ifdef sprintf
00173 #undef sprintf
00174 #endif
00175 #ifdef vfprintf
00176 #undef vfprintf
00177 #endif
00178 #ifdef fprintf
00179 #undef fprintf
00180 #endif
00181 #ifdef printf
00182 #undef printf
00183 #endif
00184 
00185 extern int  pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args);
00186 extern int
00187 pg_snprintf(char *str, size_t count, const char *fmt,...)
00188 /* This extension allows gcc to check the format string */
00189 __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
00190 extern int
00191 pg_sprintf(char *str, const char *fmt,...)
00192 /* This extension allows gcc to check the format string */
00193 __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
00194 extern int  pg_vfprintf(FILE *stream, const char *fmt, va_list args);
00195 extern int
00196 pg_fprintf(FILE *stream, const char *fmt,...)
00197 /* This extension allows gcc to check the format string */
00198 __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
00199 extern int
00200 pg_printf(const char *fmt,...)
00201 /* This extension allows gcc to check the format string */
00202 __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
00203 
00204 /*
00205  *  The GCC-specific code below prevents the __attribute__(... 'printf')
00206  *  above from being replaced, and this is required because gcc doesn't
00207  *  know anything about pg_printf.
00208  */
00209 #ifdef __GNUC__
00210 #define vsnprintf(...)  pg_vsnprintf(__VA_ARGS__)
00211 #define snprintf(...)   pg_snprintf(__VA_ARGS__)
00212 #define sprintf(...)    pg_sprintf(__VA_ARGS__)
00213 #define vfprintf(...)   pg_vfprintf(__VA_ARGS__)
00214 #define fprintf(...)    pg_fprintf(__VA_ARGS__)
00215 #define printf(...)     pg_printf(__VA_ARGS__)
00216 #else
00217 #define vsnprintf       pg_vsnprintf
00218 #define snprintf        pg_snprintf
00219 #define sprintf         pg_sprintf
00220 #define vfprintf        pg_vfprintf
00221 #define fprintf         pg_fprintf
00222 #define printf          pg_printf
00223 #endif
00224 #endif   /* USE_REPL_SNPRINTF */
00225 
00226 #if defined(WIN32)
00227 /*
00228  * Versions of libintl >= 0.18? try to replace setlocale() with a macro
00229  * to their own versions.  Remove the macro, if it exists, because it
00230  * ends up calling the wrong version when the backend and libintl use
00231  * different versions of msvcrt.
00232  */
00233 #if defined(setlocale)
00234 #undef setlocale
00235 #endif
00236 
00237 /*
00238  * Define our own wrapper macro around setlocale() to work around bugs in
00239  * Windows' native setlocale() function.
00240  */
00241 extern char *pgwin32_setlocale(int category, const char *locale);
00242 
00243 #define setlocale(a,b) pgwin32_setlocale(a,b)
00244 #endif   /* WIN32 */
00245 
00246 /* Portable prompt handling */
00247 extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
00248 
00249 #ifdef WIN32
00250 #define PG_SIGNAL_COUNT 32
00251 #define kill(pid,sig)   pgkill(pid,sig)
00252 extern int  pgkill(int pid, int sig);
00253 #endif
00254 
00255 extern int  pclose_check(FILE *stream);
00256 
00257 /* Global variable holding time zone information. */
00258 #ifndef __CYGWIN__
00259 #define TIMEZONE_GLOBAL timezone
00260 #define TZNAME_GLOBAL tzname
00261 #else
00262 #define TIMEZONE_GLOBAL _timezone
00263 #define TZNAME_GLOBAL _tzname
00264 #endif
00265 
00266 #if defined(WIN32) || defined(__CYGWIN__)
00267 /*
00268  *  Win32 doesn't have reliable rename/unlink during concurrent access.
00269  */
00270 extern int  pgrename(const char *from, const char *to);
00271 extern int  pgunlink(const char *path);
00272 
00273 /* Include this first so later includes don't see these defines */
00274 #ifdef WIN32_ONLY_COMPILER
00275 #include <io.h>
00276 #endif
00277 
00278 #define rename(from, to)        pgrename(from, to)
00279 #define unlink(path)            pgunlink(path)
00280 #endif   /* defined(WIN32) || defined(__CYGWIN__) */
00281 
00282 /*
00283  *  Win32 also doesn't have symlinks, but we can emulate them with
00284  *  junction points on newer Win32 versions.
00285  *
00286  *  Cygwin has its own symlinks which work on Win95/98/ME where
00287  *  junction points don't, so use those instead.  We have no way of
00288  *  knowing what type of system Cygwin binaries will be run on.
00289  *      Note: Some CYGWIN includes might #define WIN32.
00290  */
00291 #if defined(WIN32) && !defined(__CYGWIN__)
00292 extern int  pgsymlink(const char *oldpath, const char *newpath);
00293 extern int  pgreadlink(const char *path, char *buf, size_t size);
00294 extern bool pgwin32_is_junction(char *path);
00295 
00296 #define symlink(oldpath, newpath)   pgsymlink(oldpath, newpath)
00297 #define readlink(path, buf, size)   pgreadlink(path, buf, size)
00298 #endif
00299 
00300 extern bool rmtree(const char *path, bool rmtopdir);
00301 
00302 /*
00303  * stat() is not guaranteed to set the st_size field on win32, so we
00304  * redefine it to our own implementation that is.
00305  *
00306  * We must pull in sys/stat.h here so the system header definition
00307  * goes in first, and we redefine that, and not the other way around.
00308  *
00309  * Some frontends don't need the size from stat, so if UNSAFE_STAT_OK
00310  * is defined we don't bother with this.
00311  */
00312 #if defined(WIN32) && !defined(__CYGWIN__) && !defined(UNSAFE_STAT_OK)
00313 #include <sys/stat.h>
00314 extern int  pgwin32_safestat(const char *path, struct stat * buf);
00315 
00316 #define stat(a,b) pgwin32_safestat(a,b)
00317 #endif
00318 
00319 #if defined(WIN32) && !defined(__CYGWIN__)
00320 
00321 /*
00322  * open() and fopen() replacements to allow deletion of open files and
00323  * passing of other special options.
00324  */
00325 #define     O_DIRECT    0x80000000
00326 extern int  pgwin32_open(const char *, int,...);
00327 extern FILE *pgwin32_fopen(const char *, const char *);
00328 
00329 #ifndef FRONTEND
00330 #define     open(a,b,c) pgwin32_open(a,b,c)
00331 #define     fopen(a,b) pgwin32_fopen(a,b)
00332 #endif
00333 
00334 #ifndef popen
00335 #define popen(a,b) _popen(a,b)
00336 #endif
00337 #ifndef pclose
00338 #define pclose(a) _pclose(a)
00339 #endif
00340 
00341 /* New versions of MingW have gettimeofday, old mingw and msvc don't */
00342 #ifndef HAVE_GETTIMEOFDAY
00343 /* Last parameter not used */
00344 extern int  gettimeofday(struct timeval * tp, struct timezone * tzp);
00345 #endif
00346 #else                           /* !WIN32 */
00347 
00348 /*
00349  *  Win32 requires a special close for sockets and pipes, while on Unix
00350  *  close() does them all.
00351  */
00352 #define closesocket close
00353 #endif   /* WIN32 */
00354 
00355 /*
00356  * Default "extern" declarations or macro substitutes for library routines.
00357  * When necessary, these routines are provided by files in src/port/.
00358  */
00359 #ifndef HAVE_CRYPT
00360 extern char *crypt(const char *key, const char *setting);
00361 #endif
00362 
00363 /* WIN32 handled in port/win32.h */
00364 #ifndef WIN32
00365 #define pgoff_t off_t
00366 #ifdef __NetBSD__
00367 extern int  fseeko(FILE *stream, off_t offset, int whence);
00368 extern off_t ftello(FILE *stream);
00369 #endif
00370 #endif
00371 
00372 extern double pg_erand48(unsigned short xseed[3]);
00373 extern long pg_lrand48(void);
00374 extern void pg_srand48(long seed);
00375 
00376 #ifndef HAVE_FLS
00377 extern int  fls(int mask);
00378 #endif
00379 
00380 #ifndef HAVE_FSEEKO
00381 #define fseeko(a, b, c) fseek(a, b, c)
00382 #define ftello(a)       ftell(a)
00383 #endif
00384 
00385 #ifndef HAVE_GETOPT
00386 extern int  getopt(int nargc, char *const * nargv, const char *ostr);
00387 #endif
00388 
00389 #if !defined(HAVE_GETPEEREID) && !defined(WIN32)
00390 extern int  getpeereid(int sock, uid_t *uid, gid_t *gid);
00391 #endif
00392 
00393 #ifndef HAVE_ISINF
00394 extern int  isinf(double x);
00395 #endif
00396 
00397 #ifndef HAVE_RINT
00398 extern double rint(double x);
00399 #endif
00400 
00401 #ifndef HAVE_INET_ATON
00402 #include <netinet/in.h>
00403 #include <arpa/inet.h>
00404 extern int  inet_aton(const char *cp, struct in_addr * addr);
00405 #endif
00406 
00407 #if !HAVE_DECL_STRLCAT
00408 extern size_t strlcat(char *dst, const char *src, size_t siz);
00409 #endif
00410 
00411 #if !HAVE_DECL_STRLCPY
00412 extern size_t strlcpy(char *dst, const char *src, size_t siz);
00413 #endif
00414 
00415 #if !defined(HAVE_RANDOM) && !defined(__BORLANDC__)
00416 extern long random(void);
00417 #endif
00418 
00419 #ifndef HAVE_UNSETENV
00420 extern void unsetenv(const char *name);
00421 #endif
00422 
00423 #ifndef HAVE_SRANDOM
00424 extern void srandom(unsigned int seed);
00425 #endif
00426 
00427 /* thread.h */
00428 extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen);
00429 
00430 #if !defined(WIN32) || defined(__CYGWIN__)
00431 extern int pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
00432            size_t buflen, struct passwd ** result);
00433 #endif
00434 
00435 extern int pqGethostbyname(const char *name,
00436                 struct hostent * resultbuf,
00437                 char *buffer, size_t buflen,
00438                 struct hostent ** result,
00439                 int *herrno);
00440 
00441 extern void pg_qsort(void *base, size_t nel, size_t elsize,
00442          int (*cmp) (const void *, const void *));
00443 extern int pg_qsort_strcmp(const void *a, const void *b);
00444 
00445 #define qsort(a,b,c,d) pg_qsort(a,b,c,d)
00446 
00447 typedef int (*qsort_arg_comparator) (const void *a, const void *b, void *arg);
00448 
00449 extern void qsort_arg(void *base, size_t nel, size_t elsize,
00450           qsort_arg_comparator cmp, void *arg);
00451 
00452 /* port/chklocale.c */
00453 extern int  pg_get_encoding_from_locale(const char *ctype, bool write_message);
00454 
00455 /* port/inet_net_ntop.c */
00456 extern char *inet_net_ntop(int af, const void *src, int bits,
00457               char *dst, size_t size);
00458 
00459 /* port/pgcheckdir.c */
00460 extern int  pg_check_dir(const char *dir);
00461 
00462 /* port/pgmkdirp.c */
00463 extern int  pg_mkdir_p(char *path, int omode);
00464 
00465 /* port/pqsignal.c */
00466 typedef void (*pqsigfunc) (int signo);
00467 extern pqsigfunc pqsignal(int signo, pqsigfunc func);
00468 
00469 /* port/quotes.c */
00470 extern char *escape_single_quotes_ascii(const char *src);
00471 
00472 /* port/wait_error.c */
00473 extern char *wait_result_to_str(int exit_status);
00474 
00475 #endif   /* PG_PORT_H */