mount.cifs: fix bad free() of string returned by dirname()
[jlayton/cifs-utils.git] / 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 #include "config.h"
32
33 #ifdef HAVE_STANDARDS_H
34 #include <standards.h>
35 #endif
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <stdarg.h>
40 #include <errno.h>
41
42 #if defined(_MSC_VER) || defined(__MINGW32__)
43 #include "win32_replace.h"
44 #endif
45
46
47 #ifdef HAVE_STDINT_H
48 #include <stdint.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
52 #elif HAVE_INTTYPES_H
53 #define __STDC_FORMAT_MACROS
54 #include <inttypes.h>
55 #endif
56
57 #ifndef __PRI64_PREFIX
58 # if __WORDSIZE == 64
59 #  define __PRI64_PREFIX        "l"
60 # else
61 #  define __PRI64_PREFIX        "ll"
62 # endif
63 #endif
64
65 /* Decimal notation.  */
66 #ifndef PRId8
67 # define PRId8          "d"
68 #endif
69 #ifndef PRId16
70 # define PRId16         "d"
71 #endif
72 #ifndef PRId32
73 # define PRId32         "d"
74 #endif
75 #ifndef PRId64
76 # define PRId64         __PRI64_PREFIX "d"
77 #endif
78
79 #ifndef PRIi8
80 # define PRIi8          "i"
81 #endif
82 #ifndef PRIi8
83 # define PRIi16         "i"
84 #endif
85 #ifndef PRIi8
86 # define PRIi32         "i"
87 #endif
88 #ifndef PRIi8
89 # define PRIi64         __PRI64_PREFIX "i"
90 #endif
91
92 #ifndef PRIu8
93 # define PRIu8          "u"
94 #endif
95 #ifndef PRIu16
96 # define PRIu16         "u"
97 #endif
98 #ifndef PRIu32
99 # define PRIu32         "u"
100 #endif
101 #ifndef PRIu64
102 # define PRIu64         __PRI64_PREFIX "u"
103 #endif
104
105 #ifdef HAVE_STRING_H
106 #include <string.h>
107 #endif
108
109 #ifdef HAVE_STRINGS_H
110 #include <strings.h>
111 #endif
112
113 #ifdef HAVE_SYS_TYPES_H
114 #include <sys/types.h>
115 #endif
116
117 #if STDC_HEADERS
118 #include <stdlib.h>
119 #include <stddef.h>
120 #endif
121
122 #ifndef HAVE_STRERROR
123 extern char *sys_errlist[];
124 #define strerror(i) sys_errlist[i]
125 #endif
126
127 #ifndef HAVE_STRDUP
128 #define strdup rep_strdup
129 char *rep_strdup(const char *s);
130 #endif
131
132 #ifndef HAVE_MEMMEM
133 #define memmem rep_memmem
134 void *rep_memmem(const void *haystack, size_t haystacklen,
135                  const void *needle, size_t needlelen);
136 #endif
137
138 #ifndef HAVE_MKTIME
139 #define mktime rep_mktime
140 /* prototype is in "system/time.h" */
141 #endif
142
143 #ifndef HAVE_TIMEGM
144 #define timegm rep_timegm
145 /* prototype is in "system/time.h" */
146 #endif
147
148 #ifndef HAVE_UTIME
149 #define utime rep_utime
150 /* prototype is in "system/time.h" */
151 #endif
152
153 #ifndef HAVE_UTIMES
154 #define utimes rep_utimes
155 /* prototype is in "system/time.h" */
156 #endif
157
158 #if !HAVE_DECL_ENVIRON
159 #ifdef __APPLE__
160 #include <crt_externs.h>
161 #define environ (*_NSGetEnviron())
162 #else
163 extern char **environ;
164 #endif
165 #endif
166
167 #ifndef HAVE_SETENV
168 #define setenv rep_setenv
169 int rep_setenv(const char *name, const char *value, int overwrite);
170 #else
171 #ifndef HAVE_SETENV_DECL
172 int setenv(const char *name, const char *value, int overwrite);
173 #endif
174 #endif
175
176 #ifndef HAVE_UNSETENV
177 #define unsetenv rep_unsetenv
178 int rep_unsetenv(const char *name);
179 #endif
180
181 #ifndef HAVE_SETEUID
182 #define seteuid rep_seteuid
183 int rep_seteuid(uid_t);
184 #endif
185
186 #ifndef HAVE_SETEGID
187 #define setegid rep_setegid
188 int rep_setegid(gid_t);
189 #endif
190
191 #if (defined(USE_SETRESUID) && !defined(HAVE_SETRESUID_DECL))
192 /* stupid glibc */
193 int setresuid(uid_t ruid, uid_t euid, uid_t suid);
194 #endif
195 #if (defined(USE_SETRESUID) && !defined(HAVE_SETRESGID_DECL))
196 int setresgid(gid_t rgid, gid_t egid, gid_t sgid);
197 #endif
198
199 #ifndef HAVE_CHOWN
200 #define chown rep_chown
201 int rep_chown(const char *path, uid_t uid, gid_t gid);
202 #endif
203
204 #ifndef HAVE_CHROOT
205 #define chroot rep_chroot
206 int rep_chroot(const char *dirname);
207 #endif
208
209 #ifndef HAVE_LINK
210 #define link rep_link
211 int rep_link(const char *oldpath, const char *newpath);
212 #endif
213
214 #ifndef HAVE_READLINK
215 #define readlink rep_readlink
216 ssize_t rep_readlink(const char *path, char *buf, size_t bufsize);
217 #endif
218
219 #ifndef HAVE_SYMLINK
220 #define symlink rep_symlink
221 int rep_symlink(const char *oldpath, const char *newpath);
222 #endif
223
224 #ifndef HAVE_REALPATH
225 #define realpath rep_realpath
226 char *rep_realpath(const char *path, char *resolved_path);
227 #endif
228
229 #ifndef HAVE_LCHOWN
230 #define lchown rep_lchown
231 int rep_lchown(const char *fname,uid_t uid,gid_t gid);
232 #endif
233
234 #ifdef HAVE_UNIX_H
235 #include <unix.h>
236 #endif
237
238 #ifndef HAVE_SETLINEBUF
239 #define setlinebuf rep_setlinebuf
240 void rep_setlinebuf(FILE *);
241 #endif
242
243 #ifndef HAVE_STRCASESTR
244 #define strcasestr rep_strcasestr
245 char *rep_strcasestr(const char *haystack, const char *needle);
246 #endif
247
248 #ifndef HAVE_STRTOLL
249 #define strtoll rep_strtoll
250 long long int rep_strtoll(const char *str, char **endptr, int base);
251 #endif
252
253 #ifndef HAVE_STRTOULL
254 #define strtoull rep_strtoull
255 unsigned long long int rep_strtoull(const char *str, char **endptr, int base);
256 #endif
257
258 #ifndef HAVE_FTRUNCATE
259 #define ftruncate rep_ftruncate
260 int rep_ftruncate(int,off_t);
261 #endif
262
263 #ifndef HAVE_INITGROUPS
264 #define initgroups rep_initgroups
265 int rep_initgroups(char *name, gid_t id);
266 #endif
267
268 #ifndef HAVE_DLERROR
269 #define dlerror rep_dlerror
270 char *rep_dlerror(void);
271 #endif
272
273 #ifndef HAVE_DLOPEN
274 #define dlopen rep_dlopen
275 #ifdef DLOPEN_TAKES_UNSIGNED_FLAGS
276 void *rep_dlopen(const char *name, unsigned int flags);
277 #else
278 void *rep_dlopen(const char *name, int flags);
279 #endif
280 #endif
281
282 #ifndef HAVE_DLSYM
283 #define dlsym rep_dlsym
284 void *rep_dlsym(void *handle, const char *symbol);
285 #endif
286
287 #ifndef HAVE_DLCLOSE
288 #define dlclose rep_dlclose
289 int rep_dlclose(void *handle);
290 #endif
291
292 #ifndef HAVE_SOCKETPAIR
293 #define socketpair rep_socketpair
294 /* prototype is in system/network.h */
295 #endif
296
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
302  * properly **/
303 #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
304 #else
305 #define PRINTF_ATTRIBUTE(a1, a2)
306 #endif
307 #endif
308
309 #ifndef _DEPRECATED_
310 #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 )
311 #define _DEPRECATED_ __attribute__ ((deprecated))
312 #else
313 #define _DEPRECATED_
314 #endif
315 #endif
316
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);
320 #endif
321
322 #define slprintf snprintf
323
324 #ifndef HAVE_VA_COPY
325 #undef va_copy
326 #ifdef HAVE___VA_COPY
327 #define va_copy(dest, src) __va_copy(dest, src)
328 #else
329 #define va_copy(dest, src) (dest) = (src)
330 #endif
331 #endif
332
333 #ifndef HAVE_VOLATILE
334 #define volatile
335 #endif
336
337 #ifndef HAVE_COMPARISON_FN_T
338 typedef int (*comparison_fn_t)(const void *, const void *);
339 #endif
340
341 #ifdef REPLACE_STRPTIME
342 #define strptime rep_strptime
343 struct tm;
344 char *rep_strptime(const char *buf, const char *format, struct tm *tm);
345 #endif
346
347 #ifndef HAVE_DUP2
348 #define dup2 rep_dup2
349 int rep_dup2(int oldfd, int newfd);
350 #endif
351
352 /* Load header file for dynamic linking stuff */
353 #ifdef HAVE_DLFCN_H
354 #include <dlfcn.h>
355 #endif
356
357 #ifndef RTLD_LAZY
358 #define RTLD_LAZY 0
359 #endif
360 #ifndef RTLD_NOW
361 #define RTLD_NOW 0
362 #endif
363 #ifndef RTLD_GLOBAL
364 #define RTLD_GLOBAL 0
365 #endif
366
367 #ifndef HAVE_SECURE_MKSTEMP
368 #define mkstemp(path) rep_mkstemp(path)
369 int rep_mkstemp(char *temp);
370 #endif
371
372 #ifndef HAVE_MKDTEMP
373 #define mkdtemp rep_mkdtemp
374 char *rep_mkdtemp(char *template);
375 #endif
376
377 #ifndef HAVE_PREAD
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
381 #else
382 #define LIBREPLACE_PREAD_NOT_REPLACED 1
383 #endif
384
385 #ifndef HAVE_PWRITE
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
389 #else
390 #define LIBREPLACE_PWRITE_NOT_REPLACED 1
391 #endif
392
393 #if !defined(HAVE_INET_NTOA) || defined(REPLACE_INET_NTOA)
394 #define inet_ntoa rep_inet_ntoa
395 /* prototype is in "system/network.h" */
396 #endif
397
398 #ifndef HAVE_INET_PTON
399 #define inet_pton rep_inet_pton
400 /* prototype is in "system/network.h" */
401 #endif
402
403 #ifndef HAVE_INET_NTOP
404 #define inet_ntop rep_inet_ntop
405 /* prototype is in "system/network.h" */
406 #endif
407
408 #ifndef HAVE_INET_ATON
409 #define inet_aton rep_inet_aton
410 /* prototype is in "system/network.h" */
411 #endif
412
413 #ifndef HAVE_CONNECT
414 #define connect rep_connect
415 /* prototype is in "system/network.h" */
416 #endif
417
418 #ifndef HAVE_GETHOSTBYNAME
419 #define gethostbyname rep_gethostbyname
420 /* prototype is in "system/network.h" */
421 #endif
422
423 #ifndef HAVE_GETIFADDRS
424 #define getifaddrs rep_getifaddrs
425 /* prototype is in "system/network.h" */
426 #endif
427
428 #ifndef HAVE_FREEIFADDRS
429 #define freeifaddrs rep_freeifaddrs
430 /* prototype is in "system/network.h" */
431 #endif
432
433 #ifdef HAVE_LIMITS_H
434 #include <limits.h>
435 #endif
436
437 #ifdef HAVE_SYS_PARAM_H
438 #include <sys/param.h>
439 #endif
440
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)))
448
449 #ifndef UINT16_MAX
450 #define UINT16_MAX 65535
451 #endif
452
453 #ifndef UINT32_MAX
454 #define UINT32_MAX (4294967295U)
455 #endif
456
457 #ifndef UINT64_MAX
458 #define UINT64_MAX ((uint64_t)-1)
459 #endif
460
461 #ifndef CHAR_BIT
462 #define CHAR_BIT 8
463 #endif
464
465 #ifndef INT32_MAX
466 #define INT32_MAX _TYPE_MAXIMUM(int32_t)
467 #endif
468
469 #ifdef HAVE_STDBOOL_H
470 #include <stdbool.h>
471 #endif
472
473 #if !defined(HAVE_BOOL)
474 #ifdef HAVE__BOOL
475 #define bool _Bool
476 #else
477 typedef int bool;
478 #endif
479 #endif
480
481 /*
482  * to prevent <rpcsvc/yp_prot.h> from doing a redefine of 'bool'
483  *
484  * IRIX, HPUX, MacOS 10 and Solaris need BOOL_DEFINED
485  * Tru64 needs _BOOL_EXISTS
486  * AIX needs _BOOL,_TRUE,_FALSE
487  */
488 #ifndef BOOL_DEFINED
489 #define BOOL_DEFINED
490 #endif
491 #ifndef _BOOL_EXISTS
492 #define _BOOL_EXISTS
493 #endif
494 #ifndef _BOOL
495 #define _BOOL
496 #endif
497
498 #ifndef __bool_true_false_are_defined
499 #define __bool_true_false_are_defined
500 #endif
501
502 #ifndef true
503 #define true (1)
504 #endif
505 #ifndef false
506 #define false (0)
507 #endif
508
509 #ifndef _TRUE
510 #define _TRUE true
511 #endif
512 #ifndef _FALSE
513 #define _FALSE false
514 #endif
515
516 #ifndef HAVE_FUNCTION_MACRO
517 #ifdef HAVE_func_MACRO
518 #define __FUNCTION__ __func__
519 #else
520 #define __FUNCTION__ ("")
521 #endif
522 #endif
523
524
525 #ifndef MIN
526 #define MIN(a,b) ((a)<(b)?(a):(b))
527 #endif
528
529 #ifndef MAX
530 #define MAX(a,b) ((a)>(b)?(a):(b))
531 #endif
532
533 #if !defined(HAVE_VOLATILE)
534 #define volatile
535 #endif
536
537 /**
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
542   discard_const.
543
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
546   we get new errors.
547
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.
551
552   Also, please call this via the discard_const_p() macro interface, as that
553   makes the return type safe.
554 */
555 #define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
556
557 /** Type-safe version of discard_const */
558 #define discard_const_p(type, ptr) ((type *)discard_const(ptr))
559
560 #ifndef __STRING
561 #define __STRING(x)    #x
562 #endif
563
564 #ifndef __STRINGSTRING
565 #define __STRINGSTRING(x) __STRING(x)
566 #endif
567
568 #ifndef __LINESTR__
569 #define __LINESTR__ __STRINGSTRING(__LINE__)
570 #endif
571
572 #ifndef __location__
573 #define __location__ __FILE__ ":" __LINESTR__
574 #endif
575
576 /** 
577  * zero a structure 
578  */
579 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
580
581 /** 
582  * zero a structure given a pointer to the structure 
583  */
584 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
585
586 /** 
587  * zero a structure given a pointer to the structure - no zero check 
588  */
589 #define ZERO_STRUCTPN(x) memset((char *)(x), 0, sizeof(*(x)))
590
591 /* zero an array - note that sizeof(array) must work - ie. it must not be a
592    pointer */
593 #define ZERO_ARRAY(x) memset((char *)(x), 0, sizeof(x))
594
595 /**
596  * work out how many elements there are in a static array 
597  */
598 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
599
600 /** 
601  * pointer difference macro 
602  */
603 #define PTR_DIFF(p1,p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
604
605 #if MMAP_BLACKLIST
606 #undef HAVE_MMAP
607 #endif
608
609 #ifdef __COMPAR_FN_T
610 #define QSORT_CAST (__compar_fn_t)
611 #endif
612
613 #ifndef QSORT_CAST
614 #define QSORT_CAST (int (*)(const void *, const void *))
615 #endif
616
617 #ifndef PATH_MAX
618 #define PATH_MAX 1024
619 #endif
620
621 #ifndef MAX_DNS_NAME_LENGTH
622 #define MAX_DNS_NAME_LENGTH 256 /* Actually 255 but +1 for terminating null. */
623 #endif
624
625 #ifndef HAVE_CRYPT
626 char *ufc_crypt(const char *key, const char *salt);
627 #define crypt ufc_crypt
628 #else
629 #ifdef HAVE_CRYPT_H
630 #include <crypt.h>
631 #endif
632 #endif
633
634 /* these macros gain us a few percent of speed on gcc */
635 #if (__GNUC__ >= 3)
636 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
637    as its first argument */
638 #ifndef likely
639 #define likely(x)   __builtin_expect(!!(x), 1)
640 #endif
641 #ifndef unlikely
642 #define unlikely(x) __builtin_expect(!!(x), 0)
643 #endif
644 #else
645 #ifndef likely
646 #define likely(x) (x)
647 #endif
648 #ifndef unlikely
649 #define unlikely(x) (x)
650 #endif
651 #endif
652
653 #ifndef SAFE_FREE
654 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
655 #endif
656
657 typedef uint32_t NTSTATUS;
658 #define NT_STATUS(x) (x)
659 #define NT_STATUS_V(x) (x)
660
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
667 #else /* MIT */
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
672 #endif
673
674 #endif /* _LIBREPLACE_REPLACE_H */