r22772: - Still working on the fact that readahead() is not declared (on at least one
[nivanova/samba-autobuild/.git] / source3 / lib / replace / replace.h
index eabff67cd48b917245d76c45e27c085c8507d277..916d5a55e69cad497b6236b0032e9037c343107a 100644 (file)
@@ -28,7 +28,9 @@
 #ifndef _LIBREPLACE_REPLACE_H
 #define _LIBREPLACE_REPLACE_H
 
+#ifndef NO_CONFIG_H
 #include "config.h"
+#endif
 
 #ifdef HAVE_STANDARDS_H
 #include <standards.h>
 #include <stddef.h>
 #endif
 
+/**
+  this is a warning hack. The idea is to use this everywhere that we
+  get the "discarding const" warning from gcc. That doesn't actually
+  fix the problem of course, but it means that when we do get to
+  cleaning them up we can do it by searching the code for
+  discard_const.
+
+  It also means that other error types aren't as swamped by the noise
+  of hundreds of const warnings, so we are more likely to notice when
+  we get new errors.
+
+  Please only add more uses of this macro when you find it
+  _really_ hard to fix const warnings. Our aim is to eventually use
+  this function in only a very few places.
+
+  Also, please call this via the discard_const_p() macro interface, as that
+  makes the return type safe.
+*/
+#define discard_const(ptr) ((void *)((intptr_t)(ptr)))
+
+/** Type-safe version of discard_const */
+#define discard_const_p(type, ptr) ((type *)discard_const(ptr))
+
 #ifndef HAVE_STRERROR
 extern char *sys_errlist[];
 #define strerror(i) sys_errlist[i]
@@ -86,6 +111,11 @@ extern char *sys_errlist[];
 extern int errno;
 #endif
 
+#if defined(HAVE_LINUX_READAHEAD) && ! defined(HAVE_READAHEAD_DECL)
+ssize_t readahead(int fd, off64_t offset, size_t count);
+#endif
+
+
 #ifndef HAVE_STRDUP
 #define strdup rep_strdup
 char *rep_strdup(const char *s);
@@ -121,19 +151,30 @@ size_t rep_strlcpy(char *d, const char *s, size_t bufsize);
 size_t rep_strlcat(char *d, const char *s, size_t bufsize);
 #endif
 
-#ifndef HAVE_STRNDUP
+#if (defined(BROKEN_STRNDUP) || !defined(HAVE_STRNDUP))
+#undef HAVE_STRNDUP
 #define strndup rep_strndup
 char *rep_strndup(const char *s, size_t n);
 #endif
 
-#ifndef HAVE_STRNLEN
+#if (defined(BROKEN_STRNLEN) || !defined(HAVE_STRNLEN))
+#undef HAVE_STRNLEN
 #define strnlen rep_strnlen
 size_t rep_strnlen(const char *s, size_t n);
 #endif
 
 #ifndef HAVE_SETENV
 #define setenv rep_setenv
-int rep_setenv(const char *name, const char *value, int overwrite); 
+int rep_setenv(const char *name, const char *value, int overwrite);
+#else
+#ifndef HAVE_SETENV_DECL
+int setenv(const char *name, const char *value, int overwrite);
+#endif
+#endif
+
+#ifndef HAVE_UNSETENV
+#define unsetenv rep_unsetenv
+int rep_unsetenv(const char *name); 
 #endif
 
 #ifndef HAVE_SETEUID
@@ -177,7 +218,7 @@ int rep_ftruncate(int,off_t);
 #endif
 
 #ifndef HAVE_INITGROUPS
-#define ftruncate rep_ftruncate
+#define initgroups rep_initgroups
 int rep_initgroups(char *name, gid_t id);
 #endif
 
@@ -205,6 +246,10 @@ void *rep_dlsym(void *handle, const char *symbol);
 int rep_dlclose(void *handle);
 #endif
 
+#ifndef HAVE_SOCKETPAIR
+#define socketpair rep_socketpair
+int rep_socketpair(int d, int type, int protocol, int sv[2]);
+#endif
 
 #ifndef PRINTF_ATTRIBUTE
 #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 )
@@ -220,7 +265,7 @@ int rep_dlclose(void *handle);
 
 #ifndef HAVE_VASPRINTF
 #define vasprintf rep_vasprintf
-int rep_vasprintf(char **ptr, const char *format, va_list ap);
+int rep_vasprintf(char **ptr, const char *format, va_list ap) PRINTF_ATTRIBUTE(2,0);
 #endif
 
 #if !defined(HAVE_SNPRINTF) || !defined(HAVE_C99_VSNPRINTF)
@@ -230,7 +275,7 @@ int rep_snprintf(char *,size_t ,const char *, ...) PRINTF_ATTRIBUTE(3,4);
 
 #if !defined(HAVE_VSNPRINTF) || !defined(HAVE_C99_VSNPRINTF)
 #define vsnprintf rep_vsnprintf
-int rep_vsnprintf(char *,size_t ,const char *, va_list ap);
+int rep_vsnprintf(char *,size_t ,const char *, va_list ap) PRINTF_ATTRIBUTE(3,0);
 #endif
 
 #ifndef HAVE_ASPRINTF
@@ -238,6 +283,12 @@ int rep_vsnprintf(char *,size_t ,const char *, va_list ap);
 int rep_asprintf(char **,const char *, ...) PRINTF_ATTRIBUTE(2,3);
 #endif
 
+#ifndef HAVE_VSYSLOG
+#ifdef HAVE_SYSLOG
+#define vsyslog rep_vsyslog
+void rep_vsyslog (int facility_priority, const char *format, va_list arglist) PRINTF_ATTRIBUTE(2,0);
+#endif
+#endif
 
 /* we used to use these fns, but now we have good replacements
    for snprintf and vsnprintf */
@@ -261,6 +312,12 @@ int rep_asprintf(char **,const char *, ...) PRINTF_ATTRIBUTE(2,3);
 typedef int (*comparison_fn_t)(const void *, const void *);
 #endif
 
+#ifdef REPLACE_STRPTIME
+#define strptime rep_strptime
+struct tm;
+char *rep_strptime(const char *buf, const char *format, struct tm *tm);
+#endif
+
 /* Load header file for dynamic linking stuff */
 #ifdef HAVE_DLFCN_H
 #include <dlfcn.h>
@@ -324,17 +381,29 @@ char *rep_mkdtemp(char *template);
 #ifdef HAVE__Bool
 #define bool _Bool
 #else
-#define __bool_true_false_are_defined
+typedef int bool;
+#endif
+#endif
+
 /*
  * to prevent <rpcsvc/yp_prot.h> from doing a redefine of 'bool'
  *
  * IRIX, HPUX, MacOS 10 and Solaris need BOOL_DEFINED
  * Tru64 needs _BOOL_EXISTS
+ * AIX needs _BOOL,_TRUE,_FALSE
  */
+#ifndef BOOL_DEFINED
 #define BOOL_DEFINED
+#endif
+#ifndef _BOOL_EXISTS
 #define _BOOL_EXISTS
-typedef int bool;
 #endif
+#ifndef _BOOL
+#define _BOOL
+#endif
+
+#ifndef __bool_true_false_are_defined
+#define __bool_true_false_are_defined
 #endif
 
 #ifndef true
@@ -344,6 +413,13 @@ typedef int bool;
 #define false (0)
 #endif
 
+#ifndef _TRUE
+#define _TRUE true
+#endif
+#ifndef _FALSE
+#define _FALSE false
+#endif
+
 #ifndef HAVE_FUNCTION_MACRO
 #ifdef HAVE_func_MACRO
 #define __FUNCTION__ __func__
@@ -368,6 +444,47 @@ typedef int bool;
 #define __STRING(x)    #x
 #endif
 
+#ifndef _STRINGSTRING
+#define __STRINGSTRING(x) __STRING(x)
+#endif
+
+#ifndef __LINESTR__
+#define __LINESTR__ __STRINGSTRING(__LINE__)
+#endif
+
+#ifndef __location__
+#define __location__ __FILE__ ":" __LINESTR__
+#endif
+
+/** 
+ * zero a structure 
+ */
+#define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
+
+/** 
+ * zero a structure given a pointer to the structure 
+ */
+#define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
+
+/** 
+ * zero a structure given a pointer to the structure - no zero check 
+ */
+#define ZERO_STRUCTPN(x) memset((char *)(x), 0, sizeof(*(x)))
+
+/* zero an array - note that sizeof(array) must work - ie. it must not be a
+   pointer */
+#define ZERO_ARRAY(x) memset((char *)(x), 0, sizeof(x))
+
+/**
+ * work out how many elements there are in a static array 
+ */
+#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
+
+/** 
+ * pointer difference macro 
+ */
+#define PTR_DIFF(p1,p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
+
 #if MMAP_BLACKLIST
 #undef HAVE_MMAP
 #endif