00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef PG_PORT_H
00014 #define PG_PORT_H
00015
00016 #include <ctype.h>
00017 #include <netdb.h>
00018 #include <pwd.h>
00019
00020
00021 #ifndef WIN32
00022 typedef int pgsocket;
00023
00024 #define PGINVALID_SOCKET (-1)
00025 #else
00026 typedef SOCKET pgsocket;
00027
00028 #define PGINVALID_SOCKET INVALID_SOCKET
00029 #endif
00030
00031
00032 extern bool pg_set_noblock(pgsocket sock);
00033 extern bool pg_set_block(pgsocket sock);
00034
00035
00036
00037 extern bool has_drive_prefix(const char *filename);
00038 extern char *first_dir_separator(const char *filename);
00039 extern char *last_dir_separator(const char *filename);
00040 extern char *first_path_var_separator(const char *pathlist);
00041 extern void join_path_components(char *ret_path,
00042 const char *head, const char *tail);
00043 extern void canonicalize_path(char *path);
00044 extern void make_native_path(char *path);
00045 extern bool path_contains_parent_reference(const char *path);
00046 extern bool path_is_relative_and_below_cwd(const char *path);
00047 extern bool path_is_prefix_of_path(const char *path1, const char *path2);
00048 extern const char *get_progname(const char *argv0);
00049 extern void get_share_path(const char *my_exec_path, char *ret_path);
00050 extern void get_etc_path(const char *my_exec_path, char *ret_path);
00051 extern void get_include_path(const char *my_exec_path, char *ret_path);
00052 extern void get_pkginclude_path(const char *my_exec_path, char *ret_path);
00053 extern void get_includeserver_path(const char *my_exec_path, char *ret_path);
00054 extern void get_lib_path(const char *my_exec_path, char *ret_path);
00055 extern void get_pkglib_path(const char *my_exec_path, char *ret_path);
00056 extern void get_locale_path(const char *my_exec_path, char *ret_path);
00057 extern void get_doc_path(const char *my_exec_path, char *ret_path);
00058 extern void get_html_path(const char *my_exec_path, char *ret_path);
00059 extern void get_man_path(const char *my_exec_path, char *ret_path);
00060 extern bool get_home_path(char *ret_path);
00061 extern void get_parent_directory(char *path);
00062
00063
00064 extern char **pgfnames(const char *path);
00065 extern void pgfnames_cleanup(char **filenames);
00066
00067
00068
00069
00070
00071
00072 #ifndef WIN32
00073 #define IS_DIR_SEP(ch) ((ch) == '/')
00074
00075 #define is_absolute_path(filename) \
00076 ( \
00077 IS_DIR_SEP((filename)[0]) \
00078 )
00079 #else
00080 #define IS_DIR_SEP(ch) ((ch) == '/' || (ch) == '\\')
00081
00082
00083 #define is_absolute_path(filename) \
00084 ( \
00085 IS_DIR_SEP((filename)[0]) || \
00086 (isalpha((unsigned char) ((filename)[0])) && (filename)[1] == ':' && \
00087 IS_DIR_SEP((filename)[2])) \
00088 )
00089 #endif
00090
00091
00092 extern void set_pglocale_pgservice(const char *argv0, const char *app);
00093
00094
00095 extern int find_my_exec(const char *argv0, char *retpath);
00096 extern int find_other_exec(const char *argv0, const char *target,
00097 const char *versionstr, char *retpath);
00098
00099
00100 #ifdef WIN32
00101 extern BOOL AddUserToTokenDacl(HANDLE hToken);
00102 #endif
00103
00104
00105 #if defined(WIN32) || defined(__CYGWIN__)
00106 #define EXE ".exe"
00107 #else
00108 #define EXE ""
00109 #endif
00110
00111 #if defined(WIN32) && !defined(__CYGWIN__)
00112 #define DEVNULL "nul"
00113 #else
00114 #define DEVNULL "/dev/null"
00115 #endif
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 #if defined(WIN32) && !defined(__CYGWIN__)
00143 #define SYSTEMQUOTE "\""
00144 #else
00145 #define SYSTEMQUOTE ""
00146 #endif
00147
00148
00149 extern void pg_usleep(long microsec);
00150
00151
00152 extern int pg_strcasecmp(const char *s1, const char *s2);
00153 extern int pg_strncasecmp(const char *s1, const char *s2, size_t n);
00154 extern unsigned char pg_toupper(unsigned char ch);
00155 extern unsigned char pg_tolower(unsigned char ch);
00156 extern unsigned char pg_ascii_toupper(unsigned char ch);
00157 extern unsigned char pg_ascii_tolower(unsigned char ch);
00158
00159 #ifdef USE_REPL_SNPRINTF
00160
00161
00162
00163
00164
00165
00166 #ifdef vsnprintf
00167 #undef vsnprintf
00168 #endif
00169 #ifdef snprintf
00170 #undef snprintf
00171 #endif
00172 #ifdef sprintf
00173 #undef sprintf
00174 #endif
00175 #ifdef vfprintf
00176 #undef vfprintf
00177 #endif
00178 #ifdef fprintf
00179 #undef fprintf
00180 #endif
00181 #ifdef printf
00182 #undef printf
00183 #endif
00184
00185 extern int pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args);
00186 extern int
00187 pg_snprintf(char *str, size_t count, const char *fmt,...)
00188
00189 __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
00190 extern int
00191 pg_sprintf(char *str, const char *fmt,...)
00192
00193 __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
00194 extern int pg_vfprintf(FILE *stream, const char *fmt, va_list args);
00195 extern int
00196 pg_fprintf(FILE *stream, const char *fmt,...)
00197
00198 __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
00199 extern int
00200 pg_printf(const char *fmt,...)
00201
00202 __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
00203
00204
00205
00206
00207
00208
00209 #ifdef __GNUC__
00210 #define vsnprintf(...) pg_vsnprintf(__VA_ARGS__)
00211 #define snprintf(...) pg_snprintf(__VA_ARGS__)
00212 #define sprintf(...) pg_sprintf(__VA_ARGS__)
00213 #define vfprintf(...) pg_vfprintf(__VA_ARGS__)
00214 #define fprintf(...) pg_fprintf(__VA_ARGS__)
00215 #define printf(...) pg_printf(__VA_ARGS__)
00216 #else
00217 #define vsnprintf pg_vsnprintf
00218 #define snprintf pg_snprintf
00219 #define sprintf pg_sprintf
00220 #define vfprintf pg_vfprintf
00221 #define fprintf pg_fprintf
00222 #define printf pg_printf
00223 #endif
00224 #endif
00225
00226 #if defined(WIN32)
00227
00228
00229
00230
00231
00232
00233 #if defined(setlocale)
00234 #undef setlocale
00235 #endif
00236
00237
00238
00239
00240
00241 extern char *pgwin32_setlocale(int category, const char *locale);
00242
00243 #define setlocale(a,b) pgwin32_setlocale(a,b)
00244 #endif
00245
00246
00247 extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
00248
00249 #ifdef WIN32
00250 #define PG_SIGNAL_COUNT 32
00251 #define kill(pid,sig) pgkill(pid,sig)
00252 extern int pgkill(int pid, int sig);
00253 #endif
00254
00255 extern int pclose_check(FILE *stream);
00256
00257
00258 #ifndef __CYGWIN__
00259 #define TIMEZONE_GLOBAL timezone
00260 #define TZNAME_GLOBAL tzname
00261 #else
00262 #define TIMEZONE_GLOBAL _timezone
00263 #define TZNAME_GLOBAL _tzname
00264 #endif
00265
00266 #if defined(WIN32) || defined(__CYGWIN__)
00267
00268
00269
00270 extern int pgrename(const char *from, const char *to);
00271 extern int pgunlink(const char *path);
00272
00273
00274 #ifdef WIN32_ONLY_COMPILER
00275 #include <io.h>
00276 #endif
00277
00278 #define rename(from, to) pgrename(from, to)
00279 #define unlink(path) pgunlink(path)
00280 #endif
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291 #if defined(WIN32) && !defined(__CYGWIN__)
00292 extern int pgsymlink(const char *oldpath, const char *newpath);
00293 extern int pgreadlink(const char *path, char *buf, size_t size);
00294 extern bool pgwin32_is_junction(char *path);
00295
00296 #define symlink(oldpath, newpath) pgsymlink(oldpath, newpath)
00297 #define readlink(path, buf, size) pgreadlink(path, buf, size)
00298 #endif
00299
00300 extern bool rmtree(const char *path, bool rmtopdir);
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312 #if defined(WIN32) && !defined(__CYGWIN__) && !defined(UNSAFE_STAT_OK)
00313 #include <sys/stat.h>
00314 extern int pgwin32_safestat(const char *path, struct stat * buf);
00315
00316 #define stat(a,b) pgwin32_safestat(a,b)
00317 #endif
00318
00319 #if defined(WIN32) && !defined(__CYGWIN__)
00320
00321
00322
00323
00324
00325 #define O_DIRECT 0x80000000
00326 extern int pgwin32_open(const char *, int,...);
00327 extern FILE *pgwin32_fopen(const char *, const char *);
00328
00329 #ifndef FRONTEND
00330 #define open(a,b,c) pgwin32_open(a,b,c)
00331 #define fopen(a,b) pgwin32_fopen(a,b)
00332 #endif
00333
00334 #ifndef popen
00335 #define popen(a,b) _popen(a,b)
00336 #endif
00337 #ifndef pclose
00338 #define pclose(a) _pclose(a)
00339 #endif
00340
00341
00342 #ifndef HAVE_GETTIMEOFDAY
00343
00344 extern int gettimeofday(struct timeval * tp, struct timezone * tzp);
00345 #endif
00346 #else
00347
00348
00349
00350
00351
00352 #define closesocket close
00353 #endif
00354
00355
00356
00357
00358
00359 #ifndef HAVE_CRYPT
00360 extern char *crypt(const char *key, const char *setting);
00361 #endif
00362
00363
00364 #ifndef WIN32
00365 #define pgoff_t off_t
00366 #ifdef __NetBSD__
00367 extern int fseeko(FILE *stream, off_t offset, int whence);
00368 extern off_t ftello(FILE *stream);
00369 #endif
00370 #endif
00371
00372 extern double pg_erand48(unsigned short xseed[3]);
00373 extern long pg_lrand48(void);
00374 extern void pg_srand48(long seed);
00375
00376 #ifndef HAVE_FLS
00377 extern int fls(int mask);
00378 #endif
00379
00380 #ifndef HAVE_FSEEKO
00381 #define fseeko(a, b, c) fseek(a, b, c)
00382 #define ftello(a) ftell(a)
00383 #endif
00384
00385 #ifndef HAVE_GETOPT
00386 extern int getopt(int nargc, char *const * nargv, const char *ostr);
00387 #endif
00388
00389 #if !defined(HAVE_GETPEEREID) && !defined(WIN32)
00390 extern int getpeereid(int sock, uid_t *uid, gid_t *gid);
00391 #endif
00392
00393 #ifndef HAVE_ISINF
00394 extern int isinf(double x);
00395 #endif
00396
00397 #ifndef HAVE_RINT
00398 extern double rint(double x);
00399 #endif
00400
00401 #ifndef HAVE_INET_ATON
00402 #include <netinet/in.h>
00403 #include <arpa/inet.h>
00404 extern int inet_aton(const char *cp, struct in_addr * addr);
00405 #endif
00406
00407 #if !HAVE_DECL_STRLCAT
00408 extern size_t strlcat(char *dst, const char *src, size_t siz);
00409 #endif
00410
00411 #if !HAVE_DECL_STRLCPY
00412 extern size_t strlcpy(char *dst, const char *src, size_t siz);
00413 #endif
00414
00415 #if !defined(HAVE_RANDOM) && !defined(__BORLANDC__)
00416 extern long random(void);
00417 #endif
00418
00419 #ifndef HAVE_UNSETENV
00420 extern void unsetenv(const char *name);
00421 #endif
00422
00423 #ifndef HAVE_SRANDOM
00424 extern void srandom(unsigned int seed);
00425 #endif
00426
00427
00428 extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen);
00429
00430 #if !defined(WIN32) || defined(__CYGWIN__)
00431 extern int pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
00432 size_t buflen, struct passwd ** result);
00433 #endif
00434
00435 extern int pqGethostbyname(const char *name,
00436 struct hostent * resultbuf,
00437 char *buffer, size_t buflen,
00438 struct hostent ** result,
00439 int *herrno);
00440
00441 extern void pg_qsort(void *base, size_t nel, size_t elsize,
00442 int (*cmp) (const void *, const void *));
00443 extern int pg_qsort_strcmp(const void *a, const void *b);
00444
00445 #define qsort(a,b,c,d) pg_qsort(a,b,c,d)
00446
00447 typedef int (*qsort_arg_comparator) (const void *a, const void *b, void *arg);
00448
00449 extern void qsort_arg(void *base, size_t nel, size_t elsize,
00450 qsort_arg_comparator cmp, void *arg);
00451
00452
00453 extern int pg_get_encoding_from_locale(const char *ctype, bool write_message);
00454
00455
00456 extern char *inet_net_ntop(int af, const void *src, int bits,
00457 char *dst, size_t size);
00458
00459
00460 extern int pg_check_dir(const char *dir);
00461
00462
00463 extern int pg_mkdir_p(char *path, int omode);
00464
00465
00466 typedef void (*pqsigfunc) (int signo);
00467 extern pqsigfunc pqsignal(int signo, pqsigfunc func);
00468
00469
00470 extern char *escape_single_quotes_ascii(const char *src);
00471
00472
00473 extern char *wait_result_to_str(int exit_status);
00474
00475 #endif