lclint fiddles to annotate globals.
[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 #include <sys/stat.h>
14 #include <stdio.h>
15
16 #ifdef HAVE_SYS_PARAM_H
17 #include <sys/param.h>
18 #endif
19
20 /* <unistd.h> should be included before any preprocessor test
21    of _POSIX_VERSION.  */
22 #ifdef HAVE_UNISTD_H
23 #include <unistd.h>
24 #if defined(__LCLINT__)
25 /*@-superuser -declundef -incondefs @*/ /* LCL: modifies clause missing */
26 extern int chroot (const char *__path)
27         /*@modifies errno, systemState @*/;
28 /*@=superuser =declundef =incondefs @*/
29 #endif
30 #endif
31
32 #if TIME_WITH_SYS_TIME
33 # include <sys/time.h>
34 # include <time.h>
35 #else
36 # if HAVE_SYS_TIME_H
37 #  include <sys/time.h>
38 # else
39 #  include <time.h>
40 # endif
41 #endif
42
43 #if NEED_TIMEZONE
44 extern time_t timezone;
45 #endif
46
47 #if defined(__LCLINT__)
48 typedef unsigned int u_int32_t;
49 typedef unsigned short u_int16_t;
50 typedef unsigned char u_int8_t;
51 /*@-incondefs@*/        /* LCLint 3.0.0.15 */
52 typedef int int32_t;
53 /*@=incondefs@*/
54 #endif
55
56 /* Since major is a function on SVR4, we can't use `ifndef major'.  */
57 #if MAJOR_IN_MKDEV
58 #include <sys/mkdev.h>
59 #define HAVE_MAJOR
60 #endif
61 #if MAJOR_IN_SYSMACROS
62 #include <sys/sysmacros.h>
63 #define HAVE_MAJOR
64 #endif
65 #ifdef major                    /* Might be defined in sys/types.h.  */
66 #define HAVE_MAJOR
67 #endif
68
69 #ifndef HAVE_MAJOR
70 #define major(dev)  (((dev) >> 8) & 0xff)
71 #define minor(dev)  ((dev) & 0xff)
72 #define makedev(maj, min)  (((maj) << 8) | (min))
73 #endif
74 #undef HAVE_MAJOR
75
76 #ifdef HAVE_UTIME_H
77 #include <utime.h>
78 #endif
79
80 #ifdef HAVE_STRING_H
81 # if !STDC_HEADERS && HAVE_MEMORY_H
82 #  include <memory.h>
83 # endif
84 # include <string.h>
85 #else
86 # include <strings.h>
87 char *memchr ();
88 #endif
89
90 #if !defined(HAVE_STPCPY)
91 char * stpcpy(/*@out@*/ char * dest, const char * src);
92 #endif
93
94 #if !defined(HAVE_STPNCPY)
95 char * stpncpy(/*@out@*/ char * dest, const char * src, size_t n);
96 #endif
97
98 #include <errno.h>
99 #ifndef errno
100 /*@-declundef @*/
101 extern int errno;
102 /*@=declundef @*/
103 #endif
104
105 #ifdef STDC_HEADERS
106 /*@-macrounrecog -incondefs -globuse -mustmod @*/ /* FIX: shrug */
107 #define getopt system_getopt
108 /*@=macrounrecog =incondefs =globuse =mustmod @*/
109 /*@-skipansiheaders@*/
110 #include <stdlib.h>
111 /*@=skipansiheaders@*/
112 #undef getopt
113 #if defined(__LCLINT__)
114 /*@-declundef -incondefs @*/    /* LCL: modifies clause missing */
115 extern char * realpath (const char * file_name, /*@out@*/ char * resolved_name)
116         /*@requires maxSet(resolved_name) >=  (PATH_MAX - 1); @*/
117         /*@modifies *resolved_name, errno, fileSystem @*/;
118 /*@=declundef =incondefs @*/
119 #endif
120 #else /* not STDC_HEADERS */
121 char *getenv (const char *name);
122 #if ! HAVE_REALPATH
123 char *realpath(const char *path, char resolved_path []);
124 #endif
125 #endif /* STDC_HEADERS */
126
127 /* XXX solaris2.5.1 has not */
128 #if !defined(EXIT_FAILURE)
129 #define EXIT_FAILURE    1
130 #endif
131
132 #ifdef HAVE_FCNTL_H
133 #include <fcntl.h>
134 #else
135 #include <sys/file.h>
136 #endif
137
138 #if !defined(SEEK_SET) && !defined(__LCLINT__)
139 #define SEEK_SET 0
140 #define SEEK_CUR 1
141 #define SEEK_END 2
142 #endif
143 #if !defined(F_OK) && !defined(__LCLINT__)
144 #define F_OK 0
145 #define X_OK 1
146 #define W_OK 2
147 #define R_OK 4
148 #endif
149
150 #ifdef HAVE_DIRENT_H
151 # include <dirent.h>
152 # define NLENGTH(direct) (strlen((direct)->d_name))
153 #else /* not HAVE_DIRENT_H */
154 # define dirent direct
155 # define NLENGTH(direct) ((direct)->d_namlen)
156 # ifdef HAVE_SYS_NDIR_H
157 #  include <sys/ndir.h>
158 # endif /* HAVE_SYS_NDIR_H */
159 # ifdef HAVE_SYS_DIR_H
160 #  include <sys/dir.h>
161 # endif /* HAVE_SYS_DIR_H */
162 # ifdef HAVE_NDIR_H
163 #  include <ndir.h>
164 # endif /* HAVE_NDIR_H */
165 #endif /* HAVE_DIRENT_H */
166
167 #if defined(__LCLINT__)
168 /*@-declundef -incondefs @*/ /* LCL: missing annotation */
169 /*@only@*/ void * alloca (size_t size)
170         /*@ensures MaxSet(result) == (size - 1) @*/
171         /*@*/;
172 /*@=declundef =incondefs @*/
173 #endif
174
175 #ifdef __GNUC__
176 # undef alloca
177 # define alloca __builtin_alloca
178 #else
179 # ifdef HAVE_ALLOCA_H
180 #  include <alloca.h>
181 # else
182 #  ifndef _AIX
183 /* AIX alloca decl has to be the first thing in the file, bletch! */
184 char *alloca ();
185 #  endif
186 # endif
187 #endif
188
189 #include <ctype.h>
190
191 #if HAVE_SYS_MMAN_H && !defined(__LCLINT__)
192 #include <sys/mman.h>
193 #endif
194
195 /* XXX FIXME: popt on sunos4.1.3: <sys/resource.h> requires <sys/time.h> */
196 #if HAVE_SYS_RESOURCE_H && HAVE_SYS_TIME_H
197 #include <sys/resource.h>
198 #endif
199
200 #if HAVE_SYS_UTSNAME_H
201 #include <sys/utsname.h>
202 #endif
203
204 #if HAVE_SYS_WAIT_H
205 #include <sys/wait.h>
206 #endif
207
208 #if HAVE_GETOPT_H
209 /*@-noparams@*/
210 #include <getopt.h>
211 /*@=noparams@*/
212 #endif
213
214 #if HAVE_GRP_H
215 #include <grp.h>
216 #endif
217
218 #if HAVE_LIMITS_H
219 #include <limits.h>
220 #endif
221
222 #if HAVE_ERR_H
223 #include <err.h>
224 #endif
225
226 #if HAVE_MALLOC_H && !defined(__LCLINT__)
227 #include <malloc.h>
228 #endif
229
230 /*@-declundef -incondefs @*/ /* FIX: these are macros */
231 /**
232  */
233 /*@mayexit@*/ /*@only@*/ /*@out@*/ void * xmalloc (size_t size)
234         /*@ensures MaxSet(result) == (size - 1) @*/
235         /*@modifies errno @*/;
236
237 /**
238  */
239 /*@mayexit@*/ /*@only@*/ void * xcalloc (size_t nmemb, size_t size)
240         /*@ensures MaxSet(result) == (nmemb - 1) @*/
241         /*@*/;
242
243 /**
244  * @todo Annotate ptr with returned/out.
245  */
246 /*@mayexit@*/ /*@only@*/ void * xrealloc (/*@null@*/ /*@only@*/ void * ptr,
247                                         size_t size)
248         /*@ensures MaxSet(result) == (size - 1) @*/
249         /*@modifies *ptr @*/;
250
251 /**
252  */
253 /*@mayexit@*/ /*@only@*/ char * xstrdup (const char *str)
254         /*@*/;
255 /*@=declundef =incondefs @*/
256
257 /**
258  */
259 /*@unused@*/ /*@exits@*/ /*@only@*/ void * vmefail(size_t size)
260         /*@*/;
261
262 #if HAVE_MCHECK_H
263 #include <mcheck.h>
264 #if defined(__LCLINT__)
265 /*@-declundef -incondefs @*/ /* LCL: missing annotations */
266 #if 0
267 enum mcheck_status
268   {
269     MCHECK_DISABLED = -1,       /* Consistency checking is not turned on.  */
270     MCHECK_OK,                  /* Block is fine.  */
271     MCHECK_FREE,                /* Block freed twice.  */
272     MCHECK_HEAD,                /* Memory before the block was clobbered.  */
273     MCHECK_TAIL                 /* Memory after the block was clobbered.  */
274   };
275 #endif
276
277 extern int mcheck (void (*__abortfunc) (enum mcheck_status))
278         /*@globals internalState@*/
279         /*@modifies internalState @*/;
280 extern int mcheck_pedantic (void (*__abortfunc) (enum mcheck_status))
281         /*@globals internalState@*/
282         /*@modifies internalState @*/;
283 extern void mcheck_check_all (void)
284         /*@globals internalState@*/
285         /*@modifies internalState @*/;
286 extern enum mcheck_status mprobe (void *__ptr)
287         /*@globals internalState@*/
288         /*@modifies internalState @*/;
289 extern void mtrace (void)
290         /*@globals internalState@*/
291         /*@modifies internalState @*/;
292 extern void muntrace (void)
293         /*@globals internalState@*/
294         /*@modifies internalState @*/;
295 /*@=declundef =incondefs @*/
296 #endif /* defined(__LCLINT__) */
297
298 /* Memory allocation via macro defs to get meaningful locations from mtrace() */
299 #if defined(__GNUC__)
300 #define xmalloc(_size)          (malloc(_size) ? : vmefail(_size))
301 #define xcalloc(_nmemb, _size)  (calloc((_nmemb), (_size)) ? : vmefail(_size))
302 #define xrealloc(_ptr, _size)   (realloc((_ptr), (_size)) ? : vmefail(_size))
303 #define xstrdup(_str)   (strcpy((malloc(strlen(_str)+1) ? : vmefail(strlen(_str)+1)), (_str)))
304 #endif  /* defined(__GNUC__) */
305 #endif  /* HAVE_MCHECK_H */
306
307 /* Retrofit glibc __progname */
308 #if defined __GLIBC__ && __GLIBC__ >= 2
309 #if __GLIBC_MINOR__ >= 1
310 #define __progname      __assert_program_name
311 #endif
312 #define setprogname(pn)
313 #else
314 #define __progname      program_name
315 #define setprogname(pn) \
316   { if ((__progname = strrchr(pn, '/')) != NULL) __progname++; \
317     else __progname = pn;               \
318   }
319 #endif
320 const char *__progname;
321
322 #if HAVE_NETDB_H
323 #include <netdb.h>
324 #endif
325
326 #if HAVE_PWD_H
327 #include <pwd.h>
328 #endif
329
330 /* Take care of NLS matters.  */
331
332 #if HAVE_LOCALE_H
333 # include <locale.h>
334 #endif
335 #if !HAVE_SETLOCALE
336 # define setlocale(Category, Locale) /* empty */
337 #endif
338
339 #if ENABLE_NLS && !defined(__LCLINT__)
340 # include <libintl.h>
341 # define _(Text) gettext (Text)
342 #else
343 # undef bindtextdomain
344 # define bindtextdomain(Domain, Directory) /* empty */
345 # undef textdomain
346 # define textdomain(Domain) /* empty */
347 # define _(Text) Text
348 # undef dgettext
349 # define dgettext(DomainName, Text) Text
350 #endif
351
352 #define N_(Text) Text
353
354 /* ============== from misc/miscfn.h */
355
356 #if !defined(USE_GNU_GLOB)
357 #if HAVE_FNMATCH_H
358 /*@-noparams@*/
359 #include <fnmatch.h>
360 /*@=noparams@*/
361 #endif
362
363 #if HAVE_GLOB_H
364 /*@-noparams@*/
365 #include <glob.h>
366 /*@=noparams@*/
367 #endif
368 #else
369 /*@-noparams@*/
370 #include "misc/glob.h"
371 #include "misc/fnmatch.h"
372 /*@=noparams@*/
373 #endif
374
375 #if defined(__LCLINT__)
376 /*@-declundef -incondefs @*/ /* LCL: missing annotation */
377 #if 0
378 typedef /*@concrete@*/ struct
379   {
380     size_t gl_pathc;
381     char **gl_pathv;
382     size_t gl_offs;
383     int gl_flags;
384
385     void (*gl_closedir) (void *);
386 #ifdef _GNU_SOURCE
387     struct dirent *(*gl_readdir) (void *);
388 #else
389     void *(*gl_readdir) (void *);
390 #endif
391     ptr_t (*gl_opendir) (const char *);
392 #ifdef _GNU_SOURCE
393     int (*gl_lstat) (const char *restrict, struct stat *restrict);
394     int (*gl_stat) (const char *restrict, struct stat *restrict);
395 #else
396     int (*gl_lstat) (const char *restrict, void *restrict);
397     int (*gl_stat) (const char *restrict, void *restrict);
398 #endif
399   } glob_t;
400 #endif
401
402 #if 0
403 /*@-constuse@*/
404 /*@constant int GLOB_ERR@*/
405 /*@constant int GLOB_MARK@*/
406 /*@constant int GLOB_NOSORT@*/
407 /*@constant int GLOB_DOOFFS@*/
408 /*@constant int GLOB_NOCHECK@*/
409 /*@constant int GLOB_APPEND@*/
410 /*@constant int GLOB_NOESCAPE@*/
411 /*@constant int GLOB_PERIOD@*/
412
413 #ifdef _GNU_SOURCE
414 /*@constant int GLOB_MAGCHAR@*/
415 /*@constant int GLOB_ALTDIRFUNC@*/
416 /*@constant int GLOB_BRACE@*/
417 /*@constant int GLOB_NOMAGIC@*/
418 /*@constant int GLOB_TILDE@*/
419 /*@constant int GLOB_ONLYDIR@*/
420 /*@constant int GLOB_TILDE_CHECK@*/
421 #endif
422
423 /*@constant int GLOB_FLAGS@*/
424
425 /*@constant int GLOB_NOSPACE@*/
426 /*@constant int GLOB_ABORTED@*/
427 /*@constant int GLOB_NOMATCH@*/
428 /*@constant int GLOB_NOSYS@*/
429 #ifdef _GNU_SOURCE
430 /*@constant int GLOB_ABEND@*/
431 #endif
432 /*@=constuse@*/
433 #endif
434
435 extern int glob (const char *pattern, int flags,
436                       int (*errfunc) (const char *, int),
437                       /*@out@*/ glob_t *pglob)
438         /*@modifies *pglob, errno, fileSystem @*/;
439         /* XXX only annotation is a white lie */
440 extern void globfree (/*@only@*/ glob_t *pglob)
441         /*@modifies *pglob @*/;
442 #ifdef _GNU_SOURCE
443 extern int glob_pattern_p (const char *pattern, int quote)
444         /*@*/;
445 #endif
446
447 #if 0
448 /*@-constuse@*/
449 /*@constant int FNM_PATHNAME@*/
450 /*@constant int FNM_NOESCAPE@*/
451 /*@constant int FNM_PERIOD@*/
452
453 #ifdef _GNU_SOURCE
454 /*@constant int FNM_FILE_NAME@*/        /* GNU extension */
455 /*@constant int FNM_LEADING_DIR@*/      /* GNU extension */
456 /*@constant int FNM_CASEFOLD@*/         /* GNU extension */
457 /*@constant int FNM_EXTMATCH@*/         /* GNU extension */
458 #endif
459
460 /*@constant int FNM_NOMATCH@*/
461
462 #ifdef _XOPEN_SOURCE
463 /*@constant int FNM_NOSYS@*/            /* X/Open */
464 #endif
465 /*@=constuse@*/
466 #endif
467
468 extern int fnmatch (const char *pattern, const char *string, int flags)
469         /*@*/;
470 /*@=declundef =incondefs @*/
471 #endif
472
473 #if ! HAVE_S_IFSOCK
474 #define S_IFSOCK (0xc000)
475 #endif
476
477 #if ! HAVE_S_ISLNK
478 #define S_ISLNK(mode) ((mode & 0xf000) == S_IFLNK)
479 #endif
480
481 #if ! HAVE_S_ISSOCK
482 #define S_ISSOCK(mode) ((mode & 0xf000) == S_IFSOCK)
483 #endif
484
485 #if NEED_STRINGS_H
486 #include <strings.h>
487 #endif
488
489 #if NEED_MYREALLOC
490 #define realloc(ptr,size) myrealloc(ptr,size)
491 extern void *myrealloc(void *, size_t);
492 #endif
493
494 #if ! HAVE_SETENV
495 extern int setenv(const char *name, const char *value, int replace);
496 extern void unsetenv(const char *name);
497 #endif
498
499 #if HAVE_SYS_SOCKET_H
500 #include <sys/types.h>
501 #include <sys/socket.h>
502 #endif
503
504 #if HAVE_SYS_SELECT_H && !defined(__LCLINT__)
505 #include <sys/select.h>
506 #endif
507
508 /* Solaris <= 2.6 limits getpass return to only 8 chars */
509 #if HAVE_GETPASSPHRASE
510 #define getpass getpassphrase
511 #endif
512
513 #if ! HAVE_LCHOWN
514 #define lchown chown
515 #endif
516
517 #if HAVE_GETMNTINFO_R || HAVE_MNTCTL
518 # define GETMNTENT_ONE 0
519 # define GETMNTENT_TWO 0
520 # if HAVE_SYS_MNTCTL_H
521 #  include <sys/mntctl.h>
522 # endif
523 # if HAVE_SYS_VMOUNT_H
524 #  include <sys/vmount.h>
525 # endif
526 # if HAVE_SYS_MOUNT_H
527 #  include <sys/mount.h>
528 # endif
529 #elif HAVE_MNTENT_H || !(HAVE_GETMNTENT) || HAVE_STRUCT_MNTTAB
530 # if HAVE_MNTENT_H
531 #  include <stdio.h>
532 #  include <mntent.h>
533 #  define our_mntent struct mntent
534 #  define our_mntdir mnt_dir
535 # elif HAVE_STRUCT_MNTTAB
536 #  include <stdio.h>
537 #  include <mnttab.h>
538    struct our_mntent {
539        char * our_mntdir;
540    };
541    struct our_mntent *getmntent(FILE *filep);
542 #  define our_mntent struct our_mntent
543 # else
544 #  include <stdio.h>
545    struct our_mntent {
546        char * our_mntdir;
547    };
548    struct our_mntent *getmntent(FILE *filep);
549 #  define our_mntent struct our_mntent
550 # endif
551 # define GETMNTENT_ONE 1
552 # define GETMNTENT_TWO 0
553 #elif HAVE_SYS_MNTTAB_H
554 # include <stdio.h>
555 # include <sys/mnttab.h>
556 # define GETMNTENT_ONE 0
557 # define GETMNTENT_TWO 1
558 # define our_mntent struct mnttab
559 # define our_mntdir mnt_mountp
560 #else /* if !HAVE_MNTCTL */
561 # error Neither mntent.h, mnttab.h, or mntctl() exists. I cannot build on this system.
562 #endif
563
564 #ifndef MOUNTED
565 #define MOUNTED "/etc/mnttab"
566 #endif
567 #endif  /* H_SYSTEM */