Move malloc portability tweaks out of system.h
[platform/upstream/rpm.git] / system.h
1 /**
2  * \file system.h
3  */
4
5 #ifndef H_SYSTEM
6 #define H_SYSTEM
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11
12 #include <sys/types.h>
13
14 #include <sys/stat.h>
15 #include <stdio.h>
16
17 #ifdef HAVE_SYS_PARAM_H
18 #include <sys/param.h>
19 #endif
20
21 /* <unistd.h> should be included before any preprocessor test
22    of _POSIX_VERSION.  */
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #if !defined(__GLIBC__)
26 #ifdef __APPLE__
27 #include <crt_externs.h>
28 #define environ (*_NSGetEnviron())
29 #else
30 extern char ** environ;
31 #endif /* __APPLE__ */
32 #endif
33 #endif
34
35 #if TIME_WITH_SYS_TIME
36 # include <sys/time.h>
37 # include <time.h>
38 #else
39 # if HAVE_SYS_TIME_H
40 #  include <sys/time.h>
41 # else
42 #  include <time.h>
43 # endif
44 #endif
45
46 /* Since major is a function on SVR4, we can't use `ifndef major'.  */
47 #if MAJOR_IN_MKDEV
48 #include <sys/mkdev.h>
49 #define HAVE_MAJOR
50 #endif
51 #if MAJOR_IN_SYSMACROS
52 #include <sys/sysmacros.h>
53 #define HAVE_MAJOR
54 #endif
55 #ifdef major                    /* Might be defined in sys/types.h.  */
56 #define HAVE_MAJOR
57 #endif
58
59 #ifndef HAVE_MAJOR
60 #define major(dev)  (((dev) >> 8) & 0xff)
61 #define minor(dev)  ((dev) & 0xff)
62 #define makedev(maj, min)  (((maj) << 8) | (min))
63 #endif
64 #undef HAVE_MAJOR
65
66 #ifdef HAVE_UTIME_H
67 #include <utime.h>
68 #endif
69
70 #ifdef HAVE_STRING_H
71 # if !STDC_HEADERS && HAVE_MEMORY_H
72 #  include <memory.h>
73 # endif
74 # include <string.h>
75 #else
76 # include <strings.h>
77 #endif
78
79 #if !defined(HAVE_STPCPY)
80 char * stpcpy(char * dest, const char * src);
81 #endif
82
83 #if !defined(HAVE_STPNCPY)
84 char * stpncpy(char * dest, const char * src, size_t n);
85 #endif
86
87 #include <errno.h>
88 #ifndef errno
89 extern int errno;
90 #endif
91
92 #if HAVE_ERROR_H
93 #include <error.h>
94 #endif
95
96 #if HAVE___SECURE_GETENV
97 #define getenv(_s)      __secure_getenv(_s)
98 #endif
99
100 #ifdef STDC_HEADERS
101 /* FIX: shrug */
102 #define getopt system_getopt
103 #include <stdlib.h>
104 #undef getopt
105 #else /* not STDC_HEADERS */
106 char *getenv (const char *name);
107 #if ! HAVE_REALPATH
108 char *realpath(const char *path, char resolved_path []);
109 #endif
110 #endif /* STDC_HEADERS */
111
112 /* XXX solaris2.5.1 has not */
113 #if !defined(EXIT_FAILURE)
114 #define EXIT_FAILURE    1
115 #endif
116
117 #ifdef HAVE_FCNTL_H
118 #include <fcntl.h>
119 #else
120 #include <sys/file.h>
121 #endif
122
123 #if !defined(SEEK_SET)
124 #define SEEK_SET 0
125 #define SEEK_CUR 1
126 #define SEEK_END 2
127 #endif
128 #if !defined(F_OK) 
129 #define F_OK 0
130 #define X_OK 1
131 #define W_OK 2
132 #define R_OK 4
133 #endif
134
135 #ifdef HAVE_DIRENT_H
136 # include <dirent.h>
137 # define NLENGTH(direct) (strlen((direct)->d_name))
138 #else /* not HAVE_DIRENT_H */
139 # define dirent direct
140 # define NLENGTH(direct) ((direct)->d_namlen)
141 # ifdef HAVE_SYS_NDIR_H
142 #  include <sys/ndir.h>
143 # endif /* HAVE_SYS_NDIR_H */
144 # ifdef HAVE_SYS_DIR_H
145 #  include <sys/dir.h>
146 # endif /* HAVE_SYS_DIR_H */
147 # ifdef HAVE_NDIR_H
148 #  include <ndir.h>
149 # endif /* HAVE_NDIR_H */
150 #endif /* HAVE_DIRENT_H */
151
152 #include <ctype.h>
153
154 #if HAVE_SYS_MMAN_H
155 #include <sys/mman.h>
156 #endif
157
158 #if HAVE_SYS_UTSNAME_H
159 #include <sys/utsname.h>
160 #endif
161
162 #if HAVE_SYS_WAIT_H
163 #include <sys/wait.h>
164 #endif
165
166 #if HAVE_GETOPT_H
167 #include <getopt.h>
168 #endif
169
170 #if HAVE_GRP_H
171 #include <grp.h>
172 #endif
173
174 #if HAVE_LIMITS_H
175 #include <limits.h>
176 #endif
177
178 #ifndef PATH_MAX
179 #ifdef _POSIX_PATH_MAX
180 #define PATH_MAX _POSIX_PATH_MAX
181 #elif defined MAXPATHLEN
182 #define PATH_MAX MAXPATHLEN
183 #else
184 #define PATH_MAX 256
185 #endif
186 #endif
187
188 #if HAVE_ERR_H
189 #include <err.h>
190 #endif
191
192 #if WITH_SELINUX
193 #include <selinux/selinux.h>
194 #else
195 typedef char * security_context_t;
196
197 #define freecon(_c)
198
199 #define getfilecon(_fn, _c)     (-1)
200 #define lgetfilecon(_fn, _c)    (-1)
201 #define fgetfilecon(_fd, _c)    (-1)
202
203 #define setfilecon(_fn, _c)     (-1)
204 #define lsetfilecon(_fn, _c)    (-1)
205 #define fsetfilecon(_fd, _c)    (-1)
206
207 #define security_check_context(_c)      (0)
208
209 #define is_selinux_enabled()    (0)
210
211 #define matchpathcon_init(_fn)                  (-1)
212 #define matchpathcon_fini()                     (0)
213 #define matchpathcon(_fn, _fm, _c)              (-1)
214
215 #define rpm_execcon(_v, _fn, _av, _envp)        (0)
216 #endif
217
218 #if WITH_CAP
219 #include <sys/capability.h>
220 #else
221 typedef void * cap_t;
222 #endif
223
224 #if WITH_ACL
225 #include <acl/libacl.h>
226 #endif
227
228 #if HAVE_MCHECK_H
229 #include <mcheck.h>
230 #endif  /* HAVE_MCHECK_H */
231
232 #include "rpmio/rpmutil.h"
233 /* compatibility macros to avoid a mass-renaming all over the codebase */
234 #define xmalloc(_size) rmalloc((_size))
235 #define xcalloc(_nmemb, _size) rcalloc((_nmemb), (_size))
236 #define xrealloc(_ptr, _size) rrealloc((_ptr), (_size))
237 #define xstrdup(_str) rstrdup((_str))
238 #define _free(_ptr) rfree((_ptr))
239
240 /* Retrofit glibc __progname */
241 #if defined __GLIBC__ && __GLIBC__ >= 2
242 #if __GLIBC_MINOR__ >= 1
243 #define __progname      __assert_program_name
244 #endif
245 #define setprogname(pn)
246 #else
247 #define __progname      program_name
248 #define setprogname(pn) \
249   { if ((__progname = strrchr(pn, '/')) != NULL) __progname++; \
250     else __progname = pn;               \
251   }
252 #endif
253 extern const char *__progname;
254
255 #if HAVE_NETDB_H
256 #include <netdb.h>
257 #endif
258
259 #if HAVE_PWD_H
260 #include <pwd.h>
261 #endif
262
263 /* Take care of NLS matters.  */
264
265 #if HAVE_LOCALE_H
266 # include <locale.h>
267 #endif
268 #if !HAVE_SETLOCALE
269 # define setlocale(Category, Locale) /* empty */
270 #endif
271
272 #if ENABLE_NLS
273 # include <libintl.h>
274 # define _(Text) dgettext (PACKAGE, Text)
275 #else
276 # undef bindtextdomain
277 # define bindtextdomain(Domain, Directory) /* empty */
278 # undef textdomain
279 # define textdomain(Domain) /* empty */
280 # define _(Text) Text
281 # undef dgettext
282 # define dgettext(DomainName, Text) Text
283 #endif
284
285 #define N_(Text) Text
286
287 /* ============== from misc/miscfn.h */
288
289 #if !defined(USE_GNU_GLOB) 
290 #if HAVE_FNMATCH_H
291 #include <fnmatch.h>
292 #endif
293
294 #if HAVE_GLOB_H 
295 #include <glob.h>
296 #endif
297 #else
298 #include "misc/glob.h"
299 #include "misc/fnmatch.h"
300 #endif
301
302 #if ! HAVE_S_IFSOCK
303 #define S_IFSOCK (0xc000)
304 #endif
305
306 #if ! HAVE_S_ISLNK
307 #define S_ISLNK(mode) ((mode & 0xf000) == S_IFLNK)
308 #endif
309
310 #if ! HAVE_S_ISSOCK
311 #define S_ISSOCK(mode) ((mode & 0xf000) == S_IFSOCK)
312 #endif
313
314 #if NEED_STRINGS_H
315 #include <strings.h>
316 #endif
317
318 #if ! HAVE_SETENV
319 extern int setenv(const char *name, const char *value, int replace);
320 extern void unsetenv(const char *name);
321 #endif
322
323 #if HAVE_SYS_SOCKET_H
324 #include <sys/types.h>
325 #include <sys/socket.h>
326 #endif
327
328 #if HAVE_POLL_H
329 #include <poll.h>
330 #else
331 #if HAVE_SYS_SELECT_H
332 #include <sys/select.h>
333 #endif
334 #endif
335
336 #if ! HAVE_LCHOWN
337 #define lchown chown
338 #endif
339
340 #if HAVE_GETMNTINFO || HAVE_GETMNTINFO_R || HAVE_MNTCTL
341 # define GETMNTENT_ONE 0
342 # define GETMNTENT_TWO 0
343 # if HAVE_SYS_MNTCTL_H
344 #  include <sys/mntctl.h>
345 # endif
346 # if HAVE_SYS_VMOUNT_H
347 #  include <sys/vmount.h>
348 # endif
349 # if HAVE_SYS_MOUNT_H
350 #  include <sys/mount.h>
351 # endif
352 #elif HAVE_MNTENT_H || !(HAVE_GETMNTENT) || HAVE_STRUCT_MNTTAB
353 # if HAVE_MNTENT_H
354 #  include <stdio.h>
355 #  include <mntent.h>
356 #  define our_mntent struct mntent
357 #  define our_mntdir mnt_dir
358 # elif HAVE_STRUCT_MNTTAB
359 #  include <stdio.h>
360 #  include <mnttab.h>
361    struct our_mntent {
362        char * our_mntdir;
363    };
364    struct our_mntent *getmntent(FILE *filep);
365 #  define our_mntent struct our_mntent
366 # else
367 #  include <stdio.h>
368    struct our_mntent {
369        char * our_mntdir;
370    };
371    struct our_mntent *getmntent(FILE *filep);
372 #  define our_mntent struct our_mntent
373 # endif
374 # define GETMNTENT_ONE 1
375 # define GETMNTENT_TWO 0
376 #elif HAVE_SYS_MNTTAB_H
377 # include <stdio.h>
378 # include <sys/mnttab.h>
379 # define GETMNTENT_ONE 0
380 # define GETMNTENT_TWO 1
381 # define our_mntent struct mnttab
382 # define our_mntdir mnt_mountp
383 #else /* if !HAVE_MNTCTL */
384 # error Neither mntent.h, mnttab.h, or mntctl() exists. I cannot build on this system.
385 #endif
386
387 #ifndef MOUNTED
388 #define MOUNTED "/etc/mnttab"
389 #endif
390
391 #endif  /* H_SYSTEM */