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