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.57 2004/08/29 21:08:48 tgl 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 #if defined(WIN32) || defined(__CYGWIN__)
155 * Win32 doesn't have reliable rename/unlink during concurrent access,
156 * and we need special code to do symlinks.
158 extern int pgrename(const char *from, const char *to);
159 extern int pgunlink(const char *path);
160 extern int pgsymlink(const char *oldpath, const char *newpath);
162 #define rename(from, to) pgrename(from, to)
163 #define unlink(path) pgunlink(path)
164 #define symlink(oldpath, newpath) pgsymlink(oldpath, newpath)
167 extern bool rmtree(char *path, bool rmtopdir);
171 /* open() replacement to allow delete of held files */
172 #if !defined(_MSC_VER) && !defined(__BORLANDC__)
173 extern int win32_open(const char *, int,...);
175 #define open(a,b,...) win32_open(a,b,##__VA_ARGS__)
179 #define popen(a,b) _popen(a,b)
180 #define pclose(a) _pclose(a)
183 extern int copydir(char *fromdir, char *todir);
185 /* Missing rand functions */
186 extern long lrand48(void);
187 extern void srand48(long seed);
189 /* Last parameter not used */
190 extern int gettimeofday(struct timeval * tp, struct timezone * tzp);
195 * Win32 requires a special close for sockets and pipes, while on Unix
196 * close() does them all.
198 #define closesocket close
202 * Default "extern" declarations or macro substitutes for library routines.
203 * When necessary, these routines are provided by files in src/port/.
206 extern char *crypt(const char *key, const char *setting);
210 #define fseeko(a, b, c) fseek((a), (b), (c))
211 #define ftello(a) ftell((a))
215 extern int getopt(int nargc, char *const * nargv, const char *ostr);
219 extern int isinf(double x);
222 #if !defined(HAVE_GETHOSTNAME) && defined(KRB4)
223 extern int gethostname(char *name, int namelen);
227 extern double rint(double x);
230 #ifndef HAVE_INET_ATON
231 #if !defined(_MSC_VER) && !defined(__BORLANDC__)
232 #include <netinet/in.h>
233 #include <arpa/inet.h>
235 extern int inet_aton(const char *cp, struct in_addr * addr);
239 extern char *strdup(char const *);
243 extern long random(void);
246 #ifndef HAVE_UNSETENV
247 extern void unsetenv(const char *name);
251 extern void srandom(unsigned int seed);
255 extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen);
258 extern int pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
259 size_t buflen, struct passwd ** result);
262 extern int pqGethostbyname(const char *name,
263 struct hostent * resultbuf,
264 char *buffer, size_t buflen,
265 struct hostent ** result,