Move getopt() portability fiddles out of system.h
[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 /* Since major is a function on SVR4, we can't use `ifndef major'.  */
47 #if MAJOR_IN_MKDEV
48 #include <sys/mkdev.h>
49 #define HAVE_MAJOR
50 #endif
51 #if MAJOR_IN_SYSMACROS
52 #include <sys/sysmacros.h>
53 #define HAVE_MAJOR
54 #endif
55 #ifdef major                    /* Might be defined in sys/types.h.  */
56 #define HAVE_MAJOR
57 #endif
58
59 #ifndef HAVE_MAJOR
60 #define major(dev)  (((dev) >> 8) & 0xff)
61 #define minor(dev)  ((dev) & 0xff)
62 #define makedev(maj, min)  (((maj) << 8) | (min))
63 #endif
64 #undef HAVE_MAJOR
65
66 #ifdef HAVE_UTIME_H
67 #include <utime.h>
68 #endif
69
70 #ifdef HAVE_STRING_H
71 # if !STDC_HEADERS && HAVE_MEMORY_H
72 #  include <memory.h>
73 # endif
74 # include <string.h>
75 #else
76 # include <strings.h>
77 #endif
78
79 #if !defined(HAVE_STPCPY)
80 char * stpcpy(char * dest, const char * src);
81 #endif
82
83 #if !defined(HAVE_STPNCPY)
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 #if HAVE___SECURE_GETENV
93 #define getenv(_s)      __secure_getenv(_s)
94 #endif
95
96 #ifdef STDC_HEADERS
97 /* FIX: shrug */
98 #define getopt system_getopt
99 #include <stdlib.h>
100 #undef getopt
101 #else /* not STDC_HEADERS */
102 char *getenv (const char *name);
103 #if ! HAVE_REALPATH
104 char *realpath(const char *path, char resolved_path []);
105 #endif
106 #endif /* STDC_HEADERS */
107
108 /* XXX solaris2.5.1 has not */
109 #if !defined(EXIT_FAILURE)
110 #define EXIT_FAILURE    1
111 #endif
112
113 #ifdef HAVE_FCNTL_H
114 #include <fcntl.h>
115 #else
116 #include <sys/file.h>
117 #endif
118
119 #if !defined(SEEK_SET)
120 #define SEEK_SET 0
121 #define SEEK_CUR 1
122 #define SEEK_END 2
123 #endif
124 #if !defined(F_OK) 
125 #define F_OK 0
126 #define X_OK 1
127 #define W_OK 2
128 #define R_OK 4
129 #endif
130
131 #ifdef HAVE_DIRENT_H
132 # include <dirent.h>
133 # define NLENGTH(direct) (strlen((direct)->d_name))
134 #else /* not HAVE_DIRENT_H */
135 # define dirent direct
136 # define NLENGTH(direct) ((direct)->d_namlen)
137 # ifdef HAVE_SYS_NDIR_H
138 #  include <sys/ndir.h>
139 # endif /* HAVE_SYS_NDIR_H */
140 # ifdef HAVE_SYS_DIR_H
141 #  include <sys/dir.h>
142 # endif /* HAVE_SYS_DIR_H */
143 # ifdef HAVE_NDIR_H
144 #  include <ndir.h>
145 # endif /* HAVE_NDIR_H */
146 #endif /* HAVE_DIRENT_H */
147
148 #include <ctype.h>
149
150 #if HAVE_SYS_MMAN_H
151 #include <sys/mman.h>
152 #endif
153
154 #if HAVE_SYS_WAIT_H
155 #include <sys/wait.h>
156 #endif
157
158 #if HAVE_LIMITS_H
159 #include <limits.h>
160 #endif
161
162 #ifndef PATH_MAX
163 #ifdef _POSIX_PATH_MAX
164 #define PATH_MAX _POSIX_PATH_MAX
165 #elif defined MAXPATHLEN
166 #define PATH_MAX MAXPATHLEN
167 #else
168 #define PATH_MAX 256
169 #endif
170 #endif
171
172 #if WITH_SELINUX
173 #include <selinux/selinux.h>
174 #else
175 typedef char * security_context_t;
176
177 #define freecon(_c)
178
179 #define getfilecon(_fn, _c)     (-1)
180 #define lgetfilecon(_fn, _c)    (-1)
181 #define fgetfilecon(_fd, _c)    (-1)
182
183 #define setfilecon(_fn, _c)     (-1)
184 #define lsetfilecon(_fn, _c)    (-1)
185 #define fsetfilecon(_fd, _c)    (-1)
186
187 #define security_check_context(_c)      (0)
188
189 #define is_selinux_enabled()    (0)
190
191 #define matchpathcon_init(_fn)                  (-1)
192 #define matchpathcon_fini()                     (0)
193 #define matchpathcon(_fn, _fm, _c)              (-1)
194
195 #define rpm_execcon(_v, _fn, _av, _envp)        (0)
196 #endif
197
198 #if WITH_CAP
199 #include <sys/capability.h>
200 #else
201 typedef void * cap_t;
202 #endif
203
204 #if WITH_ACL
205 #include <acl/libacl.h>
206 #endif
207
208 #if HAVE_MCHECK_H
209 #include <mcheck.h>
210 #endif  /* HAVE_MCHECK_H */
211
212 #include "rpmio/rpmutil.h"
213 /* compatibility macros to avoid a mass-renaming all over the codebase */
214 #define xmalloc(_size) rmalloc((_size))
215 #define xcalloc(_nmemb, _size) rcalloc((_nmemb), (_size))
216 #define xrealloc(_ptr, _size) rrealloc((_ptr), (_size))
217 #define xstrdup(_str) rstrdup((_str))
218 #define _free(_ptr) rfree((_ptr))
219
220 /* Retrofit glibc __progname */
221 #if defined __GLIBC__ && __GLIBC__ >= 2
222 #if __GLIBC_MINOR__ >= 1
223 #define __progname      __assert_program_name
224 #endif
225 #define setprogname(pn)
226 #else
227 #define __progname      program_name
228 #define setprogname(pn) \
229   { if ((__progname = strrchr(pn, '/')) != NULL) __progname++; \
230     else __progname = pn;               \
231   }
232 #endif
233 extern const char *__progname;
234
235 #if HAVE_NETDB_H
236 #include <netdb.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
249 # include <libintl.h>
250 # define _(Text) dgettext (PACKAGE, 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_SETENV
295 extern int setenv(const char *name, const char *value, int replace);
296 extern void unsetenv(const char *name);
297 #endif
298
299 #if HAVE_POLL_H
300 #include <poll.h>
301 #else
302 #if HAVE_SYS_SELECT_H
303 #include <sys/select.h>
304 #endif
305 #endif
306
307 #if ! HAVE_LCHOWN
308 #define lchown chown
309 #endif
310
311 #if HAVE_GETMNTINFO || HAVE_GETMNTINFO_R || HAVE_MNTCTL
312 # define GETMNTENT_ONE 0
313 # define GETMNTENT_TWO 0
314 # if HAVE_SYS_MNTCTL_H
315 #  include <sys/mntctl.h>
316 # endif
317 # if HAVE_SYS_VMOUNT_H
318 #  include <sys/vmount.h>
319 # endif
320 # if HAVE_SYS_MOUNT_H
321 #  include <sys/mount.h>
322 # endif
323 #elif HAVE_MNTENT_H || !(HAVE_GETMNTENT) || HAVE_STRUCT_MNTTAB
324 # if HAVE_MNTENT_H
325 #  include <stdio.h>
326 #  include <mntent.h>
327 #  define our_mntent struct mntent
328 #  define our_mntdir mnt_dir
329 # elif HAVE_STRUCT_MNTTAB
330 #  include <stdio.h>
331 #  include <mnttab.h>
332    struct our_mntent {
333        char * our_mntdir;
334    };
335    struct our_mntent *getmntent(FILE *filep);
336 #  define our_mntent struct our_mntent
337 # else
338 #  include <stdio.h>
339    struct our_mntent {
340        char * our_mntdir;
341    };
342    struct our_mntent *getmntent(FILE *filep);
343 #  define our_mntent struct our_mntent
344 # endif
345 # define GETMNTENT_ONE 1
346 # define GETMNTENT_TWO 0
347 #elif HAVE_SYS_MNTTAB_H
348 # include <stdio.h>
349 # include <sys/mnttab.h>
350 # define GETMNTENT_ONE 0
351 # define GETMNTENT_TWO 1
352 # define our_mntent struct mnttab
353 # define our_mntdir mnt_mountp
354 #else /* if !HAVE_MNTCTL */
355 # error Neither mntent.h, mnttab.h, or mntctl() exists. I cannot build on this system.
356 #endif
357
358 #ifndef MOUNTED
359 #define MOUNTED "/etc/mnttab"
360 #endif
361
362 #endif  /* H_SYSTEM */