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