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