Retrofit glibc __progname.
[platform/upstream/rpm.git] / system.h
1 /* system-dependent definitions for fileutils programs.
2    Copyright (C) 89, 91, 92, 93, 94, 1996 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 #ifndef H_SYSTEM
19 #define H_SYSTEM
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28
29 #ifdef HAVE_SYS_PARAM_H
30 #include <sys/param.h>
31 #endif
32
33 /* <unistd.h> should be included before any preprocessor test
34    of _POSIX_VERSION.  */
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38
39 #if TIME_WITH_SYS_TIME
40 # include <sys/time.h>
41 # include <time.h>
42 #else
43 # if HAVE_SYS_TIME_H
44 #  include <sys/time.h>
45 # else
46 #  include <time.h>
47 # endif
48 #endif
49
50 #if NEED_TIMEZONE
51 extern time_t timezone;
52 #endif
53
54
55 /* Since major is a function on SVR4, we can't use `ifndef major'.  */
56 #if MAJOR_IN_MKDEV
57 #include <sys/mkdev.h>
58 #define HAVE_MAJOR
59 #endif
60 #if MAJOR_IN_SYSMACROS
61 #include <sys/sysmacros.h>
62 #define HAVE_MAJOR
63 #endif
64 #ifdef major                    /* Might be defined in sys/types.h.  */
65 #define HAVE_MAJOR
66 #endif
67
68 #ifndef HAVE_MAJOR
69 #define major(dev)  (((dev) >> 8) & 0xff)
70 #define minor(dev)  ((dev) & 0xff)
71 #define makedev(maj, min)  (((maj) << 8) | (min))
72 #endif
73 #undef HAVE_MAJOR
74
75 #ifdef HAVE_UTIME_H
76 #include <utime.h>
77 #endif
78
79 /* Don't use bcopy!  Use memmove if source and destination may overlap,
80    memcpy otherwise.  */
81
82 #ifdef HAVE_STRING_H
83 # if !STDC_HEADERS && HAVE_MEMORY_H
84 #  include <memory.h>
85 # endif
86 # include <string.h>
87 #else
88 # include <strings.h>
89 char *memchr ();
90 #endif
91
92 #include <errno.h>
93 #ifndef errno
94 extern int errno;
95 #endif
96
97 #ifdef STDC_HEADERS
98 #define getopt system_getopt
99 /*@-skipansiheaders@*/
100 #include <stdlib.h>
101 /*@=skipansiheaders@*/
102 #undef getopt
103 #else /* not STDC_HEADERS */
104 char *getenv (const char *name);
105 #endif /* STDC_HEADERS */
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 (void *ptr, size_t size);
198 /*@only@*/ char * xstrdup (const char *str);
199 void *vmefail(void);
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())
209 #define xcalloc(_nmemb, _size)  (calloc((_nmemb), (_size)) ? : vmefail())
210 #define xrealloc(_ptr, _size)   (realloc((_ptr), (_size)) ? : vmefail())
211 #define xstrdup(_str)   (strcpy((malloc(strlen(_str)+1) ? : vmefail()), (_str)))
212 #endif  /* HAVE_MCHECK_H && defined(__GNUC__) */
213
214 /* Retrofit glibc __progname */
215 char *__progname;
216 #if defined __GLIBC__ && __GLIBC__ >= 2
217 #define setprogname(pn)
218 #else
219 #define setprogname(pn) \
220   { if ((__progname = strrchr(pn, '/')) __progname++; \
221     else __progname = pn;               \
222   }
223 #endif
224
225 #if HAVE_NETDB_H
226 #ifndef __LCLINT__
227 #include <netdb.h>
228 #endif  /* __LCLINT__ */
229 #endif
230
231 #if HAVE_PWD_H
232 #include <pwd.h>
233 #endif
234
235 /* Take care of NLS matters.  */
236
237 #if HAVE_LOCALE_H
238 # include <locale.h>
239 #endif
240 #if !HAVE_SETLOCALE
241 # define setlocale(Category, Locale) /* empty */
242 #endif
243
244 #if ENABLE_NLS && !defined(__LCLINT__)
245 # include <libintl.h>
246 # define _(Text) gettext (Text)
247 #else
248 # undef bindtextdomain
249 # define bindtextdomain(Domain, Directory) /* empty */
250 # undef textdomain
251 # define textdomain(Domain) /* empty */
252 # define _(Text) Text
253 #endif
254
255 #define N_(Text) Text
256
257 /* ============== from misc/miscfn.h */
258
259 #if HAVE_FNMATCH_H
260 #include <fnmatch.h>
261 #else
262 #include "misc/fnmatch.h"
263 #endif
264
265 #if HAVE_GLOB_H
266 #include <glob.h>
267 #else
268 #include "misc/glob.h"
269 #endif
270
271 #if ! HAVE_S_IFSOCK
272 #define S_IFSOCK (0xC000)
273 #endif
274
275 #if ! HAVE_S_ISLNK
276 #define S_ISLNK(mode) ((mode & 0xF000) == S_IFLNK)
277 #endif
278
279 #if ! HAVE_S_ISSOCK
280 #define S_ISSOCK(mode) ((mode & 0xF000) == S_IFSOCK)
281 #endif
282
283 #if NEED_STRINGS_H
284 #include <strings.h>
285 #endif
286
287 #if ! HAVE_REALPATH
288 char *realpath(const char *path, char resolved_path []);
289 #endif
290
291 #if NEED_MYREALLOC
292 #define realloc(ptr,size) myrealloc(ptr,size)
293 extern void *myrealloc(void *, size_t);
294 #endif
295
296 #if HAVE_SYS_SOCKET_H
297 #include <sys/types.h>
298 #ifndef __LCLINT__
299 #include <sys/socket.h>
300 #endif  /* __LCLINT__ */
301 #endif
302
303 #if HAVE_SYS_SELECT_H
304 #include <sys/select.h>
305 #endif
306
307 /* Solaris <= 2.6 limits getpass return to only 8 chars */
308 #if HAVE_GETPASSPHRASE
309 #define getpass getpassphrase
310 #endif
311
312 #if ! HAVE_LCHOWN
313 #define lchown chown
314 #endif
315
316 #if HAVE_GETMNTINFO_R || HAVE_MNTCTL
317 # define GETMNTENT_ONE 0
318 # define GETMNTENT_TWO 0
319 # if HAVE_SYS_MNTCTL_H
320 #  include <sys/mntctl.h>
321 # endif
322 # if HAVE_SYS_VMOUNT_H
323 #  include <sys/vmount.h>
324 # endif
325 # if HAVE_SYS_MOUNT_H
326 #  include <sys/mount.h>
327 # endif
328 #elif HAVE_MNTENT_H || !(HAVE_GETMNTENT) || HAVE_STRUCT_MNTTAB
329 # if HAVE_MNTENT_H
330 #  include <stdio.h>
331 #  include <mntent.h>
332 #  define our_mntent struct mntent
333 #  define our_mntdir mnt_dir
334 # elif HAVE_STRUCT_MNTTAB
335 #  include <stdio.h>
336 #  include <mnttab.h>
337    struct our_mntent {
338        char * our_mntdir;
339    };
340    struct our_mntent *getmntent(FILE *filep);
341 #  define our_mntent struct our_mntent
342 # else
343 #  include <stdio.h>
344    struct our_mntent {
345        char * our_mntdir;
346    };
347    struct our_mntent *getmntent(FILE *filep);
348 #  define our_mntent struct our_mntent
349 # endif
350 # define GETMNTENT_ONE 1
351 # define GETMNTENT_TWO 0
352 #elif HAVE_SYS_MNTTAB_H
353 # include <stdio.h>
354 # include <sys/mnttab.h>
355 # define GETMNTENT_ONE 0
356 # define GETMNTENT_TWO 1
357 # define our_mntent struct mnttab
358 # define our_mntdir mnt_mountp
359 #else /* if !HAVE_MNTCTL */
360 # error Neither mntent.h, mnttab.h, or mntctl() exists. I cannot build on this system.
361 #endif
362
363 #ifndef MOUNTED
364 #define MOUNTED "/etc/mnttab"
365 #endif
366 #endif  /* H_SYSTEM */