1 /*-------------------------------------------------------------------------
4 * Header for /port compatibility functions.
6 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * $PostgreSQL: pgsql/src/include/port.h,v 1.51 2004/08/09 02:12:51 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 const char *get_progname(const char *argv0);
43 extern void get_share_path(const char *my_exec_path, char *ret_path);
44 extern void get_etc_path(const char *my_exec_path, char *ret_path);
45 extern void get_include_path(const char *my_exec_path, char *ret_path);
46 extern void get_pkginclude_path(const char *my_exec_path, char *ret_path);
47 extern void get_includeserver_path(const char *my_exec_path, char *ret_path);
48 extern void get_lib_path(const char *my_exec_path, char *ret_path);
49 extern void get_pkglib_path(const char *my_exec_path, char *ret_path);
50 extern void get_locale_path(const char *my_exec_path, char *ret_path);
51 extern void set_pglocale_pgservice(const char *argv0, const char *app);
56 * This capability is needed by libpq and initdb.c
57 * On Win32, you can't reference the same object file that is
58 * in two different libraries (pgport and libpq), so a macro is best.
61 #define is_absolute_path(filename) \
63 ((filename)[0] == '/') \
66 #define is_absolute_path(filename) \
68 ((filename)[0] == '/') || \
69 (filename)[0] == '\\' || \
70 (isalpha((filename)[0]) && (filename)[1] == ':' && \
71 ((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);
83 #if defined(WIN32) || defined(__CYGWIN__)
88 #define DEVNULL "/dev/null"
92 * Win32 needs double quotes at the beginning and end of system()
93 * strings. If not, it gets confused with multiple quoted strings.
94 * It also requires double-quotes around the executable name and
95 * any files used for redirection. Other args can use single-quotes.
97 * See the "Notes" section about quotes at:
98 * http://home.earthlink.net/~rlively/MANUALS/COMMANDS/C/CMD.HTM
101 #define SYSTEMQUOTE "\""
103 #define SYSTEMQUOTE ""
106 /* Portable delay handling */
107 extern void pg_usleep(long microsec);
109 /* Portable SQL-like case-independent comparisons and conversions */
110 extern int pg_strcasecmp(const char *s1, const char *s2);
111 extern int pg_strncasecmp(const char *s1, const char *s2, size_t n);
112 extern unsigned char pg_toupper(unsigned char ch);
113 extern unsigned char pg_tolower(unsigned char ch);
115 /* Portable prompt handling */
116 extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
118 #if defined(bsdi) || defined(netbsd)
119 extern int fseeko(FILE *stream, off_t offset, int whence);
120 extern off_t ftello(FILE *stream);
124 * WIN32 doesn't allow descriptors returned by pipe() to be used in select(),
125 * so for that platform we use socket() instead of pipe().
128 #define pgpipe(a) pipe(a)
129 #define piperead(a,b,c) read(a,b,c)
130 #define pipewrite(a,b,c) write(a,b,c)
132 extern int pgpipe(int handles[2]);
133 extern int piperead(int s, char* buf, int len);
134 #define pipewrite(a,b,c) send(a,b,c,0)
136 #define PG_SIGNAL_COUNT 32
137 #define kill(pid,sig) pgkill(pid,sig)
138 extern int pgkill(int pid, int sig);
141 extern int pclose_check(FILE *stream);
143 #if defined(WIN32) || defined(__CYGWIN__)
145 * Win32 doesn't have reliable rename/unlink during concurrent access,
146 * and we need special code to do symlinks.
148 extern int pgrename(const char *from, const char *to);
149 extern int pgunlink(const char *path);
150 extern int pgsymlink(const char *oldpath, const char *newpath);
151 #define rename(from, to) pgrename(from, to)
152 #define unlink(path) pgunlink(path)
153 #define symlink(oldpath, newpath) pgsymlink(oldpath, newpath)
157 extern bool rmtree(char *path, bool rmtopdir);
161 /* open() replacement to allow delete of held files */
162 #if !defined(_MSC_VER) && !defined(__BORLANDC__)
163 extern int win32_open(const char*,int,...);
164 #define open(a,b,...) win32_open(a,b,##__VA_ARGS__)
168 #define popen(a,b) _popen(a,b)
169 #define pclose(a) _pclose(a)
172 extern int copydir(char *fromdir, char *todir);
174 /* Missing rand functions */
175 extern long lrand48(void);
176 extern void srand48(long seed);
178 /* Last parameter not used */
179 extern int gettimeofday(struct timeval * tp, struct timezone * tzp);
184 * Win32 requires a special close for sockets and pipes, while on Unix
185 * close() does them all.
187 #define closesocket close
191 * Default "extern" declarations or macro substitutes for library routines.
192 * When necessary, these routines are provided by files in src/port/.
195 extern char *crypt(const char *key, const char *setting);
199 #define fseeko(a, b, c) fseek((a), (b), (c))
200 #define ftello(a) ftell((a))
204 extern int getopt(int nargc, char *const * nargv, const char *ostr);
208 extern int isinf(double x);
211 #if !defined(HAVE_GETHOSTNAME) && defined(KRB4)
212 extern int gethostname(char *name, int namelen);
216 extern double rint(double x);
219 #ifndef HAVE_INET_ATON
220 #if !defined(_MSC_VER) && !defined(__BORLANDC__)
221 #include <netinet/in.h>
222 #include <arpa/inet.h>
224 extern int inet_aton(const char *cp, struct in_addr * addr);
228 extern char *strdup(char const *);
232 extern long random(void);
235 #ifndef HAVE_UNSETENV
236 extern void unsetenv(const char *name);
240 extern void srandom(unsigned int seed);
244 extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen);
247 extern int pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
248 size_t buflen, struct passwd **result);
251 extern int pqGethostbyname(const char *name,
252 struct hostent *resultbuf,
253 char *buffer, size_t buflen,
254 struct hostent **result,