Fix a few errors in comments. Patch by Fujii Masao, plus the one in
[PostgreSQL.git] / src / include / port.h
blobcbf9dcf99e168c3b5d611c3e831c4deb599c131c
1 /*-------------------------------------------------------------------------
3 * port.h
4 * Header for src/port/ compatibility functions.
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * $PostgreSQL$
11 *-------------------------------------------------------------------------
13 #ifndef PG_PORT_H
14 #define PG_PORT_H
16 #include <ctype.h>
17 #include <netdb.h>
18 #include <pwd.h>
20 /* non-blocking */
21 extern bool pg_set_noblock(int sock);
22 extern bool pg_set_block(int sock);
24 /* Portable path handling for Unix/Win32 (in path.c) */
26 extern char *first_dir_separator(const char *filename);
27 extern char *last_dir_separator(const char *filename);
28 extern char *first_path_separator(const char *pathlist);
29 extern void join_path_components(char *ret_path,
30 const char *head, const char *tail);
31 extern void canonicalize_path(char *path);
32 extern void make_native_path(char *path);
33 extern bool path_contains_parent_reference(const char *path);
34 extern bool path_is_prefix_of_path(const char *path1, const char *path2);
35 extern const char *get_progname(const char *argv0);
36 extern void get_share_path(const char *my_exec_path, char *ret_path);
37 extern void get_etc_path(const char *my_exec_path, char *ret_path);
38 extern void get_include_path(const char *my_exec_path, char *ret_path);
39 extern void get_pkginclude_path(const char *my_exec_path, char *ret_path);
40 extern void get_includeserver_path(const char *my_exec_path, char *ret_path);
41 extern void get_lib_path(const char *my_exec_path, char *ret_path);
42 extern void get_pkglib_path(const char *my_exec_path, char *ret_path);
43 extern void get_locale_path(const char *my_exec_path, char *ret_path);
44 extern void get_doc_path(const char *my_exec_path, char *ret_path);
45 extern void get_html_path(const char *my_exec_path, char *ret_path);
46 extern void get_man_path(const char *my_exec_path, char *ret_path);
47 extern bool get_home_path(char *ret_path);
48 extern void get_parent_directory(char *path);
50 /* port/dirmod.c */
51 extern char **pgfnames(const char *path);
52 extern void pgfnames_cleanup(char **filenames);
55 * is_absolute_path
57 * By making this a macro we avoid needing to include path.c in libpq.
59 #ifndef WIN32
60 #define is_absolute_path(filename) \
61 ( \
62 ((filename)[0] == '/') \
64 #else
65 #define is_absolute_path(filename) \
66 ( \
67 ((filename)[0] == '/') || \
68 (filename)[0] == '\\' || \
69 (isalpha((unsigned char) ((filename)[0])) && (filename)[1] == ':' && \
70 ((filename)[2] == '\\' || (filename)[2] == '/')) \
72 #endif
74 /* Portable locale initialization (in exec.c) */
75 extern void set_pglocale_pgservice(const char *argv0, const char *app);
77 /* Portable way to find binaries (in exec.c) */
78 extern int find_my_exec(const char *argv0, char *retpath);
79 extern int find_other_exec(const char *argv0, const char *target,
80 const char *versionstr, char *retpath);
82 /* Windows security token manipulation (in exec.c) */
83 #ifdef WIN32
84 extern BOOL AddUserToDacl(HANDLE hProcess);
85 #endif
88 #if defined(WIN32) || defined(__CYGWIN__)
89 #define EXE ".exe"
90 #else
91 #define EXE ""
92 #endif
94 #if defined(WIN32) && !defined(__CYGWIN__)
95 #define DEVNULL "nul"
96 /* "con" does not work from the Msys 1.0.10 console (part of MinGW). */
97 #define DEVTTY "con"
98 #else
99 #define DEVNULL "/dev/null"
100 #define DEVTTY "/dev/tty"
101 #endif
104 * Win32 needs double quotes at the beginning and end of system()
105 * strings. If not, it gets confused with multiple quoted strings.
106 * It also requires double-quotes around the executable name and
107 * any files used for redirection. Other args can use single-quotes.
109 * Generated using Win32 "CMD /?":
111 * 1. If all of the following conditions are met, then quote characters
112 * on the command line are preserved:
114 * - no /S switch
115 * - exactly two quote characters
116 * - no special characters between the two quote characters, where special
117 * is one of: &<>()@^|
118 * - there are one or more whitespace characters between the the two quote
119 * characters
120 * - the string between the two quote characters is the name of an
121 * executable file.
123 * 2. Otherwise, old behavior is to see if the first character is a quote
124 * character and if so, strip the leading character and remove the last
125 * quote character on the command line, preserving any text after the last
126 * quote character.
128 #if defined(WIN32) && !defined(__CYGWIN__)
129 #define SYSTEMQUOTE "\""
130 #else
131 #define SYSTEMQUOTE ""
132 #endif
134 /* Portable delay handling */
135 extern void pg_usleep(long microsec);
137 /* Portable SQL-like case-independent comparisons and conversions */
138 extern int pg_strcasecmp(const char *s1, const char *s2);
139 extern int pg_strncasecmp(const char *s1, const char *s2, size_t n);
140 extern unsigned char pg_toupper(unsigned char ch);
141 extern unsigned char pg_tolower(unsigned char ch);
143 #ifdef USE_REPL_SNPRINTF
146 * Versions of libintl >= 0.13 try to replace printf() and friends with
147 * macros to their own versions that understand the %$ format. We do the
148 * same, so disable their macros, if they exist.
150 #ifdef vsnprintf
151 #undef vsnprintf
152 #endif
153 #ifdef snprintf
154 #undef snprintf
155 #endif
156 #ifdef sprintf
157 #undef sprintf
158 #endif
159 #ifdef vfprintf
160 #undef vfprintf
161 #endif
162 #ifdef fprintf
163 #undef fprintf
164 #endif
165 #ifdef printf
166 #undef printf
167 #endif
169 extern int pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args);
170 extern int
171 pg_snprintf(char *str, size_t count, const char *fmt,...)
172 /* This extension allows gcc to check the format string */
173 __attribute__((format(printf, 3, 4)));
174 extern int
175 pg_sprintf(char *str, const char *fmt,...)
176 /* This extension allows gcc to check the format string */
177 __attribute__((format(printf, 2, 3)));
178 extern int pg_vfprintf(FILE *stream, const char *fmt, va_list args);
179 extern int
180 pg_fprintf(FILE *stream, const char *fmt,...)
181 /* This extension allows gcc to check the format string */
182 __attribute__((format(printf, 2, 3)));
183 extern int
184 pg_printf(const char *fmt,...)
185 /* This extension allows gcc to check the format string */
186 __attribute__((format(printf, 1, 2)));
189 * The GCC-specific code below prevents the __attribute__(... 'printf')
190 * above from being replaced, and this is required because gcc doesn't
191 * know anything about pg_printf.
193 #ifdef __GNUC__
194 #define vsnprintf(...) pg_vsnprintf(__VA_ARGS__)
195 #define snprintf(...) pg_snprintf(__VA_ARGS__)
196 #define sprintf(...) pg_sprintf(__VA_ARGS__)
197 #define vfprintf(...) pg_vfprintf(__VA_ARGS__)
198 #define fprintf(...) pg_fprintf(__VA_ARGS__)
199 #define printf(...) pg_printf(__VA_ARGS__)
200 #else
201 #define vsnprintf pg_vsnprintf
202 #define snprintf pg_snprintf
203 #define sprintf pg_sprintf
204 #define vfprintf pg_vfprintf
205 #define fprintf pg_fprintf
206 #define printf pg_printf
207 #endif
208 #endif /* USE_REPL_SNPRINTF */
210 /* Portable prompt handling */
211 extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
214 * WIN32 doesn't allow descriptors returned by pipe() to be used in select(),
215 * so for that platform we use socket() instead of pipe().
216 * There is some inconsistency here because sometimes we require pg*, like
217 * pgpipe, but in other cases we define rename to pgrename just on Win32.
219 #ifndef WIN32
221 * The function prototypes are not supplied because every C file
222 * includes this file.
224 #define pgpipe(a) pipe(a)
225 #define piperead(a,b,c) read(a,b,c)
226 #define pipewrite(a,b,c) write(a,b,c)
227 #else
228 extern int pgpipe(int handles[2]);
229 extern int piperead(int s, char *buf, int len);
231 #define pipewrite(a,b,c) send(a,b,c,0)
233 #define PG_SIGNAL_COUNT 32
234 #define kill(pid,sig) pgkill(pid,sig)
235 extern int pgkill(int pid, int sig);
236 #endif
238 extern int pclose_check(FILE *stream);
240 /* Global variable holding time zone information. */
241 #ifndef __CYGWIN__
242 #define TIMEZONE_GLOBAL timezone
243 #define TZNAME_GLOBAL tzname
244 #else
245 #define TIMEZONE_GLOBAL _timezone
246 #define TZNAME_GLOBAL _tzname
247 #endif
249 #if defined(WIN32) || defined(__CYGWIN__)
251 * Win32 doesn't have reliable rename/unlink during concurrent access.
253 extern int pgrename(const char *from, const char *to);
254 extern int pgunlink(const char *path);
256 /* Include this first so later includes don't see these defines */
257 #ifdef WIN32_ONLY_COMPILER
258 #include <io.h>
259 #endif
261 #define rename(from, to) pgrename(from, to)
262 #define unlink(path) pgunlink(path)
263 #endif /* defined(WIN32) || defined(__CYGWIN__) */
266 * Win32 also doesn't have symlinks, but we can emulate them with
267 * junction points on newer Win32 versions.
269 * Cygwin has its own symlinks which work on Win95/98/ME where
270 * junction points don't, so use those instead. We have no way of
271 * knowing what type of system Cygwin binaries will be run on.
272 * Note: Some CYGWIN includes might #define WIN32.
274 #if defined(WIN32) && !defined(__CYGWIN__)
275 extern int pgsymlink(const char *oldpath, const char *newpath);
277 #define symlink(oldpath, newpath) pgsymlink(oldpath, newpath)
278 #endif
280 extern void copydir(char *fromdir, char *todir, bool recurse);
282 extern bool rmtree(const char *path, bool rmtopdir);
285 * stat() is not guaranteed to set the st_size field on win32, so we
286 * redefine it to our own implementation that is.
288 * We must pull in sys/stat.h here so the system header definition
289 * goes in first, and we redefine that, and not the other way around.
291 * Some frontends don't need the size from stat, so if UNSAFE_STAT_OK
292 * is defined we don't bother with this.
294 #if defined(WIN32) && !defined(__CYGWIN__) && !defined(UNSAFE_STAT_OK)
295 #include <sys/stat.h>
296 extern int pgwin32_safestat(const char *path, struct stat * buf);
298 #define stat(a,b) pgwin32_safestat(a,b)
299 #endif
301 #if defined(WIN32) && !defined(__CYGWIN__)
304 * open() and fopen() replacements to allow deletion of open files and
305 * passing of other special options.
307 #define O_DIRECT 0x80000000
308 extern int pgwin32_open(const char *, int,...);
309 extern FILE *pgwin32_fopen(const char *, const char *);
311 #ifndef FRONTEND
312 #define open(a,b,c) pgwin32_open(a,b,c)
313 #define fopen(a,b) pgwin32_fopen(a,b)
314 #endif
316 #define popen(a,b) _popen(a,b)
317 #define pclose(a) _pclose(a)
319 /* Missing rand functions */
320 extern long lrand48(void);
321 extern void srand48(long seed);
323 /* New versions of MingW have gettimeofday, old mingw and msvc don't */
324 #ifndef HAVE_GETTIMEOFDAY
325 /* Last parameter not used */
326 extern int gettimeofday(struct timeval * tp, struct timezone * tzp);
327 #endif
328 #else /* !WIN32 */
331 * Win32 requires a special close for sockets and pipes, while on Unix
332 * close() does them all.
334 #define closesocket close
335 #endif /* WIN32 */
338 * Default "extern" declarations or macro substitutes for library routines.
339 * When necessary, these routines are provided by files in src/port/.
341 #ifndef HAVE_CRYPT
342 extern char *crypt(const char *key, const char *setting);
343 #endif
345 /* WIN32 handled in port/win32.h */
346 #ifndef WIN32
347 #define pgoff_t off_t
348 #if defined(bsdi) || defined(netbsd)
349 extern int fseeko(FILE *stream, off_t offset, int whence);
350 extern off_t ftello(FILE *stream);
351 #endif
352 #endif
354 #ifndef HAVE_FSEEKO
355 #define fseeko(a, b, c) fseek(a, b, c)
356 #define ftello(a) ftell(a)
357 #endif
359 #ifndef HAVE_GETOPT
360 extern int getopt(int nargc, char *const * nargv, const char *ostr);
361 #endif
363 #ifndef HAVE_ISINF
364 extern int isinf(double x);
365 #endif
367 #ifndef HAVE_RINT
368 extern double rint(double x);
369 #endif
371 #ifndef HAVE_INET_ATON
372 #include <netinet/in.h>
373 #include <arpa/inet.h>
374 extern int inet_aton(const char *cp, struct in_addr * addr);
375 #endif
377 #ifndef HAVE_STRDUP
378 extern char *strdup(const char *str);
379 #endif
381 #if !HAVE_DECL_STRLCAT
382 extern size_t strlcat(char *dst, const char *src, size_t siz);
383 #endif
385 #if !HAVE_DECL_STRLCPY
386 extern size_t strlcpy(char *dst, const char *src, size_t siz);
387 #endif
389 #if !defined(HAVE_RANDOM) && !defined(__BORLANDC__)
390 extern long random(void);
391 #endif
393 #ifndef HAVE_UNSETENV
394 extern void unsetenv(const char *name);
395 #endif
397 #ifndef HAVE_SRANDOM
398 extern void srandom(unsigned int seed);
399 #endif
401 /* thread.h */
402 extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen);
404 #if !defined(WIN32) || defined(__CYGWIN__)
405 extern int pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
406 size_t buflen, struct passwd ** result);
407 #endif
409 extern int pqGethostbyname(const char *name,
410 struct hostent * resultbuf,
411 char *buffer, size_t buflen,
412 struct hostent ** result,
413 int *herrno);
415 extern void pg_qsort(void *base, size_t nel, size_t elsize,
416 int (*cmp) (const void *, const void *));
418 #define qsort(a,b,c,d) pg_qsort(a,b,c,d)
420 typedef int (*qsort_arg_comparator) (const void *a, const void *b, void *arg);
422 extern void qsort_arg(void *base, size_t nel, size_t elsize,
423 qsort_arg_comparator cmp, void *arg);
425 /* port/chklocale.c */
426 extern int pg_get_encoding_from_locale(const char *ctype);
428 #endif /* PG_PORT_H */