Imported Upstream version 2.5.1
[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 /*
272  * The dbmalloc package comes from:
273  *
274  *  http://www.clark.net/pub/dickey/dbmalloc/dbmalloc.tar.gz
275  *
276  * or
277  *
278  *  ftp://gatekeeper.dec.com/pub/usenet/comp.sources.misc/volume32/dbmalloc/
279  *
280  * The following functions are sprinkled through the code, but are
281  * disabled unless USE_DBMALLOC is defined:
282  *
283  *  malloc_enter(char *) -- stack trace for malloc reports
284  *  malloc_leave(char *) -- stack trace for malloc reports
285  *  malloc_mark(void *) -- mark an area as never to be free-d
286  *  malloc_chain_check(void) -- check the malloc area now
287  *  malloc_dump(int fd) -- report the malloc contents to a file descriptor
288  *  malloc_list(int fd, ulong a, ulong b) -- report memory activated since
289  *      history stamp a that is still active as of stamp b (leak check)
290  *  malloc_inuse(ulong *h) -- create history stamp h and return the amount
291  *      of memory currently in use.
292  */
293
294 #ifdef USE_DBMALLOC
295 #include "dbmalloc.h"
296 #else
297 #define malloc_enter(func)              ((void)0)
298 #define malloc_leave(func)              ((void)0)
299 #define malloc_mark(ptr)                ((void)0)
300 #define malloc_chain_check()            ((void)0)
301 #define malloc_dump(fd)                 ((void)0)
302 #define malloc_list(a,b,c)              ((void)0)
303 #define malloc_inuse(hist)              (*(hist) = 0, 0)
304 #define dbmalloc_caller_loc(x,y)        (x)
305 #endif
306
307 #if !defined(HAVE_SIGACTION) && defined(HAVE_SIGVEC)
308 /* quick'n'dirty hack for NextStep31 */
309 #  define sa_flags sv_flags
310 #  define sa_handler sv_handler
311 #  define sa_mask sv_mask
312 #  define sigaction sigvec
313 #  define sigemptyset(mask) /* no way to clear pending signals */
314 #endif
315
316 /*
317  * Most Unixen declare errno in <errno.h>, some don't.  Some multithreaded
318  * systems have errno as a per-thread macro.  So, we have to be careful.
319  */
320 #ifndef errno
321 extern int errno;
322 #endif
323
324 /*
325  * Some compilers have int for type of sizeof() some use size_t.
326  * size_t is the one we want...
327  */
328 #define SIZEOF(x)       (size_t)sizeof(x)
329
330
331 /*
332  * Some older BSD systems don't have these FD_ macros, so if not, provide them.
333  */
334 #if !defined(FD_SET) || defined(LINT) || defined(__lint)
335 #  undef FD_SETSIZE
336 #  define FD_SETSIZE      (int)(SIZEOF(fd_set) * CHAR_BIT)
337
338 #  undef FD_SET
339 #  define FD_SET(n, p)    (((fd_set *)(p))->fds_bits[(n)/WORD_BIT] |= (int)((1 << ((n) % WORD_BIT))))
340
341 #  undef FD_CLR
342 #  define FD_CLR(n, p)    (((fd_set *)(p))->fds_bits[(n)/WORD_BIT] &= (int)(~(1 << ((n) % WORD_BIT))))
343
344 #  undef FD_ISSET
345 #  define FD_ISSET(n, p)  (((fd_set *)(p))->fds_bits[(n)/WORD_BIT] & (1 << ((n) % WORD_BIT)))
346
347 #  undef FD_ZERO
348 #  define FD_ZERO(p)      memset((p), 0, SIZEOF(*(p)))
349 #endif
350
351 #ifndef FD_COPY
352 #  define FD_COPY(p, q)   memcpy((q), (p), SIZEOF(*(p)))
353 #endif
354
355
356 /*
357  * Define MAX_HOSTNAME_LENGTH as the size of arrays to hold hostname's.
358  */
359 #undef  MAX_HOSTNAME_LENGTH
360 #define MAX_HOSTNAME_LENGTH 1025
361
362 /*
363  * If void is broken, substitute char.
364  */
365 #ifdef BROKEN_VOID
366 #  define void char
367 #endif
368
369 #define stringize(x) #x
370 #define stringconcat(x, y) x ## y
371
372 /*
373  * So that we can use GNUC attributes (such as to get -Wall warnings
374  * for printf-like functions).  Only do this in gcc 2.7 or later ...
375  * it may work on earlier stuff, but why chance it.
376  */
377 #if !defined(__GNUC__) || __GNUC__ < 2 || __GNUC_MINOR__ < 7 || defined(S_SPLINT_S) || defined(LINT) || defined(__lint)
378 #undef __attribute__
379 #define __attribute__(__x)
380 #endif
381
382 /*
383  * assertions, but call error() instead of abort 
384  */
385 #ifndef ASSERTIONS
386
387 #define assert(exp) ((void)0)
388
389 #else   /* ASSERTIONS */
390
391 #define assert(exp)     do {                                            \
392     if (!(exp)) {                                                       \
393         onerror(abort);                                                 \
394         error("assert: %s false, file %s, line %d",                     \
395            stringize(exp), __FILE__, __LINE__);                         \
396         /*NOTREACHED*/                                                  \
397     }                                                                   \
398 } while (0)
399
400 #endif  /* ASSERTIONS */
401
402 /*
403  * print debug output, else compile to nothing.
404  */
405
406 #ifdef DEBUG_CODE                                                       /* { */
407 #   define dbopen(a)    debug_open(a)
408 #   define dbreopen(a,b) debug_reopen(a,b)
409 #   define dbrename(a,b) debug_rename(a,b)
410 #   define dbclose()    debug_close()
411 #   define dbprintf(p)  (debug_printf p)
412 #   define dbfd()       debug_fd()
413 #   define dbfp()       debug_fp()
414 #   define dbfn()       debug_fn()
415
416 extern void debug_open(char *subdir);
417 extern void debug_reopen(char *file, char *notation);
418 extern void debug_rename(char *config, char *subdir);
419 extern void debug_close(void);
420 extern void debug_printf(const char *format, ...)
421     __attribute__ ((format (printf, 1, 2)));
422 extern int  debug_fd(void);
423 extern FILE *  debug_fp(void);
424 extern char *  debug_fn(void);
425 extern void set_debug_prefix_pid(pid_t);
426 extern char *debug_prefix(char *);
427 extern char *debug_prefix_time(char *);
428 #else                                                                   /* }{ */
429 #   define dbopen(a)
430 #   define dbreopen(a,b)
431 #   define dbclose()
432 #   define dbprintf(p)
433 #   define dbfd()       (-1)
434 #   define dbfp()       NULL
435 #   define dbfn()       NULL
436 #   define set_debug_prefix_pid(x)
437 #   define debug_prefix(x) get_pname()
438 #   define debug_prefix_time(x) get_pname()
439 #endif                                                                  /* } */
440
441 /* amanda #days calculation, with roundoff */
442
443 #define SECS_PER_DAY    (24*60*60)
444 #define days_diff(a, b) (int)(((b) - (a) + SECS_PER_DAY/2) / SECS_PER_DAY)
445
446 /* Global constants.  */
447 #ifndef AMANDA_SERVICE_NAME
448 #define AMANDA_SERVICE_NAME "amanda"
449 #endif
450 #ifndef KAMANDA_SERVICE_NAME
451 #define KAMANDA_SERVICE_NAME "kamanda"
452 #endif
453 #ifndef SERVICE_SUFFIX
454 #define SERVICE_SUFFIX ""
455 #endif
456 #ifndef AMANDA_SERVICE_DEFAULT
457 #define AMANDA_SERVICE_DEFAULT  ((in_port_t)10080)
458 #endif
459 #ifndef KAMANDA_SERVICE_DEFAULT
460 #define KAMANDA_SERVICE_DEFAULT ((in_port_t)10081)
461 #endif
462
463 #define am_round(v,u)   ((((v) + (u) - 1) / (u)) * (u))
464 #define am_floor(v,u)   (((v) / (u)) * (u))
465
466 /* Holding disk block size.  Do not even think about changint this!  :-) */
467 #define DISK_BLOCK_KB           32
468 #define DISK_BLOCK_BYTES        (DISK_BLOCK_KB * 1024)
469
470 /* Maximum size of a tape block */
471 /* MAX_TAPE_BLOCK_KB is defined in config.h */
472 /* by configure --with-maxtapeblocksize     */
473 #define MAX_TAPE_BLOCK_BYTES (MAX_TAPE_BLOCK_KB*1024)
474
475 /* Maximum length of tape label, plus one for null-terminator. */
476 #define MAX_TAPE_LABEL_LEN (10240)
477 #define MAX_TAPE_LABEL_BUF (MAX_TAPE_LABEL_LEN+1)
478 #define MAX_TAPE_LABEL_FMT "%10240s"
479
480 /* Define miscellaneous amanda functions.  */
481 #define ERR_INTERACTIVE 1
482 #define ERR_SYSLOG      2
483 #define ERR_AMANDALOG   4
484
485 extern void   set_logerror(void (*f)(char *));
486 extern void   set_pname(char *pname);
487 extern char  *get_pname(void);
488 extern int    erroutput_type;
489 extern void   error(const char *format, ...)
490     __attribute__ ((format (printf, 1, 2), noreturn));
491 extern void   errordump(const char *format, ...)
492     __attribute__ ((format (printf, 1, 2), noreturn));
493 extern int    onerror(void (*errf)(void));
494
495 extern void *debug_alloc      (const char *c, int l, size_t size);
496 extern void *debug_newalloc   (const char *c, int l, void *old, size_t size);
497 extern char *debug_stralloc   (const char *c, int l, const char *str);
498 extern char *debug_newstralloc(const char *c, int l, char *oldstr,
499                                const char *newstr);
500 extern const char *debug_caller_loc (const char *file, int line);
501 extern int debug_alloc_push (char *file, int line);
502 extern void debug_alloc_pop (void);
503
504 #define alloc(s)                debug_alloc(__FILE__, __LINE__, (s))
505 #define newalloc(p,s)           debug_newalloc(__FILE__, __LINE__, (p), (s))
506 #define stralloc(s)             debug_stralloc(__FILE__, __LINE__, (s))
507 #define newstralloc(p,s)        debug_newstralloc(__FILE__, __LINE__, (p), (s))
508
509 /*
510  * Voodoo time.  We want to be able to mark these calls with the source
511  * line, but CPP does not handle variable argument lists so we cannot
512  * do what we did above (e.g. for alloc()).
513  *
514  * What we do is call a function to save the file and line number
515  * and have it return "false".  That triggers the "?" operator to
516  * the right side of the ":" which is a call to the debug version of
517  * vstralloc/newvstralloc but without parameters.  The compiler gets
518  * those from the next input tokens:
519  *
520  *  xx = vstralloc(a,b,NULL);
521  *
522  * becomes:
523  *
524  *  xx = debug_alloc_push(__FILE__,__LINE__)?0:debug_vstralloc(a,b,NULL);
525  *
526  * This works as long as vstralloc/newvstralloc are not part of anything
527  * very complicated.  Assignment is fine, as is an argument to another
528  * function (but you should not do that because it creates a memory leak).
529  * This will not work in arithmetic or comparison, but it is unlikely
530  * they are used like that.
531  *
532  *  xx = vstralloc(a,b,NULL);                   OK
533  *  return vstralloc(j,k,NULL);                 OK
534  *  sub(a, vstralloc(g,h,NULL), z);             OK, but a leak
535  *  if(vstralloc(s,t,NULL) == NULL) { ...       NO, but unneeded
536  *  xx = vstralloc(x,y,NULL) + 13;              NO, but why do it?
537  */
538
539 #define vstralloc debug_alloc_push(__FILE__,__LINE__)?0:debug_vstralloc
540 #define newvstralloc debug_alloc_push(__FILE__,__LINE__)?0:debug_newvstralloc
541
542 extern char  *debug_vstralloc(const char *str, ...);
543 extern char  *debug_newvstralloc(char *oldstr, const char *newstr, ...);
544
545 #define stralloc2(s1,s2)      vstralloc((s1),(s2),NULL)
546 #define newstralloc2(p,s1,s2) newvstralloc((p),(s1),(s2),NULL)
547
548 /* Usage: vstrextend(foo, "bar, "baz", NULL). Extends the existing 
549  * string, or allocates a brand new one. */
550 extern char *vstrextend(char **oldstr, ...);
551
552 extern /*@only@*/ /*@null@*/ char *debug_agets(const char *c, int l, FILE *file);
553 extern /*@only@*/ /*@null@*/ char *debug_areads(const char *c, int l, int fd);
554 #define agets(f)              debug_agets(__FILE__,__LINE__,(f))
555 #define areads(f)             debug_areads(__FILE__,__LINE__,(f))
556
557 extern int debug_amtable_alloc(const char *file,
558                                   int line,
559                                   void **table,
560                                   size_t *current,
561                                   size_t elsize,
562                                   size_t count,
563                                   int bump,
564                                   void (*init_func)(void *));
565
566 #define amtable_alloc(t,c,s,n,b,f) debug_amtable_alloc(__FILE__,      \
567                                                      __LINE__,        \
568                                                      (t),             \
569                                                      (c),             \
570                                                      (s),             \
571                                                      (n),             \
572                                                      (b),             \
573                                                      (f))
574
575 extern void amtable_free(void **table, size_t *current);
576
577 extern uid_t  client_uid;
578 extern gid_t  client_gid;
579
580 void    safe_fd(int fd_start, int fd_count);
581 void    safe_cd(void);
582 void    save_core(void);
583 char ** safe_env(void);
584 char *  validate_regexp(const char *regex);
585 char *  validate_glob(const char *glob);
586 char *  clean_regex(const char *regex);
587 int     match(const char *regex, const char *str);
588 int     match_glob(const char *glob, const char *str);
589 char *  glob_to_regex(const char *glob);
590 int     match_tar(const char *glob, const char *str);
591 char *  tar_to_regex(const char *glob);
592 int     match_host(const char *glob, const char *host);
593 int     match_disk(const char *glob, const char *disk);
594 int     match_datestamp(const char *dateexp, const char *datestamp);
595 int     match_level(const char *levelexp, const char *level);
596 time_t  unctime(char *timestr);
597 ssize_t areads_dataready(int fd);
598 void    areads_relbuf(int fd);
599
600 /*
601  * amfree(ptr) -- if allocated, release space and set ptr to NULL.
602  *
603  * In general, this should be called instead of just free(), unless
604  * the very next source line sets the pointer to a new value.
605  */
606
607 #define amfree(ptr) do {                                                \
608     if((ptr) != NULL) {                                                 \
609         int e__errno = errno;                                           \
610         free(ptr);                                                      \
611         (ptr) = NULL;                                                   \
612         errno = e__errno;                                               \
613         (void)(ptr);  /* Fix value never used warning at end of routines */ \
614     }                                                                   \
615 } while (0)
616
617 #define strappend(s1,s2) do {                                           \
618     char *t_t_t = (s1) ? stralloc2((s1),(s2)) : stralloc((s2));         \
619     amfree((s1));                                                       \
620     (s1) = t_t_t;                                                       \
621 } while(0)
622
623 /*
624  * "Safe" close macros.  Close the object then set it to a value that
625  * will cause an error if referenced.
626  *
627  * aclose(fd) -- close a file descriptor and set it to -1.
628  * afclose(f) -- close a stdio file and set it to NULL.
629  * apclose(p) -- close a stdio pipe file and set it to NULL.
630  *
631  * Note: be careful not to do the following:
632  *
633  *  for(fd = low; fd < high; fd++) {
634  *      aclose(fd);
635  *  }
636  *
637  * Since aclose() sets the argument to -1, this will loop forever.
638  * Just copy fd to a temp variable and use that with aclose().
639  *
640  * Aclose() interacts with areads() to inform it to release any buffer
641  * it has outstanding on the file descriptor.
642  */
643
644 #define aclose(fd) do {                                                 \
645     if((fd) >= 0) {                                                     \
646         close(fd);                                                      \
647         areads_relbuf(fd);                                              \
648     }                                                                   \
649     (fd) = -1;                                                          \
650     (void)(fd);  /* Fix value never used warning at end of routines */ \
651 } while(0)
652
653 #define afclose(f) do {                                                 \
654     if((f) != NULL) {                                                   \
655         fclose(f);                                                      \
656         (f) = NULL;                                                     \
657         (void)(f);  /* Fix value never used warning at end of routines */ \
658     }                                                                   \
659 } while(0)
660
661 #define apclose(p) do {                                                 \
662     if((p) != NULL) {                                                   \
663         pclose(p);                                                      \
664         (p) = NULL;                                                     \
665         (void)(p);  /* Fix value never used warning at end of routines */ \
666     }                                                                   \
667 } while(0)
668
669 /*
670  * Return the number of elements in an array.
671  */
672 #define am_countof(a)   (int)(SIZEOF(a) / SIZEOF((a)[0]))
673
674 /*
675  * min/max.  Don't do something like
676  *
677  *    x = min(y++, z);
678  *
679  * because the increment will be duplicated.
680  */
681 #undef min
682 #undef max
683 #define min(a, b)       ((a) < (b) ? (a) : (b))
684 #define max(a, b)       ((a) > (b) ? (a) : (b))
685
686 /*
687  * Utility bitmask manipulation macros.
688  */
689 #define SET(t, f)       ((t) |= (f))
690 #define CLR(t, f)       ((t) &= ~((unsigned)(f)))
691 #define ISSET(t, f)     ((t) & (f))
692
693 /*
694  * Utility string macros.  All assume a variable holds the current
695  * character and the string pointer points to the next character to
696  * be processed.  Typical setup is:
697  *
698  *  s = buffer;
699  *  ch = *s++;
700  *  skip_whitespace(s, ch);
701  *  ...
702  *
703  * If you advance the pointer "by hand" to skip over something, do
704  * it like this:
705  *
706  *  s += some_amount;
707  *  ch = s[-1];
708  *
709  * Note that ch has the character at the end of the just skipped field.
710  * It is often useful to terminate a string, make a copy, then restore
711  * the input like this:
712  *
713  *  skip_whitespace(s, ch);
714  *  fp = s-1;                   ## save the start
715  *  skip_nonwhitespace(s, ch);  ## find the end
716  *  p[-1] = '\0';               ## temporary terminate
717  *  field = stralloc(fp);       ## make a copy
718  *  p[-1] = ch;                 ## restore the input
719  *
720  * The scanning macros are:
721  *
722  *  skip_whitespace (ptr, var)
723  *    -- skip whitespace, but stops at a newline
724  *  skip_non_whitespace (ptr, var)
725  *    -- skip non whitespace
726  *  skip_non_whitespace_cs (ptr, var)
727  *    -- skip non whitespace, stop at comment
728  *  skip_integer (ptr, var)
729  *    -- skip an integer field
730  *  skip_line (ptr, var)
731  *    -- skip just past the next newline
732  *
733  * where:
734  *
735  *  ptr -- string pointer
736  *  var -- current character
737  *
738  * These macros copy a non-whitespace field to a new buffer, and should
739  * only be used if dynamic allocation is impossible (fixed size buffers
740  * are asking for trouble):
741  *
742  *  copy_string (ptr, var, field, len, fldptr)
743  *    -- copy a non-whitespace field
744  *  copy_string_cs (ptr, var, field, len, fldptr)
745  *    -- copy a non-whitespace field, stop at comment
746  *
747  * where:
748  *
749  *  ptr -- string pointer
750  *  var -- current character
751  *  field -- area to copy to
752  *  len -- length of area (needs room for null byte)
753  *  fldptr -- work pointer used in move
754  *            if NULL on exit, the field was too small for the input
755  */
756
757 #define STR_SIZE        4096            /* a generic string buffer size */
758 #define NUM_STR_SIZE    128             /* a generic number buffer size */
759
760 #define skip_whitespace(ptr,c) do {                                     \
761     while((c) != '\n' && isspace(c)) (c) = *(ptr)++;                    \
762 } while(0)
763
764 #define skip_non_whitespace(ptr,c) do {                                 \
765     while((c) != '\0' && !isspace(c)) (c) = *(ptr)++;                   \
766 } while(0)
767
768 #define skip_non_whitespace_cs(ptr,c) do {                              \
769     while((c) != '\0' && (c) != '#' && !isspace(c)) (c) = *(ptr)++;     \
770 } while(0)
771
772 #define skip_non_integer(ptr,c) do {                                    \
773     while((c) != '\0' && !isdigit(c)) (c) = *(ptr)++;                   \
774 } while(0)
775
776 #define skip_integer(ptr,c) do {                                        \
777     if((c) == '+' || (c) == '-') (c) = *(ptr)++;                        \
778     while(isdigit(c)) (c) = *(ptr)++;                                   \
779 } while(0)
780
781 #define skip_quoted_string(ptr, c) do {                                 \
782     int iq = 0;                                                         \
783     while (((c) != '\0') && !((iq == 0) && isspace(c))) {               \
784         if ((c) == '"') {                                               \
785             iq = !iq;                                                   \
786         } else if (((c) == '\\') && (*(ptr) == '"')) {                  \
787             (ptr)++;                                                    \
788         }                                                               \
789         (c) = *(ptr)++;                                                 \
790     }                                                                   \
791 } while (0)
792
793 #define skip_quoted_line(ptr, c) do {                                   \
794     int iq = 0;                                                         \
795     while((c) && !((iq == 0) && ((c) == '\n'))) {                       \
796         if ((c) == '"')                                                 \
797             iq = !iq;                                                   \
798         (c) = *(ptr)++;                                                 \
799     }                                                                   \
800     if(c)                                                               \
801         (c) = *(ptr)++;                                                 \
802 } while(0)
803
804 #define skip_line(ptr,c) do {                                           \
805     while((c) && (c) != '\n')                                           \
806         (c) = *(ptr)++;                                                 \
807     if(c)                                                               \
808         (c) = *(ptr)++;                                                 \
809 } while(0)
810
811 #define copy_string(ptr,c,f,l,fp) do {                                  \
812     (fp) = (f);                                                         \
813     while((c) != '\0' && !isspace(c)) {                                 \
814         if((fp) >= (f) + (l) - 1) {                                     \
815             *(fp) = '\0';                                               \
816             (fp) = NULL;                                                \
817             (void)(fp);  /* Fix value never used warning at end of routines */ \
818             break;                                                      \
819         }                                                               \
820         *(fp)++ = (c);                                                  \
821         (c) = *(ptr)++;                                                 \
822     }                                                                   \
823     if(fp)                                                              \
824         *fp = '\0';                                                     \
825 } while(0)
826
827 #define copy_string_cs(ptr,c,f,l,fp) do {                               \
828     (fp) = (f);                                                         \
829     while((c) != '\0' && (c) != '#' && !isspace(c)) {                   \
830         if((fp) >= (f) + (l) - 1) {                                     \
831             *(fp) = '\0';                                               \
832             (fp) = NULL;                                                \
833             break;                                                      \
834         }                                                               \
835         *(fp)++ = (c);                                                  \
836         (c) = *(ptr)++;                                                 \
837     }                                                                   \
838     if(fp) *fp = '\0';                                                  \
839 } while(0)
840
841 #define is_dot_or_dotdot(s)                                             \
842     ((s)[0] == '.'                                                      \
843      && ((s)[1] == '\0'                                                 \
844          || ((s)[1] == '.' && (s)[2] == '\0')))
845
846 /* from amflock.c */
847 extern int    amflock(int fd, char *resource);
848 extern int    amroflock(int fd, char *resource);
849 extern int    amfunlock(int fd, char *resource);
850
851 /* from file.c */
852 extern int    mkpdir(char *file, mode_t mode, uid_t uid, gid_t gid);
853 extern int    rmpdir(char *file, char *topdir);
854 extern char  *sanitise_filename(char *inp);
855
856 /* from old bsd-security.c */
857 extern int debug;
858 extern int check_security(struct sockaddr_in *, char *, unsigned long, char **);
859
860 /*
861  * Handle functions which are not always declared on all systems.  This
862  * stops gcc -Wall and lint from complaining.
863  */
864
865 /* AIX #defines accept, and provides a prototype for the alternate name */
866 #if !defined(HAVE_ACCEPT_DECL) && !defined(accept)
867 extern int accept(int s, struct sockaddr *addr, socklen_t *addrlen);
868 #endif
869
870 #ifndef HAVE_ATOF_DECL
871 extern double atof(const char *ptr);
872 #endif
873
874 #ifndef HAVE_BCOPY
875 # define bcopy(from,to,n) ((void)memmove((to), (from), (n)))
876 #else
877 # ifndef HAVE_BCOPY_DECL
878 extern void bcopy(const void *s1, void *s2, size_t n);
879 # endif
880 #endif
881
882 #ifndef HAVE_BIND_DECL
883 extern int bind(int s, const struct sockaddr *name, socklen_t namelen);
884 #endif
885
886 #ifndef HAVE_BZERO
887 #define bzero(s,n) ((void)memset((s),0,(n)))
888 #else
889 # ifndef HAVE_BZERO_DECL
890 extern void bzero(void *s, size_t n);
891 # endif
892 #endif
893
894 #ifndef HAVE_CLOSELOG_DECL
895 extern void closelog(void);
896 #endif
897
898 #ifndef HAVE_CONNECT_DECL
899 extern int connect(int s, struct sockaddr *name, socklen_t namelen);
900 #endif
901
902 #if !defined(TEXTDB) && !defined(HAVE_DBM_OPEN_DECL)
903 #undef   DBM_INSERT
904 #define  DBM_INSERT  0
905
906 #undef   DBM_REPLACE
907 #define  DBM_REPLACE 1
908
909     typedef struct {
910         int dummy[10];
911     } DBM;
912
913 #ifndef HAVE_STRUCT_DATUM
914     typedef struct {
915         char    *dptr;
916         int     dsize;
917     } datum;
918 #endif
919
920     extern DBM   *dbm_open(char *file, int flags, int mode);
921     extern void   dbm_close(DBM *db);
922     extern datum  dbm_fetch(DBM *db, datum key);
923     extern datum  dbm_firstkey(DBM *db);
924     extern datum  dbm_nextkey(DBM *db);
925     extern int    dbm_delete(DBM *db, datum key);
926     extern int    dbm_store(DBM *db, datum key, datum content, int flg);
927 #endif
928
929 #ifndef HAVE_FCLOSE_DECL
930 extern int fclose(FILE *stream);
931 #endif
932
933 #ifndef HAVE_FFLUSH_DECL
934 extern int fflush(FILE *stream);
935 #endif
936
937 #ifndef HAVE_FPRINTF_DECL
938 extern int fprintf(FILE *stream, const char *format, ...);
939 #endif
940
941 #ifndef HAVE_FPUTC_DECL
942 extern int fputc(int c, FILE *stream);
943 #endif
944
945 #ifndef HAVE_FPUTS_DECL
946 extern int fputs(const char *s, FILE *stream);
947 #endif
948
949 #ifndef HAVE_FREAD_DECL
950 extern size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
951 #endif
952
953 #ifndef HAVE_FSEEK_DECL
954 extern int fseek(FILE *stream, long offset, int ptrname);
955 #endif
956
957 #ifndef HAVE_FWRITE_DECL
958 extern size_t fwrite(const void *ptr, size_t size, size_t nitems,
959                         FILE *stream);
960 #endif
961
962 #ifndef HAVE_GETHOSTNAME_DECL
963 extern int gethostname(char *name, int namelen);
964 #endif
965
966 #ifndef HAVE_GETOPT_DECL
967 extern char *optarg;
968 extern int getopt(int argc, char * const *argv, const char *optstring);
969 #endif
970
971 /* AIX #defines getpeername, and provides a prototype for the alternate name */
972 #if !defined(HAVE_GETPEERNAME_DECL) && !defined(getpeername)
973 extern int getpeername(int s, struct sockaddr *name, socklen_t *namelen);
974 #endif
975
976 /* AIX #defines getsockname, and provides a prototype for the alternate name */
977 #if !defined(HAVE_GETSOCKNAME_DECL) && !defined(getsockname)
978 extern int getsockname(int s, struct sockaddr *name, socklen_t *namelen);
979 #endif
980
981 #ifndef HAVE_GETSOCKOPT_DECL
982 extern int getsockopt(int s, int level, int optname, char *optval,
983                          socklen_t *optlen);
984 #endif
985
986 #ifndef HAVE_GETTIMEOFDAY_DECL
987 # ifdef HAVE_TWO_ARG_GETTIMEOFDAY
988 extern int gettimeofday(struct timeval *tp, struct timezone *tzp);
989 # else
990 extern int gettimeofday(struct timeval *tp);
991 # endif
992 #endif
993
994 #ifndef HAVE_INITGROUPS
995 # define initgroups(name,basegid) 0
996 #else
997 # ifndef HAVE_INITGROUPS_DECL
998 extern int initgroups(const char *name, gid_t basegid);
999 # endif
1000 #endif
1001
1002 #ifndef HAVE_IOCTL_DECL
1003 extern int ioctl(int fildes, int request, ...);
1004 #endif
1005
1006 #ifndef isnormal
1007 #ifndef HAVE_ISNORMAL
1008 #define isnormal(f) (((f) < 0.0) || ((f) > 0.0))
1009 #endif
1010 #endif
1011
1012 #ifndef HAVE_LISTEN_DECL
1013 extern int listen(int s, int backlog);
1014 #endif
1015
1016 #ifndef HAVE_LSTAT_DECL
1017 extern int lstat(const char *path, struct stat *buf);
1018 #endif
1019
1020 #ifndef HAVE_MALLOC_DECL
1021 extern void *malloc (size_t size);
1022 #endif
1023
1024 #ifndef HAVE_MEMMOVE_DECL
1025 #ifdef HAVE_MEMMOVE
1026 extern void *memmove(void *to, const void *from, size_t n);
1027 #else
1028 extern char *memmove(char *to, /*const*/ char *from, size_t n);
1029 #endif
1030 #endif
1031
1032 #ifndef HAVE_MEMSET_DECL
1033 extern void *memset(void *s, int c, size_t n);
1034 #endif
1035
1036 #ifndef HAVE_MKTEMP_DECL
1037 extern char *mktemp(char *template);
1038 #endif
1039
1040 #ifndef HAVE_MKSTEMP_DECL
1041 extern int mkstemp(char *template);
1042 #endif
1043
1044 #ifndef HAVE_MKTIME_DECL
1045 extern time_t mktime(struct tm *timeptr);
1046 #endif
1047
1048 #ifndef HAVE_OPENLOG_DECL
1049 #ifdef LOG_AUTH
1050 extern void openlog(const char *ident, int logopt, int facility);
1051 #else
1052 extern void openlog(const char *ident, int logopt);
1053 #endif
1054 #endif
1055
1056 #ifndef HAVE_PCLOSE_DECL
1057 extern int pclose(FILE *stream);
1058 #endif
1059
1060 #ifndef HAVE_PERROR_DECL
1061 extern void perror(const char *s);
1062 #endif
1063
1064 #ifndef HAVE_PRINTF_DECL
1065 extern int printf(const char *format, ...);
1066 #endif
1067
1068 #ifndef HAVE_PUTS_DECL
1069 extern int puts(const char *s);
1070 #endif
1071
1072 #ifndef HAVE_REALLOC_DECL
1073 extern void *realloc(void *ptr, size_t size);
1074 #endif
1075
1076 /* AIX #defines recvfrom, and provides a prototype for the alternate name */
1077 #if !defined(HAVE_RECVFROM_DECL) && !defined(recvfrom)
1078 extern int recvfrom(int s, char *buf, int len, int flags,
1079                        struct sockaddr *from, socklen_t *fromlen);
1080 #endif
1081
1082 #ifndef HAVE_REMOVE_DECL
1083 extern int remove(const char *path);
1084 #endif
1085
1086 #ifndef HAVE_RENAME_DECL
1087 extern int rename(const char *old, const char *new);
1088 #endif
1089
1090 #ifndef HAVE_REWIND_DECL
1091 extern void rewind(FILE *stream);
1092 #endif
1093
1094 #ifndef HAVE_RUSEROK_DECL
1095 extern int ruserok(const char *rhost, int suser,
1096                       const char *ruser, const char *luser);
1097 #endif
1098
1099 #ifndef HAVE_SELECT_DECL
1100 extern int select(int nfds,
1101                      SELECT_ARG_TYPE *readfds,
1102                      SELECT_ARG_TYPE *writefds,
1103                      SELECT_ARG_TYPE *exceptfds,
1104                      struct timeval *timeout);
1105 #endif
1106
1107 #ifndef HAVE_SENDTO_DECL
1108 extern int sendto(int s, const char *msg, int len, int flags,
1109                      const struct sockaddr *to, int tolen);
1110 #endif
1111
1112 #ifdef HAVE_SETRESGID
1113 #define setegid(x)      setresgid((gid_t)-1,(x),(gid_t)-1)
1114 #ifndef HAVE_SETRESGID_DECL
1115 extern int setresgid(gid_t rgid, gid_t egid, gid_t sgid);
1116 #endif
1117 #else
1118 #ifndef HAVE_SETEGID_DECL
1119 extern int setegid(gid_t egid);
1120 #endif
1121 #endif
1122
1123 #ifdef HAVE_SETRESUID
1124 #define seteuid(x)      setresuid((uid_t)-1,(x),(uid_t)-1)
1125 #ifndef HAVE_SETRESUID_DECL
1126 extern int setresuid(uid_t ruid, uid_t euid, uid_t suid);
1127 #endif
1128 #else
1129 #ifndef HAVE_SETEUID_DECL
1130 extern int seteuid(uid_t euid);
1131 #endif
1132 #endif
1133
1134 #ifndef HAVE_SETPGID_DECL
1135 #ifdef HAVE_SETPGID
1136 extern int setpgid(pid_t pid, pid_t pgid);
1137 #endif
1138 #endif
1139
1140 #ifndef HAVE_SETPGRP_DECL
1141 #ifdef SETPGRP_VOID
1142 extern pid_t setpgrp(void);
1143 #else
1144 extern pid_t setpgrp(pid_t pgrp, pid_t pid);
1145 #endif
1146 #endif
1147
1148 #ifndef HAVE_SETSOCKOPT_DECL
1149 extern int setsockopt(int s, int level, int optname,
1150                          const char *optval, int optlen);
1151 #endif
1152
1153 #ifdef HAVE_SHMGET
1154 #ifndef HAVE_SHMAT_DECL
1155 extern void *shmat(int shmid, const SHM_ARG_TYPE *shmaddr, int shmflg);
1156 #endif
1157
1158 #ifndef HAVE_SHMCTL_DECL
1159 extern int shmctl(int shmid, int cmd, struct shmid_ds *buf);
1160 #endif
1161
1162 #ifndef HAVE_SHMDT_DECL
1163 extern int shmdt(SHM_ARG_TYPE *shaddr);
1164 #endif
1165
1166 #ifndef HAVE_SHMGET_DECL
1167 extern int shmget(key_t key, size_t size, int shmflg);
1168 #endif
1169 #endif
1170
1171 #ifndef HAVE_SNPRINTF_DECL
1172 #include "arglist.h"
1173 int snprintf(char *buf, size_t len, const char *format,...)
1174                     __attribute__((format(printf,3,4)));
1175 #endif
1176 #ifndef HAVE_VSNPRINTF_DECL
1177 #include "arglist.h"
1178 int vsnprintf(char *buf, size_t len, const char *format, va_list ap);
1179 #endif
1180
1181 #ifndef HAVE_SOCKET_DECL
1182 extern int socket(int domain, int type, int protocol);
1183 #endif
1184
1185 #ifndef HAVE_SOCKETPAIR_DECL
1186 extern int socketpair(int domain, int type, int protocol, int sv[2]);
1187 #endif
1188
1189 #ifndef HAVE_SSCANF_DECL
1190 extern int sscanf(const char *s, const char *format, ...);
1191 #endif
1192
1193 #ifndef HAVE_STRCASECMP_DECL
1194 extern int strcasecmp(const char *s1, const char *s2);
1195 #endif
1196
1197 #ifndef HAVE_STRERROR_DECL
1198 extern char *strerror(int errnum);
1199 #endif
1200
1201 #ifndef HAVE_STRFTIME_DECL
1202 extern size_t strftime(char *s, size_t maxsize, const char *format,
1203                           const struct tm *timeptr);
1204 #endif
1205
1206 #ifndef HAVE_STRNCASECMP_DECL
1207 extern int strncasecmp(const char *s1, const char *s2, int n);
1208 #endif
1209
1210 #ifndef HAVE_SYSLOG_DECL
1211 extern void syslog(int priority, const char *logstring, ...)
1212     __attribute__ ((format (printf, 2, 3)));
1213 #endif
1214
1215 #ifndef HAVE_SYSTEM_DECL
1216 extern int system(const char *string);
1217 #endif
1218
1219 #ifndef HAVE_TIME_DECL
1220 extern time_t time(time_t *tloc);
1221 #endif
1222
1223 #ifndef HAVE_TOLOWER_DECL
1224 extern int tolower(int c);
1225 #endif
1226
1227 #ifndef HAVE_TOUPPER_DECL
1228 extern int toupper(int c);
1229 #endif
1230
1231 #ifndef HAVE_UNGETC_DECL
1232 extern int ungetc(int c, FILE *stream);
1233 #endif
1234
1235 #ifndef HAVE_VFPRINTF_DECL
1236 #include "arglist.h"
1237 extern int vfprintf(FILE *stream, const char *format, va_list ap);
1238 #endif
1239
1240 #ifndef HAVE_VPRINTF_DECL
1241 #include "arglist.h"
1242 extern int vprintf(const char *format, va_list ap);
1243 #endif
1244
1245 #if !defined(S_ISCHR) && defined(_S_IFCHR) && defined(_S_IFMT)
1246 #define S_ISCHR(mode) (((mode) & _S_IFMT) == _S_IFCHR)
1247 #endif
1248
1249 #if !defined(S_ISREG) && defined(_S_IFREG) && defined(_S_IFMT)
1250 #define S_ISREG(mode) (((mode) & _S_IFMT) == _S_IFREG)
1251 #endif
1252
1253 #ifndef HAVE_WAITPID
1254 #ifdef HAVE_WAIT4
1255 #define waitpid(pid,status,options) wait4(pid,status,options,0)
1256 #else
1257 extern pid_t waitpid(pid_t pid, amwait_t *stat_loc, int options);
1258 #endif
1259 #endif
1260
1261 #ifndef HAVE_WRITEV_DECL
1262 extern ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
1263 #endif
1264
1265 #ifndef STDIN_FILENO
1266 #define STDIN_FILENO 0
1267 #endif
1268
1269 #ifndef STDOUT_FILENO
1270 #define STDOUT_FILENO 1
1271 #endif
1272
1273 #ifndef STDERR_FILENO
1274 #define STDERR_FILENO 2
1275 #endif
1276
1277 /* S_ISDIR is not defined on Nextstep */
1278 #ifndef S_ISDIR
1279 #if defined(_S_IFMT) && defined(_S_IFDIR)
1280 #define S_ISDIR(mode)   (((mode) & (_S_IFMT)) == (_S_IFDIR))
1281 #else
1282 #error Don t know how to define S_ISDIR
1283 #endif
1284 #endif
1285
1286 #if SIZEOF_SIZE_T == SIZEOF_INT
1287 #  define        SIZE_T_FMT     "%u"
1288 #  define        SIZE_T_FMT_TYPE unsigned
1289 #  define        SIZE_T_ATOI    (size_t)atoi
1290 #  ifndef SIZE_MAX
1291 #    define      SIZE_MAX       UINT_MAX
1292 #  endif
1293 #else
1294 #  define        SIZE_T_FMT     "%lu"
1295 #  define        SIZE_T_FMT_TYPE unsigned long
1296 #  define        SIZE_T_ATOI    (size_t)atol
1297 #  ifndef SIZE_MAX
1298 #    define      SIZE_MAX       ULONG_MAX
1299 #  endif
1300 #endif
1301
1302 #if SIZEOF_SSIZE_T == SIZEOF_INT
1303 #  define        SSIZE_T_FMT    "%d"
1304 #  define        SSIZE_T_FMT_TYPE int
1305 #  define        SSIZE_T_ATOI   (ssize_t)atoi
1306 #  ifndef SSIZE_MAX
1307 #    define      SSIZE_MAX      INT_MAX
1308 #  endif
1309 #  ifndef SSIZE_MIN
1310 #    define      SSIZE_MIN      INT_MIN
1311 #  endif
1312 #else
1313 #  define        SSIZE_T_FMT    "%ld"
1314 #  define        SSIZE_T_FMT_TYPE long
1315 #  define        SSIZE_T_ATOI   (ssize_t)atol
1316 #  ifndef SSIZE_MAX
1317 #    define      SSIZE_MAX      LONG_MAX
1318 #  endif
1319 #  ifndef SSIZE_MIN
1320 #    define      SSIZE_MIN      LONG_MIN
1321 #  endif
1322 #endif
1323
1324 #if SIZEOF_TIME_T == SIZEOF_INT
1325 #  define        TIME_T_FMT     "%u"
1326 #  define        TIME_T_FMT_TYPE unsigned
1327 #  define        TIME_T_ATOI    (time_t)atoi
1328 #  ifndef TIME_MAX
1329 #    define      TIME_MAX       UINT_MAX
1330 #  endif
1331 #else
1332 #  define        TIME_T_FMT     "%lu"
1333 #  define        TIME_T_FMT_TYPE unsigned long
1334 #  define        TIME_T_ATOI    (time_t)atol
1335 #  ifndef TIME_MAX
1336 #    define      TIME_MAX       ULONG_MAX
1337 #  endif
1338 #endif
1339
1340 #if SIZEOF_OFF_T > SIZEOF_LONG
1341 #  define        OFF_T_FMT       "%lld"
1342 #  define        OFF_T_RFMT       "lld"
1343 #  define        OFF_T_FMT_TYPE  long long
1344 #  define        OFF_T_ATOI      (off_t)atoll
1345 #  define        OFF_T_STRTOL    (off_t)strtoll
1346 #else
1347 #  if SIZEOF_OFF_T == SIZEOF_LONG
1348 #    define        OFF_T_FMT       "%ld"
1349 #    define        OFF_T_RFMT      "ld"
1350 #    define        OFF_T_FMT_TYPE  long
1351 #    define        OFF_T_ATOI    (off_t)atol
1352 #    define        OFF_T_STRTOL  (off_t)strtol
1353 #  else
1354 #    define        OFF_T_FMT       "%d"
1355 #    define        OFF_T_RFMT      "d"
1356 #    define        OFF_T_FMT_TYPE  int
1357 #    define        OFF_T_ATOI    (off_t)atoi
1358 #    define        OFF_T_STRTOL  (off_t)strtol
1359 #  endif
1360 #endif
1361
1362 #if SIZEOF_OFF_T == 8
1363 #  ifdef OFF_MAX
1364 #    define AM64_MAX (off_t)(OFF_MAX)
1365 #  else
1366 #    define AM64_MAX (off_t)(9223372036854775807LL)
1367 #  endif
1368 #  ifdef OFF_MIN
1369 #    define AM64_MIN (off_t)(OFF_MIN)
1370 #  else
1371 #    define AM64_MIN (off_t)(-9223372036854775807LL -1LL)
1372 #  endif
1373 #  define AM64_FMT OFF_T_FMT
1374 #else
1375 #if SIZEOF_LONG == 8
1376 #  ifdef LONG_MAX
1377 #    define AM64_MAX (off_t)(LONG_MAX)
1378 #  else
1379 #    define AM64_MAX (off_t)9223372036854775807L
1380 #  endif
1381 #  ifdef LONG_MIN
1382 #    define AM64_MIN (off_t)(LONG_MIN)
1383 #  else
1384 #    define AM64_MIN (off_t)(-9223372036854775807L -1L)
1385 #  endif
1386 #  define AM64_FMT "%ld"
1387 #else
1388 #if SIZEOF_LONG_LONG == 8
1389 #  ifdef LONG_LONG_MAX
1390 #    define AM64_MAX (off_t)(LONG_LONG_MAX)
1391 #  else
1392 #    define AM64_MAX (off_t)9223372036854775807LL
1393 #  endif
1394 #  ifdef LONG_LONG_MIN
1395 #    define AM64_MIN (off_t)(LONG_LONG_MIN)
1396 #  else
1397 #    define AM64_MIN (off_t)(-9223372036854775807LL -1LL)
1398 #  endif
1399 #  define AM64_FMT LL_FMT
1400 #else
1401 #if SIZEOF_INTMAX_T == 8
1402 #  ifdef INTMAX_MAX
1403 #    define AM64_MAX (off_t)(INTMAX_MAX)
1404 #  else
1405 #    define AM64_MAX (off_t)9223372036854775807LL
1406 #  endif
1407 #  ifdef INTMAX_MIN
1408 #    define AM64_MIN (off_t)(INTMAX_MIN)
1409 #  else
1410 #    define AM64_MIN (off_t)(-9223372036854775807LL -1LL)
1411 #  endif
1412 #  define AM64_FMT LL_FMT
1413 #else  /* no 64 bits type found, use long. */
1414 #  ifdef LONG_MAX
1415 #    define AM64_MAX (off_t)(LONG_MAX)
1416 #  else
1417 #    define AM64_MAX (off_t)2147483647
1418 #  endif
1419 #  ifdef LONG_MIN
1420 #    define AM64_MIN (off_t)(LONG_MIN)
1421 #  else
1422 #    define AM64_MIN (off_t)(-2147483647 -1)
1423 #  endif
1424 #  define AM64_FMT "%ld"
1425 #endif
1426 #endif
1427 #endif
1428 #endif
1429
1430 #ifdef HAVE_LIBREADLINE
1431 #  ifdef HAVE_READLINE_READLINE_H
1432 #    include <readline/readline.h>
1433 #    ifdef HAVE_READLINE_HISTORY_H
1434 #      include <readline/history.h>
1435 #    endif
1436 #  else
1437 #    ifdef HAVE_READLINE_H
1438 #      include <readline.h>
1439 #      ifdef HAVE_HISTORY_H
1440 #        include <history.h>
1441 #      endif
1442 #    else
1443 #      undef HAVE_LIBREADLINE
1444 #    endif
1445 #  endif
1446 #else
1447
1448 char *  readline(const char *prompt);
1449 void    add_history(const char *line);
1450
1451 #endif
1452
1453 #define BIND_CYCLE_RETRIES      120             /* Total of 30 minutes */
1454
1455 #define DBG_SUBDIR_SERVER  "server"
1456 #define DBG_SUBDIR_CLIENT  "client"
1457 #define DBG_SUBDIR_AMANDAD "amandad"
1458
1459 #endif  /* !AMANDA_H */