110dd2532Schristos /* 210dd2532Schristos * Copyright (c) 1984 through 2008, William LeFebvre 310dd2532Schristos * All rights reserved. 410dd2532Schristos * 510dd2532Schristos * Redistribution and use in source and binary forms, with or without 610dd2532Schristos * modification, are permitted provided that the following conditions are met: 710dd2532Schristos * 810dd2532Schristos * * Redistributions of source code must retain the above copyright 910dd2532Schristos * notice, this list of conditions and the following disclaimer. 1010dd2532Schristos * 1110dd2532Schristos * * Redistributions in binary form must reproduce the above 1210dd2532Schristos * copyright notice, this list of conditions and the following disclaimer 1310dd2532Schristos * in the documentation and/or other materials provided with the 1410dd2532Schristos * distribution. 1510dd2532Schristos * 1610dd2532Schristos * * Neither the name of William LeFebvre nor the names of other 1710dd2532Schristos * contributors may be used to endorse or promote products derived from 1810dd2532Schristos * this software without specific prior written permission. 1910dd2532Schristos * 2010dd2532Schristos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2110dd2532Schristos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2210dd2532Schristos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2310dd2532Schristos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2410dd2532Schristos * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2510dd2532Schristos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2610dd2532Schristos * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2710dd2532Schristos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2810dd2532Schristos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2910dd2532Schristos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 3010dd2532Schristos * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3110dd2532Schristos */ 3210dd2532Schristos 3310dd2532Schristos #include "config.h" 3410dd2532Schristos 3510dd2532Schristos #include <sys/types.h> 3610dd2532Schristos #include <sys/param.h> 3710dd2532Schristos #include <stdio.h> 3810dd2532Schristos 3910dd2532Schristos #ifdef HAVE_LIMITS_H 4010dd2532Schristos #include <limits.h> 4110dd2532Schristos #endif 4210dd2532Schristos 4310dd2532Schristos #if TIME_WITH_SYS_TIME 4410dd2532Schristos # include <sys/time.h> 4510dd2532Schristos # include <time.h> 4610dd2532Schristos #else 4710dd2532Schristos # if HAVE_SYS_TIME_H 4810dd2532Schristos # include <sys/time.h> 4910dd2532Schristos # else 5010dd2532Schristos # include <time.h> 5110dd2532Schristos # endif 5210dd2532Schristos #endif 5310dd2532Schristos 54*56a682bfSchristos #ifdef __NetBSD__ 55*56a682bfSchristos #include <util.h> 56*56a682bfSchristos #else 57*56a682bfSchristos #define emalloc malloc 58*56a682bfSchristos #define estrdup strdup 59*56a682bfSchristos #define ecalloc calloc 60*56a682bfSchristos #define erealloc realloc 61*56a682bfSchristos #endif 62*56a682bfSchristos 6310dd2532Schristos #if STDC_HEADERS 6410dd2532Schristos #include <string.h> 6510dd2532Schristos #include <stdlib.h> 6610dd2532Schristos #define setbuffer(f, b, s) setvbuf((f), (b), (b) ? _IOFBF : _IONBF, (s)) 6710dd2532Schristos #define memzero(a, b) memset((a), 0, (b)) 6810dd2532Schristos #else /* !STDC_HEADERS */ 6910dd2532Schristos #ifndef HAVE_STRCHR 7010dd2532Schristos #define strchr(a, b) index((a), (b)) 7110dd2532Schristos #define strrchr(a, b) rindex((a), (b)) 7210dd2532Schristos #endif /* HAVE_STRCHR */ 7310dd2532Schristos #ifdef HAVE_MEMCPY 7410dd2532Schristos #define memzero(a, b) memset((a), 0, (b)) 7510dd2532Schristos #else 7610dd2532Schristos #define memcpy(a, b, c) bcopy((b), (a), (c)) 7710dd2532Schristos #define memzero(a, b) bzero((a), (b)) 7810dd2532Schristos #define memcmp(a, b, c) bcmp((a), (b), (c)) 7910dd2532Schristos #endif /* HAVE_MEMCPY */ 8010dd2532Schristos #ifdef HAVE_STRINGS_H 8110dd2532Schristos #include <strings.h> 8210dd2532Schristos #else 8310dd2532Schristos #ifdef HAVE_STRING_H 8410dd2532Schristos #include <string.h> 8510dd2532Schristos #endif 8610dd2532Schristos #endif 8710dd2532Schristos char *getenv(); 8810dd2532Schristos caddr_t malloc(); 8910dd2532Schristos #endif /* STDC_HEADERS */ 9010dd2532Schristos 9110dd2532Schristos /* If snprintf or vsnprintf aren't available, we substitute our own. 9210dd2532Schristos But we have to include stdarg in order to be able to define them. 9310dd2532Schristos */ 9410dd2532Schristos #ifdef HAVE_STDARG_H 9510dd2532Schristos #include <stdarg.h> 9610dd2532Schristos #ifndef HAVE_SNPRINTF 9710dd2532Schristos int ap_snprintf(char *buf, size_t len, const char *format,...); 9810dd2532Schristos #define snprintf ap_snprintf 9910dd2532Schristos #endif 10010dd2532Schristos #ifndef HAVE_VSNPRINTF 10110dd2532Schristos int ap_vsnprintf(char *buf, size_t len, const char *format,va_list ap); 10210dd2532Schristos #define vsnprintf ap_vsnprintf 10310dd2532Schristos #endif 10410dd2532Schristos #endif 10510dd2532Schristos 10610dd2532Schristos #if !HAVE_PID_T 10710dd2532Schristos typedef long pid_t; 10810dd2532Schristos #endif 10910dd2532Schristos #if !HAVE_TIME_T 11010dd2532Schristos typedef long time_t; 11110dd2532Schristos #endif 11210dd2532Schristos #if !HAVE_UID_T 11310dd2532Schristos typedef long uid_t; 11410dd2532Schristos #endif 11510dd2532Schristos 11610dd2532Schristos #ifndef INT_MAX 11710dd2532Schristos #define INT_MAX (0x7fffffff) 11810dd2532Schristos #endif 11910dd2532Schristos 12010dd2532Schristos #ifndef UINT_MAX 12110dd2532Schristos #define UINT_MAX (0xffffffffU) 12210dd2532Schristos #endif 12310dd2532Schristos 12410dd2532Schristos /* we must have both sighold and sigrelse to use them */ 12510dd2532Schristos #if defined(HAVE_SIGHOLD) && !defined(HAVE_SIGRELSE) 12610dd2532Schristos #undef HAVE_SIGHOLD 12710dd2532Schristos #endif 12810dd2532Schristos 12910dd2532Schristos #ifdef HAVE_UNISTD_H 13010dd2532Schristos #include <unistd.h> 13110dd2532Schristos #endif 13210dd2532Schristos 13310dd2532Schristos #ifdef HAVE_SYSEXITS_H 13410dd2532Schristos #include <sysexits.h> 13510dd2532Schristos #else 13610dd2532Schristos #define EX_OK 0 /* successful termination */ 13710dd2532Schristos #define EX_USAGE 64 /* command line usage error */ 13810dd2532Schristos #define EX_DATAERR 65 /* data format error */ 13910dd2532Schristos #define EX_NOINPUT 66 /* cannot open input */ 14010dd2532Schristos #define EX_NOUSER 67 /* addressee unknown */ 14110dd2532Schristos #define EX_NOHOST 68 /* host name unknown */ 14210dd2532Schristos #define EX_UNAVAILABLE 69 /* service unavailable */ 14310dd2532Schristos #define EX_SOFTWARE 70 /* internal software error */ 14410dd2532Schristos #define EX_OSERR 71 /* system error (e.g., can't fork) */ 14510dd2532Schristos #define EX_OSFILE 72 /* critical OS file missing */ 14610dd2532Schristos #define EX_CANTCREAT 73 /* can't create (user) output file */ 14710dd2532Schristos #define EX_IOERR 74 /* input/output error */ 14810dd2532Schristos #define EX_TEMPFAIL 75 /* temp failure; user is invited to retry */ 14910dd2532Schristos #define EX_PROTOCOL 76 /* remote error in protocol */ 15010dd2532Schristos #define EX_NOPERM 77 /* permission denied */ 15110dd2532Schristos #define EX_CONFIG 78 /* configuration error */ 15210dd2532Schristos #endif 153