e7578cd60faba83c928711a913c6103ed5ed8182
[samba.git] / lib / replace / replace.h
1 /*
2    Unix SMB/CIFS implementation.
3
4    macros to go along with the lib/replace/ portability layer code
5
6    Copyright (C) Andrew Tridgell 2005
7    Copyright (C) Jelmer Vernooij 2006-2008
8    Copyright (C) Jeremy Allison 2007.
9
10      ** NOTE! The following LGPL license applies to the replace
11      ** library. This does NOT imply that all of Samba is released
12      ** under the LGPL
13
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.
18
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.
23
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/>.
26 */
27
28 #ifndef _LIBREPLACE_REPLACE_H
29 #define _LIBREPLACE_REPLACE_H
30
31 #ifndef NO_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #ifdef HAVE_STANDARDS_H
36 #include <standards.h>
37 #endif
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <stdarg.h>
42 #include <errno.h>
43
44 #if defined(_MSC_VER) || defined(__MINGW32__)
45 #include "win32_replace.h"
46 #endif
47
48
49 #ifdef HAVE_STDINT_H
50 #include <stdint.h>
51 /* force off HAVE_INTTYPES_H so that roken doesn't try to include both,
52    which causes a warning storm on irix */
53 #undef HAVE_INTTYPES_H
54 #elif HAVE_INTTYPES_H
55 #define __STDC_FORMAT_MACROS
56 #include <inttypes.h>
57 #endif
58
59 #ifndef __PRI64_PREFIX
60 # if __WORDSIZE == 64
61 #  define __PRI64_PREFIX        "l"
62 # else
63 #  define __PRI64_PREFIX        "ll"
64 # endif
65 #endif
66
67 /* Decimal notation.  */
68 #ifndef PRId8
69 # define PRId8          "d"
70 #endif
71 #ifndef PRId16
72 # define PRId16         "d"
73 #endif
74 #ifndef PRId32
75 # define PRId32         "d"
76 #endif
77 #ifndef PRId64
78 # define PRId64         __PRI64_PREFIX "d"
79 #endif
80
81 #ifndef PRIi8
82 # define PRIi8          "i"
83 #endif
84 #ifndef PRIi8
85 # define PRIi16         "i"
86 #endif
87 #ifndef PRIi8
88 # define PRIi32         "i"
89 #endif
90 #ifndef PRIi8
91 # define PRIi64         __PRI64_PREFIX "i"
92 #endif
93
94 #ifndef PRIu8
95 # define PRIu8          "u"
96 #endif
97 #ifndef PRIu16
98 # define PRIu16         "u"
99 #endif
100 #ifndef PRIu32
101 # define PRIu32         "u"
102 #endif
103 #ifndef PRIu64
104 # define PRIu64         __PRI64_PREFIX "u"
105 #endif
106
107 #ifdef HAVE_STRING_H
108 #include <string.h>
109 #endif
110
111 #ifdef HAVE_STRINGS_H
112 #include <strings.h>
113 #endif
114
115 #ifdef HAVE_SYS_TYPES_H
116 #include <sys/types.h>
117 #endif
118
119 #if STDC_HEADERS
120 #include <stdlib.h>
121 #include <stddef.h>
122 #endif
123
124 #ifndef HAVE_STRERROR
125 extern char *sys_errlist[];
126 #define strerror(i) sys_errlist[i]
127 #endif
128
129 #ifndef HAVE_ERRNO_DECL
130 extern int errno;
131 #endif
132
133 #ifndef HAVE_STRDUP
134 #define strdup rep_strdup
135 char *rep_strdup(const char *s);
136 #endif
137
138 #ifndef HAVE_MEMMOVE
139 #define memmove rep_memmove
140 void *rep_memmove(void *dest,const void *src,int size);
141 #endif
142
143 #ifndef HAVE_MEMMEM
144 #define memmem rep_memmem
145 void *rep_memmem(const void *haystack, size_t haystacklen,
146                  const void *needle, size_t needlelen);
147 #endif
148
149 #ifndef HAVE_MKTIME
150 #define mktime rep_mktime
151 /* prototype is in "system/time.h" */
152 #endif
153
154 #ifndef HAVE_TIMEGM
155 #define timegm rep_timegm
156 /* prototype is in "system/time.h" */
157 #endif
158
159 #ifndef HAVE_UTIME
160 #define utime rep_utime
161 /* prototype is in "system/time.h" */
162 #endif
163
164 #ifndef HAVE_UTIMES
165 #define utimes rep_utimes
166 /* prototype is in "system/time.h" */
167 #endif
168
169 #ifndef HAVE_STRLCPY
170 #define strlcpy rep_strlcpy
171 size_t rep_strlcpy(char *d, const char *s, size_t bufsize);
172 #endif
173
174 #ifndef HAVE_STRLCAT
175 #define strlcat rep_strlcat
176 size_t rep_strlcat(char *d, const char *s, size_t bufsize);
177 #endif
178
179 #if (defined(BROKEN_STRNDUP) || !defined(HAVE_STRNDUP))
180 #undef HAVE_STRNDUP
181 #define strndup rep_strndup
182 char *rep_strndup(const char *s, size_t n);
183 #endif
184
185 #if (defined(BROKEN_STRNLEN) || !defined(HAVE_STRNLEN))
186 #undef HAVE_STRNLEN
187 #define strnlen rep_strnlen
188 size_t rep_strnlen(const char *s, size_t n);
189 #endif
190
191 #if !HAVE_DECL_ENVIRON
192 #ifdef __APPLE__
193 #include <crt_externs.h>
194 #define environ (*_NSGetEnviron())
195 #else
196 extern char **environ;
197 #endif
198 #endif
199
200 #ifndef HAVE_SETENV
201 #define setenv rep_setenv
202 int rep_setenv(const char *name, const char *value, int overwrite);
203 #else
204 #ifndef HAVE_SETENV_DECL
205 int setenv(const char *name, const char *value, int overwrite);
206 #endif
207 #endif
208
209 #ifndef HAVE_UNSETENV
210 #define unsetenv rep_unsetenv
211 int rep_unsetenv(const char *name);
212 #endif
213
214 #ifndef HAVE_SETEUID
215 #define seteuid rep_seteuid
216 int rep_seteuid(uid_t);
217 #endif
218
219 #ifndef HAVE_SETEGID
220 #define setegid rep_setegid
221 int rep_setegid(gid_t);
222 #endif
223
224 #if (defined(USE_SETRESUID) && !defined(HAVE_SETRESUID_DECL))
225 /* stupid glibc */
226 int setresuid(uid_t ruid, uid_t euid, uid_t suid);
227 #endif
228 #if (defined(USE_SETRESUID) && !defined(HAVE_SETRESGID_DECL))
229 int setresgid(gid_t rgid, gid_t egid, gid_t sgid);
230 #endif
231
232 #ifndef HAVE_CHOWN
233 #define chown rep_chown
234 int rep_chown(const char *path, uid_t uid, gid_t gid);
235 #endif
236
237 #ifndef HAVE_CHROOT
238 #define chroot rep_chroot
239 int rep_chroot(const char *dirname);
240 #endif
241
242 #ifndef HAVE_LINK
243 #define link rep_link
244 int rep_link(const char *oldpath, const char *newpath);
245 #endif
246
247 #ifndef HAVE_READLINK
248 #define readlink rep_readlink
249 ssize_t rep_readlink(const char *path, char *buf, size_t bufsize);
250 #endif
251
252 #ifndef HAVE_SYMLINK
253 #define symlink rep_symlink
254 int rep_symlink(const char *oldpath, const char *newpath);
255 #endif
256
257 #ifndef HAVE_REALPATH
258 #define realpath rep_realpath
259 char *rep_realpath(const char *path, char *resolved_path);
260 #endif
261
262 #ifndef HAVE_LCHOWN
263 #define lchown rep_lchown
264 int rep_lchown(const char *fname,uid_t uid,gid_t gid);
265 #endif
266
267 #ifdef HAVE_UNIX_H
268 #include <unix.h>
269 #endif
270
271 #ifndef HAVE_SETLINEBUF
272 #define setlinebuf rep_setlinebuf
273 void rep_setlinebuf(FILE *);
274 #endif
275
276 #ifndef HAVE_STRCASESTR
277 #define strcasestr rep_strcasestr
278 char *rep_strcasestr(const char *haystack, const char *needle);
279 #endif
280
281 #ifndef HAVE_STRTOK_R
282 #define strtok_r rep_strtok_r
283 char *rep_strtok_r(char *s, const char *delim, char **save_ptr);
284 #endif
285
286 #ifndef HAVE_STRTOLL
287 #define strtoll rep_strtoll
288 long long int rep_strtoll(const char *str, char **endptr, int base);
289 #endif
290
291 #ifndef HAVE_STRTOULL
292 #define strtoull rep_strtoull
293 unsigned long long int rep_strtoull(const char *str, char **endptr, int base);
294 #endif
295
296 #ifndef HAVE_FTRUNCATE
297 #define ftruncate rep_ftruncate
298 int rep_ftruncate(int,off_t);
299 #endif
300
301 #ifndef HAVE_INITGROUPS
302 #define initgroups rep_initgroups
303 int rep_initgroups(char *name, gid_t id);
304 #endif
305
306 #if !defined(HAVE_BZERO) && defined(HAVE_MEMSET)
307 #define bzero(a,b) memset((a),'\0',(b))
308 #endif
309
310 #ifndef HAVE_DLERROR
311 #define dlerror rep_dlerror
312 char *rep_dlerror(void);
313 #endif
314
315 #ifndef HAVE_DLOPEN
316 #define dlopen rep_dlopen
317 #ifdef DLOPEN_TAKES_UNSIGNED_FLAGS
318 void *rep_dlopen(const char *name, unsigned int flags);
319 #else
320 void *rep_dlopen(const char *name, int flags);
321 #endif
322 #endif
323
324 #ifndef HAVE_DLSYM
325 #define dlsym rep_dlsym
326 void *rep_dlsym(void *handle, const char *symbol);
327 #endif
328
329 #ifndef HAVE_DLCLOSE
330 #define dlclose rep_dlclose
331 int rep_dlclose(void *handle);
332 #endif
333
334 #ifndef HAVE_SOCKETPAIR
335 #define socketpair rep_socketpair
336 /* prototype is in system/network.h */
337 #endif
338
339 #ifndef PRINTF_ATTRIBUTE
340 #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 )
341 /** Use gcc attribute to check printf fns.  a1 is the 1-based index of
342  * the parameter containing the format, and a2 the index of the first
343  * argument. Note that some gcc 2.x versions don't handle this
344  * properly **/
345 #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
346 #else
347 #define PRINTF_ATTRIBUTE(a1, a2)
348 #endif
349 #endif
350
351 #ifndef _DEPRECATED_
352 #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 )
353 #define _DEPRECATED_ __attribute__ ((deprecated))
354 #else
355 #define _DEPRECATED_
356 #endif
357 #endif
358
359 #ifndef HAVE_VASPRINTF
360 #define vasprintf rep_vasprintf
361 int rep_vasprintf(char **ptr, const char *format, va_list ap) PRINTF_ATTRIBUTE(2,0);
362 #endif
363
364 #if !defined(HAVE_SNPRINTF) || !defined(HAVE_C99_VSNPRINTF)
365 #define snprintf rep_snprintf
366 int rep_snprintf(char *,size_t ,const char *, ...) PRINTF_ATTRIBUTE(3,4);
367 #endif
368
369 #if !defined(HAVE_VSNPRINTF) || !defined(HAVE_C99_VSNPRINTF)
370 #define vsnprintf rep_vsnprintf
371 int rep_vsnprintf(char *,size_t ,const char *, va_list ap) PRINTF_ATTRIBUTE(3,0);
372 #endif
373
374 #ifndef HAVE_ASPRINTF
375 #define asprintf rep_asprintf
376 int rep_asprintf(char **,const char *, ...) PRINTF_ATTRIBUTE(2,3);
377 #endif
378
379 #ifndef HAVE_VSYSLOG
380 #ifdef HAVE_SYSLOG
381 #define vsyslog rep_vsyslog
382 void rep_vsyslog (int facility_priority, const char *format, va_list arglist) PRINTF_ATTRIBUTE(2,0);
383 #endif
384 #endif
385
386 /* we used to use these fns, but now we have good replacements
387    for snprintf and vsnprintf */
388 #define slprintf snprintf
389
390
391 #ifndef HAVE_VA_COPY
392 #undef va_copy
393 #ifdef HAVE___VA_COPY
394 #define va_copy(dest, src) __va_copy(dest, src)
395 #else
396 #define va_copy(dest, src) (dest) = (src)
397 #endif
398 #endif
399
400 #ifndef HAVE_VOLATILE
401 #define volatile
402 #endif
403
404 #ifndef HAVE_COMPARISON_FN_T
405 typedef int (*comparison_fn_t)(const void *, const void *);
406 #endif
407
408 #ifdef REPLACE_STRPTIME
409 #define strptime rep_strptime
410 struct tm;
411 char *rep_strptime(const char *buf, const char *format, struct tm *tm);
412 #endif
413
414 #ifndef HAVE_DUP2
415 #define dup2 rep_dup2
416 int rep_dup2(int oldfd, int newfd);
417 #endif
418
419 /* Load header file for dynamic linking stuff */
420 #ifdef HAVE_DLFCN_H
421 #include <dlfcn.h>
422 #endif
423
424 #ifndef RTLD_LAZY
425 #define RTLD_LAZY 0
426 #endif
427 #ifndef RTLD_NOW
428 #define RTLD_NOW 0
429 #endif
430 #ifndef RTLD_GLOBAL
431 #define RTLD_GLOBAL 0
432 #endif
433
434 #ifndef HAVE_SECURE_MKSTEMP
435 #define mkstemp(path) rep_mkstemp(path)
436 int rep_mkstemp(char *temp);
437 #endif
438
439 #ifndef HAVE_MKDTEMP
440 #define mkdtemp rep_mkdtemp
441 char *rep_mkdtemp(char *template);
442 #endif
443
444 #ifndef HAVE_PREAD
445 #define pread rep_pread
446 ssize_t rep_pread(int __fd, void *__buf, size_t __nbytes, off_t __offset);
447 #define LIBREPLACE_PREAD_REPLACED 1
448 #else
449 #define LIBREPLACE_PREAD_NOT_REPLACED 1
450 #endif
451
452 #ifndef HAVE_PWRITE
453 #define pwrite rep_pwrite
454 ssize_t rep_pwrite(int __fd, const void *__buf, size_t __nbytes, off_t __offset);
455 #define LIBREPLACE_PWRITE_REPLACED 1
456 #else
457 #define LIBREPLACE_PWRITE_NOT_REPLACED 1
458 #endif
459
460 #if !defined(HAVE_INET_NTOA) || defined(REPLACE_INET_NTOA)
461 #define inet_ntoa rep_inet_ntoa
462 /* prototype is in "system/network.h" */
463 #endif
464
465 #ifndef HAVE_INET_PTON
466 #define inet_pton rep_inet_pton
467 /* prototype is in "system/network.h" */
468 #endif
469
470 #ifndef HAVE_INET_NTOP
471 #define inet_ntop rep_inet_ntop
472 /* prototype is in "system/network.h" */
473 #endif
474
475 #ifndef HAVE_INET_ATON
476 #define inet_aton rep_inet_aton
477 /* prototype is in "system/network.h" */
478 #endif
479
480 #ifndef HAVE_CONNECT
481 #define connect rep_connect
482 /* prototype is in "system/network.h" */
483 #endif
484
485 #ifndef HAVE_GETHOSTBYNAME
486 #define gethostbyname rep_gethostbyname
487 /* prototype is in "system/network.h" */
488 #endif
489
490 #ifndef HAVE_GETIFADDRS
491 #define getifaddrs rep_getifaddrs
492 /* prototype is in "system/network.h" */
493 #endif
494
495 #ifndef HAVE_FREEIFADDRS
496 #define freeifaddrs rep_freeifaddrs
497 /* prototype is in "system/network.h" */
498 #endif
499
500 #ifndef HAVE_GET_CURRENT_DIR_NAME
501 #define get_current_dir_name rep_get_current_dir_name
502 char *rep_get_current_dir_name(void);
503 #endif
504
505 #ifdef HAVE_LIMITS_H
506 #include <limits.h>
507 #endif
508
509 #ifdef HAVE_SYS_PARAM_H
510 #include <sys/param.h>
511 #endif
512
513 /* The extra casts work around common compiler bugs.  */
514 #define _TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
515 /* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
516    It is necessary at least when t == time_t.  */
517 #define _TYPE_MINIMUM(t) ((t) (_TYPE_SIGNED (t) \
518                               ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) : (t) 0))
519 #define _TYPE_MAXIMUM(t) ((t) (~ (t) 0 - _TYPE_MINIMUM (t)))
520
521 #ifndef UINT16_MAX
522 #define UINT16_MAX 65535
523 #endif
524
525 #ifndef UINT32_MAX
526 #define UINT32_MAX (4294967295U)
527 #endif
528
529 #ifndef UINT64_MAX
530 #define UINT64_MAX ((uint64_t)-1)
531 #endif
532
533 #ifndef CHAR_BIT
534 #define CHAR_BIT 8
535 #endif
536
537 #ifndef INT32_MAX
538 #define INT32_MAX _TYPE_MAXIMUM(int32_t)
539 #endif
540
541 #ifdef HAVE_STDBOOL_H
542 #include <stdbool.h>
543 #endif
544
545 #if !defined(HAVE_BOOL)
546 #ifdef HAVE__Bool
547 #define bool _Bool
548 #else
549 typedef int bool;
550 #endif
551 #endif
552
553 #if !defined(HAVE_INTPTR_T)
554 typedef long long intptr_t ;
555 #endif
556
557 #if !defined(HAVE_UINTPTR_T)
558 typedef unsigned long long uintptr_t ;
559 #endif
560
561 #if !defined(HAVE_PTRDIFF_T)
562 typedef unsigned long long ptrdiff_t ;
563 #endif
564
565 /*
566  * to prevent <rpcsvc/yp_prot.h> from doing a redefine of 'bool'
567  *
568  * IRIX, HPUX, MacOS 10 and Solaris need BOOL_DEFINED
569  * Tru64 needs _BOOL_EXISTS
570  * AIX needs _BOOL,_TRUE,_FALSE
571  */
572 #ifndef BOOL_DEFINED
573 #define BOOL_DEFINED
574 #endif
575 #ifndef _BOOL_EXISTS
576 #define _BOOL_EXISTS
577 #endif
578 #ifndef _BOOL
579 #define _BOOL
580 #endif
581
582 #ifndef __bool_true_false_are_defined
583 #define __bool_true_false_are_defined
584 #endif
585
586 #ifndef true
587 #define true (1)
588 #endif
589 #ifndef false
590 #define false (0)
591 #endif
592
593 #ifndef _TRUE
594 #define _TRUE true
595 #endif
596 #ifndef _FALSE
597 #define _FALSE false
598 #endif
599
600 #ifndef HAVE_FUNCTION_MACRO
601 #ifdef HAVE_func_MACRO
602 #define __FUNCTION__ __func__
603 #else
604 #define __FUNCTION__ ("")
605 #endif
606 #endif
607
608
609 #ifndef MIN
610 #define MIN(a,b) ((a)<(b)?(a):(b))
611 #endif
612
613 #ifndef MAX
614 #define MAX(a,b) ((a)>(b)?(a):(b))
615 #endif
616
617 #if !defined(HAVE_VOLATILE)
618 #define volatile
619 #endif
620
621 /**
622   this is a warning hack. The idea is to use this everywhere that we
623   get the "discarding const" warning from gcc. That doesn't actually
624   fix the problem of course, but it means that when we do get to
625   cleaning them up we can do it by searching the code for
626   discard_const.
627
628   It also means that other error types aren't as swamped by the noise
629   of hundreds of const warnings, so we are more likely to notice when
630   we get new errors.
631
632   Please only add more uses of this macro when you find it
633   _really_ hard to fix const warnings. Our aim is to eventually use
634   this function in only a very few places.
635
636   Also, please call this via the discard_const_p() macro interface, as that
637   makes the return type safe.
638 */
639 #define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
640
641 /** Type-safe version of discard_const */
642 #define discard_const_p(type, ptr) ((type *)discard_const(ptr))
643
644 #ifndef __STRING
645 #define __STRING(x)    #x
646 #endif
647
648 #ifndef __STRINGSTRING
649 #define __STRINGSTRING(x) __STRING(x)
650 #endif
651
652 #ifndef __LINESTR__
653 #define __LINESTR__ __STRINGSTRING(__LINE__)
654 #endif
655
656 #ifndef __location__
657 #define __location__ __FILE__ ":" __LINESTR__
658 #endif
659
660 /** 
661  * zero a structure 
662  */
663 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
664
665 /** 
666  * zero a structure given a pointer to the structure 
667  */
668 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
669
670 /** 
671  * zero a structure given a pointer to the structure - no zero check 
672  */
673 #define ZERO_STRUCTPN(x) memset((char *)(x), 0, sizeof(*(x)))
674
675 /* zero an array - note that sizeof(array) must work - ie. it must not be a
676    pointer */
677 #define ZERO_ARRAY(x) memset((char *)(x), 0, sizeof(x))
678
679 /**
680  * work out how many elements there are in a static array 
681  */
682 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
683
684 /** 
685  * pointer difference macro 
686  */
687 #define PTR_DIFF(p1,p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
688
689 #if MMAP_BLACKLIST
690 #undef HAVE_MMAP
691 #endif
692
693 #ifdef __COMPAR_FN_T
694 #define QSORT_CAST (__compar_fn_t)
695 #endif
696
697 #ifndef QSORT_CAST
698 #define QSORT_CAST (int (*)(const void *, const void *))
699 #endif
700
701 #ifndef PATH_MAX
702 #define PATH_MAX 1024
703 #endif
704
705 #ifndef MAX_DNS_NAME_LENGTH
706 #define MAX_DNS_NAME_LENGTH 256 /* Actually 255 but +1 for terminating null. */
707 #endif
708
709 #ifndef HAVE_CRYPT
710 char *ufc_crypt(const char *key, const char *salt);
711 #define crypt ufc_crypt
712 #else
713 #ifdef HAVE_CRYPT_H
714 #include <crypt.h>
715 #endif
716 #endif
717
718 /* these macros gain us a few percent of speed on gcc */
719 #if (__GNUC__ >= 3)
720 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
721    as its first argument */
722 #ifndef likely
723 #define likely(x)   __builtin_expect(!!(x), 1)
724 #endif
725 #ifndef unlikely
726 #define unlikely(x) __builtin_expect(!!(x), 0)
727 #endif
728 #else
729 #ifndef likely
730 #define likely(x) (x)
731 #endif
732 #ifndef unlikely
733 #define unlikely(x) (x)
734 #endif
735 #endif
736
737 #ifndef HAVE_FDATASYNC
738 #define fdatasync(fd) fsync(fd)
739 #endif
740
741 #endif /* _LIBREPLACE_REPLACE_H */