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