Imported Upstream version 2.5.2p1
[debian/amanda] / common-src / amanda.h
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1999 University of Maryland at College Park
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of U.M. not be used in advertising or
11  * publicity pertaining to distribution of the software without specific,
12  * written prior permission.  U.M. makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  *
16  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26 /*
27  * $Id: amanda.h,v 1.131 2006/07/25 18:27:56 martinea Exp $
28  *
29  * the central header file included by all amanda sources
30  */
31 #ifndef AMANDA_H
32 #define AMANDA_H
33
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 /*
39  * Force large file source even if configure guesses wrong.
40  */
41 #ifndef _LARGE_FILE_SOURCE
42 #define _LARGE_FILES 1
43 #endif
44
45 #ifndef _LARGEFILE64_SOURCE
46 #define _LARGEFILE64_SOURCE 1
47 #endif
48
49 #ifndef  _FILE_OFFSET_BITS
50 #define _FILE_OFFSET_BITS 64
51 #endif
52
53 #ifdef HAVE_SYS_TYPES_H
54 #  include <sys/types.h>
55 #endif
56
57 /*
58  * I would prefer that each Amanda module include only those system headers
59  * that are locally needed, but on most Unixes the system header files are not
60  * protected against multiple inclusion, so this can lead to problems.
61  *
62  * Also, some systems put key files in different places, so by including 
63  * everything here the rest of the system is isolated from such things.
64  */
65 #ifdef HAVE_ALLOCA_H
66 #  include <alloca.h>
67 #endif
68
69 /* from the autoconf documentation */
70 #ifdef HAVE_DIRENT_H
71 #  include <dirent.h>
72 #  define NAMLEN(dirent) strlen((dirent)->d_name)
73 #else
74 #  define dirent direct
75 #  define NAMLEN(dirent) (dirent)->d_namlen
76 #  if HAVE_SYS_NDIR_H
77 #    include <sys/ndir.h>
78 #  endif
79 #  if HAVE_SYS_DIR_H
80 #    include <sys/dir.h>
81 #  endif
82 #  if HAVE_NDIR_H
83 #    include <ndir.h>
84 #  endif
85 #endif
86
87 #ifdef HAVE_FCNTL_H
88 #  include <fcntl.h>
89 #endif
90
91 #ifdef HAVE_GRP_H
92 #  include <grp.h>
93 #endif
94
95 #if defined(USE_DB_H)
96 #  include <db.h>
97 #else
98 #if defined(USE_DBM_H)
99 #  include <dbm.h>
100 #else
101 #if defined(USE_GDBM_H)
102 #  include <gdbm.h>
103 #else
104 #if defined(USE_NDBM_H)
105 #  include <ndbm.h>
106 #endif
107 #endif
108 #endif
109 #endif
110
111 #ifdef HAVE_NETDB_H
112 #  include <netdb.h>
113 #endif
114
115 #ifdef TIME_WITH_SYS_TIME
116 #  include <sys/time.h>
117 #  include <time.h>
118 #else
119 #  ifdef HAVE_SYS_TIME_H
120 #    include <sys/time.h>
121 #  else
122 #    include <time.h>
123 #  endif
124 #endif
125
126 #ifdef HAVE_LIBC_H
127 #  include <libc.h>
128 #endif
129
130 #ifdef HAVE_STDLIB_H
131 #  include <stdlib.h>
132 #endif
133
134 #ifdef HAVE_LIBGEN_H
135 #  include <libgen.h>
136 #endif
137
138 #ifdef HAVE_STRING_H
139 #  include <string.h>
140 #endif
141
142 #ifdef HAVE_STRINGS_H
143 #  include <strings.h>
144 #endif
145
146 #ifdef HAVE_SYSLOG_H
147 #  include <syslog.h>
148 #endif
149
150 #ifdef HAVE_MATH_H
151 #  include <math.h>
152 #endif
153
154 #ifdef HAVE_SYS_FILE_H
155 #  include <sys/file.h>
156 #endif
157
158 #ifdef HAVE_SYS_IOCTL_H
159 #  include <sys/ioctl.h>
160 #endif
161
162 #ifdef HAVE_LIMITS_H
163 #include <limits.h>
164 #endif
165
166 #ifdef HAVE_SYS_PARAM_H
167 #  include <sys/param.h>
168 #endif
169
170 #if defined(HAVE_SYS_IPC_H) && defined(HAVE_SYS_SHM_H)
171 #  include <sys/ipc.h>
172 #  include <sys/shm.h>
173 #else
174 #  ifdef HAVE_SYS_MMAN_H
175 #    include <sys/mman.h>
176 #  endif
177 #endif
178
179 #ifdef HAVE_SYS_SELECT_H
180 #  include <sys/select.h>
181 #endif
182
183 #ifdef HAVE_SYS_STAT_H
184 #  include <sys/stat.h>
185 #endif
186
187 #ifdef HAVE_SYS_UIO_H
188 #  include <sys/uio.h>
189 #else
190 struct iovec {
191     void *iov_base;
192     int iov_len;
193 };
194 #endif
195
196 #ifdef HAVE_WAIT_H
197 #  include <wait.h>
198 #endif
199
200 #ifdef HAVE_SYS_WAIT_H
201 #  include <sys/wait.h>
202 #endif
203
204 #ifdef HAVE_STDARG_H
205 #include <stdarg.h>
206 #endif
207
208 #ifdef WAIT_USES_INT
209   typedef int amwait_t;
210 # ifndef WEXITSTATUS
211 #  define WEXITSTATUS(stat_val) (*(unsigned*)&(stat_val) >> 8)
212 # endif
213 # ifndef WTERMSIG
214 #  define WTERMSIG(stat_val) (*(unsigned*)&(stat_val) & 0x7F)
215 # endif
216 # ifndef WIFEXITED
217 #  define WIFEXITED(stat_val) ((*(unsigned*)&(stat_val) & 255) == 0)
218 # endif
219 #else
220 # ifdef WAIT_USES_UNION
221    typedef union wait amwait_t;
222 #  ifndef WEXITSTATUS
223 #  define WEXITSTATUS(stat_val) (((amwait_t*)&(stat_val))->w_retcode)
224 #  endif
225 #  ifndef WTERMSIG
226 #   define WTERMSIG(stat_val) (((amwait_t*)&(stat_val))->w_termsig)
227 #  endif
228 #  ifndef WIFEXITED
229 #   define WIFEXITED(stat_val) (WTERMSIG(stat_val) == 0)
230 #  endif
231 # else
232    typedef int amwait_t;
233 #  ifndef WEXITSTATUS
234 #   define WEXITSTATUS(stat_val) (*(unsigned*)&(stat_val) >> 8)
235 #  endif
236 #  ifndef WTERMSIG
237 #   define WTERMSIG(stat_val) (*(unsigned*)&(stat_val) & 0x7F)
238 #  endif
239 #  ifndef WIFEXITED
240 #   define WIFEXITED(stat_val) ((*(unsigned*)&(stat_val) & 255) == 0)
241 #  endif
242 # endif
243 #endif
244
245 #ifndef WIFSIGNALED
246 # define WIFSIGNALED(stat_val)  (WTERMSIG(stat_val) != 0)
247 #endif
248
249 #ifdef HAVE_UNISTD_H
250 #  include <unistd.h>
251 #endif
252
253 #include <ctype.h>
254 #include <errno.h>
255 #include <netinet/in.h>
256 #include <pwd.h>
257 #include <signal.h>
258 #include <setjmp.h>
259 #include <stdio.h>
260 #include <sys/resource.h>
261 #include <sys/socket.h>
262
263 #if !defined(CONFIGURE_TEST)
264 #  include "amanda-int.h"
265 #endif
266
267 #ifdef HAVE_ARPA_INET_H
268 #include <arpa/inet.h>
269 #endif
270
271 #ifndef HAVE_SOCKADDR_STORAGE
272 #  define sockaddr_storage sockaddr_in
273 #  define ss_family sin_family
274 #endif
275
276 #ifndef INET_ADDRSTRLEN
277 #define INET_ADDRSTRLEN 16
278 #endif
279
280 /* Calculate the length of the data in a struct sockaddr_storage.
281  * THIS IS A HACK.
282  *
283  * To be truly portable, the length of an address should be passed
284  * in a companion variable.  When such lengths are available
285  * everywhere they are needed, this macro should be removed.
286  */
287 #ifdef WORKING_IPV6
288 # define SS_LEN(ss) (((struct sockaddr *)(ss))->sa_family==AF_INET6?sizeof(struct sockaddr_in6):sizeof(struct sockaddr_in))
289 #else
290 # define SS_LEN(ss) (sizeof(struct sockaddr_in))
291 #endif
292
293
294 /* AF_NATIVE is the "best" address family we support, backward compatible
295  * through to AF_INET.
296  */
297 #ifdef WORKING_IPV6
298 #define AF_NATIVE AF_INET6
299 #else
300 #define AF_NATIVE AF_INET
301 #endif
302
303 /* SS_INIT(ss, family) initializes ss to all zeroes (as directed by RFC),
304  * and sets its ss_family as specified
305  */
306 #define SS_INIT(ss, family) do { \
307     memset((ss), 0, sizeof(*(ss))); \
308     (ss)->ss_family = (family); \
309 } while (0);
310
311 /* SS_SET_INADDR_ANY(ss) sets ss to the family-appropriate equivalent of
312  * INADDR_ANY, a wildcard address and port.
313  */
314 #ifdef WORKING_IPV6
315 #define SS_SET_INADDR_ANY(ss) do { \
316     switch ((ss)->ss_family) { \
317         case AF_INET6: \
318             ((struct sockaddr_in6 *)(ss))->sin6_flowinfo = 0; \
319             ((struct sockaddr_in6 *)(ss))->sin6_addr = in6addr_any; \
320             break; \
321         case AF_INET: \
322             ((struct sockaddr_in *)(ss))->sin_addr.s_addr = INADDR_ANY; \
323             break; \
324     } \
325 } while (0);
326 #else
327 #define SS_SET_INADDR_ANY(ss) do { \
328     ((struct sockaddr_in *)(ss))->sin_addr.s_addr = INADDR_ANY; \
329 } while (0);
330 #endif
331
332 /* Set/get the port in a sockaddr_storage that already has an family */
333 #ifdef WORKING_IPV6
334 #define SS_SET_PORT(ss, port) \
335 switch ((ss)->ss_family) { \
336     case AF_INET: \
337         ((struct sockaddr_in *)(ss))->sin_port = (in_port_t)htons((port)); \
338         break; \
339     case AF_INET6: \
340         ((struct sockaddr_in6 *)(ss))->sin6_port = (in_port_t)htons((port)); \
341         break; \
342     default: assert(0); \
343 }
344 #else
345 #define SS_SET_PORT(ss, port) \
346         ((struct sockaddr_in *)(ss))->sin_port = (in_port_t)htons((port));
347 #endif
348
349 #ifdef WORKING_IPV6
350 #define SS_GET_PORT(ss) (ntohs( \
351        (ss)->ss_family == AF_INET6? \
352         ((struct sockaddr_in6 *)(ss))->sin6_port \
353        :((struct sockaddr_in *)(ss))->sin_port))
354 #else
355 #define SS_GET_PORT(ss) (ntohs( \
356         ((struct sockaddr_in *)(ss))->sin_port))
357 #endif
358
359 /*
360  * The dbmalloc package comes from:
361  *
362  *  http://www.clark.net/pub/dickey/dbmalloc/dbmalloc.tar.gz
363  *
364  * or
365  *
366  *  ftp://gatekeeper.dec.com/pub/usenet/comp.sources.misc/volume32/dbmalloc/
367  *
368  * The following functions are sprinkled through the code, but are
369  * disabled unless USE_DBMALLOC is defined:
370  *
371  *  malloc_enter(char *) -- stack trace for malloc reports
372  *  malloc_leave(char *) -- stack trace for malloc reports
373  *  malloc_mark(void *) -- mark an area as never to be free-d
374  *  malloc_chain_check(void) -- check the malloc area now
375  *  malloc_dump(int fd) -- report the malloc contents to a file descriptor
376  *  malloc_list(int fd, ulong a, ulong b) -- report memory activated since
377  *      history stamp a that is still active as of stamp b (leak check)
378  *  malloc_inuse(ulong *h) -- create history stamp h and return the amount
379  *      of memory currently in use.
380  */
381
382 #ifdef USE_DBMALLOC
383 #include "dbmalloc.h"
384 #else
385 #define malloc_enter(func)              ((void)0)
386 #define malloc_leave(func)              ((void)0)
387 #define malloc_mark(ptr)                ((void)0)
388 #define malloc_chain_check()            ((void)0)
389 #define malloc_dump(fd)                 ((void)0)
390 #define malloc_list(a,b,c)              ((void)0)
391 #define malloc_inuse(hist)              (*(hist) = 0, 0)
392 #define dbmalloc_caller_loc(x,y)        (x)
393 #endif
394
395 #if !defined(HAVE_SIGACTION) && defined(HAVE_SIGVEC)
396 /* quick'n'dirty hack for NextStep31 */
397 #  define sa_flags sv_flags
398 #  define sa_handler sv_handler
399 #  define sa_mask sv_mask
400 #  define sigaction sigvec
401 #  define sigemptyset(mask) /* no way to clear pending signals */
402 #endif
403
404 /*
405  * Most Unixen declare errno in <errno.h>, some don't.  Some multithreaded
406  * systems have errno as a per-thread macro.  So, we have to be careful.
407  */
408 #ifndef errno
409 extern int errno;
410 #endif
411
412 /*
413  * Some compilers have int for type of sizeof() some use size_t.
414  * size_t is the one we want...
415  */
416 #define SIZEOF(x)       (size_t)sizeof(x)
417
418
419 /*
420  * Some older BSD systems don't have these FD_ macros, so if not, provide them.
421  */
422 #if !defined(FD_SET) || defined(LINT) || defined(__lint)
423 #  undef FD_SETSIZE
424 #  define FD_SETSIZE      (int)(SIZEOF(fd_set) * CHAR_BIT)
425
426 #  undef FD_SET
427 #  define FD_SET(n, p)    (((fd_set *)(p))->fds_bits[(n)/WORD_BIT] |= (int)((1 << ((n) % WORD_BIT))))
428
429 #  undef FD_CLR
430 #  define FD_CLR(n, p)    (((fd_set *)(p))->fds_bits[(n)/WORD_BIT] &= (int)(~(1 << ((n) % WORD_BIT))))
431
432 #  undef FD_ISSET
433 #  define FD_ISSET(n, p)  (((fd_set *)(p))->fds_bits[(n)/WORD_BIT] & (1 << ((n) % WORD_BIT)))
434
435 #  undef FD_ZERO
436 #  define FD_ZERO(p)      memset((p), 0, SIZEOF(*(p)))
437 #endif
438
439 #ifndef FD_COPY
440 #  define FD_COPY(p, q)   memcpy((q), (p), SIZEOF(*(p)))
441 #endif
442
443
444 /*
445  * Define MAX_HOSTNAME_LENGTH as the size of arrays to hold hostname's.
446  */
447 #undef  MAX_HOSTNAME_LENGTH
448 #define MAX_HOSTNAME_LENGTH 1025
449
450 /*
451  * If void is broken, substitute char.
452  */
453 #ifdef BROKEN_VOID
454 #  define void char
455 #endif
456
457 #define stringize(x) #x
458 #define stringconcat(x, y) x ## y
459
460 /*
461  * So that we can use GNUC attributes (such as to get -Wall warnings
462  * for printf-like functions).  Only do this in gcc 2.7 or later ...
463  * it may work on earlier stuff, but why chance it.
464  */
465 #if !defined(__GNUC__) || __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7) || defined(S_SPLINT_S) || defined(LINT) || defined(__lint)
466 #undef __attribute__
467 #define __attribute__(__x)
468 #endif
469
470 /*
471  * assertions, but call error() instead of abort 
472  */
473 #ifndef ASSERTIONS
474
475 #define assert(exp) ((void)0)
476
477 #else   /* ASSERTIONS */
478
479 #define assert(exp)     do {                                            \
480     if (!(exp)) {                                                       \
481         onerror(abort);                                                 \
482         error("assert: %s false, file %s, line %d",                     \
483            stringize(exp), __FILE__, __LINE__);                         \
484         /*NOTREACHED*/                                                  \
485     }                                                                   \
486 } while (0)
487
488 #endif  /* ASSERTIONS */
489
490 /*
491  * print debug output, else compile to nothing.
492  */
493
494 #ifdef DEBUG_CODE                                                       /* { */
495 #   define dbopen(a)    debug_open(a)
496 #   define dbreopen(a,b) debug_reopen(a,b)
497 #   define dbrename(a,b) debug_rename(a,b)
498 #   define dbclose()    debug_close()
499 #   define dbprintf(p)  (debug_printf p)
500 #   define dbfd()       debug_fd()
501 #   define dbfp()       debug_fp()
502 #   define dbfn()       debug_fn()
503
504 extern void debug_open(char *subdir);
505 extern void debug_reopen(char *file, char *notation);
506 extern void debug_rename(char *config, char *subdir);
507 extern void debug_close(void);
508 extern void debug_printf(const char *format, ...)
509     __attribute__ ((format (printf, 1, 2)));
510 extern int  debug_fd(void);
511 extern FILE *  debug_fp(void);
512 extern char *  debug_fn(void);
513 extern void set_debug_prefix_pid(pid_t);
514 extern char *debug_prefix(char *);
515 extern char *debug_prefix_time(char *);
516 #else                                                                   /* }{ */
517 #   define dbopen(a)
518 #   define dbreopen(a,b)
519 #   define dbrename(a,b)
520 #   define dbclose()
521 #   define dbprintf(p)
522 #   define dbfd()       (-1)
523 #   define dbfp()       NULL
524 #   define dbfn()       NULL
525 #   define set_debug_prefix_pid(x)
526 #   define debug_prefix(x) get_pname()
527 #   define debug_prefix_time(x) get_pname()
528 #endif                                                                  /* } */
529
530 /* amanda #days calculation, with roundoff */
531
532 #define SECS_PER_DAY    (24*60*60)
533 #define days_diff(a, b) (int)(((b) - (a) + SECS_PER_DAY/2) / SECS_PER_DAY)
534
535 /* Global constants.  */
536 #ifndef AMANDA_SERVICE_NAME
537 #define AMANDA_SERVICE_NAME "amanda"
538 #endif
539 #ifndef KAMANDA_SERVICE_NAME
540 #define KAMANDA_SERVICE_NAME "kamanda"
541 #endif
542 #ifndef SERVICE_SUFFIX
543 #define SERVICE_SUFFIX ""
544 #endif
545 #ifndef AMANDA_SERVICE_DEFAULT
546 #define AMANDA_SERVICE_DEFAULT  ((in_port_t)10080)
547 #endif
548 #ifndef KAMANDA_SERVICE_DEFAULT
549 #define KAMANDA_SERVICE_DEFAULT ((in_port_t)10081)
550 #endif
551
552 #define am_round(v,u)   ((((v) + (u) - 1) / (u)) * (u))
553 #define am_floor(v,u)   (((v) / (u)) * (u))
554
555 /* Holding disk block size.  Do not even think about changint this!  :-) */
556 #define DISK_BLOCK_KB           32
557 #define DISK_BLOCK_BYTES        (DISK_BLOCK_KB * 1024)
558
559 /* Maximum size of a tape block */
560 /* MAX_TAPE_BLOCK_KB is defined in config.h */
561 /* by configure --with-maxtapeblocksize     */
562 #define MAX_TAPE_BLOCK_BYTES (MAX_TAPE_BLOCK_KB*1024)
563
564 /* Maximum length of tape label, plus one for null-terminator. */
565 #define MAX_TAPE_LABEL_LEN (10240)
566 #define MAX_TAPE_LABEL_BUF (MAX_TAPE_LABEL_LEN+1)
567 #define MAX_TAPE_LABEL_FMT "%10240s"
568
569 /* Define miscellaneous amanda functions.  */
570 #define ERR_INTERACTIVE 1
571 #define ERR_SYSLOG      2
572 #define ERR_AMANDALOG   4
573
574 extern void   set_logerror(void (*f)(char *));
575 extern void   set_pname(char *pname);
576 extern char  *get_pname(void);
577 extern int    erroutput_type;
578 extern void   error(const char *format, ...)
579     __attribute__ ((format (printf, 1, 2), noreturn));
580 extern void   errordump(const char *format, ...)
581     __attribute__ ((format (printf, 1, 2), noreturn));
582 extern int    onerror(void (*errf)(void));
583
584 extern void *debug_alloc      (const char *c, int l, size_t size);
585 extern void *debug_newalloc   (const char *c, int l, void *old, size_t size);
586 extern char *debug_stralloc   (const char *c, int l, const char *str);
587 extern char *debug_newstralloc(const char *c, int l, char *oldstr,
588                                const char *newstr);
589 extern const char *debug_caller_loc (const char *file, int line);
590 extern int debug_alloc_push (char *file, int line);
591 extern void debug_alloc_pop (void);
592
593 #define alloc(s)                debug_alloc(__FILE__, __LINE__, (s))
594 #define newalloc(p,s)           debug_newalloc(__FILE__, __LINE__, (p), (s))
595 #define stralloc(s)             debug_stralloc(__FILE__, __LINE__, (s))
596 #define newstralloc(p,s)        debug_newstralloc(__FILE__, __LINE__, (p), (s))
597
598 /*
599  * Voodoo time.  We want to be able to mark these calls with the source
600  * line, but CPP does not handle variable argument lists so we cannot
601  * do what we did above (e.g. for alloc()).
602  *
603  * What we do is call a function to save the file and line number
604  * and have it return "false".  That triggers the "?" operator to
605  * the right side of the ":" which is a call to the debug version of
606  * vstralloc/newvstralloc but without parameters.  The compiler gets
607  * those from the next input tokens:
608  *
609  *  xx = vstralloc(a,b,NULL);
610  *
611  * becomes:
612  *
613  *  xx = debug_alloc_push(__FILE__,__LINE__)?0:debug_vstralloc(a,b,NULL);
614  *
615  * This works as long as vstralloc/newvstralloc are not part of anything
616  * very complicated.  Assignment is fine, as is an argument to another
617  * function (but you should not do that because it creates a memory leak).
618  * This will not work in arithmetic or comparison, but it is unlikely
619  * they are used like that.
620  *
621  *  xx = vstralloc(a,b,NULL);                   OK
622  *  return vstralloc(j,k,NULL);                 OK
623  *  sub(a, vstralloc(g,h,NULL), z);             OK, but a leak
624  *  if(vstralloc(s,t,NULL) == NULL) { ...       NO, but unneeded
625  *  xx = vstralloc(x,y,NULL) + 13;              NO, but why do it?
626  */
627
628 #define vstralloc debug_alloc_push(__FILE__,__LINE__)?0:debug_vstralloc
629 #define newvstralloc debug_alloc_push(__FILE__,__LINE__)?0:debug_newvstralloc
630 #define vstrallocf debug_alloc_push(__FILE__,__LINE__)?0:debug_vstrallocf
631
632 extern char  *debug_vstralloc(const char *str, ...);
633 extern char  *debug_newvstralloc(char *oldstr, const char *newstr, ...);
634 extern char  *debug_vstrallocf(const char *fmt, ...)
635                                __attribute__ ((format (printf, 1, 2)));
636
637 #define stralloc2(s1,s2)      vstralloc((s1),(s2),NULL)
638 #define newstralloc2(p,s1,s2) newvstralloc((p),(s1),(s2),NULL)
639
640 /* Usage: vstrextend(foo, "bar, "baz", NULL). Extends the existing 
641  * string, or allocates a brand new one. */
642 extern char *vstrextend(char **oldstr, ...);
643
644 extern /*@only@*/ /*@null@*/ char *debug_agets(const char *c, int l, FILE *file);
645 extern /*@only@*/ /*@null@*/ char *debug_areads(const char *c, int l, int fd);
646 #define agets(f)              debug_agets(__FILE__,__LINE__,(f))
647 #define areads(f)             debug_areads(__FILE__,__LINE__,(f))
648
649 extern int debug_amtable_alloc(const char *file,
650                                   int line,
651                                   void **table,
652                                   size_t *current,
653                                   size_t elsize,
654                                   size_t count,
655                                   int bump,
656                                   void (*init_func)(void *));
657
658 #define amtable_alloc(t,c,s,n,b,f) debug_amtable_alloc(__FILE__,      \
659                                                      __LINE__,        \
660                                                      (t),             \
661                                                      (c),             \
662                                                      (s),             \
663                                                      (n),             \
664                                                      (b),             \
665                                                      (f))
666
667 extern void amtable_free(void **table, size_t *current);
668
669 extern uid_t  client_uid;
670 extern gid_t  client_gid;
671
672 void    safe_fd(int fd_start, int fd_count);
673 void    safe_cd(void);
674 void    save_core(void);
675 char ** safe_env(void);
676 char *  validate_regexp(const char *regex);
677 char *  validate_glob(const char *glob);
678 char *  clean_regex(const char *regex);
679 int     match(const char *regex, const char *str);
680 int     match_glob(const char *glob, const char *str);
681 char *  glob_to_regex(const char *glob);
682 int     match_tar(const char *glob, const char *str);
683 char *  tar_to_regex(const char *glob);
684 int     match_host(const char *glob, const char *host);
685 int     match_disk(const char *glob, const char *disk);
686 int     match_datestamp(const char *dateexp, const char *datestamp);
687 int     match_level(const char *levelexp, const char *level);
688 time_t  unctime(char *timestr);
689 ssize_t areads_dataready(int fd);
690 void    areads_relbuf(int fd);
691
692 /*
693  * amfree(ptr) -- if allocated, release space and set ptr to NULL.
694  *
695  * In general, this should be called instead of just free(), unless
696  * the very next source line sets the pointer to a new value.
697  */
698
699 #define amfree(ptr) do {                                                \
700     if((ptr) != NULL) {                                                 \
701         int e__errno = errno;                                           \
702         free(ptr);                                                      \
703         (ptr) = NULL;                                                   \
704         errno = e__errno;                                               \
705         (void)(ptr);  /* Fix value never used warning at end of routines */ \
706     }                                                                   \
707 } while (0)
708
709 #define strappend(s1,s2) do {                                           \
710     char *t_t_t = (s1) ? stralloc2((s1),(s2)) : stralloc((s2));         \
711     amfree((s1));                                                       \
712     (s1) = t_t_t;                                                       \
713 } while(0)
714
715 /*
716  * "Safe" close macros.  Close the object then set it to a value that
717  * will cause an error if referenced.
718  *
719  * aclose(fd) -- close a file descriptor and set it to -1.
720  * afclose(f) -- close a stdio file and set it to NULL.
721  * apclose(p) -- close a stdio pipe file and set it to NULL.
722  *
723  * Note: be careful not to do the following:
724  *
725  *  for(fd = low; fd < high; fd++) {
726  *      aclose(fd);
727  *  }
728  *
729  * Since aclose() sets the argument to -1, this will loop forever.
730  * Just copy fd to a temp variable and use that with aclose().
731  *
732  * Aclose() interacts with areads() to inform it to release any buffer
733  * it has outstanding on the file descriptor.
734  */
735
736 #define aclose(fd) do {                                                 \
737     if((fd) >= 0) {                                                     \
738         close(fd);                                                      \
739         areads_relbuf(fd);                                              \
740     }                                                                   \
741     (fd) = -1;                                                          \
742     (void)(fd);  /* Fix value never used warning at end of routines */ \
743 } while(0)
744
745 #define afclose(f) do {                                                 \
746     if((f) != NULL) {                                                   \
747         fclose(f);                                                      \
748         (f) = NULL;                                                     \
749         (void)(f);  /* Fix value never used warning at end of routines */ \
750     }                                                                   \
751 } while(0)
752
753 #define apclose(p) do {                                                 \
754     if((p) != NULL) {                                                   \
755         pclose(p);                                                      \
756         (p) = NULL;                                                     \
757         (void)(p);  /* Fix value never used warning at end of routines */ \
758     }                                                                   \
759 } while(0)
760
761 /*
762  * Return the number of elements in an array.
763  */
764 #define am_countof(a)   (int)(SIZEOF(a) / SIZEOF((a)[0]))
765
766 /*
767  * min/max.  Don't do something like
768  *
769  *    x = min(y++, z);
770  *
771  * because the increment will be duplicated.
772  */
773 #undef min
774 #undef max
775 #define min(a, b)       ((a) < (b) ? (a) : (b))
776 #define max(a, b)       ((a) > (b) ? (a) : (b))
777
778 /*
779  * Utility bitmask manipulation macros.
780  */
781 #define SET(t, f)       ((t) |= (f))
782 #define CLR(t, f)       ((t) &= ~((unsigned)(f)))
783 #define ISSET(t, f)     ((t) & (f))
784
785 /*
786  * Utility string macros.  All assume a variable holds the current
787  * character and the string pointer points to the next character to
788  * be processed.  Typical setup is:
789  *
790  *  s = buffer;
791  *  ch = *s++;
792  *  skip_whitespace(s, ch);
793  *  ...
794  *
795  * If you advance the pointer "by hand" to skip over something, do
796  * it like this:
797  *
798  *  s += some_amount;
799  *  ch = s[-1];
800  *
801  * Note that ch has the character at the end of the just skipped field.
802  * It is often useful to terminate a string, make a copy, then restore
803  * the input like this:
804  *
805  *  skip_whitespace(s, ch);
806  *  fp = s-1;                   ## save the start
807  *  skip_nonwhitespace(s, ch);  ## find the end
808  *  p[-1] = '\0';               ## temporary terminate
809  *  field = stralloc(fp);       ## make a copy
810  *  p[-1] = ch;                 ## restore the input
811  *
812  * The scanning macros are:
813  *
814  *  skip_whitespace (ptr, var)
815  *    -- skip whitespace, but stops at a newline
816  *  skip_non_whitespace (ptr, var)
817  *    -- skip non whitespace
818  *  skip_non_whitespace_cs (ptr, var)
819  *    -- skip non whitespace, stop at comment
820  *  skip_integer (ptr, var)
821  *    -- skip an integer field
822  *  skip_line (ptr, var)
823  *    -- skip just past the next newline
824  *  strncmp_const (str, const_str)
825  *    -- compare str to const_str, a string constant
826  *  strncmp_const_skip (str, const_var, ptr, var)
827  *    -- like strncmp_const, but skip the string if a match is
828  *       found; this macro only tests for equality, discarding
829  *       ordering information.
830  *
831  * where:
832  *
833  *  ptr -- string pointer
834  *  var -- current character
835  *
836  * These macros copy a non-whitespace field to a new buffer, and should
837  * only be used if dynamic allocation is impossible (fixed size buffers
838  * are asking for trouble):
839  *
840  *  copy_string (ptr, var, field, len, fldptr)
841  *    -- copy a non-whitespace field
842  *  copy_string_cs (ptr, var, field, len, fldptr)
843  *    -- copy a non-whitespace field, stop at comment
844  *
845  * where:
846  *
847  *  ptr -- string pointer
848  *  var -- current character
849  *  field -- area to copy to
850  *  len -- length of area (needs room for null byte)
851  *  fldptr -- work pointer used in move
852  *            if NULL on exit, the field was too small for the input
853  */
854
855 #define STR_SIZE        4096            /* a generic string buffer size */
856 #define NUM_STR_SIZE    128             /* a generic number buffer size */
857
858 #define skip_whitespace(ptr,c) do {                                     \
859     while((c) != '\n' && isspace(c)) (c) = *(ptr)++;                    \
860 } while(0)
861
862 #define skip_non_whitespace(ptr,c) do {                                 \
863     while((c) != '\0' && !isspace(c)) (c) = *(ptr)++;                   \
864 } while(0)
865
866 #define skip_non_whitespace_cs(ptr,c) do {                              \
867     while((c) != '\0' && (c) != '#' && !isspace(c)) (c) = *(ptr)++;     \
868 } while(0)
869
870 #define skip_non_integer(ptr,c) do {                                    \
871     while((c) != '\0' && !isdigit(c)) (c) = *(ptr)++;                   \
872 } while(0)
873
874 #define skip_integer(ptr,c) do {                                        \
875     if((c) == '+' || (c) == '-') (c) = *(ptr)++;                        \
876     while(isdigit(c)) (c) = *(ptr)++;                                   \
877 } while(0)
878
879 #define skip_quoted_string(ptr, c) do {                                 \
880     int iq = 0;                                                         \
881     while (((c) != '\0') && !((iq == 0) && isspace(c))) {               \
882         if ((c) == '"') {                                               \
883             iq = !iq;                                                   \
884         } else if (((c) == '\\') && (*(ptr) == '"')) {                  \
885             (ptr)++;                                                    \
886         }                                                               \
887         (c) = *(ptr)++;                                                 \
888     }                                                                   \
889 } while (0)
890
891 #define skip_quoted_line(ptr, c) do {                                   \
892     int iq = 0;                                                         \
893     while((c) && !((iq == 0) && ((c) == '\n'))) {                       \
894         if ((c) == '"')                                                 \
895             iq = !iq;                                                   \
896         (c) = *(ptr)++;                                                 \
897     }                                                                   \
898     if(c)                                                               \
899         (c) = *(ptr)++;                                                 \
900 } while(0)
901
902 #define skip_line(ptr,c) do {                                           \
903     while((c) && (c) != '\n')                                           \
904         (c) = *(ptr)++;                                                 \
905     if(c)                                                               \
906         (c) = *(ptr)++;                                                 \
907 } while(0)
908
909 #define copy_string(ptr,c,f,l,fp) do {                                  \
910     (fp) = (f);                                                         \
911     while((c) != '\0' && !isspace(c)) {                                 \
912         if((fp) >= (f) + (l) - 1) {                                     \
913             *(fp) = '\0';                                               \
914             (fp) = NULL;                                                \
915             (void)(fp);  /* Fix value never used warning at end of routines */ \
916             break;                                                      \
917         }                                                               \
918         *(fp)++ = (c);                                                  \
919         (c) = *(ptr)++;                                                 \
920     }                                                                   \
921     if(fp)                                                              \
922         *fp = '\0';                                                     \
923 } while(0)
924
925 #define copy_string_cs(ptr,c,f,l,fp) do {                               \
926     (fp) = (f);                                                         \
927     while((c) != '\0' && (c) != '#' && !isspace(c)) {                   \
928         if((fp) >= (f) + (l) - 1) {                                     \
929             *(fp) = '\0';                                               \
930             (fp) = NULL;                                                \
931             break;                                                      \
932         }                                                               \
933         *(fp)++ = (c);                                                  \
934         (c) = *(ptr)++;                                                 \
935     }                                                                   \
936     if(fp) *fp = '\0';                                                  \
937 } while(0)
938
939 #define is_dot_or_dotdot(s)                                             \
940     ((s)[0] == '.'                                                      \
941      && ((s)[1] == '\0'                                                 \
942          || ((s)[1] == '.' && (s)[2] == '\0')))
943
944 #define strncmp_const(str, cnst)                                        \
945         strncmp((str), (cnst), sizeof((cnst))-1)
946
947 /* (have to roll this up in an expression, so it can be used in if()) */
948 #define strncmp_const_skip(str, cnst, ptr, var)                         \
949         ((strncmp((str), (cnst), sizeof((cnst))-1) == 0)?               \
950                  ((ptr)+=sizeof((cnst))-1, (var)=(ptr)[-1], 0)          \
951                 :1)
952
953 /* from amflock.c */
954 extern int    amflock(int fd, char *resource);
955 extern int    amroflock(int fd, char *resource);
956 extern int    amfunlock(int fd, char *resource);
957
958 /* from file.c */
959 extern int    mkpdir(char *file, mode_t mode, uid_t uid, gid_t gid);
960 extern int    rmpdir(char *file, char *topdir);
961 extern char  *sanitise_filename(char *inp);
962 extern char  *old_sanitise_filename(char *inp);
963
964 /* from old bsd-security.c */
965 extern int debug;
966 extern int check_security(struct sockaddr_storage *, char *, unsigned long, char **);
967
968 /*
969  * Handle functions which are not always declared on all systems.  This
970  * stops gcc -Wall and lint from complaining.
971  */
972
973 /* AIX #defines accept, and provides a prototype for the alternate name */
974 #if !defined(HAVE_ACCEPT_DECL) && !defined(accept)
975 extern int accept(int s, struct sockaddr *addr, socklen_t *addrlen);
976 #endif
977
978 #ifndef HAVE_ATOF_DECL
979 extern double atof(const char *ptr);
980 #endif
981
982 #ifndef HAVE_BCOPY
983 # define bcopy(from,to,n) ((void)memmove((to), (from), (n)))
984 #else
985 # ifndef HAVE_BCOPY_DECL
986 extern void bcopy(const void *s1, void *s2, size_t n);
987 # endif
988 #endif
989
990 #ifndef HAVE_BIND_DECL
991 extern int bind(int s, const struct sockaddr *name, socklen_t namelen);
992 #endif
993
994 #ifndef HAVE_BZERO
995 #define bzero(s,n) ((void)memset((s),0,(n)))
996 #else
997 # ifndef HAVE_BZERO_DECL
998 extern void bzero(void *s, size_t n);
999 # endif
1000 #endif
1001
1002 #ifndef HAVE_CLOSELOG_DECL
1003 extern void closelog(void);
1004 #endif
1005
1006 #ifndef HAVE_CONNECT_DECL
1007 extern int connect(int s, struct sockaddr *name, socklen_t namelen);
1008 #endif
1009
1010 #if !defined(TEXTDB) && !defined(HAVE_DBM_OPEN_DECL)
1011 #undef   DBM_INSERT
1012 #define  DBM_INSERT  0
1013
1014 #undef   DBM_REPLACE
1015 #define  DBM_REPLACE 1
1016
1017     typedef struct {
1018         int dummy[10];
1019     } DBM;
1020
1021 #ifndef HAVE_STRUCT_DATUM
1022     typedef struct {
1023         char    *dptr;
1024         int     dsize;
1025     } datum;
1026 #endif
1027
1028     extern DBM   *dbm_open(char *file, int flags, int mode);
1029     extern void   dbm_close(DBM *db);
1030     extern datum  dbm_fetch(DBM *db, datum key);
1031     extern datum  dbm_firstkey(DBM *db);
1032     extern datum  dbm_nextkey(DBM *db);
1033     extern int    dbm_delete(DBM *db, datum key);
1034     extern int    dbm_store(DBM *db, datum key, datum content, int flg);
1035 #endif
1036
1037 #ifndef HAVE_FCLOSE_DECL
1038 extern int fclose(FILE *stream);
1039 #endif
1040
1041 #ifndef HAVE_FFLUSH_DECL
1042 extern int fflush(FILE *stream);
1043 #endif
1044
1045 #ifndef HAVE_FPRINTF_DECL
1046 extern int fprintf(FILE *stream, const char *format, ...);
1047 #endif
1048
1049 #ifndef HAVE_FPUTC_DECL
1050 extern int fputc(int c, FILE *stream);
1051 #endif
1052
1053 #ifndef HAVE_FPUTS_DECL
1054 extern int fputs(const char *s, FILE *stream);
1055 #endif
1056
1057 #ifndef HAVE_FREAD_DECL
1058 extern size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
1059 #endif
1060
1061 #ifndef HAVE_FSEEK_DECL
1062 extern int fseek(FILE *stream, long offset, int ptrname);
1063 #endif
1064
1065 #ifndef HAVE_FWRITE_DECL
1066 extern size_t fwrite(const void *ptr, size_t size, size_t nitems,
1067                         FILE *stream);
1068 #endif
1069
1070 #ifndef HAVE_GETHOSTNAME_DECL
1071 extern int gethostname(char *name, int namelen);
1072 #endif
1073
1074 #ifndef HAVE_GETOPT_DECL
1075 extern char *optarg;
1076 extern int getopt(int argc, char * const *argv, const char *optstring);
1077 #endif
1078
1079 /* AIX #defines getpeername, and provides a prototype for the alternate name */
1080 #if !defined(HAVE_GETPEERNAME_DECL) && !defined(getpeername)
1081 extern int getpeername(int s, struct sockaddr *name, socklen_t *namelen);
1082 #endif
1083
1084 /* AIX #defines getsockname, and provides a prototype for the alternate name */
1085 #if !defined(HAVE_GETSOCKNAME_DECL) && !defined(getsockname)
1086 extern int getsockname(int s, struct sockaddr *name, socklen_t *namelen);
1087 #endif
1088
1089 #ifndef HAVE_GETSOCKOPT_DECL
1090 extern int getsockopt(int s, int level, int optname, char *optval,
1091                          socklen_t *optlen);
1092 #endif
1093
1094 #ifndef HAVE_GETTIMEOFDAY_DECL
1095 # ifdef HAVE_TWO_ARG_GETTIMEOFDAY
1096 extern int gettimeofday(struct timeval *tp, struct timezone *tzp);
1097 # else
1098 extern int gettimeofday(struct timeval *tp);
1099 # endif
1100 #endif
1101
1102 #ifndef HAVE_INITGROUPS
1103 # define initgroups(name,basegid) 0
1104 #else
1105 # ifndef HAVE_INITGROUPS_DECL
1106 extern int initgroups(const char *name, gid_t basegid);
1107 # endif
1108 #endif
1109
1110 #ifndef HAVE_IOCTL_DECL
1111 extern int ioctl(int fildes, int request, ...);
1112 #endif
1113
1114 #ifndef isnormal
1115 #ifndef HAVE_ISNORMAL
1116 #define isnormal(f) (((f) < 0.0) || ((f) > 0.0))
1117 #endif
1118 #endif
1119
1120 #ifndef HAVE_LISTEN_DECL
1121 extern int listen(int s, int backlog);
1122 #endif
1123
1124 #ifndef HAVE_LSTAT_DECL
1125 extern int lstat(const char *path, struct stat *buf);
1126 #endif
1127
1128 #ifndef HAVE_MALLOC_DECL
1129 extern void *malloc (size_t size);
1130 #endif
1131
1132 #ifndef HAVE_MEMMOVE_DECL
1133 #ifdef HAVE_MEMMOVE
1134 extern void *memmove(void *to, const void *from, size_t n);
1135 #else
1136 extern char *memmove(char *to, /*const*/ char *from, size_t n);
1137 #endif
1138 #endif
1139
1140 #ifndef HAVE_MEMSET_DECL
1141 extern void *memset(void *s, int c, size_t n);
1142 #endif
1143
1144 #ifndef HAVE_MKTEMP_DECL
1145 extern char *mktemp(char *template);
1146 #endif
1147
1148 #ifndef HAVE_MKSTEMP_DECL
1149 extern int mkstemp(char *template);
1150 #endif
1151
1152 #ifndef HAVE_MKTIME_DECL
1153 extern time_t mktime(struct tm *timeptr);
1154 #endif
1155
1156 #ifndef HAVE_OPENLOG_DECL
1157 #ifdef LOG_AUTH
1158 extern void openlog(const char *ident, int logopt, int facility);
1159 #else
1160 extern void openlog(const char *ident, int logopt);
1161 #endif
1162 #endif
1163
1164 #ifndef HAVE_PCLOSE_DECL
1165 extern int pclose(FILE *stream);
1166 #endif
1167
1168 #ifndef HAVE_PERROR_DECL
1169 extern void perror(const char *s);
1170 #endif
1171
1172 #ifndef HAVE_PRINTF_DECL
1173 extern int printf(const char *format, ...);
1174 #endif
1175
1176 #ifndef HAVE_PUTS_DECL
1177 extern int puts(const char *s);
1178 #endif
1179
1180 #ifndef HAVE_REALLOC_DECL
1181 extern void *realloc(void *ptr, size_t size);
1182 #endif
1183
1184 /* AIX #defines recvfrom, and provides a prototype for the alternate name */
1185 #if !defined(HAVE_RECVFROM_DECL) && !defined(recvfrom)
1186 extern int recvfrom(int s, char *buf, int len, int flags,
1187                        struct sockaddr *from, socklen_t *fromlen);
1188 #endif
1189
1190 #ifndef HAVE_REMOVE_DECL
1191 extern int remove(const char *path);
1192 #endif
1193
1194 #ifndef HAVE_RENAME_DECL
1195 extern int rename(const char *old, const char *new);
1196 #endif
1197
1198 #ifndef HAVE_REWIND_DECL
1199 extern void rewind(FILE *stream);
1200 #endif
1201
1202 #ifndef HAVE_RUSEROK_DECL
1203 extern int ruserok(const char *rhost, int suser,
1204                       const char *ruser, const char *luser);
1205 #endif
1206
1207 #ifndef HAVE_SELECT_DECL
1208 extern int select(int nfds,
1209                      SELECT_ARG_TYPE *readfds,
1210                      SELECT_ARG_TYPE *writefds,
1211                      SELECT_ARG_TYPE *exceptfds,
1212                      struct timeval *timeout);
1213 #endif
1214
1215 #ifndef HAVE_SENDTO_DECL
1216 extern int sendto(int s, const char *msg, int len, int flags,
1217                      const struct sockaddr *to, int tolen);
1218 #endif
1219
1220 #ifdef HAVE_SETRESGID
1221 #define setegid(x)      setresgid((gid_t)-1,(x),(gid_t)-1)
1222 #ifndef HAVE_SETRESGID_DECL
1223 extern int setresgid(gid_t rgid, gid_t egid, gid_t sgid);
1224 #endif
1225 #else
1226 #ifndef HAVE_SETEGID_DECL
1227 extern int setegid(gid_t egid);
1228 #endif
1229 #endif
1230
1231 #ifdef HAVE_SETRESUID
1232 #define seteuid(x)      setresuid((uid_t)-1,(x),(uid_t)-1)
1233 #ifndef HAVE_SETRESUID_DECL
1234 extern int setresuid(uid_t ruid, uid_t euid, uid_t suid);
1235 #endif
1236 #else
1237 #ifndef HAVE_SETEUID_DECL
1238 extern int seteuid(uid_t euid);
1239 #endif
1240 #endif
1241
1242 #ifndef HAVE_SETPGID_DECL
1243 #ifdef HAVE_SETPGID
1244 extern int setpgid(pid_t pid, pid_t pgid);
1245 #endif
1246 #endif
1247
1248 #ifndef HAVE_SETPGRP_DECL
1249 #ifdef SETPGRP_VOID
1250 extern pid_t setpgrp(void);
1251 #else
1252 extern pid_t setpgrp(pid_t pgrp, pid_t pid);
1253 #endif
1254 #endif
1255
1256 #ifndef HAVE_SETSOCKOPT_DECL
1257 extern int setsockopt(int s, int level, int optname,
1258                          const char *optval, int optlen);
1259 #endif
1260
1261 #ifdef HAVE_SHMGET
1262 #ifndef HAVE_SHMAT_DECL
1263 extern void *shmat(int shmid, const SHM_ARG_TYPE *shmaddr, int shmflg);
1264 #endif
1265
1266 #ifndef HAVE_SHMCTL_DECL
1267 extern int shmctl(int shmid, int cmd, struct shmid_ds *buf);
1268 #endif
1269
1270 #ifndef HAVE_SHMDT_DECL
1271 extern int shmdt(SHM_ARG_TYPE *shaddr);
1272 #endif
1273
1274 #ifndef HAVE_SHMGET_DECL
1275 extern int shmget(key_t key, size_t size, int shmflg);
1276 #endif
1277 #endif
1278
1279 #ifndef HAVE_SNPRINTF_DECL
1280 #include "arglist.h"
1281 int snprintf(char *buf, size_t len, const char *format,...)
1282                     __attribute__((format(printf,3,4)));
1283 #endif
1284 #ifndef HAVE_VSNPRINTF_DECL
1285 #include "arglist.h"
1286 int vsnprintf(char *buf, size_t len, const char *format, va_list ap);
1287 #endif
1288
1289 #ifndef HAVE_SOCKET_DECL
1290 extern int socket(int domain, int type, int protocol);
1291 #endif
1292
1293 #ifndef HAVE_SOCKETPAIR_DECL
1294 extern int socketpair(int domain, int type, int protocol, int sv[2]);
1295 #endif
1296
1297 #ifndef HAVE_SSCANF_DECL
1298 extern int sscanf(const char *s, const char *format, ...);
1299 #endif
1300
1301 #ifndef HAVE_STRCASECMP_DECL
1302 extern int strcasecmp(const char *s1, const char *s2);
1303 #endif
1304
1305 #ifndef HAVE_STRERROR_DECL
1306 extern char *strerror(int errnum);
1307 #endif
1308
1309 #ifndef HAVE_STRFTIME_DECL
1310 extern size_t strftime(char *s, size_t maxsize, const char *format,
1311                           const struct tm *timeptr);
1312 #endif
1313
1314 #ifndef HAVE_STRNCASECMP_DECL
1315 extern int strncasecmp(const char *s1, const char *s2, int n);
1316 #endif
1317
1318 #ifndef HAVE_SYSLOG_DECL
1319 extern void syslog(int priority, const char *logstring, ...)
1320     __attribute__ ((format (printf, 2, 3)));
1321 #endif
1322
1323 #ifndef HAVE_SYSTEM_DECL
1324 extern int system(const char *string);
1325 #endif
1326
1327 #ifndef HAVE_TIME_DECL
1328 extern time_t time(time_t *tloc);
1329 #endif
1330
1331 #ifndef HAVE_TOLOWER_DECL
1332 extern int tolower(int c);
1333 #endif
1334
1335 #ifndef HAVE_TOUPPER_DECL
1336 extern int toupper(int c);
1337 #endif
1338
1339 #ifndef HAVE_UNGETC_DECL
1340 extern int ungetc(int c, FILE *stream);
1341 #endif
1342
1343 #ifndef HAVE_VFPRINTF_DECL
1344 #include "arglist.h"
1345 extern int vfprintf(FILE *stream, const char *format, va_list ap);
1346 #endif
1347
1348 #ifndef HAVE_VPRINTF_DECL
1349 #include "arglist.h"
1350 extern int vprintf(const char *format, va_list ap);
1351 #endif
1352
1353 /* GNULIB include */
1354 #ifndef CONFIGURE_TEST
1355 #include "getaddrinfo.h"
1356 #include "inet_ntop.h"
1357 #endif
1358
1359 #if !defined(S_ISCHR) && defined(_S_IFCHR) && defined(_S_IFMT)
1360 #define S_ISCHR(mode) (((mode) & _S_IFMT) == _S_IFCHR)
1361 #endif
1362
1363 #if !defined(S_ISREG) && defined(_S_IFREG) && defined(_S_IFMT)
1364 #define S_ISREG(mode) (((mode) & _S_IFMT) == _S_IFREG)
1365 #endif
1366
1367 #ifndef HAVE_WAITPID
1368 #ifdef HAVE_WAIT4
1369 #define waitpid(pid,status,options) wait4(pid,status,options,0)
1370 #else
1371 extern pid_t waitpid(pid_t pid, amwait_t *stat_loc, int options);
1372 #endif
1373 #endif
1374
1375 #ifndef HAVE_WRITEV_DECL
1376 extern ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
1377 #endif
1378
1379 #ifndef STDIN_FILENO
1380 #define STDIN_FILENO 0
1381 #endif
1382
1383 #ifndef STDOUT_FILENO
1384 #define STDOUT_FILENO 1
1385 #endif
1386
1387 #ifndef STDERR_FILENO
1388 #define STDERR_FILENO 2
1389 #endif
1390
1391 /* S_ISDIR is not defined on Nextstep */
1392 #ifndef S_ISDIR
1393 #if defined(_S_IFMT) && defined(_S_IFDIR)
1394 #define S_ISDIR(mode)   (((mode) & (_S_IFMT)) == (_S_IFDIR))
1395 #else
1396 #error Don t know how to define S_ISDIR
1397 #endif
1398 #endif
1399
1400 #if SIZEOF_SIZE_T == SIZEOF_INT
1401 #  define        SIZE_T_FMT     "%u"
1402 #  define        SIZE_T_FMT_TYPE unsigned
1403 #  define        SIZE_T_ATOI    (size_t)atoi
1404 #  ifndef SIZE_MAX
1405 #    define      SIZE_MAX       UINT_MAX
1406 #  endif
1407 #else
1408 #  define        SIZE_T_FMT     "%lu"
1409 #  define        SIZE_T_FMT_TYPE unsigned long
1410 #  define        SIZE_T_ATOI    (size_t)atol
1411 #  ifndef SIZE_MAX
1412 #    define      SIZE_MAX       ULONG_MAX
1413 #  endif
1414 #endif
1415
1416 #if SIZEOF_SSIZE_T == SIZEOF_INT
1417 #  define        SSIZE_T_FMT    "%d"
1418 #  define        SSIZE_T_FMT_TYPE int
1419 #  define        SSIZE_T_ATOI   (ssize_t)atoi
1420 #  ifndef SSIZE_MAX
1421 #    define      SSIZE_MAX      INT_MAX
1422 #  endif
1423 #  ifndef SSIZE_MIN
1424 #    define      SSIZE_MIN      INT_MIN
1425 #  endif
1426 #else
1427 #  define        SSIZE_T_FMT    "%ld"
1428 #  define        SSIZE_T_FMT_TYPE long
1429 #  define        SSIZE_T_ATOI   (ssize_t)atol
1430 #  ifndef SSIZE_MAX
1431 #    define      SSIZE_MAX      LONG_MAX
1432 #  endif
1433 #  ifndef SSIZE_MIN
1434 #    define      SSIZE_MIN      LONG_MIN
1435 #  endif
1436 #endif
1437
1438 #if SIZEOF_TIME_T == SIZEOF_INT
1439 #  define        TIME_T_FMT     "%u"
1440 #  define        TIME_T_FMT_TYPE unsigned
1441 #  define        TIME_T_ATOI    (time_t)atoi
1442 #  ifndef TIME_MAX
1443 #    define      TIME_MAX       UINT_MAX
1444 #  endif
1445 #else
1446 #  define        TIME_T_FMT     "%lu"
1447 #  define        TIME_T_FMT_TYPE unsigned long
1448 #  define        TIME_T_ATOI    (time_t)atol
1449 #  ifndef TIME_MAX
1450 #    define      TIME_MAX       ULONG_MAX
1451 #  endif
1452 #endif
1453
1454 #if SIZEOF_OFF_T > SIZEOF_LONG
1455 #  define        OFF_T_FMT       LL_FMT
1456 #  define        OFF_T_RFMT      LL_RFMT
1457 #  define        OFF_T_FMT_TYPE  long long
1458 #  ifdef HAVE_ATOLL
1459 #    define      OFF_T_ATOI      (off_t)atoll
1460 #  else
1461 #    define      OFF_T_ATOI      (off_t)atol
1462 #  endif
1463 #  ifdef HAVE_STRTOLL
1464 #    define      OFF_T_STRTOL    (off_t)strtoll
1465 #  else
1466 #    define      OFF_T_STRTOL      (off_t)strtol
1467 #  endif
1468 #else
1469 #  if SIZEOF_OFF_T == SIZEOF_LONG
1470 #    define        OFF_T_FMT       "%ld"
1471 #    define        OFF_T_RFMT      "ld"
1472 #    define        OFF_T_FMT_TYPE  long
1473 #    define        OFF_T_ATOI    (off_t)atol
1474 #    define        OFF_T_STRTOL  (off_t)strtol
1475 #  else
1476 #    define        OFF_T_FMT       "%d"
1477 #    define        OFF_T_RFMT      "d"
1478 #    define        OFF_T_FMT_TYPE  int
1479 #    define        OFF_T_ATOI    (off_t)atoi
1480 #    define        OFF_T_STRTOL  (off_t)strtol
1481 #  endif
1482 #endif
1483
1484 #if SIZEOF_OFF_T == 8
1485 #  ifdef OFF_MAX
1486 #    define AM64_MAX (off_t)(OFF_MAX)
1487 #  else
1488 #    define AM64_MAX (off_t)(9223372036854775807LL)
1489 #  endif
1490 #  ifdef OFF_MIN
1491 #    define AM64_MIN (off_t)(OFF_MIN)
1492 #  else
1493 #    define AM64_MIN (off_t)(-9223372036854775807LL -1LL)
1494 #  endif
1495 #  define AM64_FMT OFF_T_FMT
1496 #else
1497 #if SIZEOF_LONG == 8
1498 #  ifdef LONG_MAX
1499 #    define AM64_MAX (off_t)(LONG_MAX)
1500 #  else
1501 #    define AM64_MAX (off_t)9223372036854775807L
1502 #  endif
1503 #  ifdef LONG_MIN
1504 #    define AM64_MIN (off_t)(LONG_MIN)
1505 #  else
1506 #    define AM64_MIN (off_t)(-9223372036854775807L -1L)
1507 #  endif
1508 #  define AM64_FMT "%ld"
1509 #else
1510 #if SIZEOF_LONG_LONG == 8
1511 #  ifdef LONG_LONG_MAX
1512 #    define AM64_MAX (off_t)(LONG_LONG_MAX)
1513 #  else
1514 #    define AM64_MAX (off_t)9223372036854775807LL
1515 #  endif
1516 #  ifdef LONG_LONG_MIN
1517 #    define AM64_MIN (off_t)(LONG_LONG_MIN)
1518 #  else
1519 #    define AM64_MIN (off_t)(-9223372036854775807LL -1LL)
1520 #  endif
1521 #  define AM64_FMT LL_FMT
1522 #else
1523 #if SIZEOF_INTMAX_T == 8
1524 #  ifdef INTMAX_MAX
1525 #    define AM64_MAX (off_t)(INTMAX_MAX)
1526 #  else
1527 #    define AM64_MAX (off_t)9223372036854775807LL
1528 #  endif
1529 #  ifdef INTMAX_MIN
1530 #    define AM64_MIN (off_t)(INTMAX_MIN)
1531 #  else
1532 #    define AM64_MIN (off_t)(-9223372036854775807LL -1LL)
1533 #  endif
1534 #  define AM64_FMT LL_FMT
1535 #else  /* no 64 bits type found, use long. */
1536 #  ifdef LONG_MAX
1537 #    define AM64_MAX (off_t)(LONG_MAX)
1538 #  else
1539 #    define AM64_MAX (off_t)2147483647
1540 #  endif
1541 #  ifdef LONG_MIN
1542 #    define AM64_MIN (off_t)(LONG_MIN)
1543 #  else
1544 #    define AM64_MIN (off_t)(-2147483647 -1)
1545 #  endif
1546 #  define AM64_FMT "%ld"
1547 #endif
1548 #endif
1549 #endif
1550 #endif
1551
1552 #ifdef HAVE_LIBREADLINE
1553 #  ifdef HAVE_READLINE_READLINE_H
1554 #    include <readline/readline.h>
1555 #    ifdef HAVE_READLINE_HISTORY_H
1556 #      include <readline/history.h>
1557 #    endif
1558 #  else
1559 #    ifdef HAVE_READLINE_H
1560 #      include <readline.h>
1561 #      ifdef HAVE_HISTORY_H
1562 #        include <history.h>
1563 #      endif
1564 #    else
1565 #      undef HAVE_LIBREADLINE
1566 #    endif
1567 #  endif
1568 #else
1569
1570 char *  readline(const char *prompt);
1571 void    add_history(const char *line);
1572
1573 #endif
1574
1575 #define BIND_CYCLE_RETRIES      120             /* Total of 30 minutes */
1576
1577 #define DBG_SUBDIR_SERVER  "server"
1578 #define DBG_SUBDIR_CLIENT  "client"
1579 #define DBG_SUBDIR_AMANDAD "amandad"
1580
1581 #define MAX_DUMPERS 63
1582
1583 #ifndef NI_MAXHOST
1584 #define NI_MAXHOST 1025
1585 #endif
1586
1587 #define _(x) x
1588
1589 #ifndef AI_V4MAPPED
1590 #define AI_V4MAPPED 0
1591 #endif
1592
1593 #ifndef AI_ALL
1594 #define AI_ALL 0
1595 #endif
1596
1597 #endif  /* !AMANDA_H */