port.h 7.57 KB
Newer Older
1 2 3 4 5
/*-------------------------------------------------------------------------
 *
 * port.h
 *	  Header for /port compatibility functions.
 *
Bruce Momjian's avatar
Bruce Momjian committed
6
 * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
7 8
 * 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 $
10 11 12 13
 *
 *-------------------------------------------------------------------------
 */

Bruce Momjian's avatar
Bruce Momjian committed
14
#if !defined(_MSC_VER) && !defined(__BORLANDC__)
15 16 17
/* for thread.c */
#include <pwd.h>
#include <netdb.h>
Bruce Momjian's avatar
Bruce Momjian committed
18
#endif
19
#include <ctype.h>
20

21
/* non-blocking */
Bruce Momjian's avatar
Bruce Momjian committed
22
bool		set_noblock(int sock);
23

24
/* Portable path handling for Unix/Win32 */
25 26 27 28 29 30 31 32 33 34 35 36 37 38

/* 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.
 */
39
extern char *first_path_separator(const char *filename);
40

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 45 46 47
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);
extern void get_include_path(const char *my_exec_path, char *ret_path);
extern void get_pkginclude_path(const char *my_exec_path, char *ret_path);
48 49
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);
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);
55

56 57 58
/*
 *	is_absolute_path
 *
Bruce Momjian's avatar
Bruce Momjian committed
59
 *	This capability is needed by libpq and initdb.c
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
 *	On Win32, you can't reference the same object file that is
 *	in two different libraries (pgport and libpq), so a macro is best.
 */
#ifndef WIN32
#define is_absolute_path(filename) \
( \
	((filename)[0] == '/') \
)
#else
#define is_absolute_path(filename) \
( \
	((filename)[0] == '/') || \
	(filename)[0] == '\\' || \
	(isalpha((filename)[0]) && (filename)[1] == ':' && \
	((filename)[2] == '\\' || (filename)[2] == '/')) \
)
#endif


79
/* Portable way to find binaries */
Bruce Momjian's avatar
Bruce Momjian committed
80
extern int	find_my_exec(const char *argv0, char *retpath);
81
extern int find_other_exec(const char *argv0, const char *target,
Bruce Momjian's avatar
Bruce Momjian committed
82 83
				const char *versionstr, char *retpath);

84
#if defined(WIN32) || defined(__CYGWIN__)
85 86 87 88 89 90 91
#define EXE ".exe"
#define DEVNULL "nul"
#else
#define EXE ""
#define DEVNULL "/dev/null"
#endif

92 93 94
/*
 *	Win32 needs double quotes at the beginning and end of system()
 *	strings.  If not, it gets confused with multiple quoted strings.
Bruce Momjian's avatar
Bruce Momjian committed
95 96
 *	It also requires double-quotes around the executable name and
 *	any files used for redirection.  Other args can use single-quotes.
97 98 99
 *
 *	See the "Notes" section about quotes at:
 *		http://home.earthlink.net/~rlively/MANUALS/COMMANDS/C/CMD.HTM
100 101 102 103 104 105 106
 */
#ifdef WIN32
#define SYSTEMQUOTE "\""
#else
#define SYSTEMQUOTE ""
#endif

107
#ifdef WIN32
Bruce Momjian's avatar
Bruce Momjian committed
108
#define HOMEDIR "USERPROFILE"
109
#else
Bruce Momjian's avatar
Bruce Momjian committed
110
#define HOMEDIR "HOME"
111 112
#endif

113 114 115
/* Portable delay handling */
extern void pg_usleep(long microsec);

116 117 118 119 120 121
/* Portable SQL-like case-independent comparisons and conversions */
extern int	pg_strcasecmp(const char *s1, const char *s2);
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);

122
/* Portable prompt handling */
123
extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
124 125

#if defined(bsdi) || defined(netbsd)
126 127
extern int	fseeko(FILE *stream, off_t offset, int whence);
extern off_t ftello(FILE *stream);
128 129
#endif

130 131 132
/*
 *	WIN32 doesn't allow descriptors returned by pipe() to be used in select(),
 *	so for that platform we use socket() instead of pipe().
133 134
 *	There is some inconsistency here because sometimes we require pg*, like
 *	pgpipe, but in other cases we define rename to pgrename just on Win32.
135 136 137 138 139 140
 */
#ifndef WIN32
#define pgpipe(a)			pipe(a)
#define piperead(a,b,c)		read(a,b,c)
#define pipewrite(a,b,c)	write(a,b,c)
#else
Bruce Momjian's avatar
Bruce Momjian committed
141 142 143
extern int	pgpipe(int handles[2]);
extern int	piperead(int s, char *buf, int len);

144
#define pipewrite(a,b,c)	send(a,b,c,0)
Bruce Momjian's avatar
Bruce Momjian committed
145 146

#define PG_SIGNAL_COUNT 32
Bruce Momjian's avatar
Bruce Momjian committed
147 148
#define kill(pid,sig)	pgkill(pid,sig)
extern int	pgkill(int pid, int sig);
149
#endif
Bruce Momjian's avatar
Bruce Momjian committed
150

Bruce Momjian's avatar
Bruce Momjian committed
151
extern int	pclose_check(FILE *stream);
152

153 154 155 156 157 158 159 160 161
/* Global variable holding time zone information. */
#ifndef __CYGWIN__
#define TIMEZONE_GLOBAL timezone
#define TZNAME_GLOBAL tzname
#else
#define TIMEZONE_GLOBAL _timezone
#define TZNAME_GLOBAL _tzname
#endif

162
#if defined(WIN32) || defined(__CYGWIN__)
163
/*
164 165
 *	Win32 doesn't have reliable rename/unlink during concurrent access,
 *	and we need special code to do symlinks.
166
 */
167 168
extern int	pgrename(const char *from, const char *to);
extern int	pgunlink(const char *path);
169
extern int	pgsymlink(const char *oldpath, const char *newpath);
Bruce Momjian's avatar
Bruce Momjian committed
170

171 172 173 174 175
/* Include this first so later includes don't see these defines */
#ifdef _MSC_VER
#include <io.h>
#endif

176 177 178
#define rename(from, to)		pgrename(from, to)
#define unlink(path)			pgunlink(path)
#define symlink(oldpath, newpath)	pgsymlink(oldpath, newpath)
179 180
#endif

Bruce Momjian's avatar
Bruce Momjian committed
181 182
extern bool rmtree(char *path, bool rmtopdir);

183
#ifdef WIN32
184 185

/* open() replacement to allow delete of held files */
Bruce Momjian's avatar
Bruce Momjian committed
186
#if !defined(_MSC_VER) && !defined(__BORLANDC__)
Bruce Momjian's avatar
Bruce Momjian committed
187 188 189
extern int	win32_open(const char *, int,...);

#define		open(a,b,...)	win32_open(a,b,##__VA_ARGS__)
190
#endif
191

192 193 194 195 196
#ifndef __BORLANDC__
#define popen(a,b) _popen(a,b)
#define pclose(a) _pclose(a)
#endif

Bruce Momjian's avatar
Bruce Momjian committed
197 198
extern int	copydir(char *fromdir, char *todir);

Bruce Momjian's avatar
Bruce Momjian committed
199
/* Missing rand functions */
Bruce Momjian's avatar
Bruce Momjian committed
200 201
extern long lrand48(void);
extern void srand48(long seed);
Bruce Momjian's avatar
Bruce Momjian committed
202

203
/* Last parameter not used */
Bruce Momjian's avatar
Bruce Momjian committed
204
extern int	gettimeofday(struct timeval * tp, struct timezone * tzp);
205 206 207

#else

208 209 210 211
/*
 *	Win32 requires a special close for sockets and pipes, while on Unix
 *	close() does them all.
 */
Bruce Momjian's avatar
Bruce Momjian committed
212
#define closesocket close
213
#endif
Bruce Momjian's avatar
Bruce Momjian committed
214

215 216 217 218 219
/*
 * Default "extern" declarations or macro substitutes for library routines.
 * When necessary, these routines are provided by files in src/port/.
 */
#ifndef HAVE_CRYPT
220
extern char *crypt(const char *key, const char *setting);
221 222 223 224 225 226 227 228
#endif

#ifndef HAVE_FSEEKO
#define fseeko(a, b, c) fseek((a), (b), (c))
#define ftello(a) ftell((a))
#endif

#ifndef HAVE_GETOPT
Bruce Momjian's avatar
Bruce Momjian committed
229
extern int	getopt(int nargc, char *const * nargv, const char *ostr);
230 231 232
#endif

#ifndef HAVE_ISINF
Bruce Momjian's avatar
Bruce Momjian committed
233
extern int	isinf(double x);
234 235 236
#endif

#if !defined(HAVE_GETHOSTNAME) && defined(KRB4)
Bruce Momjian's avatar
Bruce Momjian committed
237
extern int	gethostname(char *name, int namelen);
238 239 240 241 242 243 244
#endif

#ifndef HAVE_RINT
extern double rint(double x);
#endif

#ifndef HAVE_INET_ATON
245
#if !defined(_MSC_VER) && !defined(__BORLANDC__)
Bruce Momjian's avatar
Bruce Momjian committed
246 247
#include <netinet/in.h>
#include <arpa/inet.h>
248
#endif
Bruce Momjian's avatar
Bruce Momjian committed
249
extern int	inet_aton(const char *cp, struct in_addr * addr);
250 251 252 253 254 255 256 257 258 259
#endif

#ifndef HAVE_STRDUP
extern char *strdup(char const *);
#endif

#ifndef HAVE_RANDOM
extern long random(void);
#endif

260 261 262 263
#ifndef HAVE_UNSETENV
extern void unsetenv(const char *name);
#endif

264 265 266
#ifndef HAVE_SRANDOM
extern void srandom(unsigned int seed);
#endif
267 268 269 270

/* thread.h */
extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen);

271
#ifndef WIN32
Bruce Momjian's avatar
Bruce Momjian committed
272
extern int pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
Bruce Momjian's avatar
Bruce Momjian committed
273
		   size_t buflen, struct passwd ** result);
274
#endif
275 276

extern int pqGethostbyname(const char *name,
Bruce Momjian's avatar
Bruce Momjian committed
277
				struct hostent * resultbuf,
278
				char *buffer, size_t buflen,
Bruce Momjian's avatar
Bruce Momjian committed
279
				struct hostent ** result,
Bruce Momjian's avatar
Bruce Momjian committed
280
				int *herrno);