1 /*-------------------------------------------------------------------------
4 * Header for src/port/ compatibility functions.
6 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * $PostgreSQL: pgsql/src/include/port.h,v 1.74 2005/03/25 00:34:24 tgl Exp $
11 *-------------------------------------------------------------------------
14 #ifndef WIN32_CLIENT_ONLY
23 extern bool pg_set_noblock(int sock);
24 extern bool pg_set_block(int sock);
26 /* Portable path handling for Unix/Win32 */
28 extern char *first_dir_separator(const char *filename);
29 extern char *last_dir_separator(const char *filename);
30 extern char *first_path_separator(const char *pathlist);
31 extern void join_path_components(char *ret_path,
32 const char *head, const char *tail);
33 extern void canonicalize_path(char *path);
34 extern void make_native_path(char *path);
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 set_pglocale_pgservice(const char *argv0, const char *app);
45 extern bool get_home_path(char *ret_path);
46 extern void get_parent_directory(char *path);
51 * By making this a macro we prevent the need for libpq to include
52 * path.c which uses exec.c.
55 #define is_absolute_path(filename) \
57 ((filename)[0] == '/') \
60 #define is_absolute_path(filename) \
62 ((filename)[0] == '/') || \
63 (filename)[0] == '\\' || \
64 (isalpha((filename)[0]) && (filename)[1] == ':' && \
65 ((filename)[2] == '\\' || (filename)[2] == '/')) \
70 /* Portable way to find binaries */
71 extern int find_my_exec(const char *argv0, char *retpath);
72 extern int find_other_exec(const char *argv0, const char *target,
73 const char *versionstr, char *retpath);
75 #if defined(WIN32) || defined(__CYGWIN__)
81 #if defined(WIN32) && !defined(__CYGWIN__)
84 #define DEVNULL "/dev/null"
88 * Win32 needs double quotes at the beginning and end of system()
89 * strings. If not, it gets confused with multiple quoted strings.
90 * It also requires double-quotes around the executable name and
91 * any files used for redirection. Other args can use single-quotes.
93 * See the "Notes" section about quotes at:
94 * http://home.earthlink.net/~rlively/MANUALS/COMMANDS/C/CMD.HTM
96 #if defined(WIN32) && !defined(__CYGWIN__)
97 #define SYSTEMQUOTE "\""
99 #define SYSTEMQUOTE ""
102 /* Portable delay handling */
103 extern void pg_usleep(long microsec);
105 /* Portable SQL-like case-independent comparisons and conversions */
106 extern int pg_strcasecmp(const char *s1, const char *s2);
107 extern int pg_strncasecmp(const char *s1, const char *s2, size_t n);
108 extern unsigned char pg_toupper(unsigned char ch);
109 extern unsigned char pg_tolower(unsigned char ch);
112 extern int pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args);
113 extern int pg_snprintf(char *str, size_t count, const char *fmt,...)
114 /* This extension allows gcc to check the format string */
115 __attribute__((format(printf, 3, 4)));
116 extern int pg_sprintf(char *str, const char *fmt,...)
117 /* This extension allows gcc to check the format string */
118 __attribute__((format(printf, 2, 3)));
119 extern int pg_fprintf(FILE *stream, const char *fmt,...)
120 /* This extension allows gcc to check the format string */
121 __attribute__((format(printf, 2, 3)));
122 extern int pg_printf(const char *fmt,...)
123 /* This extension allows gcc to check the format string */
124 __attribute__((format(printf, 1, 2)));
127 * The GCC-specific code below prevents the __attribute__(... 'printf')
128 * above from being replaced, and this is required because gcc doesn't
129 * know anything about pg_printf.
132 #define vsnprintf(...) pg_vsnprintf(__VA_ARGS__)
133 #define snprintf(...) pg_snprintf(__VA_ARGS__)
134 #define sprintf(...) pg_sprintf(__VA_ARGS__)
135 #define fprintf(...) pg_fprintf(__VA_ARGS__)
136 #define printf(...) pg_printf(__VA_ARGS__)
138 #define vsnprintf pg_vsnprintf
139 #define snprintf pg_snprintf
140 #define sprintf pg_sprintf
141 #define fprintf pg_fprintf
142 #define printf pg_printf
146 /* Portable prompt handling */
147 extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
150 * WIN32 doesn't allow descriptors returned by pipe() to be used in select(),
151 * so for that platform we use socket() instead of pipe().
152 * There is some inconsistency here because sometimes we require pg*, like
153 * pgpipe, but in other cases we define rename to pgrename just on Win32.
156 #define pgpipe(a) pipe(a)
157 #define piperead(a,b,c) read(a,b,c)
158 #define pipewrite(a,b,c) write(a,b,c)
160 extern int pgpipe(int handles[2]);
161 extern int piperead(int s, char *buf, int len);
163 #define pipewrite(a,b,c) send(a,b,c,0)
165 #define PG_SIGNAL_COUNT 32
166 #define kill(pid,sig) pgkill(pid,sig)
167 extern int pgkill(int pid, int sig);
170 extern int pclose_check(FILE *stream);
172 /* Global variable holding time zone information. */
174 #define TIMEZONE_GLOBAL timezone
175 #define TZNAME_GLOBAL tzname
177 #define TIMEZONE_GLOBAL _timezone
178 #define TZNAME_GLOBAL _tzname
181 #if defined(WIN32) || defined(__CYGWIN__)
183 * Win32 doesn't have reliable rename/unlink during concurrent access,
184 * and we need special code to do symlinks.
186 extern int pgrename(const char *from, const char *to);
187 extern int pgunlink(const char *path);
188 /* Include this first so later includes don't see these defines */
189 #ifdef WIN32_CLIENT_ONLY
193 #define rename(from, to) pgrename(from, to)
194 #define unlink(path) pgunlink(path)
197 * Cygwin has its own symlinks which work on Win95/98/ME where
198 * junction points don't, so use it instead. We have no way of
199 * knowing what type of system Cygwin binaries will be run on.
200 * Note: Some CYGWIN includes might #define WIN32.
202 #if defined(WIN32) && !defined(__CYGWIN__)
203 extern int pgsymlink(const char *oldpath, const char *newpath);
204 #define symlink(oldpath, newpath) pgsymlink(oldpath, newpath)
207 #endif /* defined(WIN32) || defined(__CYGWIN__) */
209 extern bool rmtree(char *path, bool rmtopdir);
211 #if defined(WIN32) && !defined(__CYGWIN__)
213 /* open() replacement to allow delete of held files and passing
214 * of special options. */
215 #ifndef WIN32_CLIENT_ONLY
216 extern int win32_open(const char *, int,...);
218 #define open(a,b,...) win32_open(a,b,##__VA_ARGS__)
222 #define popen(a,b) _popen(a,b)
223 #define pclose(a) _pclose(a)
226 extern int copydir(char *fromdir, char *todir);
228 /* Missing rand functions */
229 extern long lrand48(void);
230 extern void srand48(long seed);
232 /* Last parameter not used */
233 extern int gettimeofday(struct timeval * tp, struct timezone * tzp);
238 * Win32 requires a special close for sockets and pipes, while on Unix
239 * close() does them all.
241 #define closesocket close
245 * Default "extern" declarations or macro substitutes for library routines.
246 * When necessary, these routines are provided by files in src/port/.
249 extern char *crypt(const char *key, const char *setting);
252 #if defined(bsdi) || defined(netbsd)
253 extern int fseeko(FILE *stream, off_t offset, int whence);
254 extern off_t ftello(FILE *stream);
258 #define fseeko(a, b, c) fseek((a), (b), (c))
259 #define ftello(a) ftell((a))
263 extern int getopt(int nargc, char *const * nargv, const char *ostr);
267 extern int isinf(double x);
270 #if !defined(HAVE_GETHOSTNAME) && defined(KRB4)
271 extern int gethostname(char *name, int namelen);
275 extern double rint(double x);
278 #ifndef HAVE_INET_ATON
279 #ifndef WIN32_CLIENT_ONLY
280 #include <netinet/in.h>
281 #include <arpa/inet.h>
283 extern int inet_aton(const char *cp, struct in_addr * addr);
287 extern char *strdup(char const *);
291 extern long random(void);
294 #ifndef HAVE_UNSETENV
295 extern void unsetenv(const char *name);
299 extern void srandom(unsigned int seed);
303 extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen);
305 #if !defined(WIN32) || defined(__CYGWIN__)
306 extern int pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
307 size_t buflen, struct passwd ** result);
310 extern int pqGethostbyname(const char *name,
311 struct hostent * resultbuf,
312 char *buffer, size_t buflen,
313 struct hostent ** result,