Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

common.h

Go to the documentation of this file.
00001 /*
00002  * $Id: common.h 13938 2007-06-23 04:52:29Z cbiere $
00003  *
00004  * Copyright (c) 2001-2003, Richard Eckart
00005  *
00006  *----------------------------------------------------------------------
00007  * This file is part of gtk-gnutella.
00008  *
00009  *  gtk-gnutella is free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  gtk-gnutella is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *  GNU General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU General Public License
00020  *  along with gtk-gnutella; if not, write to the Free Software
00021  *  Foundation, Inc.:
00022  *      59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *----------------------------------------------------------------------
00024  */
00025 
00036 #ifndef _common_h_
00037 #define _common_h_
00038 
00039 #include "config.h"
00040 
00041 /*
00042  * Constants
00043  */
00044 
00045 #define GTA_VERSION 0               
00046 #define GTA_SUBVERSION 96           
00047 #define GTA_PATCHLEVEL 4            
00048 #define GTA_REVISION "unstable"     
00049 #define GTA_REVCHAR "u"             
00050 #define GTA_RELEASE "2007-06-23"    
00051 #define GTA_WEBSITE "http://gtk-gnutella.sourceforge.net/"
00052 
00053 #if defined(USE_GTK1)
00054 #define GTA_INTERFACE "GTK1"
00055 #elif defined(USE_GTK2)
00056 #define GTA_INTERFACE "GTK2"
00057 #elif defined(USE_TOPLESS)
00058 #define GTA_INTERFACE "Topless"
00059 #else
00060 #error "Expected Gtk+ 1.2, Gtk+ 2.x or headless as user-interface."
00061 #endif
00062 
00063 #ifndef HAS_LIBXML2
00064 #error "You need libxml2 (http://www.xmlsoft.org/) to compile gtk-gnutella"
00065 #endif
00066 
00067 /*
00068  * Main includes
00069  */
00070 
00071 #include <errno.h>
00072 
00073 #ifdef I_STDLIB
00074 #include <stdlib.h>
00075 #endif
00076 
00077 #include <stdio.h>
00078 #include <signal.h>
00079 
00080 #ifdef I_UNISTD
00081 #include <unistd.h>
00082 #endif
00083 
00084 #ifdef I_SYS_TYPES
00085 #include <sys/types.h>
00086 #endif
00087 
00088 #ifdef I_SYS_TIME
00089 #include <sys/time.h>
00090 #endif
00091 
00092 #ifdef I_SYS_TIME_KERNEL
00093 #define KERNEL
00094 #include <sys/time.h>
00095 #undef KERNEL
00096 #endif
00097 
00098 #ifdef MINGW32
00099 #include <ws2tcpip.h>
00100 #include <winsock.h>
00101 #define ECONNRESET WSAECONNRESET
00102 #define ECONNREFUSED WSAECONNREFUSED
00103 #define ECONNABORTED WSAECONNABORTED
00104 #define ENETUNREACH WSAENETUNREACH
00105 #define EHOSTUNREACH WSAEHOSTUNREACH
00106 #define ETIMEDOUT WSAETIMEDOUT
00107 #define EINPROGRESS WSAEINPROGRESS
00108 #define ENOTCONN WSAENOTCONN
00109 #define ENOBUFS WSAENOBUFS
00110 #define EADDRNOTAVAIL WSAEADDRNOTAVAIL
00111 #define ENETRESET WSAENETRESET
00112 #define ENETDOWN WSAENETDOWN
00113 #define EHOSTDOWN WSAEHOSTDOWN
00114 #define ENOPROTOOPT WSAENOPROTOOPT
00115 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
00116 
00117 #define SHUT_RD   SD_RECEIVE
00118 #define SHUT_WR   SD_SEND
00119 #define SHUT_RDWR SD_BOTH
00120 
00121 #define S_IXGRP  _S_IEXEC
00122 #define S_IWGRP  _S_IWRITE
00123 #define S_IRGRP  _S_IREAD
00124 
00125 #define S_IRWXG _S_IREAD
00126 #define S_IRWXO _S_IREAD
00127 
00128 #endif /* !MINGW32 */
00129 
00130 #ifdef I_SYS_SOCKET
00131 #include <sys/socket.h>
00132 #endif
00133 
00134 #ifdef I_SYS_RESOURCE
00135 #include <sys/resource.h>
00136 #endif
00137 
00138 #ifdef I_SYS_UN
00139 #include <sys/un.h>
00140 #endif
00141 
00142 #ifdef I_SYS_STAT
00143 #include <sys/stat.h>
00144 #endif
00145 
00146 #ifdef MINGW32
00147 struct iovec 
00148 {
00149     char  *iov_base;
00150     int  iov_len; 
00151 };
00152 
00153 struct passwd
00154 {
00155     char *pw_name;                /* Username.  */
00156     char *pw_passwd;              /* Password.  */
00157 #if 0
00158     __uid_t pw_uid;               /* User ID.  */
00159     __gid_t pw_gid;               /* Group ID.  */
00160     char *pw_gecos;               /* Real name.  */
00161     char *pw_dir;                 /* Home directory.  */
00162     char *pw_shell;               /* Shell program.  */
00163 #endif /* 0 */
00164 };
00165 
00166 #else /* !MINGW32 */
00167 
00168 #include <sys/uio.h>        /* For writev(), readv(), struct iovec */
00169 #include <sys/wait.h>
00170 
00171 #ifdef I_NETINET_IN
00172 #include <netinet/in.h>
00173 #endif
00174 
00175 #include <netinet/tcp.h>
00176 
00177 #endif /* MINGW32 */
00178 
00179 #ifdef I_ARPA_INET
00180 #include <arpa/inet.h>      /* For ntohl(), htonl() */
00181 #endif
00182 
00183 #include <ctype.h>
00184 
00185 #ifdef I_SYS_FILE
00186 #include <sys/file.h>
00187 #endif
00188 
00189 #include <fcntl.h>
00190 
00191 #if !defined(I_FCNTL) && !defined(I_SYS_FILE)
00192 #include <sys/fcntl.h>      /* Fallback */
00193 #endif
00194 
00195 #ifdef I_STRING
00196 #include <string.h>
00197 #else
00198 #include <strings.h>
00199 #endif
00200 
00201 #ifdef I_DIRENT
00202 #include <dirent.h>
00203 #endif
00204 
00205 #include <setjmp.h>
00206 
00207 #ifdef I_TIME
00208 #include <time.h>
00209 #endif
00210 
00211 #ifdef I_SYS_PARAM
00212 #include <sys/param.h>
00213 #endif
00214 
00215 #ifdef I_INTTYPES
00216 #include <inttypes.h>
00217 #endif /* I_INTTYPES */
00218 
00219 #ifdef I_SYS_UTSNAME
00220 #include <sys/utsname.h>        /* For uname() */
00221 #endif
00222 
00223 #ifdef I_SYS_MMAN
00224 #include <sys/mman.h>
00225 #endif
00226 
00227 #ifndef MAP_FAILED
00228 #define MAP_FAILED ((void *) -1)
00229 #endif  /* !MMAP_FAILED */
00230 
00231 #ifdef I_SYS_SENDFILE
00232 #include <sys/sendfile.h>
00233 #else   /* !I_SYS_SENDFILE */
00234 #ifdef HAS_SENDFILE
00235 #define USE_BSD_SENDFILE    
00236 #else
00237 /*
00238  * Proper mmap() support for memory-mapped files requires ISO C functions like
00239  * sigsetjmp().
00240  */
00241 #if defined(HAS_MMAP) && defined(HAS_SIGSETJMP)
00242 #define USE_MMAP 1
00243 #endif  /* ISO C */
00244 
00245 #endif  /* HAS_SENDFILE */
00246 #endif  /* I_SYS_SENDFILE_H */
00247 
00248 #if defined(USE_IP_TOS) && defined(I_NETINET_IP)
00249 #include <netinet/in_systm.h>
00250 #include <netinet/ip.h>
00251 #endif
00252 
00253 #if defined(HAS_POLL)
00254 #include <poll.h>
00255 #endif  /* HAS_POLL */
00256 
00257 #if !defined(AF_LOCAL) && defined(AF_UNIX)
00258 #define AF_LOCAL AF_UNIX
00259 #endif  /* !AF_LOCAL && AF_UNIX */
00260 
00261 #if !defined(PF_LOCAL) && defined(PF_UNIX)
00262 #define PF_LOCAL PF_UNIX
00263 #endif  /* !PF_LOCAL && PF_UNIX */
00264 
00265 /* For pedantic lint checks, define USE_LINT. We override some definitions
00266  * and hide ``inline'' to prevent certain useless warnings. */
00267 #ifdef USE_LINT
00268 #undef G_GNUC_INTERNAL
00269 #define G_GNUC_INTERNAL
00270 #undef G_INLINE_FUNC
00271 #define G_INLINE_FUNC
00272 #define inline
00273 #endif
00274 
00275 /*
00276  * Determine how large an I/O vector the kernel can accept.
00277  */
00278 
00279 #if defined(IOV_MAX)
00280 #define MAX_IOV_COUNT   IOV_MAX         
00281 #elif defined(MAXIOV)
00282 #define MAX_IOV_COUNT   MAXIOV          
00283 #elif defined(UIO_MAXIOV)
00284 #define MAX_IOV_COUNT   UIO_MAXIOV      
00285 #elif defined(_XOPEN_IOV_MAX)
00286 #define MAX_IOV_COUNT   _XOPEN_IOV_MAX  
00287 #else
00288 #define MAX_IOV_COUNT   16              
00289 #endif
00290 
00291 #include <glib.h>
00292 
00293 #if defined(USE_GLIB1) && !defined(GLIB_MAJOR_VERSION)
00294 #error "Install GLib 1.2 to compile gtk-gnutella against GLib 1.2."
00295 #endif
00296 
00297 #if defined(USE_GLIB2) && !defined(GLIB_MAJOR_VERSION)
00298 #error "Install GLib 2.x to compile gtk-gnutella against GLib 2.x."
00299 #endif
00300 
00301 #ifdef USE_LINT
00302 #undef G_GNUC_INTERNAL
00303 #define G_GNUC_INTERNAL
00304 #undef G_INLINE_FUNC
00305 #define G_INLINE_FUNC
00306 #define inline
00307 #endif
00308 
00309 typedef guint64 filesize_t; 
00311 #ifdef I_STDARG
00312 #include <stdarg.h>
00313 #endif
00314 
00315 #ifdef I_REGEX
00316 #include <regex.h>
00317 #endif
00318 
00319 #ifdef USE_GLIB1
00320 typedef void (*GCallback) (void);
00321 #define G_STRLOC __FILE__ ":" STRINGIFY(__LINE__)
00322 #endif
00323 #ifdef USE_GLIB2
00324 #include <glib-object.h>
00325 #endif
00326 
00327 /*
00328  * Array size determination
00329  */
00330 #ifndef G_N_ELEMENTS
00331 #define G_N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0]))
00332 #endif
00333 
00334 /*
00335  * Portability macros.
00336  */
00337 
00338 /*
00339  * Can only use the `args' obtained via va_start(args) ONCE.  If we need
00340  * to call another vararg routine, we need to copy the original args.
00341  * The __va_copy macro is a GNU extension.
00342  */
00343 #ifdef va_copy
00344 #define VA_COPY(dest, src) va_copy(dest, src)
00345 #elif defined(__va_copy)
00346 #define VA_COPY(dest, src)  __va_copy(dest, src)
00347 #else
00348 #define VA_COPY(dest, src)  (dest) = (src)
00349 #endif
00350 
00351 /*
00352  * Standard file descriptor numbers
00353  */
00354 
00355 #ifndef STDIN_FILENO
00356 #define STDIN_FILENO 0
00357 #endif /* STDIN_FILENO */
00358 
00359 #ifndef STDOUT_FILENO
00360 #define STDOUT_FILENO 1
00361 #endif /* STDOUT_FILENO */
00362 
00363 #ifndef STDERR_FILENO
00364 #define STDERR_FILENO 2
00365 #endif /* STDERR_FILENO */
00366 
00367 /*
00368  * S_ISLNK() is in POSIX.1-1997 but not in older revisions of the standard.
00369  */
00370 #ifndef S_ISLNK
00371 #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
00372 #endif  /* S_ISLNK */
00373 
00374 /*
00375  * These macros determine the maximum/minimum value of the given integer type
00376  * "t". This works for signed as well as unsigned types. This code does
00377  * carefully avoid integer overflows and undefined behaviour.
00378  * However, it's assumed the type consists of exactly sizeof(type) * CHAR_BIT
00379  * bits.
00380  */
00381 
00382 #define MAX_INT_VAL_STEP(t) \
00383     ((t) 1 << (CHAR_BIT * sizeof(t) - 1 - ((t) -1 < 1)))
00384      
00385 #define MAX_INT_VAL(t) \
00386     ((MAX_INT_VAL_STEP(t) - 1) + MAX_INT_VAL_STEP(t))
00387 
00388 #define MIN_INT_VAL(t) \
00389     ((t) -MAX_INT_VAL(t) - 1)
00390 
00391 #ifndef TIME_T_MAX
00392 /* This assumes time_t is an integer, not a float */
00393 #define TIME_T_MAX MAX_INT_VAL(time_t)
00394 #endif /* TIME_T_MAX */
00395 
00396 #ifndef OFF_T_MAX
00397 #define OFF_T_MAX MAX_INT_VAL(off_t)
00398 #endif /* OFF_T_MAX */
00399 
00400 /*
00401  * Other common macros.
00402  */
00403 
00404 #define SRC_PREFIX  "src/"      
00406 /*
00407  * Sources should use _WHERE_ instead of __FILE__ and call short_filename()
00408  * on the resulting string before perusing it to remove the common prefix
00409  * defined by SRC_PREFIX.
00410  */
00411 #ifdef CURDIR                   /* Set by makefile */
00412 #define _WHERE_ STRINGIFY(CURDIR) "/" __FILE__
00413 #else
00414 #define _WHERE_ __FILE__
00415 #endif
00416 
00417 /*
00418  * PACKAGE_EXTRA_SOURCE_DIR is set to $srcdir/extra_files when not compiling an
00419  * official build so it's not required to install these files for testing.
00420  */
00421 #ifdef OFFICIAL_BUILD
00422 #undef PACKAGE_EXTRA_SOURCE_DIR
00423 #else
00424 #define PACKAGE_EXTRA_SOURCE_DIR \
00425     PACKAGE_SOURCE_DIR G_DIR_SEPARATOR_S "extra_files"
00426 #endif
00427 
00433 #define G_FREE_NULL(p)  \
00434 G_STMT_START {          \
00435     if (p) {            \
00436         g_free(p);      \
00437         p = NULL;       \
00438     }                   \
00439 } G_STMT_END
00440 
00441 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
00442 
00451 #define HAVE_GCC(major, minor) \
00452     ((__GNUC__ > (major)) || \
00453      (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
00454 #else
00455 #define HAVE_GCC(major, minor) 0
00456 #endif
00457 
00458 /*
00459  * If functions have this attribute GCC warns if it one of the specified
00460  * parameters is NULL. This macro takes a list of parameter indices. The
00461  * list must be embraced with parentheses for compatibility with C89
00462  * compilers. Example:
00463  *
00464  * void my_memcpy(void *dst, const void *src, size_t n) NON_NULL_PARAM((1, 2));
00465  */
00466 #if HAVE_GCC(3, 3)
00467 #define NON_NULL_PARAM(x) __attribute__((__nonnull__ x))
00468 #else /* GCC < 3.3 */
00469 #define NON_NULL_PARAM(x)
00470 #endif
00471 
00476 #if HAVE_GCC(3, 0)
00477 #define PRINTF_FUNC_PTR(x, y) __attribute__((format(__printf__, (x), (y))))
00478 #else /* GCC < 3.0 */
00479 #define PRINTF_FUNC_PTR(x, y)
00480 #endif
00481 
00482 /* Functions using this attribute cause a warning if the returned
00483  * value is not used. */
00484 #if HAVE_GCC(3, 4)
00485 #define WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
00486 #else /* GCC < 3.4 */
00487 #define WARN_UNUSED_RESULT
00488 #endif
00489 
00490 /* Instructs the compiler to emit code for this function even if it is
00491  * or seems to be unused. */
00492 #if HAVE_GCC(3, 1)
00493 #define KEEP_FUNCTION __attribute__((__used__))
00494 #else /* GCC < 3.1 || !GCC */
00495 #define KEEP_FUNCTION
00496 #endif
00497 
00498 /* The antidote for WARN_UNUSED_RESULT. This attribute is sometimes
00499  * misused for functions that return a result which SHOULD NOT be
00500  * ignored in contrast to MUST NOT. Unfortunately, a simple "(void)"
00501  * does not suppress this warning.
00502  */
00503 #define IGNORE_RESULT(x) \
00504     G_STMT_START { switch (0 != (x)) { default: ; } }  G_STMT_END
00505 
00506 /* Functions using this attribute cause a warning if the variable
00507  * argument list does not contain a NULL pointer. */
00508 #ifndef G_GNUC_NULL_TERMINATED
00509 #if HAVE_GCC(4, 0)
00510 #define G_GNUC_NULL_TERMINATED __attribute__((__sentinel__))
00511 #else   /* GCC < 4 */
00512 #define G_GNUC_NULL_TERMINATED
00513 #endif  /* GCC >= 4 */
00514 #endif  /* G_GNUC_NULL_TERMINATED */
00515 
00516 /* Define G_LIKELY() and G_UNLIKELY() so that they are available when
00517  * using GLib 1.2 as well. These allow optimization by static branch
00518  * prediction with GCC. */
00519 #ifndef G_LIKELY
00520 #if HAVE_GCC(3, 4)  /* Just a guess, a Configure check would be better */
00521 #define G_LIKELY(x)     (__builtin_expect(x, 1))
00522 #define G_UNLIKELY(x)   (__builtin_expect(x, 0))
00523 #else /* !GCC >= 3.4 */
00524 #define G_LIKELY(x)     (x)
00525 #define G_UNLIKELY(x)   (x)
00526 #endif /* GCC >= 3.4 */
00527 #endif /* !G_LIKELY */
00528 
00529 #ifndef G_GNUC_MALLOC
00530 #if HAVE_GCC(3, 0)
00531 #define G_GNUC_MALLOC __attribute__((__malloc__))
00532 #else
00533 #define G_GNUC_MALLOC
00534 #endif  /* GCC >= 3.0 */
00535 #endif  /* G_GNUC_MALLOC */
00536 
00537 #if HAVE_GCC(3, 1)
00538 #define ALWAYS_INLINE __attribute__((__always_inline__))
00539 #else
00540 #define ALWAYS_INLINE
00541 #endif  /* GCC >= 3.1 */
00542 
00543 #ifdef HAS_REGPARM
00544 #define REGPARM(n)  __attribute__((__regparm__((n))))
00545 #else
00546 #define REGPARM(n)
00547 #endif  /* HAS_REGPARM */
00548 
00552 #define CMP(a, b) ((a) == (b) ? 0 : (a) > (b) ? 1 : (-1))
00553 
00557 #define SIGN(x) ((x) == 0 ? 0 : (x) > 0 ? 1 : (-1))
00558 
00559 
00573 #define STATIC_ASSERT(x) \
00574     do { switch (0) { case ((x) ? 1 : 0): case 0: break; } } while(0)
00575 
00576 #if defined(GTA_PATCHLEVEL) && (GTA_PATCHLEVEL != 0)
00577 #define GTA_VERSION_NUMBER \
00578     STRINGIFY(GTA_VERSION) "." \
00579     STRINGIFY(GTA_SUBVERSION) "." \
00580     STRINGIFY(GTA_PATCHLEVEL) GTA_REVCHAR
00581 #else
00582 #define GTA_VERSION_NUMBER \
00583     STRINGIFY(GTA_VERSION) "." STRINGIFY(GTA_SUBVERSION) GTA_REVCHAR
00584 #endif
00585 
00586 #define GTA_PORT            6346    
00587 #define MAX_HOSTLEN         256     
00588 #define GTA_BUILD                   "$Revision: 13938 $"
00589 
00590 /* The next two defines came from huge.h --- Emile */
00591 #define SHA1_BASE16_SIZE    40      
00592 #define SHA1_BASE32_SIZE    32      
00593 #define SHA1_RAW_SIZE       20      
00595 #define TTH_BASE32_SIZE     39      
00596 #define TTH_RAW_SIZE        24      
00598 #define BITPRINT_BASE32_SIZE    72  
00600 #define BITPRINT_RAW_SIZE       44  
00603 #define FILENAME_MAXBYTES 256
00604 
00605 /*
00606  * Forbidden glib calls.
00607  */
00608 
00609 #define g_snprintf  DONT_CALL_g_snprintf 
00610 #define g_vsnprintf DONT_CALL_g_vsnprintf 
00612 /*
00613  * Typedefs
00614  */
00615 
00616 typedef gboolean (*reclaim_fd_t)(void);
00617 
00618 /*
00619  * Standard gettext macros.
00620  */
00621 
00622 #ifdef ENABLE_NLS
00623 #  include <libintl.h>
00624 #  undef _
00625 #  define _(String) dgettext(PACKAGE, String)
00626 #  define Q_(String) g_strip_context ((String), gettext (String))
00627 #  ifdef gettext_noop
00628 #    define N_(String) gettext_noop(String)
00629 #  else
00630 #    define N_(String) (String)
00631 #  endif
00632 
00633 
00634 #else
00635 #  define textdomain(String) (String)
00636 #  define gettext(String) (String)
00637 #  define dgettext(Domain,Message) (Message)
00638 #  define dcgettext(Domain,Message,Type) (Message)
00639 #  define bindtextdomain(Domain,Directory) (Domain)
00640 #  define ngettext(Single, Plural, Number) ((Number) == 1 ? (Single) : (Plural))
00641 #  define _(String) (String)
00642 #  define N_(String) (String)
00643 #  define Q_(String) g_strip_context ((String), (String))
00644 #endif /* ENABLE_NLS */
00645 
00646 static inline const gchar *
00647 ngettext_(const gchar *msg1, const gchar *msg2, gulong n)
00648 G_GNUC_FORMAT(1) G_GNUC_FORMAT(2);
00649 
00650 static inline const gchar *
00651 ngettext_(const gchar *msg1, const gchar *msg2, gulong n)
00652 {
00653     return ngettext(msg1, msg2, n);
00654 }
00655 
00659 #define NG_(Single, Plural, Number) ngettext_((Single), (Plural), (Number))
00660 
00668 #define RCSID(x) \
00669 static KEEP_FUNCTION inline const char *    \
00670 get_rcsid(void) \
00671 {   \
00672     static const char rcsid[] = "@(#) " x;  \
00673     const char *s = rcsid;  \
00674     while (*s != '\0') {    \
00675         if (*s++ == '$')    \
00676             break;  \
00677     }   \
00678     return s;   \
00679 }
00680 
00681 #include "casts.h"
00682 
00683 #endif /* _common_h_ */
00684 
00685 /* vi: set ts=4 sw=4 cindent: */

Generated on Sat Jun 30 17:53:22 2007 for gtk-gnutella by  doxygen 1.3.9.1