]> granicus.if.org Git - postgresql/blob - src/include/port.h
Add new macro as shorthand for MS VC and Borland C++:
[postgresql] / src / include / port.h
1 /*-------------------------------------------------------------------------
2  *
3  * port.h
4  *        Header for /port compatibility functions.
5  *
6  * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * $PostgreSQL: pgsql/src/include/port.h,v 1.63 2004/09/27 23:24:37 momjian Exp $
10  *
11  *-------------------------------------------------------------------------
12  */
13
14 #ifndef WIN32_CLIENT_ONLY
15 /* for thread.c */
16 #include <pwd.h>
17 #include <netdb.h>
18 #endif
19
20 #include <ctype.h>
21
22 /* non-blocking */
23 bool            set_noblock(int sock);
24
25 /* Portable path handling for Unix/Win32 */
26
27 /* Find the location of the first directory separator, return
28  * NULL if not found.
29  */
30 extern char *first_dir_separator(const char *filename);
31
32 /* Find the location of the last directory separator, return
33  * NULL if not found.
34  */
35 extern char *last_dir_separator(const char *filename);
36
37 /* Find the location of the first path separator (i.e. ':' on
38  * Unix, ';' on Windows), return NULL if not found.
39  */
40 extern char *first_path_separator(const char *filename);
41
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);
56
57 /*
58  *      is_absolute_path
59  *
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.
63  */
64 #ifndef WIN32
65 #define is_absolute_path(filename) \
66 ( \
67         ((filename)[0] == '/') \
68 )
69 #else
70 #define is_absolute_path(filename) \
71 ( \
72         ((filename)[0] == '/') || \
73         (filename)[0] == '\\' || \
74         (isalpha((filename)[0]) && (filename)[1] == ':' && \
75         ((filename)[2] == '\\' || (filename)[2] == '/')) \
76 )
77 #endif
78
79
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);
84
85 #if defined(WIN32) || defined(__CYGWIN__)
86 #define EXE ".exe"
87 #define DEVNULL "nul"
88 #else
89 #define EXE ""
90 #define DEVNULL "/dev/null"
91 #endif
92
93 /*
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.
98  *
99  *      See the "Notes" section about quotes at:
100  *              http://home.earthlink.net/~rlively/MANUALS/COMMANDS/C/CMD.HTM
101  */
102 #ifdef WIN32
103 #define SYSTEMQUOTE "\""
104 #else
105 #define SYSTEMQUOTE ""
106 #endif
107
108 #ifdef WIN32
109 #define HOMEDIR "USERPROFILE"
110 #else
111 #define HOMEDIR "HOME"
112 #endif
113
114 /* Portable delay handling */
115 extern void pg_usleep(long microsec);
116
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);
122
123 /* Portable prompt handling */
124 extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
125
126 #if defined(bsdi) || defined(netbsd)
127 extern int      fseeko(FILE *stream, off_t offset, int whence);
128 extern off_t ftello(FILE *stream);
129 #endif
130
131 /*
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.
136  */
137 #ifndef 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)
141 #else
142 extern int      pgpipe(int handles[2]);
143 extern int      piperead(int s, char *buf, int len);
144
145 #define pipewrite(a,b,c)        send(a,b,c,0)
146
147 #define PG_SIGNAL_COUNT 32
148 #define kill(pid,sig)   pgkill(pid,sig)
149 extern int      pgkill(int pid, int sig);
150 #endif
151
152 extern int      pclose_check(FILE *stream);
153
154 /* Global variable holding time zone information. */
155 #ifndef __CYGWIN__
156 #define TIMEZONE_GLOBAL timezone
157 #define TZNAME_GLOBAL tzname
158 #else
159 #define TIMEZONE_GLOBAL _timezone
160 #define TZNAME_GLOBAL _tzname
161 #endif
162
163 #if defined(WIN32) || defined(__CYGWIN__)
164 /*
165  *      Win32 doesn't have reliable rename/unlink during concurrent access,
166  *      and we need special code to do symlinks.
167  */
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);
171
172 /* Include this first so later includes don't see these defines */
173 #ifdef WIN32_CLIENT_ONLY
174 #include <io.h>
175 #endif
176
177 #define rename(from, to)                pgrename(from, to)
178 #define unlink(path)                    pgunlink(path)
179 #define symlink(oldpath, newpath)       pgsymlink(oldpath, newpath)
180 #endif
181
182 extern bool rmtree(char *path, bool rmtopdir);
183
184 #ifdef WIN32
185
186 /* open() replacement to allow delete of held files */
187 #ifndef WIN32_CLIENT_ONLY
188 extern int      win32_open(const char *, int,...);
189
190 #define         open(a,b,...)   win32_open(a,b,##__VA_ARGS__)
191 #endif
192
193 #ifndef __BORLANDC__
194 #define popen(a,b) _popen(a,b)
195 #define pclose(a) _pclose(a)
196 #endif
197
198 extern int      copydir(char *fromdir, char *todir);
199
200 /* Missing rand functions */
201 extern long lrand48(void);
202 extern void srand48(long seed);
203
204 /* Last parameter not used */
205 extern int      gettimeofday(struct timeval * tp, struct timezone * tzp);
206
207 #else
208
209 /*
210  *      Win32 requires a special close for sockets and pipes, while on Unix
211  *      close() does them all.
212  */
213 #define closesocket close
214 #endif
215
216 /*
217  * Default "extern" declarations or macro substitutes for library routines.
218  * When necessary, these routines are provided by files in src/port/.
219  */
220 #ifndef HAVE_CRYPT
221 extern char *crypt(const char *key, const char *setting);
222 #endif
223
224 #ifndef HAVE_FSEEKO
225 #define fseeko(a, b, c) fseek((a), (b), (c))
226 #define ftello(a) ftell((a))
227 #endif
228
229 #ifndef HAVE_GETOPT
230 extern int      getopt(int nargc, char *const * nargv, const char *ostr);
231 #endif
232
233 #ifndef HAVE_ISINF
234 extern int      isinf(double x);
235 #endif
236
237 #if !defined(HAVE_GETHOSTNAME) && defined(KRB4)
238 extern int      gethostname(char *name, int namelen);
239 #endif
240
241 #ifndef HAVE_RINT
242 extern double rint(double x);
243 #endif
244
245 #ifndef HAVE_INET_ATON
246 #ifndef WIN32_CLIENT_ONLY
247 #include <netinet/in.h>
248 #include <arpa/inet.h>
249 #endif
250 extern int      inet_aton(const char *cp, struct in_addr * addr);
251 #endif
252
253 #ifndef HAVE_STRDUP
254 extern char *strdup(char const *);
255 #endif
256
257 #ifndef HAVE_RANDOM
258 extern long random(void);
259 #endif
260
261 #ifndef HAVE_UNSETENV
262 extern void unsetenv(const char *name);
263 #endif
264
265 #ifndef HAVE_SRANDOM
266 extern void srandom(unsigned int seed);
267 #endif
268
269 /* thread.h */
270 extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen);
271
272 #ifndef WIN32
273 extern int pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
274                    size_t buflen, struct passwd ** result);
275 #endif
276
277 extern int pqGethostbyname(const char *name,
278                                 struct hostent * resultbuf,
279                                 char *buffer, size_t buflen,
280                                 struct hostent ** result,
281                                 int *herrno);