OpenBSD: disable compiler-time param checking for string_sprintf() etc
[exim.git] / src / src / mytypes.h
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* Copyright (c) The Exim Maintainers 2020 */
7 /* See the file NOTICE for conditions of use and distribution. */
8
9
10 /* This header file contains type definitions and macros that I use as
11 "standard" in the code of Exim and its utilities. Make it idempotent because
12 local_scan.h includes it and exim.h includes them both (to get this earlier). */
13
14 #ifndef MYTYPES_H
15 #define MYTYPES_H
16
17 # include <string.h>
18
19 #ifndef FALSE
20 # define FALSE         0
21 #endif
22
23 #ifndef TRUE
24 # define TRUE          1
25 #endif
26
27 #ifndef TRUE_UNSET
28 # define TRUE_UNSET    2
29 #endif
30
31
32 /* If gcc is being used to compile Exim, we can use its facility for checking
33 the arguments of printf-like functions. This is done by a macro.
34 OpenBSD has unfortunately taken to objecting to use of %n in printf
35 so we have to give up on all of the available parameter checking. */
36
37 #if defined(__GNUC__) || defined(__clang__)
38 # ifndef __OpenBSD__
39 #  define PRINTF_FUNCTION(A,B)  __attribute__((format(printf,A,B)))
40 # endif
41 # define ARG_UNUSED             __attribute__((__unused__))
42 # define WARN_UNUSED_RESULT     __attribute__((__warn_unused_result__))
43 # define ALLOC                  __attribute__((malloc))
44 # define ALLOC_SIZE(A)          __attribute__((alloc_size(A)))
45 # define NORETURN               __attribute__((noreturn))
46 #else
47 # define ARG_UNUSED             /**/
48 # define WARN_UNUSED_RESULT     /**/
49 # define ALLOC                  /**/
50 # define ALLOC_SIZE(A)          /**/
51 # define NORETURN               /**/
52 #endif
53
54 #ifndef PRINTF_FUNCTION
55 # define PRINTF_FUNCTION(A,B)   /**/
56 #endif
57
58 #ifdef WANT_DEEPER_PRINTF_CHECKS
59 # define ALMOST_PRINTF(A, B) PRINTF_FUNCTION(A, B)
60 #else
61 # define ALMOST_PRINTF(A, B)    /**/
62 #endif
63
64
65 /* Some operating systems (naughtily, imo) include a definition for "uchar" in
66 the standard header files, so we use "uschar". Solaris has u_char in
67 sys/types.h. This is just a typing convenience, of course. */
68
69 typedef unsigned char uschar;
70 typedef unsigned BOOL;
71 /* We also have SIGNAL_BOOL, which requires signal.h be included, so is defined
72 elsewhere */
73
74
75 /* These macros save typing for the casting that is needed to cope with the
76 mess that is "char" in ISO/ANSI C. Having now been bitten enough times by
77 systems where "char" is actually signed, I've converted Exim to use entirely
78 unsigned chars, except in a few special places such as arguments that are
79 almost always literal strings. */
80
81 #define CS   (char *)
82 #define CCS  (const char *)
83 #define CSS  (char **)
84 #define US   (unsigned char *)
85 #define CUS  (const unsigned char *)
86 #define USS  (unsigned char **)
87 #define CUSS (const unsigned char **)
88 #define CCSS (const char **)
89
90 /* The C library string functions expect "char *" arguments. Use macros to
91 avoid having to write a cast each time. We do this for string and file
92 functions that are called quite often; for other calls to external libraries
93 (which are on the whole special-purpose) we just use individual casts. */
94
95 #define Uatoi(s)           atoi(CCS(s))
96 #define Uatol(s)           atol(CCS(s))
97 #define Uchdir(s)          chdir(CCS(s))
98 #define Uchmod(s,n)        chmod(CCS(s),n)
99 #define Ufgets(b,n,f)      fgets(CS(b),n,f)
100 #define Ufopen(s,t)        exim_fopen(CCS(s),CCS(t))
101 #define Ulink(s,t)         link(CCS(s),CCS(t))
102 #define Ulstat(s,t)        lstat(CCS(s),t)
103
104 #ifdef O_BINARY                                                 /* This is for Cygwin,  */
105 # define Uopen(s,n,m)       exim_open(CCS(s),(n)|O_BINARY,m)    /* where all files must */
106 # define Uopen2(s,n)        exim_open2(CCS(s),(n)|O_BINARY)
107 #else                                                           /* be opened as binary  */
108 # define Uopen(s,n,m)       exim_open(CCS(s),n,m)               /* to avoid problems    */
109 # define Uopen2(s,n)        exim_open2(CCS(s),n)        
110 #endif                                                          /* with CRLF endings.   */
111 #define Uread(f,b,l)       read(f,CS(b),l)
112 #define Urename(s,t)       rename(CCS(s),CCS(t))
113 #define Ustat(s,t)         stat(CCS(s),t)
114 #define Ustrchr(s,n)       US strchr(CCS(s),n)
115 #define CUstrchr(s,n)      CUS strchr(CCS(s),n)
116 #define CUstrerror(n)      CUS strerror(n)
117 #define Ustrcmp(s,t)       strcmp(CCS(s),CCS(t))
118 #define Ustrcpy_nt(s,t)    strcpy(CS s, CCS t)          /* no taint check */
119 #define Ustrcspn(s,t)      strcspn(CCS(s),CCS(t))
120 #define Ustrftime(s,m,f,t) strftime(CS(s),m,f,t)
121 #define Ustrlen(s)         (int)strlen(CCS(s))
122 #define Ustrncmp(s,t,n)    strncmp(CCS(s),CCS(t),n)
123 #define Ustrncpy_nt(s,t,n) strncpy(CS s, CCS t, n)      /* no taint check */
124 #define Ustrpbrk(s,t)      strpbrk(CCS(s),CCS(t))
125 #define Ustrrchr(s,n)      US strrchr(CCS(s),n)
126 #define CUstrrchr(s,n)     CUS strrchr(CCS(s),n)
127 #define Ustrspn(s,t)       strspn(CCS(s),CCS(t))
128 #define Ustrstr(s,t)       US strstr(CCS(s),CCS(t))
129 #define CUstrstr(s,t)      CUS strstr(CCS(s),CCS(t))
130 #define Ustrtod(s,t)       strtod(CCS(s),CSS(t))
131 #define Ustrtol(s,t,b)     strtol(CCS(s),CSS(t),b)
132 #define Ustrtoul(s,t,b)    strtoul(CCS(s),CSS(t),b)
133 #define Uunlink(s)         unlink(CCS(s))
134
135 #if defined(EM_VERSION_C) || defined(LOCAL_SCAN) || defined(DLFUNC_IMPL)
136 # define Ustrcat(s,t)       strcat(CS(s), CCS(t))
137 # define Ustrcpy(s,t)       strcpy(CS(s), CCS(t))
138 # define Ustrncat(s,t,n)    strncat(CS(s), CCS(t), n)
139 # define Ustrncpy(s,t,n)    strncpy(CS(s), CCS(t), n)
140 #else
141 # define Ustrcat(s,t)       __Ustrcat(s, CUS(t), __FUNCTION__, __LINE__)
142 # define Ustrcpy(s,t)       __Ustrcpy(s, CUS(t), __FUNCTION__, __LINE__)
143 # define Ustrncat(s,t,n)    __Ustrncat(s, CUS(t), n, __FUNCTION__, __LINE__)
144 # define Ustrncpy(s,t,n)    __Ustrncpy(s, CUS(t), n, __FUNCTION__, __LINE__)
145 #endif
146
147 #endif
148 /* End of mytypes.h */