]> granicus.if.org Git - postgresql/blob - src/include/port.h
Update copyright for 2006. Update scripts.
[postgresql] / src / include / port.h
1 /*-------------------------------------------------------------------------
2  *
3  * port.h
4  *        Header for src/port/ compatibility functions.
5  *
6  * Portions Copyright (c) 1996-2006, 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.90 2006/03/05 15:58:53 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 extern bool pg_set_noblock(int sock);
24 extern bool pg_set_block(int sock);
25
26 /* Portable path handling for Unix/Win32 */
27
28 extern char *first_dir_separator(const char *filename);
29 extern char *last_dir_separator(const char *filename);
30 extern char *first_path_separator(const char *pathlist);
31 extern void join_path_components(char *ret_path,
32                                          const char *head, const char *tail);
33 extern void canonicalize_path(char *path);
34 extern void make_native_path(char *path);
35 extern bool path_contains_parent_reference(const char *path);
36 extern bool path_is_prefix_of_path(const char *path1, const char *path2);
37 extern const char *get_progname(const char *argv0);
38 extern void get_share_path(const char *my_exec_path, char *ret_path);
39 extern void get_etc_path(const char *my_exec_path, char *ret_path);
40 extern void get_include_path(const char *my_exec_path, char *ret_path);
41 extern void get_pkginclude_path(const char *my_exec_path, char *ret_path);
42 extern void get_includeserver_path(const char *my_exec_path, char *ret_path);
43 extern void get_lib_path(const char *my_exec_path, char *ret_path);
44 extern void get_pkglib_path(const char *my_exec_path, char *ret_path);
45 extern void get_locale_path(const char *my_exec_path, char *ret_path);
46 extern void get_doc_path(const char *my_exec_path, char *ret_path);
47 extern void get_man_path(const char *my_exec_path, char *ret_path);
48 extern void set_pglocale_pgservice(const char *argv0, const char *app);
49 extern bool get_home_path(char *ret_path);
50 extern void get_parent_directory(char *path);
51
52 /*
53  *      is_absolute_path
54  *
55  *      By making this a macro we prevent the need for libpq to include
56  *      path.c which uses exec.c.
57  */
58 #ifndef WIN32
59 #define is_absolute_path(filename) \
60 ( \
61         ((filename)[0] == '/') \
62 )
63 #else
64 #define is_absolute_path(filename) \
65 ( \
66         ((filename)[0] == '/') || \
67         (filename)[0] == '\\' || \
68         (isalpha((filename)[0]) && (filename)[1] == ':' && \
69         ((filename)[2] == '\\' || (filename)[2] == '/')) \
70 )
71 #endif
72
73
74 /* Portable way to find binaries */
75 extern int      find_my_exec(const char *argv0, char *retpath);
76 extern int find_other_exec(const char *argv0, const char *target,
77                                 const char *versionstr, char *retpath);
78
79 #if defined(WIN32) || defined(__CYGWIN__)
80 #define EXE ".exe"
81 #else
82 #define EXE ""
83 #endif
84
85 #if defined(WIN32) && !defined(__CYGWIN__)
86 #define DEVNULL "nul"
87 /* "con" does not work from the Msys 1.0.10 console (part of MinGW). */
88 #define DEVTTY  "con"
89 #else
90 #define DEVNULL "/dev/null"
91 #define DEVTTY "/dev/tty"
92 #endif
93
94 /*
95  *      Win32 needs double quotes at the beginning and end of system()
96  *      strings.  If not, it gets confused with multiple quoted strings.
97  *      It also requires double-quotes around the executable name and
98  *      any files used for redirection.  Other args can use single-quotes.
99  *
100  *      See the "Notes" section about quotes at:
101  *              http://home.earthlink.net/~rlively/MANUALS/COMMANDS/C/CMD.HTM
102  */
103 #if defined(WIN32) && !defined(__CYGWIN__)
104 #define SYSTEMQUOTE "\""
105 #else
106 #define SYSTEMQUOTE ""
107 #endif
108
109 /* Portable delay handling */
110 extern void pg_usleep(long microsec);
111
112 /* Portable SQL-like case-independent comparisons and conversions */
113 extern int      pg_strcasecmp(const char *s1, const char *s2);
114 extern int      pg_strncasecmp(const char *s1, const char *s2, size_t n);
115 extern unsigned char pg_toupper(unsigned char ch);
116 extern unsigned char pg_tolower(unsigned char ch);
117
118 #ifdef USE_REPL_SNPRINTF
119
120 /*
121  * Versions of libintl >= 0.13 try to replace printf() and friends with
122  * macros to their own versions that understand the %$ format.  We do the
123  * same, so disable their macros, if they exist.
124  */
125 #ifdef vsnprintf
126 #undef vsnprintf
127 #endif
128 #ifdef snprintf
129 #undef snprintf
130 #endif
131 #ifdef sprintf
132 #undef sprintf
133 #endif
134 #ifdef fprintf
135 #undef fprintf
136 #endif
137 #ifdef printf
138 #undef printf
139 #endif
140
141 extern int      pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args);
142 extern int
143 pg_snprintf(char *str, size_t count, const char *fmt,...)
144 /* This extension allows gcc to check the format string */
145 __attribute__((format(printf, 3, 4)));
146 extern int
147 pg_sprintf(char *str, const char *fmt,...)
148 /* This extension allows gcc to check the format string */
149 __attribute__((format(printf, 2, 3)));
150 extern int
151 pg_fprintf(FILE *stream, const char *fmt,...)
152 /* This extension allows gcc to check the format string */
153 __attribute__((format(printf, 2, 3)));
154 extern int
155 pg_printf(const char *fmt,...)
156 /* This extension allows gcc to check the format string */
157 __attribute__((format(printf, 1, 2)));
158
159 /*
160  *      The GCC-specific code below prevents the __attribute__(... 'printf')
161  *      above from being replaced, and this is required because gcc doesn't
162  *      know anything about pg_printf.
163  */
164 #ifdef __GNUC__
165 #define vsnprintf(...)  pg_vsnprintf(__VA_ARGS__)
166 #define snprintf(...)   pg_snprintf(__VA_ARGS__)
167 #define sprintf(...)    pg_sprintf(__VA_ARGS__)
168 #define fprintf(...)    pg_fprintf(__VA_ARGS__)
169 #define printf(...)             pg_printf(__VA_ARGS__)
170 #else
171 #define vsnprintf               pg_vsnprintf
172 #define snprintf                pg_snprintf
173 #define sprintf                 pg_sprintf
174 #define fprintf                 pg_fprintf
175 #define printf                  pg_printf
176 #endif
177
178 #endif /* USE_REPL_SNPRINTF */
179
180 /* Portable prompt handling */
181 extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
182
183 /*
184  *      WIN32 doesn't allow descriptors returned by pipe() to be used in select(),
185  *      so for that platform we use socket() instead of pipe().
186  *      There is some inconsistency here because sometimes we require pg*, like
187  *      pgpipe, but in other cases we define rename to pgrename just on Win32.
188  */
189 #ifndef WIN32
190 #define pgpipe(a)                       pipe(a)
191 #define piperead(a,b,c)         read(a,b,c)
192 #define pipewrite(a,b,c)        write(a,b,c)
193 #else
194 extern int      pgpipe(int handles[2]);
195 extern int      piperead(int s, char *buf, int len);
196
197 #define pipewrite(a,b,c)        send(a,b,c,0)
198
199 #define PG_SIGNAL_COUNT 32
200 #define kill(pid,sig)   pgkill(pid,sig)
201 extern int      pgkill(int pid, int sig);
202 #endif
203
204 extern int      pclose_check(FILE *stream);
205
206 /* Global variable holding time zone information. */
207 #ifndef __CYGWIN__
208 #define TIMEZONE_GLOBAL timezone
209 #define TZNAME_GLOBAL tzname
210 #else
211 #define TIMEZONE_GLOBAL _timezone
212 #define TZNAME_GLOBAL _tzname
213 #endif
214
215 #if defined(WIN32) || defined(__CYGWIN__)
216 /*
217  *      Win32 doesn't have reliable rename/unlink during concurrent access,
218  *      and we need special code to do symlinks.
219  */
220 extern int      pgrename(const char *from, const char *to);
221 extern int      pgunlink(const char *path);
222
223 /* Include this first so later includes don't see these defines */
224 #ifdef WIN32_CLIENT_ONLY
225 #include <io.h>
226 #endif
227
228 #define rename(from, to)                pgrename(from, to)
229 #define unlink(path)                    pgunlink(path)
230
231 /*
232  *      Cygwin has its own symlinks which work on Win95/98/ME where
233  *      junction points don't, so use it instead.  We have no way of
234  *      knowing what type of system Cygwin binaries will be run on.
235  *              Note: Some CYGWIN includes might #define WIN32.
236  */
237 #if defined(WIN32) && !defined(__CYGWIN__)
238 extern int      pgsymlink(const char *oldpath, const char *newpath);
239
240 #define symlink(oldpath, newpath)       pgsymlink(oldpath, newpath)
241 #endif
242 #endif   /* defined(WIN32) || defined(__CYGWIN__) */
243
244 extern void copydir(char *fromdir, char *todir, bool recurse);
245
246 extern bool rmtree(char *path, bool rmtopdir);
247
248 #if defined(WIN32) && !defined(__CYGWIN__)
249
250 /* open() replacement to allow delete of held files and passing
251  * of special options. */
252 #ifndef WIN32_CLIENT_ONLY
253 extern int      win32_open(const char *, int,...);
254
255 #define         open(a,b,...)   win32_open(a,b,##__VA_ARGS__)
256 #endif
257
258 #ifndef __BORLANDC__
259 #define popen(a,b) _popen(a,b)
260 #define pclose(a) _pclose(a)
261 #endif
262
263 /* Missing rand functions */
264 extern long lrand48(void);
265 extern void srand48(long seed);
266
267 /* Last parameter not used */
268 extern int      gettimeofday(struct timeval * tp, struct timezone * tzp);
269 #else                                                   /* !WIN32 */
270
271 /*
272  *      Win32 requires a special close for sockets and pipes, while on Unix
273  *      close() does them all.
274  */
275 #define closesocket close
276 #endif   /* WIN32 */
277
278 /*
279  * Default "extern" declarations or macro substitutes for library routines.
280  * When necessary, these routines are provided by files in src/port/.
281  */
282 #ifndef HAVE_CRYPT
283 extern char *crypt(const char *key, const char *setting);
284 #endif
285
286 #if defined(bsdi) || defined(netbsd)
287 extern int      fseeko(FILE *stream, off_t offset, int whence);
288 extern off_t ftello(FILE *stream);
289 #endif
290
291 #ifndef HAVE_FSEEKO
292 #define fseeko(a, b, c) fseek(a, b, c)
293 #define ftello(a)               ftell(a)
294 #endif
295
296 #ifndef HAVE_GETOPT
297 extern int      getopt(int nargc, char *const * nargv, const char *ostr);
298 #endif
299
300 #ifndef HAVE_ISINF
301 extern int      isinf(double x);
302 #endif
303
304 #ifndef HAVE_RINT
305 extern double rint(double x);
306 #endif
307
308 #ifndef HAVE_INET_ATON
309 #ifndef WIN32_CLIENT_ONLY
310 #include <netinet/in.h>
311 #include <arpa/inet.h>
312 #endif
313 extern int      inet_aton(const char *cp, struct in_addr * addr);
314 #endif
315
316 #ifndef HAVE_STRDUP
317 extern char *strdup(char const *);
318 #endif
319
320 #ifndef HAVE_RANDOM
321 extern long random(void);
322 #endif
323
324 #ifndef HAVE_UNSETENV
325 extern void unsetenv(const char *name);
326 #endif
327
328 #ifndef HAVE_SRANDOM
329 extern void srandom(unsigned int seed);
330 #endif
331
332 /* thread.h */
333 extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen);
334
335 #if !defined(WIN32) || defined(__CYGWIN__)
336 extern int pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
337                    size_t buflen, struct passwd ** result);
338 #endif
339
340 extern int pqGethostbyname(const char *name,
341                                 struct hostent * resultbuf,
342                                 char *buffer, size_t buflen,
343                                 struct hostent ** result,
344                                 int *herrno);