]> granicus.if.org Git - postgresql/blobdiff - src/include/port.h
Per-column collation support
[postgresql] / src / include / port.h
index e6bde3de6e0b02687fc485c8c57fbb70e7e6cf07..f6d6d2e444bd535701f200fabe38954f63bdba6c 100644 (file)
@@ -1,46 +1,48 @@
 /*-------------------------------------------------------------------------
  *
  * port.h
- *       Header for /port compatibility functions.
+ *       Header for src/port/ compatibility functions.
  *
- * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/port.h,v 1.64 2004/10/11 22:50:33 momjian Exp $
+ * src/include/port.h
  *
  *-------------------------------------------------------------------------
  */
+#ifndef PG_PORT_H
+#define PG_PORT_H
 
-#ifndef WIN32_CLIENT_ONLY
-/* for thread.c */
-#include <pwd.h>
+#include <ctype.h>
 #include <netdb.h>
-#endif
+#include <pwd.h>
 
-#include <ctype.h>
+/* socket has a different definition on WIN32 */
+#ifndef WIN32
+typedef int pgsocket;
+
+#define PGINVALID_SOCKET (-1)
+#else
+typedef SOCKET pgsocket;
+
+#define PGINVALID_SOCKET INVALID_SOCKET
+#endif
 
 /* non-blocking */
-bool           set_noblock(int sock);
+extern bool pg_set_noblock(pgsocket sock);
+extern bool pg_set_block(pgsocket sock);
 
-/* Portable path handling for Unix/Win32 */
+/* Portable path handling for Unix/Win32 (in path.c) */
 
-/* Find the location of the first directory separator, return
- * NULL if not found.
- */
 extern char *first_dir_separator(const char *filename);
-
-/* Find the location of the last directory separator, return
- * NULL if not found.
- */
 extern char *last_dir_separator(const char *filename);
-
-/* Find the location of the first path separator (i.e. ':' on
- * Unix, ';' on Windows), return NULL if not found.
- */
-extern char *first_path_separator(const char *filename);
-
+extern char *first_path_var_separator(const char *pathlist);
+extern void join_path_components(char *ret_path,
+                                        const char *head, const char *tail);
 extern void canonicalize_path(char *path);
 extern void make_native_path(char *path);
+extern bool path_contains_parent_reference(const char *path);
+extern bool path_is_prefix_of_path(const char *path1, const char *path2);
 extern const char *get_progname(const char *argv0);
 extern void get_share_path(const char *my_exec_path, char *ret_path);
 extern void get_etc_path(const char *my_exec_path, char *ret_path);
@@ -50,44 +52,73 @@ extern void get_includeserver_path(const char *my_exec_path, char *ret_path);
 extern void get_lib_path(const char *my_exec_path, char *ret_path);
 extern void get_pkglib_path(const char *my_exec_path, char *ret_path);
 extern void get_locale_path(const char *my_exec_path, char *ret_path);
-extern void set_pglocale_pgservice(const char *argv0, const char *app);
+extern void get_doc_path(const char *my_exec_path, char *ret_path);
+extern void get_html_path(const char *my_exec_path, char *ret_path);
+extern void get_man_path(const char *my_exec_path, char *ret_path);
 extern bool get_home_path(char *ret_path);
 extern void get_parent_directory(char *path);
 
+/* port/dirmod.c */
+extern char **pgfnames(const char *path);
+extern void pgfnames_cleanup(char **filenames);
+
 /*
  *     is_absolute_path
  *
- *     This capability is needed by libpq and initdb.c
- *     On Win32, you can't reference the same object file that is
- *     in two different libraries (pgport and libpq), so a macro is best.
+ *     By making this a macro we avoid needing to include path.c in libpq.
  */
 #ifndef WIN32
+#define IS_DIR_SEP(ch) ((ch) == '/')
+
 #define is_absolute_path(filename) \
 ( \
-       ((filename)[0] == '/') \
+       IS_DIR_SEP((filename)[0]) \
 )
 #else
+#define IS_DIR_SEP(ch) ((ch) == '/' || (ch) == '\\')
+
+/*
+ * On Win32, a drive letter _not_ followed by a slash, e.g. 'E:abc', is
+ * relative to the cwd on that drive, or the drive's root directory
+ * if that drive has no cwd.  Because the path itself cannot tell us
+ * which is the case, we have to assume the worst, i.e. that it is not
+ * absolute;  this check is done by IS_DIR_SEP(filename[2]).
+ */
 #define is_absolute_path(filename) \
 ( \
-       ((filename)[0] == '/') || \
-       (filename)[0] == '\\' || \
-       (isalpha((filename)[0]) && (filename)[1] == ':' && \
-       ((filename)[2] == '\\' || (filename)[2] == '/')) \
+       IS_DIR_SEP((filename)[0]) || \
+       (isalpha((unsigned char) ((filename)[0])) && (filename)[1] == ':' && \
+        IS_DIR_SEP((filename)[2])) \
 )
 #endif
 
+/* Portable locale initialization (in exec.c) */
+extern void set_pglocale_pgservice(const char *argv0, const char *app);
 
-/* Portable way to find binaries */
+/* Portable way to find binaries (in exec.c) */
 extern int     find_my_exec(const char *argv0, char *retpath);
 extern int find_other_exec(const char *argv0, const char *target,
                                const char *versionstr, char *retpath);
 
+/* Windows security token manipulation (in exec.c) */
+#ifdef WIN32
+extern BOOL AddUserToTokenDacl(HANDLE hToken);
+#endif
+
+
 #if defined(WIN32) || defined(__CYGWIN__)
 #define EXE ".exe"
-#define DEVNULL "nul"
 #else
 #define EXE ""
+#endif
+
+#if defined(WIN32) && !defined(__CYGWIN__)
+#define DEVNULL "nul"
+/* "con" does not work from the Msys 1.0.10 console (part of MinGW). */
+#define DEVTTY "con"
+#else
 #define DEVNULL "/dev/null"
+#define DEVTTY "/dev/tty"
 #endif
 
 /*
@@ -96,21 +127,31 @@ extern int find_other_exec(const char *argv0, const char *target,
  *     It also requires double-quotes around the executable name and
  *     any files used for redirection.  Other args can use single-quotes.
  *
- *     See the "Notes" section about quotes at:
- *             http://home.earthlink.net/~rlively/MANUALS/COMMANDS/C/CMD.HTM
+ *     Generated using Win32 "CMD /?":
+ *
+ *     1. If all of the following conditions are met, then quote characters
+ *     on the command line are preserved:
+ *
+ *      - no /S switch
+ *      - exactly two quote characters
+ *      - no special characters between the two quote characters, where special
+ *        is one of: &<>()@^|
+ *      - there are one or more whitespace characters between the two quote
+ *        characters
+ *      - the string between the two quote characters is the name of an
+ *        executable file.
+ *
+ *      2. Otherwise, old behavior is to see if the first character is a quote
+ *      character and if so, strip the leading character and remove the last
+ *      quote character on the command line, preserving any text after the last
+ *      quote character.
  */
-#ifdef WIN32
+#if defined(WIN32) && !defined(__CYGWIN__)
 #define SYSTEMQUOTE "\""
 #else
 #define SYSTEMQUOTE ""
 #endif
 
-#ifdef WIN32
-#define HOMEDIR "USERPROFILE"
-#else
-#define HOMEDIR "HOME"
-#endif
-
 /* Portable delay handling */
 extern void pg_usleep(long microsec);
 
@@ -120,13 +161,84 @@ extern int        pg_strncasecmp(const char *s1, const char *s2, size_t n);
 extern unsigned char pg_toupper(unsigned char ch);
 extern unsigned char pg_tolower(unsigned char ch);
 
-/* Portable prompt handling */
-extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
+#ifdef USE_REPL_SNPRINTF
 
-#if defined(bsdi) || defined(netbsd)
-extern int     fseeko(FILE *stream, off_t offset, int whence);
-extern off_t ftello(FILE *stream);
+/*
+ * Versions of libintl >= 0.13 try to replace printf() and friends with
+ * macros to their own versions that understand the %$ format. We do the
+ * same, so disable their macros, if they exist.
+ */
+#ifdef vsnprintf
+#undef vsnprintf
+#endif
+#ifdef snprintf
+#undef snprintf
+#endif
+#ifdef sprintf
+#undef sprintf
+#endif
+#ifdef vfprintf
+#undef vfprintf
+#endif
+#ifdef fprintf
+#undef fprintf
+#endif
+#ifdef printf
+#undef printf
+#endif
+/*
+ * Versions of libintl >= 0.18? try to replace setlocale() with a macro
+ * to their own versions.  Remove the macro, if it exists, because it
+ * ends up calling the wrong version when the backend and libintl use
+ * different versions of msvcrt.
+ */
+#if defined(setlocale) && defined(WIN32)
+#undef setlocale
+#endif
+
+extern int     pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args);
+extern int
+pg_snprintf(char *str, size_t count, const char *fmt,...)
+/* This extension allows gcc to check the format string */
+__attribute__((format(printf, 3, 4)));
+extern int
+pg_sprintf(char *str, const char *fmt,...)
+/* This extension allows gcc to check the format string */
+__attribute__((format(printf, 2, 3)));
+extern int     pg_vfprintf(FILE *stream, const char *fmt, va_list args);
+extern int
+pg_fprintf(FILE *stream, const char *fmt,...)
+/* This extension allows gcc to check the format string */
+__attribute__((format(printf, 2, 3)));
+extern int
+pg_printf(const char *fmt,...)
+/* This extension allows gcc to check the format string */
+__attribute__((format(printf, 1, 2)));
+
+/*
+ *     The GCC-specific code below prevents the __attribute__(... 'printf')
+ *     above from being replaced, and this is required because gcc doesn't
+ *     know anything about pg_printf.
+ */
+#ifdef __GNUC__
+#define vsnprintf(...) pg_vsnprintf(__VA_ARGS__)
+#define snprintf(...)  pg_snprintf(__VA_ARGS__)
+#define sprintf(...)   pg_sprintf(__VA_ARGS__)
+#define vfprintf(...)  pg_vfprintf(__VA_ARGS__)
+#define fprintf(...)   pg_fprintf(__VA_ARGS__)
+#define printf(...)            pg_printf(__VA_ARGS__)
+#else
+#define vsnprintf              pg_vsnprintf
+#define snprintf               pg_snprintf
+#define sprintf                        pg_sprintf
+#define vfprintf               pg_vfprintf
+#define fprintf                        pg_fprintf
+#define printf                 pg_printf
 #endif
+#endif   /* USE_REPL_SNPRINTF */
+
+/* Portable prompt handling */
+extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
 
 /*
  *     WIN32 doesn't allow descriptors returned by pipe() to be used in select(),
@@ -135,6 +247,10 @@ extern off_t ftello(FILE *stream);
  *     pgpipe, but in other cases we define rename to pgrename just on Win32.
  */
 #ifndef WIN32
+/*
+ *     The function prototypes are not supplied because every C file
+ *     includes this file.
+ */
 #define pgpipe(a)                      pipe(a)
 #define piperead(a,b,c)                read(a,b,c)
 #define pipewrite(a,b,c)       write(a,b,c)
@@ -162,64 +278,92 @@ extern int        pclose_check(FILE *stream);
 
 #if defined(WIN32) || defined(__CYGWIN__)
 /*
- *     Win32 doesn't have reliable rename/unlink during concurrent access,
- *     and we need special code to do symlinks.
+ *     Win32 doesn't have reliable rename/unlink during concurrent access.
  */
 extern int     pgrename(const char *from, const char *to);
 extern int     pgunlink(const char *path);
+
 /* Include this first so later includes don't see these defines */
-#ifdef WIN32_CLIENT_ONLY
+#ifdef WIN32_ONLY_COMPILER
 #include <io.h>
 #endif
 
 #define rename(from, to)               pgrename(from, to)
 #define unlink(path)                   pgunlink(path)
+#endif   /* defined(WIN32) || defined(__CYGWIN__) */
 
 /*
+ *     Win32 also doesn't have symlinks, but we can emulate them with
+ *     junction points on newer Win32 versions.
+ *
  *     Cygwin has its own symlinks which work on Win95/98/ME where
- *     junction points don't, so use it instead.  We have no way of
+ *     junction points don't, so use those instead.  We have no way of
  *     knowing what type of system Cygwin binaries will be run on.
+ *             Note: Some CYGWIN includes might #define WIN32.
  */
-#ifdef WIN32   
+#if defined(WIN32) && !defined(__CYGWIN__)
 extern int     pgsymlink(const char *oldpath, const char *newpath);
+extern int     pgreadlink(const char *path, char *buf, size_t size);
+extern bool pgwin32_is_junction(char *path);
+
 #define symlink(oldpath, newpath)      pgsymlink(oldpath, newpath)
+#define readlink(path, buf, size)      pgreadlink(path, buf, size)
 #endif
 
-#endif
+extern bool rmtree(const char *path, bool rmtopdir);
 
-extern bool rmtree(char *path, bool rmtopdir);
+/*
+ * stat() is not guaranteed to set the st_size field on win32, so we
+ * redefine it to our own implementation that is.
+ *
+ * We must pull in sys/stat.h here so the system header definition
+ * goes in first, and we redefine that, and not the other way around.
+ *
+ * Some frontends don't need the size from stat, so if UNSAFE_STAT_OK
+ * is defined we don't bother with this.
+ */
+#if defined(WIN32) && !defined(__CYGWIN__) && !defined(UNSAFE_STAT_OK)
+#include <sys/stat.h>
+extern int     pgwin32_safestat(const char *path, struct stat * buf);
 
-#ifdef WIN32
+#define stat(a,b) pgwin32_safestat(a,b)
+#endif
 
-/* open() replacement to allow delete of held files */
-#ifndef WIN32_CLIENT_ONLY
-extern int     win32_open(const char *, int,...);
+#if defined(WIN32) && !defined(__CYGWIN__)
 
-#define                open(a,b,...)   win32_open(a,b,##__VA_ARGS__)
+/*
+ * open() and fopen() replacements to allow deletion of open files and
+ * passing of other special options.
+ */
+#define                O_DIRECT        0x80000000
+extern int     pgwin32_open(const char *, int,...);
+extern FILE *pgwin32_fopen(const char *, const char *);
+
+#ifndef FRONTEND
+#define                open(a,b,c) pgwin32_open(a,b,c)
+#define                fopen(a,b) pgwin32_fopen(a,b)
 #endif
 
-#ifndef __BORLANDC__
+#ifndef popen
 #define popen(a,b) _popen(a,b)
+#endif
+#ifndef pclose
 #define pclose(a) _pclose(a)
 #endif
 
-extern int     copydir(char *fromdir, char *todir);
-
-/* Missing rand functions */
-extern long lrand48(void);
-extern void srand48(long seed);
-
+/* New versions of MingW have gettimeofday, old mingw and msvc don't */
+#ifndef HAVE_GETTIMEOFDAY
 /* Last parameter not used */
 extern int     gettimeofday(struct timeval * tp, struct timezone * tzp);
-
-#else
+#endif
+#else                                                  /* !WIN32 */
 
 /*
  *     Win32 requires a special close for sockets and pipes, while on Unix
  *     close() does them all.
  */
 #define closesocket close
-#endif
+#endif   /* WIN32 */
 
 /*
  * Default "extern" declarations or macro substitutes for library routines.
@@ -229,9 +373,25 @@ extern int gettimeofday(struct timeval * tp, struct timezone * tzp);
 extern char *crypt(const char *key, const char *setting);
 #endif
 
+/* WIN32 handled in port/win32.h */
+#ifndef WIN32
+#define pgoff_t off_t
+#if defined(__bsdi__) || defined(__NetBSD__)
+extern int     fseeko(FILE *stream, off_t offset, int whence);
+extern off_t ftello(FILE *stream);
+#endif
+#endif
+
+#ifndef HAVE_ERAND48
+/* we assume all of these are present or missing together */
+extern double erand48(unsigned short xseed[3]);
+extern long lrand48(void);
+extern void srand48(long seed);
+#endif
+
 #ifndef HAVE_FSEEKO
-#define fseeko(a, b, c) fseek((a), (b), (c))
-#define ftello(a) ftell((a))
+#define fseeko(a, b, c) fseek(a, b, c)
+#define ftello(a)              ftell(a)
 #endif
 
 #ifndef HAVE_GETOPT
@@ -242,27 +402,29 @@ extern int        getopt(int nargc, char *const * nargv, const char *ostr);
 extern int     isinf(double x);
 #endif
 
-#if !defined(HAVE_GETHOSTNAME) && defined(KRB4)
-extern int     gethostname(char *name, int namelen);
-#endif
-
 #ifndef HAVE_RINT
 extern double rint(double x);
 #endif
 
 #ifndef HAVE_INET_ATON
-#ifndef WIN32_CLIENT_ONLY
 #include <netinet/in.h>
 #include <arpa/inet.h>
-#endif
 extern int     inet_aton(const char *cp, struct in_addr * addr);
 #endif
 
 #ifndef HAVE_STRDUP
-extern char *strdup(char const *);
+extern char *strdup(const char *str);
 #endif
 
-#ifndef HAVE_RANDOM
+#if !HAVE_DECL_STRLCAT
+extern size_t strlcat(char *dst, const char *src, size_t siz);
+#endif
+
+#if !HAVE_DECL_STRLCPY
+extern size_t strlcpy(char *dst, const char *src, size_t siz);
+#endif
+
+#if !defined(HAVE_RANDOM) && !defined(__BORLANDC__)
 extern long random(void);
 #endif
 
@@ -277,7 +439,7 @@ extern void srandom(unsigned int seed);
 /* thread.h */
 extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen);
 
-#ifndef WIN32
+#if !defined(WIN32) || defined(__CYGWIN__)
 extern int pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
                   size_t buflen, struct passwd ** result);
 #endif
@@ -287,3 +449,28 @@ extern int pqGethostbyname(const char *name,
                                char *buffer, size_t buflen,
                                struct hostent ** result,
                                int *herrno);
+
+extern void pg_qsort(void *base, size_t nel, size_t elsize,
+                int (*cmp) (const void *, const void *));
+
+#define qsort(a,b,c,d) pg_qsort(a,b,c,d)
+
+typedef int (*qsort_arg_comparator) (const void *a, const void *b, void *arg);
+
+extern void qsort_arg(void *base, size_t nel, size_t elsize,
+                 qsort_arg_comparator cmp, void *arg);
+
+/* port/chklocale.c */
+extern int     pg_get_encoding_from_locale(const char *ctype, bool write_message);
+
+/* port/inet_net_ntop.c */
+extern char *inet_net_ntop(int af, const void *src, int bits,
+                         char *dst, size_t size);
+
+/* port/pgcheckdir.c */
+extern int     pg_check_dir(const char *dir);
+
+/* port/pgmkdirp.c */
+extern int     pg_mkdir_p(char *path, int omode);
+
+#endif   /* PG_PORT_H */