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