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.63 2004/09/27 23:24:37 momjian Exp $
11 *-------------------------------------------------------------------------
14 #ifndef WIN32_CLIENT_ONLY
23 bool set_noblock(int sock);
25 /* Portable path handling for Unix/Win32 */
27 /* Find the location of the first directory separator, return
30 extern char *first_dir_separator(const char *filename);
32 /* Find the location of the last directory separator, return
35 extern char *last_dir_separator(const char *filename);
37 /* Find the location of the first path separator (i.e. ':' on
38 * Unix, ';' on Windows), return NULL if not found.
40 extern char *first_path_separator(const char *filename);
42 extern void canonicalize_path(char *path);
43 extern void make_native_path(char *path);
44 extern const char *get_progname(const char *argv0);
45 extern void get_share_path(const char *my_exec_path, char *ret_path);
46 extern void get_etc_path(const char *my_exec_path, char *ret_path);
47 extern void get_include_path(const char *my_exec_path, char *ret_path);
48 extern void get_pkginclude_path(const char *my_exec_path, char *ret_path);
49 extern void get_includeserver_path(const char *my_exec_path, char *ret_path);
50 extern void get_lib_path(const char *my_exec_path, char *ret_path);
51 extern void get_pkglib_path(const char *my_exec_path, char *ret_path);
52 extern void get_locale_path(const char *my_exec_path, char *ret_path);
53 extern void set_pglocale_pgservice(const char *argv0, const char *app);
54 extern bool get_home_path(char *ret_path);
55 extern void get_parent_directory(char *path);
60 * This capability is needed by libpq and initdb.c
61 * On Win32, you can't reference the same object file that is
62 * in two different libraries (pgport and libpq), so a macro is best.
65 #define is_absolute_path(filename) \
67 ((filename)[0] == '/') \
70 #define is_absolute_path(filename) \
72 ((filename)[0] == '/') || \
73 (filename)[0] == '\\' || \
74 (isalpha((filename)[0]) && (filename)[1] == ':' && \
75 ((filename)[2] == '\\' || (filename)[2] == '/')) \
80 /* Portable way to find binaries */
81 extern int find_my_exec(const char *argv0, char *retpath);
82 extern int find_other_exec(const char *argv0, const char *target,
83 const char *versionstr, char *retpath);
85 #if defined(WIN32) || defined(__CYGWIN__)
90 #define DEVNULL "/dev/null"
94 * Win32 needs double quotes at the beginning and end of system()
95 * strings. If not, it gets confused with multiple quoted strings.
96 * It also requires double-quotes around the executable name and
97 * any files used for redirection. Other args can use single-quotes.
99 * See the "Notes" section about quotes at:
100 * http://home.earthlink.net/~rlively/MANUALS/COMMANDS/C/CMD.HTM
103 #define SYSTEMQUOTE "\""
105 #define SYSTEMQUOTE ""
109 #define HOMEDIR "USERPROFILE"
111 #define HOMEDIR "HOME"
114 /* Portable delay handling */
115 extern void pg_usleep(long microsec);
117 /* Portable SQL-like case-independent comparisons and conversions */
118 extern int pg_strcasecmp(const char *s1, const char *s2);
119 extern int pg_strncasecmp(const char *s1, const char *s2, size_t n);
120 extern unsigned char pg_toupper(unsigned char ch);
121 extern unsigned char pg_tolower(unsigned char ch);
123 /* Portable prompt handling */
124 extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
126 #if defined(bsdi) || defined(netbsd)
127 extern int fseeko(FILE *stream, off_t offset, int whence);
128 extern off_t ftello(FILE *stream);
132 * WIN32 doesn't allow descriptors returned by pipe() to be used in select(),
133 * so for that platform we use socket() instead of pipe().
134 * There is some inconsistency here because sometimes we require pg*, like
135 * pgpipe, but in other cases we define rename to pgrename just on Win32.
138 #define pgpipe(a) pipe(a)
139 #define piperead(a,b,c) read(a,b,c)
140 #define pipewrite(a,b,c) write(a,b,c)
142 extern int pgpipe(int handles[2]);
143 extern int piperead(int s, char *buf, int len);
145 #define pipewrite(a,b,c) send(a,b,c,0)
147 #define PG_SIGNAL_COUNT 32
148 #define kill(pid,sig) pgkill(pid,sig)
149 extern int pgkill(int pid, int sig);
152 extern int pclose_check(FILE *stream);
154 /* Global variable holding time zone information. */
156 #define TIMEZONE_GLOBAL timezone
157 #define TZNAME_GLOBAL tzname
159 #define TIMEZONE_GLOBAL _timezone
160 #define TZNAME_GLOBAL _tzname
163 #if defined(WIN32) || defined(__CYGWIN__)
165 * Win32 doesn't have reliable rename/unlink during concurrent access,
166 * and we need special code to do symlinks.
168 extern int pgrename(const char *from, const char *to);
169 extern int pgunlink(const char *path);
170 extern int pgsymlink(const char *oldpath, const char *newpath);
172 /* Include this first so later includes don't see these defines */
173 #ifdef WIN32_CLIENT_ONLY
177 #define rename(from, to) pgrename(from, to)
178 #define unlink(path) pgunlink(path)
179 #define symlink(oldpath, newpath) pgsymlink(oldpath, newpath)
182 extern bool rmtree(char *path, bool rmtopdir);
186 /* open() replacement to allow delete of held files */
187 #ifndef WIN32_CLIENT_ONLY
188 extern int win32_open(const char *, int,...);
190 #define open(a,b,...) win32_open(a,b,##__VA_ARGS__)
194 #define popen(a,b) _popen(a,b)
195 #define pclose(a) _pclose(a)
198 extern int copydir(char *fromdir, char *todir);
200 /* Missing rand functions */
201 extern long lrand48(void);
202 extern void srand48(long seed);
204 /* Last parameter not used */
205 extern int gettimeofday(struct timeval * tp, struct timezone * tzp);
210 * Win32 requires a special close for sockets and pipes, while on Unix
211 * close() does them all.
213 #define closesocket close
217 * Default "extern" declarations or macro substitutes for library routines.
218 * When necessary, these routines are provided by files in src/port/.
221 extern char *crypt(const char *key, const char *setting);
225 #define fseeko(a, b, c) fseek((a), (b), (c))
226 #define ftello(a) ftell((a))
230 extern int getopt(int nargc, char *const * nargv, const char *ostr);
234 extern int isinf(double x);
237 #if !defined(HAVE_GETHOSTNAME) && defined(KRB4)
238 extern int gethostname(char *name, int namelen);
242 extern double rint(double x);
245 #ifndef HAVE_INET_ATON
246 #ifndef WIN32_CLIENT_ONLY
247 #include <netinet/in.h>
248 #include <arpa/inet.h>
250 extern int inet_aton(const char *cp, struct in_addr * addr);
254 extern char *strdup(char const *);
258 extern long random(void);
261 #ifndef HAVE_UNSETENV
262 extern void unsetenv(const char *name);
266 extern void srandom(unsigned int seed);
270 extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen);
273 extern int pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
274 size_t buflen, struct passwd ** result);
277 extern int pqGethostbyname(const char *name,
278 struct hostent * resultbuf,
279 char *buffer, size_t buflen,
280 struct hostent ** result,