2 Unix SMB/CIFS implementation.
4 macros to go along with the lib/replace/ portability layer code
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Jelmer Vernooij 2006-2008
8 Copyright (C) Jeremy Allison 2007.
10 ** NOTE! The following LGPL license applies to the replace
11 ** library. This does NOT imply that all of Samba is released
14 This library is free software; you can redistribute it and/or
15 modify it under the terms of the GNU Lesser General Public
16 License as published by the Free Software Foundation; either
17 version 3 of the License, or (at your option) any later version.
19 This library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Lesser General Public License for more details.
24 You should have received a copy of the GNU Lesser General Public
25 License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 #ifndef _LIBREPLACE_REPLACE_H
29 #define _LIBREPLACE_REPLACE_H
33 #ifdef HAVE_STANDARDS_H
34 #include <standards.h>
42 #if defined(_MSC_VER) || defined(__MINGW32__)
43 #include "win32_replace.h"
49 /* force off HAVE_INTTYPES_H so that roken doesn't try to include both,
50 which causes a warning storm on irix */
51 #undef HAVE_INTTYPES_H
53 #define __STDC_FORMAT_MACROS
57 #ifndef __PRI64_PREFIX
59 # define __PRI64_PREFIX "l"
61 # define __PRI64_PREFIX "ll"
65 /* Decimal notation. */
76 # define PRId64 __PRI64_PREFIX "d"
89 # define PRIi64 __PRI64_PREFIX "i"
102 # define PRIu64 __PRI64_PREFIX "u"
109 #ifdef HAVE_STRINGS_H
113 #ifdef HAVE_SYS_TYPES_H
114 #include <sys/types.h>
122 #ifndef HAVE_STRERROR
123 extern char *sys_errlist[];
124 #define strerror(i) sys_errlist[i]
127 #ifndef HAVE_ERRNO_DECL
132 #define strdup rep_strdup
133 char *rep_strdup(const char *s);
137 #define memmem rep_memmem
138 void *rep_memmem(const void *haystack, size_t haystacklen,
139 const void *needle, size_t needlelen);
143 #define mktime rep_mktime
144 /* prototype is in "system/time.h" */
148 #define timegm rep_timegm
149 /* prototype is in "system/time.h" */
153 #define utime rep_utime
154 /* prototype is in "system/time.h" */
158 #define utimes rep_utimes
159 /* prototype is in "system/time.h" */
162 #if !HAVE_DECL_ENVIRON
164 #include <crt_externs.h>
165 #define environ (*_NSGetEnviron())
167 extern char **environ;
172 #define setenv rep_setenv
173 int rep_setenv(const char *name, const char *value, int overwrite);
175 #ifndef HAVE_SETENV_DECL
176 int setenv(const char *name, const char *value, int overwrite);
180 #ifndef HAVE_UNSETENV
181 #define unsetenv rep_unsetenv
182 int rep_unsetenv(const char *name);
186 #define seteuid rep_seteuid
187 int rep_seteuid(uid_t);
191 #define setegid rep_setegid
192 int rep_setegid(gid_t);
195 #if (defined(USE_SETRESUID) && !defined(HAVE_SETRESUID_DECL))
197 int setresuid(uid_t ruid, uid_t euid, uid_t suid);
199 #if (defined(USE_SETRESUID) && !defined(HAVE_SETRESGID_DECL))
200 int setresgid(gid_t rgid, gid_t egid, gid_t sgid);
204 #define chown rep_chown
205 int rep_chown(const char *path, uid_t uid, gid_t gid);
209 #define chroot rep_chroot
210 int rep_chroot(const char *dirname);
214 #define link rep_link
215 int rep_link(const char *oldpath, const char *newpath);
218 #ifndef HAVE_READLINK
219 #define readlink rep_readlink
220 ssize_t rep_readlink(const char *path, char *buf, size_t bufsize);
224 #define symlink rep_symlink
225 int rep_symlink(const char *oldpath, const char *newpath);
228 #ifndef HAVE_REALPATH
229 #define realpath rep_realpath
230 char *rep_realpath(const char *path, char *resolved_path);
234 #define lchown rep_lchown
235 int rep_lchown(const char *fname,uid_t uid,gid_t gid);
242 #ifndef HAVE_SETLINEBUF
243 #define setlinebuf rep_setlinebuf
244 void rep_setlinebuf(FILE *);
247 #ifndef HAVE_STRCASESTR
248 #define strcasestr rep_strcasestr
249 char *rep_strcasestr(const char *haystack, const char *needle);
253 #define strtoll rep_strtoll
254 long long int rep_strtoll(const char *str, char **endptr, int base);
257 #ifndef HAVE_STRTOULL
258 #define strtoull rep_strtoull
259 unsigned long long int rep_strtoull(const char *str, char **endptr, int base);
262 #ifndef HAVE_FTRUNCATE
263 #define ftruncate rep_ftruncate
264 int rep_ftruncate(int,off_t);
267 #ifndef HAVE_INITGROUPS
268 #define initgroups rep_initgroups
269 int rep_initgroups(char *name, gid_t id);
273 #define dlerror rep_dlerror
274 char *rep_dlerror(void);
278 #define dlopen rep_dlopen
279 #ifdef DLOPEN_TAKES_UNSIGNED_FLAGS
280 void *rep_dlopen(const char *name, unsigned int flags);
282 void *rep_dlopen(const char *name, int flags);
287 #define dlsym rep_dlsym
288 void *rep_dlsym(void *handle, const char *symbol);
292 #define dlclose rep_dlclose
293 int rep_dlclose(void *handle);
296 #ifndef HAVE_SOCKETPAIR
297 #define socketpair rep_socketpair
298 /* prototype is in system/network.h */
301 #ifndef PRINTF_ATTRIBUTE
302 #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 )
303 /** Use gcc attribute to check printf fns. a1 is the 1-based index of
304 * the parameter containing the format, and a2 the index of the first
305 * argument. Note that some gcc 2.x versions don't handle this
307 #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
309 #define PRINTF_ATTRIBUTE(a1, a2)
314 #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 )
315 #define _DEPRECATED_ __attribute__ ((deprecated))
321 #ifndef HAVE_VASPRINTF
322 #define vasprintf rep_vasprintf
323 int rep_vasprintf(char **ptr, const char *format, va_list ap) PRINTF_ATTRIBUTE(2,0);
326 #define slprintf snprintf
330 #ifdef HAVE___VA_COPY
331 #define va_copy(dest, src) __va_copy(dest, src)
333 #define va_copy(dest, src) (dest) = (src)
337 #ifndef HAVE_VOLATILE
341 #ifndef HAVE_COMPARISON_FN_T
342 typedef int (*comparison_fn_t)(const void *, const void *);
345 #ifdef REPLACE_STRPTIME
346 #define strptime rep_strptime
348 char *rep_strptime(const char *buf, const char *format, struct tm *tm);
352 #define dup2 rep_dup2
353 int rep_dup2(int oldfd, int newfd);
356 /* Load header file for dynamic linking stuff */
368 #define RTLD_GLOBAL 0
371 #ifndef HAVE_SECURE_MKSTEMP
372 #define mkstemp(path) rep_mkstemp(path)
373 int rep_mkstemp(char *temp);
377 #define mkdtemp rep_mkdtemp
378 char *rep_mkdtemp(char *template);
382 #define pread rep_pread
383 ssize_t rep_pread(int __fd, void *__buf, size_t __nbytes, off_t __offset);
384 #define LIBREPLACE_PREAD_REPLACED 1
386 #define LIBREPLACE_PREAD_NOT_REPLACED 1
390 #define pwrite rep_pwrite
391 ssize_t rep_pwrite(int __fd, const void *__buf, size_t __nbytes, off_t __offset);
392 #define LIBREPLACE_PWRITE_REPLACED 1
394 #define LIBREPLACE_PWRITE_NOT_REPLACED 1
397 #if !defined(HAVE_INET_NTOA) || defined(REPLACE_INET_NTOA)
398 #define inet_ntoa rep_inet_ntoa
399 /* prototype is in "system/network.h" */
402 #ifndef HAVE_INET_PTON
403 #define inet_pton rep_inet_pton
404 /* prototype is in "system/network.h" */
407 #ifndef HAVE_INET_NTOP
408 #define inet_ntop rep_inet_ntop
409 /* prototype is in "system/network.h" */
412 #ifndef HAVE_INET_ATON
413 #define inet_aton rep_inet_aton
414 /* prototype is in "system/network.h" */
418 #define connect rep_connect
419 /* prototype is in "system/network.h" */
422 #ifndef HAVE_GETHOSTBYNAME
423 #define gethostbyname rep_gethostbyname
424 /* prototype is in "system/network.h" */
427 #ifndef HAVE_GETIFADDRS
428 #define getifaddrs rep_getifaddrs
429 /* prototype is in "system/network.h" */
432 #ifndef HAVE_FREEIFADDRS
433 #define freeifaddrs rep_freeifaddrs
434 /* prototype is in "system/network.h" */
441 #ifdef HAVE_SYS_PARAM_H
442 #include <sys/param.h>
445 /* The extra casts work around common compiler bugs. */
446 #define _TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
447 /* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
448 It is necessary at least when t == time_t. */
449 #define _TYPE_MINIMUM(t) ((t) (_TYPE_SIGNED (t) \
450 ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) : (t) 0))
451 #define _TYPE_MAXIMUM(t) ((t) (~ (t) 0 - _TYPE_MINIMUM (t)))
454 #define UINT16_MAX 65535
458 #define UINT32_MAX (4294967295U)
462 #define UINT64_MAX ((uint64_t)-1)
470 #define INT32_MAX _TYPE_MAXIMUM(int32_t)
473 #ifdef HAVE_STDBOOL_H
477 #if !defined(HAVE_BOOL)
486 * to prevent <rpcsvc/yp_prot.h> from doing a redefine of 'bool'
488 * IRIX, HPUX, MacOS 10 and Solaris need BOOL_DEFINED
489 * Tru64 needs _BOOL_EXISTS
490 * AIX needs _BOOL,_TRUE,_FALSE
502 #ifndef __bool_true_false_are_defined
503 #define __bool_true_false_are_defined
520 #ifndef HAVE_FUNCTION_MACRO
521 #ifdef HAVE_func_MACRO
522 #define __FUNCTION__ __func__
524 #define __FUNCTION__ ("")
530 #define MIN(a,b) ((a)<(b)?(a):(b))
534 #define MAX(a,b) ((a)>(b)?(a):(b))
537 #if !defined(HAVE_VOLATILE)
542 this is a warning hack. The idea is to use this everywhere that we
543 get the "discarding const" warning from gcc. That doesn't actually
544 fix the problem of course, but it means that when we do get to
545 cleaning them up we can do it by searching the code for
548 It also means that other error types aren't as swamped by the noise
549 of hundreds of const warnings, so we are more likely to notice when
552 Please only add more uses of this macro when you find it
553 _really_ hard to fix const warnings. Our aim is to eventually use
554 this function in only a very few places.
556 Also, please call this via the discard_const_p() macro interface, as that
557 makes the return type safe.
559 #define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
561 /** Type-safe version of discard_const */
562 #define discard_const_p(type, ptr) ((type *)discard_const(ptr))
565 #define __STRING(x) #x
568 #ifndef __STRINGSTRING
569 #define __STRINGSTRING(x) __STRING(x)
573 #define __LINESTR__ __STRINGSTRING(__LINE__)
577 #define __location__ __FILE__ ":" __LINESTR__
583 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
586 * zero a structure given a pointer to the structure
588 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
591 * zero a structure given a pointer to the structure - no zero check
593 #define ZERO_STRUCTPN(x) memset((char *)(x), 0, sizeof(*(x)))
595 /* zero an array - note that sizeof(array) must work - ie. it must not be a
597 #define ZERO_ARRAY(x) memset((char *)(x), 0, sizeof(x))
600 * work out how many elements there are in a static array
602 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
605 * pointer difference macro
607 #define PTR_DIFF(p1,p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
614 #define QSORT_CAST (__compar_fn_t)
618 #define QSORT_CAST (int (*)(const void *, const void *))
622 #define PATH_MAX 1024
625 #ifndef MAX_DNS_NAME_LENGTH
626 #define MAX_DNS_NAME_LENGTH 256 /* Actually 255 but +1 for terminating null. */
630 char *ufc_crypt(const char *key, const char *salt);
631 #define crypt ufc_crypt
638 /* these macros gain us a few percent of speed on gcc */
640 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
641 as its first argument */
643 #define likely(x) __builtin_expect(!!(x), 1)
646 #define unlikely(x) __builtin_expect(!!(x), 0)
650 #define likely(x) (x)
653 #define unlikely(x) (x)
658 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
661 typedef uint32_t NTSTATUS;
662 #define NT_STATUS(x) (x)
663 #define NT_STATUS_V(x) (x)
665 /* These macros unify the keyblock handling of Heimdal and MIT somewhat */
666 #ifdef HAVE_KRB5_KEYBLOCK_KEYVALUE /* Heimdal */
667 #define KRB5_KEY_TYPE(k) ((k)->keytype)
668 #define KRB5_KEY_LENGTH(k) ((k)->keyvalue.length)
669 #define KRB5_KEY_DATA(k) ((k)->keyvalue.data)
670 #define KRB5_KEY_DATA_CAST void
672 #define KRB5_KEY_TYPE(k) ((k)->enctype)
673 #define KRB5_KEY_LENGTH(k) ((k)->length)
674 #define KRB5_KEY_DATA(k) ((k)->contents)
675 #define KRB5_KEY_DATA_CAST krb5_octet
678 #endif /* _LIBREPLACE_REPLACE_H */