Move key size fixups from rpmdbAdd & Remove to td2key()
[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 #if NEED_TIMEZONE
47 extern time_t timezone;
48 #endif
49
50 /* Since major is a function on SVR4, we can't use `ifndef major'.  */
51 #if MAJOR_IN_MKDEV
52 #include <sys/mkdev.h>
53 #define HAVE_MAJOR
54 #endif
55 #if MAJOR_IN_SYSMACROS
56 #include <sys/sysmacros.h>
57 #define HAVE_MAJOR
58 #endif
59 #ifdef major                    /* Might be defined in sys/types.h.  */
60 #define HAVE_MAJOR
61 #endif
62
63 #ifndef HAVE_MAJOR
64 #define major(dev)  (((dev) >> 8) & 0xff)
65 #define minor(dev)  ((dev) & 0xff)
66 #define makedev(maj, min)  (((maj) << 8) | (min))
67 #endif
68 #undef HAVE_MAJOR
69
70 #ifdef HAVE_UTIME_H
71 #include <utime.h>
72 #endif
73
74 #ifdef HAVE_STRING_H
75 # if !STDC_HEADERS && HAVE_MEMORY_H
76 #  include <memory.h>
77 # endif
78 # include <string.h>
79 #else
80 # include <strings.h>
81 char *memchr ();
82 #endif
83
84 #if !defined(HAVE_STPCPY)
85 char * stpcpy(char * dest, const char * src);
86 #endif
87
88 #if !defined(HAVE_STPNCPY)
89 char * stpncpy(char * dest, const char * src, size_t n);
90 #endif
91
92 #include <errno.h>
93 #ifndef errno
94 extern int errno;
95 #endif
96
97 #if HAVE_ERROR && HAVE_ERROR_H
98 #include <error.h>
99 #endif
100
101 #if HAVE___SECURE_GETENV
102 #define getenv(_s)      __secure_getenv(_s)
103 #endif
104
105 #ifdef STDC_HEADERS
106 /* FIX: shrug */
107 #define getopt system_getopt
108 #include <stdlib.h>
109 #undef getopt
110 #else /* not STDC_HEADERS */
111 char *getenv (const char *name);
112 #if ! HAVE_REALPATH
113 char *realpath(const char *path, char resolved_path []);
114 #endif
115 #endif /* STDC_HEADERS */
116
117 /* XXX solaris2.5.1 has not */
118 #if !defined(EXIT_FAILURE)
119 #define EXIT_FAILURE    1
120 #endif
121
122 #ifdef HAVE_FCNTL_H
123 #include <fcntl.h>
124 #else
125 #include <sys/file.h>
126 #endif
127
128 #if !defined(SEEK_SET)
129 #define SEEK_SET 0
130 #define SEEK_CUR 1
131 #define SEEK_END 2
132 #endif
133 #if !defined(F_OK) 
134 #define F_OK 0
135 #define X_OK 1
136 #define W_OK 2
137 #define R_OK 4
138 #endif
139
140 #ifdef HAVE_DIRENT_H
141 # include <dirent.h>
142 # define NLENGTH(direct) (strlen((direct)->d_name))
143 #else /* not HAVE_DIRENT_H */
144 # define dirent direct
145 # define NLENGTH(direct) ((direct)->d_namlen)
146 # ifdef HAVE_SYS_NDIR_H
147 #  include <sys/ndir.h>
148 # endif /* HAVE_SYS_NDIR_H */
149 # ifdef HAVE_SYS_DIR_H
150 #  include <sys/dir.h>
151 # endif /* HAVE_SYS_DIR_H */
152 # ifdef HAVE_NDIR_H
153 #  include <ndir.h>
154 # endif /* HAVE_NDIR_H */
155 #endif /* HAVE_DIRENT_H */
156
157 #include <ctype.h>
158
159 #if HAVE_SYS_MMAN_H
160 #include <sys/mman.h>
161 #endif
162
163 /* XXX FIXME: popt on sunos4.1.3: <sys/resource.h> requires <sys/time.h> */
164 #if HAVE_SYS_RESOURCE_H && HAVE_SYS_TIME_H
165 #include <sys/resource.h>
166 #endif
167
168 #if HAVE_SYS_UTSNAME_H
169 #include <sys/utsname.h>
170 #endif
171
172 #if HAVE_SYS_WAIT_H
173 #include <sys/wait.h>
174 #endif
175
176 #if HAVE_GETOPT_H
177 #include <getopt.h>
178 #endif
179
180 #if HAVE_GRP_H
181 #include <grp.h>
182 #endif
183
184 #if HAVE_LIMITS_H
185 #include <limits.h>
186 #endif
187
188 #if HAVE_ERR_H
189 #include <err.h>
190 #endif
191
192 #if HAVE_MALLOC_H 
193 #include <malloc.h>
194 #endif
195
196 #if WITH_SELINUX
197 #include <selinux/selinux.h>
198 #else
199 typedef char * security_context_t;
200
201 #define freecon(_c)
202
203 #define getfilecon(_fn, _c)     (-1)
204 #define lgetfilecon(_fn, _c)    (-1)
205 #define fgetfilecon(_fd, _c)    (-1)
206
207 #define setfilecon(_fn, _c)     (-1)
208 #define lsetfilecon(_fn, _c)    (-1)
209 #define fsetfilecon(_fd, _c)    (-1)
210
211 #define security_check_context(_c)      (0)
212
213 #define is_selinux_enabled()    (0)
214
215 #define matchpathcon_init(_fn)                  (-1)
216 #define matchpathcon_fini()                     (0)
217 #define matchpathcon(_fn, _fm, _c)              (-1)
218
219 #define rpm_execcon(_v, _fn, _av, _envp)        (0)
220 #endif
221
222 /**
223  * Wrapper to free(3), permit NULL, return NULL.
224  * @param p             memory to free
225  * @return              NULL always
226  */
227 static inline
228 void * _free(void * p)
229 {
230     if (p != NULL)      free(p);
231     return NULL;
232 }
233
234 /**
235  * Wrapper to free(3), permit NULL, return NULL. 
236  * For documenting cases where const is used to protect long-lived 
237  * non-const data that's supposed to be freed.
238  * @param p             memory to free
239  * @return              NULL always
240  */
241 static inline
242 void * _constfree(const void * p)
243 {
244     if (p != NULL)      free((void *)p);
245     return NULL;
246 }
247
248 /* FIX: these are macros */
249 /**
250  */
251 void * xmalloc (size_t size);
252
253 /**
254  */
255 void * xcalloc (size_t nmemb, size_t size);
256
257 /**
258  * @todo Annotate ptr with returned/out.
259  */
260 void * xrealloc (void * ptr,
261                                         size_t size);
262
263 /**
264  */
265 char * xstrdup (const char *str);
266
267 /**
268  */
269 void * vmefail(size_t size);
270
271 #if HAVE_MCHECK_H
272 #include <mcheck.h>
273 /* Memory allocation via macro defs to get meaningful locations from mtrace() */
274 #if defined(__GNUC__)
275 #define xmalloc(_size)          (malloc(_size) ? : vmefail(_size))
276 #define xcalloc(_nmemb, _size)  (calloc((_nmemb), (_size)) ? : vmefail(_size))
277 #define xrealloc(_ptr, _size)   (realloc((_ptr), (_size)) ? : vmefail(_size))
278 #define xstrdup(_str)   (strcpy((malloc(strlen(_str)+1) ? : vmefail(strlen(_str)+1)), (_str)))
279 #endif  /* defined(__GNUC__) */
280 #endif  /* HAVE_MCHECK_H */
281
282 /* Retrofit glibc __progname */
283 #if defined __GLIBC__ && __GLIBC__ >= 2
284 #if __GLIBC_MINOR__ >= 1
285 #define __progname      __assert_program_name
286 #endif
287 #define setprogname(pn)
288 #else
289 #define __progname      program_name
290 #define setprogname(pn) \
291   { if ((__progname = strrchr(pn, '/')) != NULL) __progname++; \
292     else __progname = pn;               \
293   }
294 #endif
295 extern const char *__progname;
296
297 #if HAVE_NETDB_H
298 #include <netdb.h>
299 #endif
300
301 #if HAVE_PWD_H
302 #include <pwd.h>
303 #endif
304
305 /* Take care of NLS matters.  */
306
307 #if HAVE_LOCALE_H
308 # include <locale.h>
309 #endif
310 #if !HAVE_SETLOCALE
311 # define setlocale(Category, Locale) /* empty */
312 #endif
313
314 #if ENABLE_NLS
315 # include <libintl.h>
316 # define _(Text) dgettext (PACKAGE, Text)
317 #else
318 # undef bindtextdomain
319 # define bindtextdomain(Domain, Directory) /* empty */
320 # undef textdomain
321 # define textdomain(Domain) /* empty */
322 # define _(Text) Text
323 # undef dgettext
324 # define dgettext(DomainName, Text) Text
325 #endif
326
327 #define N_(Text) Text
328
329 /* ============== from misc/miscfn.h */
330
331 #if !defined(USE_GNU_GLOB) 
332 #if HAVE_FNMATCH_H
333 #include <fnmatch.h>
334 #endif
335
336 #if HAVE_GLOB_H 
337 #include <glob.h>
338 #endif
339 #else
340 #include "misc/glob.h"
341 #include "misc/fnmatch.h"
342 #endif
343
344 #if ! HAVE_S_IFSOCK
345 #define S_IFSOCK (0xc000)
346 #endif
347
348 #if ! HAVE_S_ISLNK
349 #define S_ISLNK(mode) ((mode & 0xf000) == S_IFLNK)
350 #endif
351
352 #if ! HAVE_S_ISSOCK
353 #define S_ISSOCK(mode) ((mode & 0xf000) == S_IFSOCK)
354 #endif
355
356 #if NEED_STRINGS_H
357 #include <strings.h>
358 #endif
359
360 #if NEED_MYREALLOC
361 #define realloc(ptr,size) myrealloc(ptr,size)
362 extern void *myrealloc(void *, size_t);
363 #endif
364
365 #if ! HAVE_SETENV
366 extern int setenv(const char *name, const char *value, int replace);
367 extern void unsetenv(const char *name);
368 #endif
369
370 #if HAVE_SYS_SOCKET_H
371 #include <sys/types.h>
372 #include <sys/socket.h>
373 #endif
374
375 #if HAVE_POLL_H
376 #include <poll.h>
377 #else
378 #if HAVE_SYS_SELECT_H
379 #include <sys/select.h>
380 #endif
381 #endif
382
383 /* Solaris <= 2.6 limits getpass return to only 8 chars */
384 #if HAVE_GETPASSPHRASE
385 #define getpass getpassphrase
386 #endif
387
388 #if ! HAVE_LCHOWN
389 #define lchown chown
390 #endif
391
392 #if HAVE_GETMNTINFO || HAVE_GETMNTINFO_R || HAVE_MNTCTL
393 # define GETMNTENT_ONE 0
394 # define GETMNTENT_TWO 0
395 # if HAVE_SYS_MNTCTL_H
396 #  include <sys/mntctl.h>
397 # endif
398 # if HAVE_SYS_VMOUNT_H
399 #  include <sys/vmount.h>
400 # endif
401 # if HAVE_SYS_MOUNT_H
402 #  include <sys/mount.h>
403 # endif
404 #elif HAVE_MNTENT_H || !(HAVE_GETMNTENT) || HAVE_STRUCT_MNTTAB
405 # if HAVE_MNTENT_H
406 #  include <stdio.h>
407 #  include <mntent.h>
408 #  define our_mntent struct mntent
409 #  define our_mntdir mnt_dir
410 # elif HAVE_STRUCT_MNTTAB
411 #  include <stdio.h>
412 #  include <mnttab.h>
413    struct our_mntent {
414        char * our_mntdir;
415    };
416    struct our_mntent *getmntent(FILE *filep);
417 #  define our_mntent struct our_mntent
418 # else
419 #  include <stdio.h>
420    struct our_mntent {
421        char * our_mntdir;
422    };
423    struct our_mntent *getmntent(FILE *filep);
424 #  define our_mntent struct our_mntent
425 # endif
426 # define GETMNTENT_ONE 1
427 # define GETMNTENT_TWO 0
428 #elif HAVE_SYS_MNTTAB_H
429 # include <stdio.h>
430 # include <sys/mnttab.h>
431 # define GETMNTENT_ONE 0
432 # define GETMNTENT_TWO 1
433 # define our_mntent struct mnttab
434 # define our_mntdir mnt_mountp
435 #else /* if !HAVE_MNTCTL */
436 # error Neither mntent.h, mnttab.h, or mntctl() exists. I cannot build on this system.
437 #endif
438
439 #ifndef MOUNTED
440 #define MOUNTED "/etc/mnttab"
441 #endif
442
443 #endif  /* H_SYSTEM */