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