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]
128 #define strdup rep_strdup
129 char *rep_strdup(const char *s);
133 #define memmem rep_memmem
134 void *rep_memmem(const void *haystack, size_t haystacklen,
135 const void *needle, size_t needlelen);
139 #define mktime rep_mktime
140 /* prototype is in "system/time.h" */
144 #define timegm rep_timegm
145 /* prototype is in "system/time.h" */
149 #define utime rep_utime
150 /* prototype is in "system/time.h" */
154 #define utimes rep_utimes
155 /* prototype is in "system/time.h" */
158 #if !HAVE_DECL_ENVIRON
160 #include <crt_externs.h>
161 #define environ (*_NSGetEnviron())
163 extern char **environ;
168 #define setenv rep_setenv
169 int rep_setenv(const char *name, const char *value, int overwrite);
171 #ifndef HAVE_SETENV_DECL
172 int setenv(const char *name, const char *value, int overwrite);
176 #ifndef HAVE_UNSETENV
177 #define unsetenv rep_unsetenv
178 int rep_unsetenv(const char *name);
182 #define seteuid rep_seteuid
183 int rep_seteuid(uid_t);
187 #define setegid rep_setegid
188 int rep_setegid(gid_t);
191 #if (defined(USE_SETRESUID) && !defined(HAVE_SETRESUID_DECL))
193 int setresuid(uid_t ruid, uid_t euid, uid_t suid);
195 #if (defined(USE_SETRESUID) && !defined(HAVE_SETRESGID_DECL))
196 int setresgid(gid_t rgid, gid_t egid, gid_t sgid);
200 #define chown rep_chown
201 int rep_chown(const char *path, uid_t uid, gid_t gid);
205 #define chroot rep_chroot
206 int rep_chroot(const char *dirname);
210 #define link rep_link
211 int rep_link(const char *oldpath, const char *newpath);
214 #ifndef HAVE_READLINK
215 #define readlink rep_readlink
216 ssize_t rep_readlink(const char *path, char *buf, size_t bufsize);
220 #define symlink rep_symlink
221 int rep_symlink(const char *oldpath, const char *newpath);
224 #ifndef HAVE_REALPATH
225 #define realpath rep_realpath
226 char *rep_realpath(const char *path, char *resolved_path);
230 #define lchown rep_lchown
231 int rep_lchown(const char *fname,uid_t uid,gid_t gid);
238 #ifndef HAVE_SETLINEBUF
239 #define setlinebuf rep_setlinebuf
240 void rep_setlinebuf(FILE *);
243 #ifndef HAVE_STRCASESTR
244 #define strcasestr rep_strcasestr
245 char *rep_strcasestr(const char *haystack, const char *needle);
249 #define strtoll rep_strtoll
250 long long int rep_strtoll(const char *str, char **endptr, int base);
253 #ifndef HAVE_STRTOULL
254 #define strtoull rep_strtoull
255 unsigned long long int rep_strtoull(const char *str, char **endptr, int base);
258 #ifndef HAVE_FTRUNCATE
259 #define ftruncate rep_ftruncate
260 int rep_ftruncate(int,off_t);
263 #ifndef HAVE_INITGROUPS
264 #define initgroups rep_initgroups
265 int rep_initgroups(char *name, gid_t id);
269 #define dlerror rep_dlerror
270 char *rep_dlerror(void);
274 #define dlopen rep_dlopen
275 #ifdef DLOPEN_TAKES_UNSIGNED_FLAGS
276 void *rep_dlopen(const char *name, unsigned int flags);
278 void *rep_dlopen(const char *name, int flags);
283 #define dlsym rep_dlsym
284 void *rep_dlsym(void *handle, const char *symbol);
288 #define dlclose rep_dlclose
289 int rep_dlclose(void *handle);
292 #ifndef HAVE_SOCKETPAIR
293 #define socketpair rep_socketpair
294 /* prototype is in system/network.h */
297 #ifndef PRINTF_ATTRIBUTE
298 #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 )
299 /** Use gcc attribute to check printf fns. a1 is the 1-based index of
300 * the parameter containing the format, and a2 the index of the first
301 * argument. Note that some gcc 2.x versions don't handle this
303 #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
305 #define PRINTF_ATTRIBUTE(a1, a2)
310 #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 )
311 #define _DEPRECATED_ __attribute__ ((deprecated))
317 #ifndef HAVE_VASPRINTF
318 #define vasprintf rep_vasprintf
319 int rep_vasprintf(char **ptr, const char *format, va_list ap) PRINTF_ATTRIBUTE(2,0);
322 #define slprintf snprintf
326 #ifdef HAVE___VA_COPY
327 #define va_copy(dest, src) __va_copy(dest, src)
329 #define va_copy(dest, src) (dest) = (src)
333 #ifndef HAVE_VOLATILE
337 #ifndef HAVE_COMPARISON_FN_T
338 typedef int (*comparison_fn_t)(const void *, const void *);
341 #ifdef REPLACE_STRPTIME
342 #define strptime rep_strptime
344 char *rep_strptime(const char *buf, const char *format, struct tm *tm);
348 #define dup2 rep_dup2
349 int rep_dup2(int oldfd, int newfd);
352 /* Load header file for dynamic linking stuff */
364 #define RTLD_GLOBAL 0
367 #ifndef HAVE_SECURE_MKSTEMP
368 #define mkstemp(path) rep_mkstemp(path)
369 int rep_mkstemp(char *temp);
373 #define mkdtemp rep_mkdtemp
374 char *rep_mkdtemp(char *template);
378 #define pread rep_pread
379 ssize_t rep_pread(int __fd, void *__buf, size_t __nbytes, off_t __offset);
380 #define LIBREPLACE_PREAD_REPLACED 1
382 #define LIBREPLACE_PREAD_NOT_REPLACED 1
386 #define pwrite rep_pwrite
387 ssize_t rep_pwrite(int __fd, const void *__buf, size_t __nbytes, off_t __offset);
388 #define LIBREPLACE_PWRITE_REPLACED 1
390 #define LIBREPLACE_PWRITE_NOT_REPLACED 1
393 #if !defined(HAVE_INET_NTOA) || defined(REPLACE_INET_NTOA)
394 #define inet_ntoa rep_inet_ntoa
395 /* prototype is in "system/network.h" */
398 #ifndef HAVE_INET_PTON
399 #define inet_pton rep_inet_pton
400 /* prototype is in "system/network.h" */
403 #ifndef HAVE_INET_NTOP
404 #define inet_ntop rep_inet_ntop
405 /* prototype is in "system/network.h" */
408 #ifndef HAVE_INET_ATON
409 #define inet_aton rep_inet_aton
410 /* prototype is in "system/network.h" */
414 #define connect rep_connect
415 /* prototype is in "system/network.h" */
418 #ifndef HAVE_GETHOSTBYNAME
419 #define gethostbyname rep_gethostbyname
420 /* prototype is in "system/network.h" */
423 #ifndef HAVE_GETIFADDRS
424 #define getifaddrs rep_getifaddrs
425 /* prototype is in "system/network.h" */
428 #ifndef HAVE_FREEIFADDRS
429 #define freeifaddrs rep_freeifaddrs
430 /* prototype is in "system/network.h" */
437 #ifdef HAVE_SYS_PARAM_H
438 #include <sys/param.h>
441 /* The extra casts work around common compiler bugs. */
442 #define _TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
443 /* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
444 It is necessary at least when t == time_t. */
445 #define _TYPE_MINIMUM(t) ((t) (_TYPE_SIGNED (t) \
446 ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) : (t) 0))
447 #define _TYPE_MAXIMUM(t) ((t) (~ (t) 0 - _TYPE_MINIMUM (t)))
450 #define UINT16_MAX 65535
454 #define UINT32_MAX (4294967295U)
458 #define UINT64_MAX ((uint64_t)-1)
466 #define INT32_MAX _TYPE_MAXIMUM(int32_t)
469 #ifdef HAVE_STDBOOL_H
473 #if !defined(HAVE_BOOL)
482 * to prevent <rpcsvc/yp_prot.h> from doing a redefine of 'bool'
484 * IRIX, HPUX, MacOS 10 and Solaris need BOOL_DEFINED
485 * Tru64 needs _BOOL_EXISTS
486 * AIX needs _BOOL,_TRUE,_FALSE
498 #ifndef __bool_true_false_are_defined
499 #define __bool_true_false_are_defined
516 #ifndef HAVE_FUNCTION_MACRO
517 #ifdef HAVE_func_MACRO
518 #define __FUNCTION__ __func__
520 #define __FUNCTION__ ("")
526 #define MIN(a,b) ((a)<(b)?(a):(b))
530 #define MAX(a,b) ((a)>(b)?(a):(b))
533 #if !defined(HAVE_VOLATILE)
538 this is a warning hack. The idea is to use this everywhere that we
539 get the "discarding const" warning from gcc. That doesn't actually
540 fix the problem of course, but it means that when we do get to
541 cleaning them up we can do it by searching the code for
544 It also means that other error types aren't as swamped by the noise
545 of hundreds of const warnings, so we are more likely to notice when
548 Please only add more uses of this macro when you find it
549 _really_ hard to fix const warnings. Our aim is to eventually use
550 this function in only a very few places.
552 Also, please call this via the discard_const_p() macro interface, as that
553 makes the return type safe.
555 #define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
557 /** Type-safe version of discard_const */
558 #define discard_const_p(type, ptr) ((type *)discard_const(ptr))
561 #define __STRING(x) #x
564 #ifndef __STRINGSTRING
565 #define __STRINGSTRING(x) __STRING(x)
569 #define __LINESTR__ __STRINGSTRING(__LINE__)
573 #define __location__ __FILE__ ":" __LINESTR__
579 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
582 * zero a structure given a pointer to the structure
584 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
587 * zero a structure given a pointer to the structure - no zero check
589 #define ZERO_STRUCTPN(x) memset((char *)(x), 0, sizeof(*(x)))
591 /* zero an array - note that sizeof(array) must work - ie. it must not be a
593 #define ZERO_ARRAY(x) memset((char *)(x), 0, sizeof(x))
596 * work out how many elements there are in a static array
598 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
601 * pointer difference macro
603 #define PTR_DIFF(p1,p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
610 #define QSORT_CAST (__compar_fn_t)
614 #define QSORT_CAST (int (*)(const void *, const void *))
618 #define PATH_MAX 1024
621 #ifndef MAX_DNS_NAME_LENGTH
622 #define MAX_DNS_NAME_LENGTH 256 /* Actually 255 but +1 for terminating null. */
626 char *ufc_crypt(const char *key, const char *salt);
627 #define crypt ufc_crypt
634 /* these macros gain us a few percent of speed on gcc */
636 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
637 as its first argument */
639 #define likely(x) __builtin_expect(!!(x), 1)
642 #define unlikely(x) __builtin_expect(!!(x), 0)
646 #define likely(x) (x)
649 #define unlikely(x) (x)
654 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
657 typedef uint32_t NTSTATUS;
658 #define NT_STATUS(x) (x)
659 #define NT_STATUS_V(x) (x)
661 /* These macros unify the keyblock handling of Heimdal and MIT somewhat */
662 #ifdef HAVE_KRB5_KEYBLOCK_KEYVALUE /* Heimdal */
663 #define KRB5_KEY_TYPE(k) ((k)->keytype)
664 #define KRB5_KEY_LENGTH(k) ((k)->keyvalue.length)
665 #define KRB5_KEY_DATA(k) ((k)->keyvalue.data)
666 #define KRB5_KEY_DATA_CAST void
668 #define KRB5_KEY_TYPE(k) ((k)->enctype)
669 #define KRB5_KEY_LENGTH(k) ((k)->length)
670 #define KRB5_KEY_DATA(k) ((k)->contents)
671 #define KRB5_KEY_DATA_CAST krb5_octet
674 #endif /* _LIBREPLACE_REPLACE_H */