Solaris compatibility fixes.

. INADDR_NONE is -1
. Change bindport to an unsigned short (ntohs implies short).
. Conditionally implement our own errx() and err().
. Conditionally use a custom vasprintf() that depends on vsnprintf().
This commit is contained in:
Emil Mikulic 2003-11-18 12:33:02 +00:00
parent 466de20e2e
commit 41d2f5867a

View File

@ -24,6 +24,8 @@
* x Ensure URIs requested are safe.
*/
/* Note: Solaris users: link with -lxnet */
#ifdef __linux
#define _GNU_SOURCE /* for strsignal() and vasprintf() */
#endif
@ -35,7 +37,6 @@
#include <arpa/inet.h>
#include <assert.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <pwd.h>
@ -54,6 +55,54 @@
#define min(a,b) ( ((a)<(b)) ? (a) : (b) )
#endif
#ifndef INADDR_NONE
#define INADDR_NONE -1
#endif
#if defined(__FreeBSD__) || defined(__linux)
#include <err.h>
#else
/* ---------------------------------------------------------------------------
* errx - prints "error: [...]\n" to stderr and exit()s with [code]
*
* Replacement for the BSD errx() which is usually in <err.h>
*/
static void errx(const int code, const char *format, ...)
{
va_list va;
va_start(va, format);
fprintf(stderr, "error: ");
vfprintf(stderr, format, va);
fprintf(stderr, "\n");
va_end(va);
exit(code);
}
/* ---------------------------------------------------------------------------
* err - prints "error: [...]: strerror\n" to stderr and exit()s with [code]
*
* Replacement for the BSD err() which is usually in <err.h>
*/
void err(const int code, const char *format, ...)
{
va_list va;
va_start(va, format);
fprintf(stderr, "error: ");
vfprintf(stderr, format, va);
fprintf(stderr, ": %s\n", strerror(errno));
va_end(va);
exit(code);
}
#endif
/* ---------------------------------------------------------------------------
@ -185,7 +234,7 @@ static char *keep_alive_field = NULL;
/* Defaults can be overridden on the command-line */
static in_addr_t bindaddr = INADDR_ANY;
static u_int16_t bindport = 80;
static unsigned short bindport = 80;
static int max_connections = -1; /* kern.ipc.somaxconn */
static const char *index_name = "index.html";
@ -262,6 +311,19 @@ static char *xstrdup(const char *src)
#ifdef __sun__ /* unimpressed by Solaris */
static int vasprintf(char **strp, const char *fmt, va_list ap)
{
char tmp;
int result = vsnprintf(&tmp, 1, fmt, ap);
*strp = xmalloc(result+1);
result = vsnprintf(*strp, result+1, fmt, ap);
return result;
}
#endif
/* ---------------------------------------------------------------------------
* asprintf() that errx()s if it fails.
*/
@ -846,7 +908,7 @@ static void parse_commandline(const int argc, char *argv[])
if (strcmp(argv[i], "--port") == 0)
{
if (++i >= argc) errx(1, "missing number after --port");
bindport = (u_int16_t)atoi(argv[i]);
bindport = (unsigned short)atoi(argv[i]);
}
else if (strcmp(argv[i], "--addr") == 0)
{