1 /*-------------------------------------------------------------------------
4 * Header for /port compatibility functions.
6 * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * $PostgreSQL: pgsql/src/include/port.h,v 1.62 2004/09/27 20:37:20 momjian Exp $
11 *-------------------------------------------------------------------------
14 #if !defined(_MSC_VER) && !defined(__BORLANDC__)
22 bool set_noblock(int sock);
24 /* Portable path handling for Unix/Win32 */
26 /* Find the location of the first directory separator, return
29 extern char *first_dir_separator(const char *filename);
31 /* Find the location of the last directory separator, return
34 extern char *last_dir_separator(const char *filename);
36 /* Find the location of the first path separator (i.e. ':' on
37 * Unix, ';' on Windows), return NULL if not found.
39 extern char *first_path_separator(const char *filename);
41 extern void canonicalize_path(char *path);
42 extern void make_native_path(char *path);
43 extern const char *get_progname(const char *argv0);
44 extern void get_share_path(const char *my_exec_path, char *ret_path);
45 extern void get_etc_path(const char *my_exec_path, char *ret_path);
46 extern void get_include_path(const char *my_exec_path, char *ret_path);
47 extern void get_pkginclude_path(const char *my_exec_path, char *ret_path);
48 extern void get_includeserver_path(const char *my_exec_path, char *ret_path);
49 extern void get_lib_path(const char *my_exec_path, char *ret_path);
50 extern void get_pkglib_path(const char *my_exec_path, char *ret_path);
51 extern void get_locale_path(const char *my_exec_path, char *ret_path);
52 extern void set_pglocale_pgservice(const char *argv0, const char *app);
53 extern bool get_home_path(char *ret_path);
54 extern void get_parent_directory(char *path);
59 * This capability is needed by libpq and initdb.c
60 * On Win32, you can't reference the same object file that is
61 * in two different libraries (pgport and libpq), so a macro is best.
64 #define is_absolute_path(filename) \
66 ((filename)[0] == '/') \
69 #define is_absolute_path(filename) \
71 ((filename)[0] == '/') || \
72 (filename)[0] == '\\' || \
73 (isalpha((filename)[0]) && (filename)[1] == ':' && \
74 ((filename)[2] == '\\' || (filename)[2] == '/')) \
79 /* Portable way to find binaries */
80 extern int find_my_exec(const char *argv0, char *retpath);
81 extern int find_other_exec(const char *argv0, const char *target,
82 const char *versionstr, char *retpath);
84 #if defined(WIN32) || defined(__CYGWIN__)
89 #define DEVNULL "/dev/null"
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 * See the "Notes" section about quotes at:
99 * http://home.earthlink.net/~rlively/MANUALS/COMMANDS/C/CMD.HTM
102 #define SYSTEMQUOTE "\""
104 #define SYSTEMQUOTE ""
108 #define HOMEDIR "USERPROFILE"
110 #define HOMEDIR "HOME"
113 /* Portable delay handling */
114 extern void pg_usleep(long microsec);
116 /* Portable SQL-like case-independent comparisons and conversions */
117 extern int pg_strcasecmp(const char *s1, const char *s2);
118 extern int pg_strncasecmp(const char *s1, const char *s2, size_t n);
119 extern unsigned char pg_toupper(unsigned char ch);
120 extern unsigned char pg_tolower(unsigned char ch);
122 /* Portable prompt handling */
123 extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
125 #if defined(bsdi) || defined(netbsd)
126 extern int fseeko(FILE *stream, off_t offset, int whence);
127 extern off_t ftello(FILE *stream);
131 * WIN32 doesn't allow descriptors returned by pipe() to be used in select(),
132 * so for that platform we use socket() instead of pipe().
133 * There is some inconsistency here because sometimes we require pg*, like
134 * pgpipe, but in other cases we define rename to pgrename just on Win32.
137 #define pgpipe(a) pipe(a)
138 #define piperead(a,b,c) read(a,b,c)
139 #define pipewrite(a,b,c) write(a,b,c)
141 extern int pgpipe(int handles[2]);
142 extern int piperead(int s, char *buf, int len);
144 #define pipewrite(a,b,c) send(a,b,c,0)
146 #define PG_SIGNAL_COUNT 32
147 #define kill(pid,sig) pgkill(pid,sig)
148 extern int pgkill(int pid, int sig);
151 extern int pclose_check(FILE *stream);
153 /* Global variable holding time zone information. */
155 #define TIMEZONE_GLOBAL timezone
156 #define TZNAME_GLOBAL tzname
158 #define TIMEZONE_GLOBAL _timezone
159 #define TZNAME_GLOBAL _tzname
162 #if defined(WIN32) || defined(__CYGWIN__)
164 * Win32 doesn't have reliable rename/unlink during concurrent access,
165 * and we need special code to do symlinks.
167 extern int pgrename(const char *from, const char *to);
168 extern int pgunlink(const char *path);
169 extern int pgsymlink(const char *oldpath, const char *newpath);
171 /* Include this first so later includes don't see these defines */
176 #define rename(from, to) pgrename(from, to)
177 #define unlink(path) pgunlink(path)
178 #define symlink(oldpath, newpath) pgsymlink(oldpath, newpath)
181 extern bool rmtree(char *path, bool rmtopdir);
185 /* open() replacement to allow delete of held files */
186 #if !defined(_MSC_VER) && !defined(__BORLANDC__)
187 extern int win32_open(const char *, int,...);
189 #define open(a,b,...) win32_open(a,b,##__VA_ARGS__)
193 #define popen(a,b) _popen(a,b)
194 #define pclose(a) _pclose(a)
197 extern int copydir(char *fromdir, char *todir);
199 /* Missing rand functions */
200 extern long lrand48(void);
201 extern void srand48(long seed);
203 /* Last parameter not used */
204 extern int gettimeofday(struct timeval * tp, struct timezone * tzp);
209 * Win32 requires a special close for sockets and pipes, while on Unix
210 * close() does them all.
212 #define closesocket close
216 * Default "extern" declarations or macro substitutes for library routines.
217 * When necessary, these routines are provided by files in src/port/.
220 extern char *crypt(const char *key, const char *setting);
224 #define fseeko(a, b, c) fseek((a), (b), (c))
225 #define ftello(a) ftell((a))
229 extern int getopt(int nargc, char *const * nargv, const char *ostr);
233 extern int isinf(double x);
236 #if !defined(HAVE_GETHOSTNAME) && defined(KRB4)
237 extern int gethostname(char *name, int namelen);
241 extern double rint(double x);
244 #ifndef HAVE_INET_ATON
245 #if !defined(_MSC_VER) && !defined(__BORLANDC__)
246 #include <netinet/in.h>
247 #include <arpa/inet.h>
249 extern int inet_aton(const char *cp, struct in_addr * addr);
253 extern char *strdup(char const *);
257 extern long random(void);
260 #ifndef HAVE_UNSETENV
261 extern void unsetenv(const char *name);
265 extern void srandom(unsigned int seed);
269 extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen);
272 extern int pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
273 size_t buflen, struct passwd ** result);
276 extern int pqGethostbyname(const char *name,
277 struct hostent * resultbuf,
278 char *buffer, size_t buflen,
279 struct hostent ** result,