b2c9d833a6e5498da7f61b93f9be524439149603
[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 #endif
25
26 #if TIME_WITH_SYS_TIME
27 # include <sys/time.h>
28 # include <time.h>
29 #else
30 # if HAVE_SYS_TIME_H
31 #  include <sys/time.h>
32 # else
33 #  include <time.h>
34 # endif
35 #endif
36
37 #if NEED_TIMEZONE
38 extern time_t timezone;
39 #endif
40
41
42 /* Since major is a function on SVR4, we can't use `ifndef major'.  */
43 #if MAJOR_IN_MKDEV
44 #include <sys/mkdev.h>
45 #define HAVE_MAJOR
46 #endif
47 #if MAJOR_IN_SYSMACROS
48 #include <sys/sysmacros.h>
49 #define HAVE_MAJOR
50 #endif
51 #ifdef major                    /* Might be defined in sys/types.h.  */
52 #define HAVE_MAJOR
53 #endif
54
55 #ifndef HAVE_MAJOR
56 #define major(dev)  (((dev) >> 8) & 0xff)
57 #define minor(dev)  ((dev) & 0xff)
58 #define makedev(maj, min)  (((maj) << 8) | (min))
59 #endif
60 #undef HAVE_MAJOR
61
62 #ifdef HAVE_UTIME_H
63 #include <utime.h>
64 #endif
65
66 /* Don't use bcopy!  Use memmove if source and destination may overlap,
67    memcpy otherwise.  */
68
69 #ifdef HAVE_STRING_H
70 # if !STDC_HEADERS && HAVE_MEMORY_H
71 #  include <memory.h>
72 # endif
73 # include <string.h>
74 #else
75 # include <strings.h>
76 char *memchr ();
77 #endif
78
79 #if !defined(HAVE_STPCPY) || defined(__LCLINT__)
80 char * stpcpy(char * dest, const char * src);
81 #endif
82
83 #if !defined(HAVE_STPNCPY) || defined(__LCLINT__)
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 #ifdef STDC_HEADERS
93 #define getopt system_getopt
94 /*@-skipansiheaders@*/
95 #include <stdlib.h>
96 /*@=skipansiheaders@*/
97 #undef getopt
98 #else /* not STDC_HEADERS */
99 char *getenv (const char *name);
100 #endif /* STDC_HEADERS */
101
102 /* XXX solaris2.5.1 has not */
103 #if !defined(EXIT_FAILURE)
104 #define EXIT_FAILURE    1
105 #endif
106
107 #ifdef HAVE_FCNTL_H
108 #include <fcntl.h>
109 #else
110 #include <sys/file.h>
111 #endif
112
113 #ifndef SEEK_SET
114 #define SEEK_SET 0
115 #define SEEK_CUR 1
116 #define SEEK_END 2
117 #endif
118 #ifndef F_OK
119 #define F_OK 0
120 #define X_OK 1
121 #define W_OK 2
122 #define R_OK 4
123 #endif
124
125 #ifdef HAVE_DIRENT_H
126 # include <dirent.h>
127 # define NLENGTH(direct) (strlen((direct)->d_name))
128 #else /* not HAVE_DIRENT_H */
129 # define dirent direct
130 # define NLENGTH(direct) ((direct)->d_namlen)
131 # ifdef HAVE_SYS_NDIR_H
132 #  include <sys/ndir.h>
133 # endif /* HAVE_SYS_NDIR_H */
134 # ifdef HAVE_SYS_DIR_H
135 #  include <sys/dir.h>
136 # endif /* HAVE_SYS_DIR_H */
137 # ifdef HAVE_NDIR_H
138 #  include <ndir.h>
139 # endif /* HAVE_NDIR_H */
140 #endif /* HAVE_DIRENT_H */
141
142 #ifdef __GNUC__
143 # undef alloca
144 # define alloca __builtin_alloca
145 #else
146 # ifdef HAVE_ALLOCA_H
147 #  include <alloca.h>
148 # else
149 #  ifndef _AIX
150 /* AIX alloca decl has to be the first thing in the file, bletch! */
151 char *alloca ();
152 #  endif
153 # endif
154 #endif
155
156 #include <ctype.h>
157
158 #if HAVE_SYS_MMAN_H
159 #include <sys/mman.h>
160 #endif
161
162 /* XXX FIXME: popt on sunos4.1.3: <sys/resource.h> requires <sys/time.h> */
163 #if HAVE_SYS_RESOURCE_H && HAVE_SYS_TIME_H
164 #include <sys/resource.h>
165 #endif
166
167 #if HAVE_SYS_UTSNAME_H
168 #include <sys/utsname.h>
169 #endif
170
171 #if HAVE_SYS_WAIT_H
172 #include <sys/wait.h>
173 #endif
174
175 #if HAVE_GETOPT_H
176 #include <getopt.h>
177 #endif
178
179 #if HAVE_GRP_H
180 #include <grp.h>
181 #endif
182
183 #if HAVE_LIMITS_H
184 #include <limits.h>
185 #endif
186
187 #if HAVE_ERR_H
188 #include <err.h>
189 #endif
190
191 #if HAVE_MALLOC_H
192 #include <malloc.h>
193 #endif
194
195 /*@only@*/ void * xmalloc (size_t size);
196 /*@only@*/ void * xcalloc (size_t nmemb, size_t size);
197 /*@only@*/ void * xrealloc (/*@only@*/ /*@null@*/ void * ptr, size_t size);
198 /*@only@*/ char * xstrdup (const char *str);
199 /*@only@*/ void *vmefail(size_t size);
200
201 #if HAVE_MCHECK_H
202 #include <mcheck.h>
203 #endif
204
205
206 /* Memory allocation via macro defs to get meaningful locations from mtrace() */
207 #if HAVE_MCHECK_H && defined(__GNUC__)
208 #define xmalloc(_size)          (malloc(_size) ? : vmefail(_size))
209 #define xcalloc(_nmemb, _size)  (calloc((_nmemb), (_size)) ? : vmefail(_size))
210 #define xrealloc(_ptr, _size)   (realloc((_ptr), (_size)) ? : vmefail(_size))
211 #define xstrdup(_str)   (strcpy((malloc(strlen(_str)+1) ? : vmefail(strlen(_str)+1)), (_str)))
212 #endif  /* HAVE_MCHECK_H && defined(__GNUC__) */
213
214 /* Retrofit glibc __progname */
215 #if defined __GLIBC__ && __GLIBC__ >= 2
216 #if __GLIBC_MINOR__ >= 1
217 #define __progname      __assert_program_name
218 #endif
219 #define setprogname(pn)
220 #else
221 #define __progname      program_name
222 #define setprogname(pn) \
223   { if ((__progname = strrchr(pn, '/')) != NULL) __progname++; \
224     else __progname = pn;               \
225   }
226 #endif
227 const char *__progname;
228
229 #if HAVE_NETDB_H
230 #ifndef __LCLINT__
231 #include <netdb.h>
232 #endif  /* __LCLINT__ */
233 #endif
234
235 #if HAVE_PWD_H
236 #include <pwd.h>
237 #endif
238
239 /* Take care of NLS matters.  */
240
241 #if HAVE_LOCALE_H
242 # include <locale.h>
243 #endif
244 #if !HAVE_SETLOCALE
245 # define setlocale(Category, Locale) /* empty */
246 #endif
247
248 #if ENABLE_NLS && !defined(__LCLINT__)
249 # include <libintl.h>
250 # define _(Text) gettext (Text)
251 #else
252 # undef bindtextdomain
253 # define bindtextdomain(Domain, Directory) /* empty */
254 # undef textdomain
255 # define textdomain(Domain) /* empty */
256 # define _(Text) Text
257 # undef dgettext
258 # define dgettext(DomainName, Text) Text
259 #endif
260
261 #define N_(Text) Text
262
263 /* ============== from misc/miscfn.h */
264
265 #if !defined(USE_GNU_GLOB)
266 #if HAVE_FNMATCH_H
267 #include <fnmatch.h>
268 #endif
269
270 #if HAVE_GLOB_H
271 #include <glob.h>
272 #endif
273 #else
274 #include "misc/glob.h"
275 #include "misc/fnmatch.h"
276 #endif
277
278 #if ! HAVE_S_IFSOCK
279 #define S_IFSOCK (0xC000)
280 #endif
281
282 #if ! HAVE_S_ISLNK
283 #define S_ISLNK(mode) ((mode & 0xF000) == S_IFLNK)
284 #endif
285
286 #if ! HAVE_S_ISSOCK
287 #define S_ISSOCK(mode) ((mode & 0xF000) == S_IFSOCK)
288 #endif
289
290 #if NEED_STRINGS_H
291 #include <strings.h>
292 #endif
293
294 #if ! HAVE_REALPATH
295 char *realpath(const char *path, char resolved_path []);
296 #endif
297
298 #if NEED_MYREALLOC
299 #define realloc(ptr,size) myrealloc(ptr,size)
300 extern void *myrealloc(void *, size_t);
301 #endif
302
303 #if ! HAVE_SETENV
304 extern int setenv(const char *name, const char *value, int replace);
305 extern void unsetenv(const char *name);
306 #endif
307
308 #if HAVE_SYS_SOCKET_H
309 #include <sys/types.h>
310 #ifndef __LCLINT__
311 #include <sys/socket.h>
312 #endif  /* __LCLINT__ */
313 #endif
314
315 #if HAVE_SYS_SELECT_H
316 #include <sys/select.h>
317 #endif
318
319 /* Solaris <= 2.6 limits getpass return to only 8 chars */
320 #if HAVE_GETPASSPHRASE
321 #define getpass getpassphrase
322 #endif
323
324 #if ! HAVE_LCHOWN
325 #define lchown chown
326 #endif
327
328 #if HAVE_GETMNTINFO_R || HAVE_MNTCTL
329 # define GETMNTENT_ONE 0
330 # define GETMNTENT_TWO 0
331 # if HAVE_SYS_MNTCTL_H
332 #  include <sys/mntctl.h>
333 # endif
334 # if HAVE_SYS_VMOUNT_H
335 #  include <sys/vmount.h>
336 # endif
337 # if HAVE_SYS_MOUNT_H
338 #  include <sys/mount.h>
339 # endif
340 #elif HAVE_MNTENT_H || !(HAVE_GETMNTENT) || HAVE_STRUCT_MNTTAB
341 # if HAVE_MNTENT_H
342 #  include <stdio.h>
343 #  include <mntent.h>
344 #  define our_mntent struct mntent
345 #  define our_mntdir mnt_dir
346 # elif HAVE_STRUCT_MNTTAB
347 #  include <stdio.h>
348 #  include <mnttab.h>
349    struct our_mntent {
350        char * our_mntdir;
351    };
352    struct our_mntent *getmntent(FILE *filep);
353 #  define our_mntent struct our_mntent
354 # else
355 #  include <stdio.h>
356    struct our_mntent {
357        char * our_mntdir;
358    };
359    struct our_mntent *getmntent(FILE *filep);
360 #  define our_mntent struct our_mntent
361 # endif
362 # define GETMNTENT_ONE 1
363 # define GETMNTENT_TWO 0
364 #elif HAVE_SYS_MNTTAB_H
365 # include <stdio.h>
366 # include <sys/mnttab.h>
367 # define GETMNTENT_ONE 0
368 # define GETMNTENT_TWO 1
369 # define our_mntent struct mnttab
370 # define our_mntdir mnt_mountp
371 #else /* if !HAVE_MNTCTL */
372 # error Neither mntent.h, mnttab.h, or mntctl() exists. I cannot build on this system.
373 #endif
374
375 #ifndef MOUNTED
376 #define MOUNTED "/etc/mnttab"
377 #endif
378 #endif  /* H_SYSTEM */