Imported Upstream version 2.4.4p3
[debian/amanda] / common-src / amanda.h
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998 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.66.2.7.4.5.2.14 2004/04/30 12:13:20 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  * I would prefer that each Amanda module include only those system headers
40  * that are locally needed, but on most Unixes the system header files are not
41  * protected against multiple inclusion, so this can lead to problems.
42  *
43  * Also, some systems put key files in different places, so by including 
44  * everything here the rest of the system is isolated from such things.
45  */
46 #ifdef HAVE_ALLOCA_H
47 #  include <alloca.h>
48 #endif
49
50 #if 0
51 /* an explanation for this is available in the CHANGES file for
52    amanda-2.4.0b5 */
53 #ifdef HAVE_ASM_BYTEORDER_H
54 #  include <asm/byteorder.h>
55 #endif
56 #endif
57
58 #ifdef HAVE_SYS_TYPES_H
59 #  include <sys/types.h>
60 #endif
61
62 /* from the autoconf documentation */
63 #ifdef HAVE_DIRENT_H
64 #  include <dirent.h>
65 #  define NAMLEN(dirent) strlen((dirent)->d_name)
66 #else
67 #  define dirent direct
68 #  define NAMLEN(dirent) (dirent)->d_namlen
69 #  if HAVE_SYS_NDIR_H
70 #    include <sys/ndir.h>
71 #  endif
72 #  if HAVE_SYS_DIR_H
73 #    include <sys/dir.h>
74 #  endif
75 #  if HAVE_NDIR_H
76 #    include <ndir.h>
77 #  endif
78 #endif
79
80 #ifdef HAVE_FCNTL_H
81 #  include <fcntl.h>
82 #endif
83
84 #ifdef HAVE_GRP_H
85 #  include <grp.h>
86 #endif
87
88 #if defined(USE_DB_H)
89 #  include <db.h>
90 #else
91 #if defined(USE_DBM_H)
92 #  include <dbm.h>
93 #else
94 #if defined(USE_GDBM_H)
95 #  include <gdbm.h>
96 #else
97 #if defined(USE_NDBM_H)
98 #  include <ndbm.h>
99 #endif
100 #endif
101 #endif
102 #endif
103
104 #ifdef HAVE_NETDB_H
105 #  include <netdb.h>
106 #endif
107
108 #ifdef TIME_WITH_SYS_TIME
109 #  include <sys/time.h>
110 #  include <time.h>
111 #else
112 #  ifdef HAVE_SYS_TIME_H
113 #    include <sys/time.h>
114 #  else
115 #    include <time.h>
116 #  endif
117 #endif
118
119 #ifdef HAVE_LIBC_H
120 #  include <libc.h>
121 #endif
122
123 #ifdef HAVE_STDLIB_H
124 #  include <stdlib.h>
125 #endif
126
127 #ifdef HAVE_STRING_H
128 #  include <string.h>
129 #endif
130
131 #ifdef HAVE_STRINGS_H
132 #  include <strings.h>
133 #endif
134
135 #ifdef HAVE_SYSLOG_H
136 #  include <syslog.h>
137 #endif
138
139 #ifdef HAVE_SYS_FILE_H
140 #  include <sys/file.h>
141 #endif
142
143 #ifdef HAVE_SYS_IOCTL_H
144 #  include <sys/ioctl.h>
145 #endif
146
147 #ifdef HAVE_SYS_PARAM_H
148 #  include <sys/param.h>
149 #endif
150
151 #if defined(HAVE_SYS_IPC_H) && defined(HAVE_SYS_SHM_H)
152 #  include <sys/ipc.h>
153 #  include <sys/shm.h>
154 #else
155 #  ifdef HAVE_SYS_MMAN_H
156 #    include <sys/mman.h>
157 #  endif
158 #endif
159
160 #ifdef HAVE_SYS_SELECT_H
161 #  include <sys/select.h>
162 #endif
163
164 #ifdef HAVE_SYS_STAT_H
165 #  include <sys/stat.h>
166 #endif
167
168 #ifdef HAVE_WAIT_H
169 #  include <wait.h>
170 #endif
171
172 #ifdef HAVE_SYS_WAIT_H
173 #  include <sys/wait.h>
174 #endif
175
176 #ifdef WAIT_USES_UNION
177   typedef union wait amwait_t;
178 # ifndef WEXITSTATUS
179 #  define WEXITSTATUS(stat_val) (((amwait_t*)&(stat_val))->w_retcode)
180 # endif
181 # ifndef WTERMSIG
182 #  define WTERMSIG(stat_val) (((amwait_t*)&(stat_val))->w_termsig)
183 # endif
184 # ifndef WIFEXITED
185 #  define WIFEXITED(stat_val) (WTERMSIG(stat_val) == 0)
186 # endif
187 #else
188   typedef int amwait_t;
189 # ifndef WEXITSTATUS
190 #  define WEXITSTATUS(stat_val) (*(unsigned*)&(stat_val) >> 8)
191 # endif
192 # ifndef WTERMSIG
193 #  define WTERMSIG(stat_val) (*(unsigned*)&(stat_val) & 0x7F)
194 # endif
195 # ifndef WIFEXITED
196 #  define WIFEXITED(stat_val) ((*(unsigned*)&(stat_val) & 255) == 0)
197 # endif
198 #endif
199
200 #ifdef HAVE_UNISTD_H
201 #  include <unistd.h>
202 #endif
203
204 /*
205  * At present, the kerberos routines require atexit(), or equivilent.  If 
206  * you're not using kerberos, you don't need it at all.  If you just null
207  * out the definition, you'll end up with ticket files hanging around in
208  * /tmp.
209  */
210 #if defined(KRB4_SECURITY)
211 #   if !defined(HAVE_ATEXIT) 
212 #      if defined(HAVE_ON_EXIT)
213 #          define atexit(func) on_exit(func, 0)
214 #      else
215 #          define atexit(func) (you must to resolve lack of atexit in amanda.h)
216 #      endif
217 #   endif
218 #endif
219
220 #include <ctype.h>
221 #include <errno.h>
222 #include <netinet/in.h>
223 #include <pwd.h>
224 #include <signal.h>
225 #include <setjmp.h>
226 #include <stdio.h>
227 #include <sys/resource.h>
228 #include <sys/socket.h>
229 #if !defined(CONFIGURE_TEST)
230 #  include "amanda-int.h"
231 #endif
232
233 #ifdef HAVE_ARPA_INET_H
234 #include <arpa/inet.h>
235 #endif
236
237 #ifdef KRB4_SECURITY
238 #  include <des.h>
239 #  include <krb.h>
240 #endif
241
242 /*
243  * The dbmalloc package comes from:
244  *
245  *  http://www.clark.net/pub/dickey/dbmalloc/dbmalloc.tar.gz
246  *
247  * or
248  *
249  *  ftp://gatekeeper.dec.com/pub/usenet/comp.sources.misc/volume32/dbmalloc/
250  *
251  * The following functions are sprinkled through the code, but are
252  * disabled unless USE_DBMALLOC is defined:
253  *
254  *  malloc_enter(char *) -- stack trace for malloc reports
255  *  malloc_leave(char *) -- stack trace for malloc reports
256  *  malloc_mark(void *) -- mark an area as never to be free-d
257  *  malloc_chain_check(void) -- check the malloc area now
258  *  malloc_dump(int fd) -- report the malloc contents to a file descriptor
259  *  malloc_list(int fd, ulong a, ulong b) -- report memory activated since
260  *      history stamp a that is still active as of stamp b (leak check)
261  *  malloc_inuse(ulong *h) -- create history stamp h and return the amount
262  *      of memory currently in use.
263  */
264
265 #ifdef USE_DBMALLOC
266 #include "dbmalloc.h"
267 #else
268 #define malloc_enter(func)
269 #define malloc_leave(func)
270 #define malloc_mark(ptr)
271 #define malloc_chain_check()
272 #define malloc_dump(fd)
273 #define malloc_list(a,b,c)
274 #define malloc_inuse(hist)              (*(hist) = 0, 0)
275 #define dbmalloc_caller_loc(x,y)        (x)
276 #endif
277
278 #if !defined(HAVE_SIGACTION) && defined(HAVE_SIGVEC)
279 /* quick'n'dirty hack for NextStep31 */
280 #  define sa_flags sv_flags
281 #  define sa_handler sv_handler
282 #  define sa_mask sv_mask
283 #  define sigaction sigvec
284 #  define sigemptyset(mask) /* no way to clear pending signals */
285 #endif
286
287 /*
288  * Most Unixen declare errno in <errno.h>, some don't.  Some multithreaded
289  * systems have errno as a per-thread macro.  So, we have to be careful.
290  */
291 #ifndef errno
292 extern int errno;
293 #endif
294
295
296 /*
297  * Some older BSD systems don't have these FD_ macros, so if not, provide them.
298  */
299 #ifndef FD_SET
300 #  define FD_SETSIZE      (sizeof(fd_set) * 8)
301 #  define FD_SET(n, p)    (((fd_set *) (p))->fds_bits[0] |= (1 << ((n) % 32)))
302 #  define FD_CLR(n, p)    (((fd_set *) (p))->fds_bits[0] &= ~(1 << ((n) % 32)))
303 #  define FD_ISSET(n, p)  (((fd_set *) (p))->fds_bits[0] & (1 << ((n) % 32)))
304 #  define FD_ZERO(p)      memset((p), 0, sizeof(*(p)))
305 #endif
306
307
308 /*
309  * Define MAX_HOSTNAME_LENGTH as the size of arrays to hold hostname's.
310  */
311 #undef  MAX_HOSTNAME_LENGTH
312 #define MAX_HOSTNAME_LENGTH 1025
313
314 /*
315  * If void is broken, substitute char.
316  */
317 #ifdef BROKEN_VOID
318 #  define void char
319 #endif
320
321 /*
322  * define prototype macro so that prototypes can be declared in both ANSI
323  * and classic C environments.
324  */
325 #if STDC_HEADERS
326 #  define P(parms)      parms
327 #  define stringize(x) #x
328 #else
329 #  define P(parms)      ()
330 #  define stringize(x) "x"
331 #endif
332
333 /*
334  * So that we can use GNUC attributes (such as to get -Wall warnings
335  * for printf-like functions).  Only do this in gcc 2.7 or later ...
336  * it may work on earlier stuff, but why chance it.
337  */
338 #if !defined(__GNUC__) || __GNUC__ < 2 || __GNUC_MINOR__ < 7
339 #define __attribute__(__x)
340 #endif
341
342 /*
343  * assertions, but call error() instead of abort 
344  */
345 #ifndef ASSERTIONS
346
347 #define assert(exp) ((void)0)
348
349 #else   /* ASSERTIONS */
350
351 #define assert(exp) {if(!(exp)) error("assert: %s false, file %s, line %d", \
352                                    stringize(exp), __FILE__, __LINE__);}
353
354 #endif  /* ASSERTIONS */
355
356 /*
357  * print debug output, else compile to nothing.
358  */
359
360 #ifdef DEBUG_CODE                                                       /* { */
361 #   define dbopen()     debug_open()
362 #   define dbclose()    debug_close()
363 #   define dbprintf(p)  (debug? (debug_printf p, 0) : 0)
364 #   define dbfd()       debug_fd()
365 #   define dbfp()       debug_fp()
366 #   define dbfn()       debug_fn()
367
368 extern void debug_open P((void));
369 extern void debug_close P((void));
370 extern void debug_printf P((char *format, ...))
371     __attribute__ ((format (printf, 1, 2)));
372 extern int  debug_fd P((void));
373 extern FILE *  debug_fp P((void));
374 extern char *  debug_fn P((void));
375 extern void set_debug_prefix_pid P((pid_t));
376 extern char *debug_prefix P((char *));
377 extern char *debug_prefix_time P((char *));
378 #else                                                                   /* }{ */
379 #   define dbopen()
380 #   define dbclose()
381 #   define dbprintf(p)
382 #   define dbfd()       (-1)
383 #   define dbfp()       NULL
384 #   define dbfn()       NULL
385 #   define set_debug_prefix_pid(x)
386 #   define debug_prefix(x) get_pname()
387 #   define debug_prefix_time(x) get_pname()
388 #endif                                                                  /* } */
389
390 /* amanda #days calculation, with roundoff */
391
392 #define SECS_PER_DAY    (24*60*60)
393 #define days_diff(a, b) (((b) - (a) + SECS_PER_DAY/2) / SECS_PER_DAY)
394
395 /* Global constants.  */
396 #ifndef AMANDA_SERVICE_NAME
397 #define AMANDA_SERVICE_NAME "amanda"
398 #endif
399 #ifndef KAMANDA_SERVICE_NAME
400 #define KAMANDA_SERVICE_NAME "kamanda"
401 #endif
402 #ifndef SERVICE_SUFFIX
403 #define SERVICE_SUFFIX ""
404 #endif
405 #ifndef AMANDA_SERVICE_DEFAULT
406 #define AMANDA_SERVICE_DEFAULT  10080
407 #endif
408 #ifndef KAMANDA_SERVICE_DEFAULT
409 #define KAMANDA_SERVICE_DEFAULT 10081
410 #endif
411
412 #define am_round(v,u)   ((((v) + (u) - 1) / (u)) * (u))
413 #define am_floor(v,u)   (((v) / (u)) * (u))
414
415 /* Holding disk block size.  Do not even think about changing this!  :-) */
416 #define DISK_BLOCK_KB           32
417 #define DISK_BLOCK_BYTES        (DISK_BLOCK_KB * 1024)
418
419 /* Maximum size of a tape block */
420 /* MAX_TAPE_BLOCK_KB is defined in config.h */
421 /* by configure --with-maxtapeblocksize     */
422 #define MAX_TAPE_BLOCK_BYTES (MAX_TAPE_BLOCK_KB*1024)
423
424 /* Define miscellaneous amanda functions.  */
425 #define ERR_INTERACTIVE 1
426 #define ERR_SYSLOG      2
427 #define ERR_AMANDALOG   4
428
429 /* For static buffer manager [alloc.c:sbuf_man()] */
430 #define SBUF_MAGIC 42
431 #define SBUF_DEF(name, len) static SBUF2_DEF(len) name = {SBUF_MAGIC, len, -1}
432 #define SBUF2_DEF(len)          \
433     struct {                    \
434         int magic;              \
435         int max, cur;           \
436         void *bufp[len];        \
437     }
438
439 extern void   set_logerror P((void (*f)(char *)));
440 extern void   set_pname P((char *pname));
441 extern char  *get_pname P((void));
442 extern int    erroutput_type;
443 extern void   error     P((char *format, ...))
444     __attribute__ ((format (printf, 1, 2)));
445 extern void   errordump P((char *format, ...))
446     __attribute__ ((format (printf, 1, 2)));
447 extern int    onerror         P((void (*errf)(void)));
448
449 extern void  *debug_alloc           P((char *c, int l, size_t size));
450 extern void  *debug_newalloc        P((char *c, int l, void *old, size_t size));
451 extern char  *debug_stralloc        P((char *c, int l, const char *str));
452 extern char  *debug_newstralloc     P((char *c, int l, char *oldstr, const char *newstr));
453 extern char  *debug_caller_loc      P((char *file, int line));
454
455 extern int   debug_alloc_push       P((char *file, int line));
456 extern void  debug_alloc_pop        P((void));
457
458 #define alloc(s)                debug_alloc(__FILE__, __LINE__, (s))
459 #define newalloc(p,s)           debug_newalloc(__FILE__, __LINE__, (p), (s))
460 #define stralloc(s)             debug_stralloc(__FILE__, __LINE__, (s))
461 #define newstralloc(p,s)        debug_newstralloc(__FILE__, __LINE__, (p), (s))
462
463 /*
464  * Voodoo time.  We want to be able to mark these calls with the source
465  * line, but CPP does not handle variable argument lists so we cannot
466  * do what we did above (e.g. for alloc()).
467  *
468  * What we do is call a function to save the file and line number
469  * and have it return "false".  That triggers the "?" operator to
470  * the right side of the ":" which is a call to the debug version of
471  * vstralloc/newvstralloc but without parameters.  The compiler gets
472  * those from the next input tokens:
473  *
474  *  xx = vstralloc(a,b,NULL);
475  *
476  * becomes:
477  *
478  *  xx = debug_alloc_push(__FILE__,__LINE__)?0:debug_vstralloc(a,b,NULL);
479  *
480  * This works as long as vstralloc/newvstralloc are not part of anything
481  * very complicated.  Assignment is fine, as is an argument to another
482  * function (but you should not do that because it creates a memory leak).
483  * This will not work in arithmetic or comparison, but it is unlikely
484  * they are used like that.
485  *
486  *  xx = vstralloc(a,b,NULL);                   OK
487  *  return vstralloc(j,k,NULL);                 OK
488  *  sub(a, vstralloc(g,h,NULL), z);             OK, but a leak
489  *  if(vstralloc(s,t,NULL) == NULL) { ...       NO, but unneeded
490  *  xx = vstralloc(x,y,NULL) + 13;              NO, but why do it?
491  */
492
493 #define vstralloc debug_alloc_push(__FILE__,__LINE__)?0:debug_vstralloc
494 #define newvstralloc debug_alloc_push(__FILE__,__LINE__)?0:debug_newvstralloc
495
496 extern char  *debug_vstralloc       P((const char *str, ...));
497 extern char  *debug_newvstralloc    P((char *oldstr, const char *newstr, ...));
498
499 #define stralloc2(s1,s2)      vstralloc((s1),(s2),NULL)
500 #define newstralloc2(p,s1,s2) newvstralloc((p),(s1),(s2),NULL)
501
502 extern char  *debug_agets     P((char *c, int l, FILE *file));
503 extern char  *debug_areads    P((char *c, int l, int fd));
504 #define agets(f)              debug_agets(__FILE__,__LINE__,(f))
505 #define areads(f)             debug_areads(__FILE__,__LINE__,(f))
506
507 extern int debug_amtable_alloc P((char *file,
508                                   int line,
509                                   void **table,
510                                   int *current,
511                                   size_t elsize,
512                                   int count,
513                                   int bump,
514                                   void (*init_func)(void *)));
515 #define amtable_alloc(t,c,s,n,b,f) debug_amtable_alloc(__FILE__,        \
516                                                        __LINE__,        \
517                                                        (t),             \
518                                                        (c),             \
519                                                        (s),             \
520                                                        (n),             \
521                                                        (b),             \
522                                                        (f))
523 extern void amtable_free      P((void **table, int *current));
524
525 extern void  *sbuf_man        P((void *bufs, void *ptr));
526 extern uid_t  client_uid;
527 extern gid_t  client_gid;
528 extern void   safe_cd         P((void));
529 extern void   save_core       P((void));
530 extern char **safe_env        P((void));
531 extern char  *validate_regexp P((char *regex));
532 extern char  *validate_glob   P((char *glob));
533 extern char  *clean_regex     P((char *regex));
534 extern int    match           P((char *regex, char *str));
535 extern int    match_glob      P((char *glob, char *str));
536 extern char  *glob_to_regex   P((char *glob));
537 extern int    match_host      P((char *glob, char *host));
538 extern int    match_disk      P((char *glob, char *disk));
539 extern int    match_datestamp P((char *dateexp, char *datestamp));
540 extern time_t unctime         P((char *timestr));
541 extern ssize_t  areads_dataready  P((int fd));
542 extern void     areads_relbuf     P((int fd));
543
544 /*
545  * amfree(ptr) -- if allocated, release space and set ptr to NULL.
546  *
547  * In general, this should be called instead of just free(), unless
548  * the very next source line sets the pointer to a new value.
549  */
550
551 #define amfree(ptr) do {                                                \
552     if(ptr) {                                                           \
553         int e__errno = errno;                                           \
554         free(ptr);                                                      \
555         (ptr) = NULL;                                                   \
556         errno = e__errno;                                               \
557     }                                                                   \
558 } while(0)
559
560 #define strappend(s1,s2) do {                                           \
561     char *t_t_t = (s1) ? stralloc2((s1),(s2)) : stralloc((s2));         \
562     amfree((s1));                                                       \
563     (s1) = t_t_t;                                                       \
564 } while(0)
565
566 /*
567  * "Safe" close macros.  Close the object then set it to a value that
568  * will cause an error if referenced.
569  *
570  * aclose(fd) -- close a file descriptor and set it to -1.
571  * afclose(f) -- close a stdio file and set it to NULL.
572  * apclose(p) -- close a stdio pipe file and set it to NULL.
573  *
574  * Note: be careful not to do the following:
575  *
576  *  for(fd = low; fd < high; fd++) {
577  *      aclose(fd);
578  *  }
579  *
580  * Since aclose() sets the argument to -1, this will loop forever.
581  * Just copy fd to a temp variable and use that with aclose().
582  *
583  * Aclose() interacts with areads() to inform it to release any buffer
584  * it has outstanding on the file descriptor.
585  */
586
587 #define aclose(fd) do {                                                 \
588     if((fd) >= 0) {                                                     \
589         close(fd);                                                      \
590         areads_relbuf(fd);                                              \
591     }                                                                   \
592     (fd) = -1;                                                          \
593 } while(0)
594
595 #define afclose(f) do {                                                 \
596     if((f) != NULL) {                                                   \
597         fclose(f);                                                      \
598     }                                                                   \
599     (f) = NULL;                                                         \
600 } while(0)
601
602 #define apclose(p) do {                                                 \
603     if((p) != NULL) {                                                   \
604         pclose(p);                                                      \
605     }                                                                   \
606     (p) = NULL;                                                         \
607 } while(0)
608
609 /*
610  * Return the number of elements in an array.
611  */
612 #define am_countof(a)        (sizeof(a) / sizeof((a)[0]))
613
614 /*
615  * min/max.  Don't do something like
616  *
617  *    x = min(y++, z);
618  *
619  * because the increment will be duplicated.
620  */
621 #undef min
622 #undef max
623 #define min(a, b)       ((a) < (b) ? (a) : (b))
624 #define max(a, b)       ((a) > (b) ? (a) : (b))
625
626 /*
627  * Utility string macros.  All assume a variable holds the current
628  * character and the string pointer points to the next character to
629  * be processed.  Typical setup is:
630  *
631  *  s = buffer;
632  *  ch = *s++;
633  *  skip_whitespace(s, ch);
634  *  ...
635  *
636  * If you advance the pointer "by hand" to skip over something, do
637  * it like this:
638  *
639  *  s += some_amount;
640  *  ch = s[-1];
641  *
642  * Note that ch has the character at the end of the just skipped field.
643  * It is often useful to terminate a string, make a copy, then restore
644  * the input like this:
645  *
646  *  skip_whitespace(s, ch);
647  *  fp = s-1;                   ## save the start
648  *  skip_nonwhitespace(s, ch);  ## find the end
649  *  p[-1] = '\0';               ## temporary terminate
650  *  field = stralloc(fp);       ## make a copy
651  *  p[-1] = ch;                 ## restore the input
652  *
653  * The scanning macros are:
654  *
655  *  skip_whitespace (ptr, var)
656  *    -- skip whitespace, but stops at a newline
657  *  skip_non_whitespace (ptr, var)
658  *    -- skip non whitespace
659  *  skip_non_whitespace_cs (ptr, var)
660  *    -- skip non whitespace, stop at comment
661  *  skip_integer (ptr, var)
662  *    -- skip an integer field
663  *  skip_line (ptr, var)
664  *    -- skip just past the next newline
665  *
666  * where:
667  *
668  *  ptr -- string pointer
669  *  var -- current character
670  *
671  * These macros copy a non-whitespace field to a new buffer, and should
672  * only be used if dynamic allocation is impossible (fixed size buffers
673  * are asking for trouble):
674  *
675  *  copy_string (ptr, var, field, len, fldptr)
676  *    -- copy a non-whitespace field
677  *  copy_string_cs (ptr, var, field, len, fldptr)
678  *    -- copy a non-whitespace field, stop at comment
679  *
680  * where:
681  *
682  *  ptr -- string pointer
683  *  var -- current character
684  *  field -- area to copy to
685  *  len -- length of area (needs room for null byte)
686  *  fldptr -- work pointer used in move
687  *            if NULL on exit, the field was too small for the input
688  */
689
690 #define STR_SIZE        1024            /* a generic string buffer size */
691 #define NUM_STR_SIZE    32              /* a generic number buffer size */
692
693 #define skip_whitespace(ptr,c) do {                                     \
694     while((c) != '\n' && isspace(c)) (c) = *(ptr)++;                    \
695 } while(0)
696
697 #define skip_non_whitespace(ptr,c) do {                                 \
698     while((c) != '\0' && !isspace(c)) (c) = *(ptr)++;                   \
699 } while(0)
700
701 #define skip_non_whitespace_cs(ptr,c) do {                              \
702     while((c) != '\0' && (c) != '#' && !isspace(c)) (c) = *(ptr)++;     \
703 } while(0)
704
705 #define skip_non_integer(ptr,c) do {                                    \
706     while((c) != '\0' && !isdigit(c)) (c) = *(ptr)++;                   \
707 } while(0)
708
709 #define skip_integer(ptr,c) do {                                        \
710     if((c) == '+' || (c) == '-') (c) = *(ptr)++;                        \
711     while(isdigit(c)) (c) = *(ptr)++;                                   \
712 } while(0)
713
714 #define skip_line(ptr,c) do {                                           \
715     while((c) && (c) != '\n') (c) = *(ptr)++;                           \
716     if(c) (c) = *(ptr)++;                                               \
717 } while(0)
718
719 #define copy_string(ptr,c,f,l,fp) do {                                  \
720     (fp) = (f);                                                         \
721     while((c) != '\0' && !isspace(c)) {                                 \
722         if((fp) >= (f) + (l) - 1) {                                     \
723             *(fp) = '\0';                                               \
724             (fp) = NULL;                                                \
725             break;                                                      \
726         }                                                               \
727         *(fp)++ = (c);                                                  \
728         (c) = *(ptr)++;                                                 \
729     }                                                                   \
730     if(fp) *fp = '\0';                                                  \
731 } while(0)
732
733 #define copy_string_cs(ptr,c,f,l,fp) do {                               \
734     (fp) = (f);                                                         \
735     while((c) != '\0' && (c) != '#' && !isspace(c)) {                   \
736         if((fp) >= (f) + (l) - 1) {                                     \
737             *(fp) = '\0';                                               \
738             (fp) = NULL;                                                \
739             break;                                                      \
740         }                                                               \
741         *(fp)++ = (c);                                                  \
742         (c) = *(ptr)++;                                                 \
743     }                                                                   \
744     if(fp) *fp = '\0';                                                  \
745 } while(0)
746
747 #define is_dot_or_dotdot(s)                                             \
748     ((s)[0] == '.'                                                      \
749      && ((s)[1] == '\0'                                                 \
750          || ((s)[1] == '.' && (s)[2] == '\0')))
751
752 /* from amflock.c */
753 extern int    amflock   P((int fd, char *resource));
754 extern int    amroflock P((int fd, char *resource));
755 extern int    amfunlock P((int fd, char *resource));
756
757 /* from file.c */
758 extern int    mkpdir    P((char *file, int mode, uid_t uid, gid_t gid));
759 extern int    rmpdir    P((char *file, char *topdir));
760 extern char  *sanitise_filename P((char *inp));
761
762 extern int debug;
763 extern char *version_info[];
764
765 /* from security.c */
766 extern int security_ok P((struct sockaddr_in *addr,
767                           char *str, uint32_t cksum, char **errstr));
768 extern char *get_bsd_security P((void));
769
770 /*
771  * Handle functions which are not always declared on all systems.  This
772  * stops gcc -Wall and lint from complaining.
773  */
774
775 /* AIX #defines accept, and provides a prototype for the alternate name */
776 #if !defined(HAVE_ACCEPT_DECL) && !defined(accept)
777 extern int accept P((int s, struct sockaddr *addr, int *addrlen));
778 #endif
779
780 #ifndef HAVE_ATOF_DECL
781 extern double atof P((const char *ptr));
782 #endif
783
784 #ifndef HAVE_BCOPY
785 # define bcopy(from,to,n) ((void)memmove((to), (from), (n)))
786 #else
787 # ifndef HAVE_BCOPY_DECL
788 extern void bcopy P((const void *s1, void *s2, size_t n));
789 # endif
790 #endif
791
792 #ifndef HAVE_BIND_DECL
793 extern int bind P((int s, const struct sockaddr *name, int namelen));
794 #endif
795
796 #ifndef HAVE_BZERO
797 #define bzero(s,n) ((void)memset((s),0,(n)))
798 #else
799 # ifndef HAVE_BZERO_DECL
800 extern void bzero P((void *s, size_t n));
801 # endif
802 #endif
803
804 #ifndef HAVE_CLOSELOG_DECL
805 extern void closelog P((void));
806 #endif
807
808 #ifndef HAVE_CONNECT_DECL
809 extern int connect P((int s, struct sockaddr *name, int namelen));
810 #endif
811
812 #if !defined(TEXTDB) && !defined(HAVE_DBM_OPEN_DECL)
813 #undef   DBM_INSERT
814 #define  DBM_INSERT  0
815
816 #undef   DBM_REPLACE
817 #define  DBM_REPLACE 1
818
819     typedef struct {
820         int dummy[10];
821     } DBM;
822
823 #ifndef HAVE_STRUCT_DATUM
824     typedef struct {
825         char    *dptr;
826         int     dsize;
827     } datum;
828 #endif
829
830     extern DBM   *dbm_open     P((char *file, int flags, int mode));
831     extern void   dbm_close    P((DBM *db));
832     extern datum  dbm_fetch    P((DBM *db, datum key));
833     extern datum  dbm_firstkey P((DBM *db));
834     extern datum  dbm_nextkey  P((DBM *db));
835     extern int    dbm_delete   P((DBM *db, datum key));
836     extern int    dbm_store    P((DBM *db, datum key, datum content, int flg));
837 #endif
838
839 #ifndef HAVE_FCLOSE_DECL
840 extern int fclose P((FILE *stream));
841 #endif
842
843 #ifndef HAVE_FFLUSH_DECL
844 extern int fflush P((FILE *stream));
845 #endif
846
847 #ifndef HAVE_FPRINTF_DECL
848 extern int fprintf P((FILE *stream, const char *format, ...));
849 #endif
850
851 #ifndef HAVE_FPUTC_DECL
852 extern int fputc P((int c, FILE *stream));
853 #endif
854
855 #ifndef HAVE_FPUTS_DECL
856 extern int fputs P((const char *s, FILE *stream));
857 #endif
858
859 #ifndef HAVE_FREAD_DECL
860 extern size_t fread P((void *ptr, size_t size, size_t nitems, FILE *stream));
861 #endif
862
863 #ifndef HAVE_FSEEK_DECL
864 extern int fseek P((FILE *stream, long offset, int ptrname));
865 #endif
866
867 #ifndef HAVE_FWRITE_DECL
868 extern size_t fwrite P((const void *ptr, size_t size, size_t nitems,
869                         FILE *stream));
870 #endif
871
872 #ifndef HAVE_GETHOSTNAME_DECL
873 extern int gethostname P((char *name, int namelen));
874 #endif
875
876 #ifndef HAVE_GETOPT_DECL
877 extern char *optarg;
878 extern int getopt P((int argc, char * const *argv, const char *optstring));
879 #endif
880
881 /* AIX #defines getpeername, and provides a prototype for the alternate name */
882 #if !defined(HAVE_GETPEERNAME_DECL) && !defined(getpeername)
883 extern int getpeername P((int s, struct sockaddr *name, int *namelen));
884 #endif
885
886 /* AIX #defines getsockname, and provides a prototype for the alternate name */
887 #if !defined(HAVE_GETSOCKNAME_DECL) && !defined(getsockname)
888 extern int getsockname P((int s, struct sockaddr *name, int *namelen));
889 #endif
890
891 #ifndef HAVE_GETSOCKOPT_DECL
892 extern int getsockopt P((int s, int level, int optname, char *optval,
893                          int *optlen));
894 #endif
895
896 #ifndef HAVE_GETTIMEOFDAY_DECL
897 # ifdef HAVE_TWO_ARG_GETTIMEOFDAY
898 extern int gettimeofday P((struct timeval *tp, struct timezone *tzp));
899 # else
900 extern int gettimeofday P((struct timeval *tp));
901 # endif
902 #endif
903
904 #ifndef HAVE_INITGROUPS
905 # define initgroups(name,basegid) 0
906 #else
907 # ifndef HAVE_INITGROUPS_DECL
908 /* modification by BIS@BBN 5/20/2003:
909  * In some Unix systems, basegid is defined as a gid_t; in others
910  * it is defined as an int.  On Mac OS X, there is a "pre-compiled"
911  * unistd.h which causes the gcc -E command in the ICE_CHECK_DECL
912  * autoconf macro to not succeed.  Thus, on Mac OS X, configure thinks
913  * we don't have this declaration when we actually do.  Since Mac OS X
914  * defines basegid as an int, this declaration causes a compilation
915  * failure.  The path of least resistance for fixing this problem
916  * is to just change the basegid declaration from gid_t to int, since
917  * other (but not all) UNIX flavors also defined basegid as an int.
918 extern int initgroups P((const char *name, gid_t basegid));
919  */
920 extern int initgroups P((const char *name, int basegid));
921 # endif
922 #endif
923
924 #ifndef HAVE_IOCTL_DECL
925 extern int ioctl P((int fildes, int request, ...));
926 #endif
927
928 #ifndef HAVE_LISTEN_DECL
929 extern int listen P((int s, int backlog));
930 #endif
931
932 #ifndef HAVE_LSTAT_DECL
933 extern int lstat P((const char *path, struct stat *buf));
934 #endif
935
936 #ifndef HAVE_MALLOC_DECL
937 extern void *malloc P((size_t size));
938 #endif
939
940 #ifndef HAVE_MEMMOVE_DECL
941 #ifdef HAVE_MEMMOVE
942 extern void *memmove P((void *to, const void *from, size_t n));
943 #else
944 extern char *memmove P((char *to, /*const*/ char *from, size_t n));
945 #endif
946 #endif
947
948 #ifndef HAVE_MEMSET_DECL
949 extern void *memset P((void *s, int c, size_t n));
950 #endif
951
952 #ifndef HAVE_MKTEMP_DECL
953 extern char *mktemp P((char *template));
954 #endif
955
956 #ifndef HAVE_MKTIME_DECL
957 extern time_t mktime P((struct tm *timeptr));
958 #endif
959
960 #ifndef HAVE_OPENLOG_DECL
961 #ifdef LOG_AUTH
962 extern void openlog P((const char *ident, int logopt, int facility));
963 #else
964 extern void openlog P((const char *ident, int logopt));
965 #endif
966 #endif
967
968 #ifndef HAVE_PCLOSE_DECL
969 extern int pclose P((FILE *stream));
970 #endif
971
972 #ifndef HAVE_PERROR_DECL
973 extern void perror P((const char *s));
974 #endif
975
976 #ifndef HAVE_PRINTF_DECL
977 extern int printf P((const char *format, ...));
978 #endif
979
980 #ifndef HAVE_PUTS_DECL
981 extern int puts P((const char *s));
982 #endif
983
984 #ifndef HAVE_REALLOC_DECL
985 extern void *realloc P((void *ptr, size_t size));
986 #endif
987
988 /* AIX #defines recvfrom, and provides a prototype for the alternate name */
989 #if !defined(HAVE_RECVFROM_DECL) && !defined(recvfrom)
990 extern int recvfrom P((int s, char *buf, int len, int flags,
991                        struct sockaddr *from, int *fromlen));
992 #endif
993
994 #ifndef HAVE_REMOVE_DECL
995 extern int remove P((const char *path));
996 #endif
997
998 #ifndef HAVE_RENAME_DECL
999 extern int rename P((const char *old, const char *new));
1000 #endif
1001
1002 #ifndef HAVE_REWIND_DECL
1003 extern void rewind P((FILE *stream));
1004 #endif
1005
1006 #ifndef HAVE_RUSEROK_DECL
1007 extern int ruserok P((const char *rhost, int suser,
1008                       const char *ruser, const char *luser));
1009 #endif
1010
1011 #ifndef HAVE_SELECT_DECL
1012 extern int select P((int nfds,
1013                      SELECT_ARG_TYPE *readfds,
1014                      SELECT_ARG_TYPE *writefds,
1015                      SELECT_ARG_TYPE *exceptfds,
1016                      struct timeval *timeout));
1017 #endif
1018
1019 #ifndef HAVE_SENDTO_DECL
1020 extern int sendto P((int s, const char *msg, int len, int flags,
1021                      const struct sockaddr *to, int tolen));
1022 #endif
1023
1024 #ifdef HAVE_SETRESGID
1025 #define setegid(x)      setresgid(-1,(x),-1)
1026 #ifndef HAVE_SETRESGID_DECL
1027 extern int setresgid P((gid_t rgid, gid_t egid, gid_t sgid));
1028 #endif
1029 #else
1030 #ifndef HAVE_SETEGID_DECL
1031 extern int setegid P((gid_t egid));
1032 #endif
1033 #endif
1034
1035 #ifdef HAVE_SETRESUID
1036 #define seteuid(x)      setresuid(-1,(x),-1)
1037 #ifndef HAVE_SETRESUID_DECL
1038 extern int setresuid P((uid_t ruid, uid_t euid, uid_t suid));
1039 #endif
1040 #else
1041 #ifndef HAVE_SETEUID_DECL
1042 extern int seteuid P((uid_t euid));
1043 #endif
1044 #endif
1045
1046 #ifndef HAVE_SETPGID_DECL
1047 #ifdef HAVE_SETPGID
1048 extern int setpgid P((int pid, int pgid));
1049 #endif
1050 #endif
1051
1052 #ifndef HAVE_SETPGRP_DECL
1053 #ifdef SETPGRP_VOID
1054 extern pid_t setpgrp P((void));
1055 #else
1056 extern pid_t setpgrp P((int pgrp, int pid));
1057 #endif
1058 #endif
1059
1060 #ifndef HAVE_SETSOCKOPT_DECL
1061 extern int setsockopt P((int s, int level, int optname,
1062                          const char *optval, int optlen));
1063 #endif
1064
1065 #ifdef HAVE_SHMGET
1066 #ifndef HAVE_SHMAT_DECL
1067 extern void *shmat P((int shmid, const SHM_ARG_TYPE *shmaddr, int shmflg));
1068 #endif
1069
1070 #ifndef HAVE_SHMCTL_DECL
1071 extern int shmctl P((int shmid, int cmd, struct shmid_ds *buf));
1072 #endif
1073
1074 #ifndef HAVE_SHMDT_DECL
1075 extern int shmdt P((SHM_ARG_TYPE *shaddr));
1076 #endif
1077
1078 #ifndef HAVE_SHMGET_DECL
1079 extern int shmget P((key_t key, size_t size, int shmflg));
1080 #endif
1081 #endif
1082
1083 #if defined(HAVE_SNPRINTF) && defined(HAVE_VSNPRINTF)
1084 #define ap_snprintf     snprintf
1085 #define ap_vsnprintf    vsnprintf
1086 #endif
1087 #ifndef HAVE_SNPRINTF_DECL
1088 #include "arglist.h"
1089 int ap_snprintf  P((char *buf, size_t len, const char *format,...))
1090                     __attribute__((format(printf,3,4)));
1091 #endif
1092 #ifndef HAVE_VSNPRINTF_DECL
1093 #include "arglist.h"
1094 int ap_vsnprintf P((char *buf, size_t len, const char *format, va_list ap));
1095 #endif
1096
1097 #ifndef HAVE_SOCKET_DECL
1098 extern int socket P((int domain, int type, int protocol));
1099 #endif
1100
1101 #ifndef HAVE_SOCKETPAIR_DECL
1102 extern int socketpair P((int domain, int type, int protocol, int sv[2]));
1103 #endif
1104
1105 #ifndef HAVE_SSCANF_DECL
1106 extern int sscanf P((const char *s, const char *format, ...));
1107 #endif
1108
1109 #ifndef HAVE_STRCASECMP_DECL
1110 extern int strcasecmp P((const char *s1, const char *s2));
1111 #endif
1112
1113 #ifndef HAVE_STRERROR_DECL
1114 extern char *strerror P((int errnum));
1115 #endif
1116
1117 #ifndef HAVE_STRFTIME_DECL
1118 extern size_t strftime P((char *s, size_t maxsize, const char *format,
1119                           const struct tm *timeptr));
1120 #endif
1121
1122 #ifndef HAVE_STRNCASECMP_DECL
1123 extern int strncasecmp P((const char *s1, const char *s2, int n));
1124 #endif
1125
1126 #ifndef HAVE_SYSLOG_DECL
1127 extern void syslog P((int priority, const char *logstring, ...))
1128     __attribute__ ((format (printf, 2, 3)));
1129 #endif
1130
1131 #ifndef HAVE_SYSTEM_DECL
1132 extern int system P((const char *string));
1133 #endif
1134
1135 #ifndef HAVE_TIME_DECL
1136 extern time_t time P((time_t *tloc));
1137 #endif
1138
1139 #ifndef HAVE_TOLOWER_DECL
1140 extern int tolower P((int c));
1141 #endif
1142
1143 #ifndef HAVE_TOUPPER_DECL
1144 extern int toupper P((int c));
1145 #endif
1146
1147 #ifndef HAVE_UNGETC_DECL
1148 extern int ungetc P((int c, FILE *stream));
1149 #endif
1150
1151 #ifndef HAVE_VFPRINTF_DECL
1152 #include "arglist.h"
1153 extern int vfprintf P((FILE *stream, const char *format, va_list ap));
1154 #endif
1155
1156 #ifndef HAVE_VPRINTF_DECL
1157 #include "arglist.h"
1158 extern int vprintf P((const char *format, va_list ap));
1159 #endif
1160
1161 #if !defined(S_ISCHR) && defined(_S_IFCHR) && defined(_S_IFMT)
1162 #define S_ISCHR(mode) (((mode) & _S_IFMT) == _S_IFCHR)
1163 #endif
1164
1165 #if !defined(S_ISREG) && defined(_S_IFREG) && defined(_S_IFMT)
1166 #define S_ISREG(mode) (((mode) & _S_IFMT) == _S_IFREG)
1167 #endif
1168
1169 #ifndef HAVE_WAITPID
1170 #ifdef HAVE_WAIT4
1171 #define waitpid(pid,status,options) wait4(pid,status,options,0)
1172 #else
1173 extern pid_t waitpid P((pid_t pid, amwait_t *stat_loc, int options));
1174 #endif
1175 #endif
1176
1177 #ifndef STDIN_FILENO
1178 #define STDIN_FILENO 0
1179 #endif
1180
1181 #ifndef STDOUT_FILENO
1182 #define STDOUT_FILENO 1
1183 #endif
1184
1185 #ifndef STDERR_FILENO
1186 #define STDERR_FILENO 2
1187 #endif
1188
1189 /* S_ISDIR is not defined on Nextstep */
1190 #ifndef S_ISDIR
1191 #if defined(_S_IFMT) && defined(_S_IFDIR)
1192 #define S_ISDIR(mode)   (((mode) & (_S_IFMT)) == (_S_IFDIR))
1193 #else
1194 error: Don t know how to define S_ISDIR
1195 #endif
1196 #endif
1197
1198 #endif  /* !AMANDA_H */