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