Mailing List Archive

for apache team (fwd)
One for post-1.0. I'll put this in ftp://hyperreal.com/httpd/incoming/.
No ack sent. I like his email address.

Brian

---------- Forwarded message ----------
Date: Sat, 28 Oct 1995 22:15:05 +0000
From: Jon Boede <jon@bounced.email.net>
To: postmaster@apache.org
Subject: for apache team

One of the TODO items is:

*) Varargs printf-like log_reason (would allow me to ditch most of the
remaining instances of MAX_STRING_LENGTH).

Here's code to do that. Note that the function "writeitout" takes a string
and is the function that actually does whatever it is that you want done with
the string. It returns the number of bytes written, strlen(that_string).

# This is a shell archive. Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file". Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
# log_reason.c
#
echo x - log_reason.c
sed 's/^X//' >log_reason.c << 'END-of-log_reason.c'
X#include <stdio.h>
X#include <ctype.h>
X#include <varargs.h>
X
X/* VARARGS */
Xlog_reason(va_alist)
Xva_dcl
X{
X va_list args;
X char buf[2048], fbuf[32], wbuf[16], pbuf[16];
X char *cs, *fmt, *name, *str;
X char *had_pound, *had_space, *had_plus, *had_size, *had_zero;
X int had_width, width, had_precision, precision;
X int i, len, sign, sum;
X
X va_start(args);
X fmt = va_arg(args,char *);
X cs = buf;
X sum = 0;
X
X while (*fmt) {
X if (cs > (buf + sizeof(buf) - 256)) {
X *cs = '\0';
X sum += writeitout(buf);
X cs = buf;
X }
X if (*fmt != '%') {
X if ((*cs++ = *fmt++) == '\n') {
X *cs = '\0';
X sum += writeitout(buf);
X cs = buf;
X }
X continue;
X }
X if (*++fmt == '%') {
X *cs++ = *fmt++;
X continue;
X }
X
X had_width = had_precision = 0;
X sign = 1;
X had_pound = had_space = had_plus = had_zero = had_size = "";
X
X while (*fmt == '#' || *fmt == ' ' || *fmt == '+' ||
X *fmt == '0' || *fmt == '-')
X switch (*fmt++) {
X case '#':
X had_pound = "#";
X break;
X case ' ':
X had_space = " ";
X break;
X case '+':
X had_plus = "+";
X break;
X case '0':
X had_zero = "0";
X break;
X case '-':
X sign = -1;
X break;
X }
X
X if (*had_plus && *had_space)
X had_space = "";
X if (*had_zero && (sign == -1))
X had_zero = "";
X
X if (*fmt == '*') {
X had_width = 1;
X width = va_arg(args,int);
X ++fmt;
X }
X else if (*fmt && isdigit(*fmt)) {
X had_width = 1;
X width = atoi(fmt);
X while (*fmt && isdigit(*fmt))
X ++fmt;
X }
X if (width < 0) {
X sign = -1;
X width = 0 - width;
X }
X if (width > 128)
X width = 128;
X if (*fmt == '.') {
X had_precision = 1;
X precision = 0;
X ++fmt;
X if (*fmt == '*') {
X precision = va_arg(args,int);
X ++fmt;
X }
X else if (*fmt && isdigit(*fmt)) {
X precision = atoi(fmt);
X while (*fmt && isdigit(*fmt))
X ++fmt;
X }
X if (precision < 0)
X precision = 0;
X }
X while (*fmt == 'l' || *fmt == 'L' || *fmt == 'h')
X switch (*fmt++) {
X case 'l':
X had_size = "l";
X break;
X case 'L':
X had_size = "L";
X break;
X case 'h':
X had_size = "h";
X break;
X }
X
X sprintf(wbuf,"%d",sign * width);
X sprintf(pbuf,"%d",precision);
X sprintf(fbuf,"%%%s%s%s%s%s%s%s%s%c",
X had_pound,had_plus,had_space,had_zero,
X had_width ? wbuf : "",had_precision ? "." : "",
X had_precision ? pbuf : "",had_size,*fmt);
X switch (*fmt++) {
X case 'd': case 'i': case 'o': case 'u':
X case 'x': case 'X': case 'c':
X switch (*had_size) {
X case '\0':
X cs += sprintf(cs,fbuf,
X va_arg(args,int));
X break;
X case 'l': case 'L':
X cs += sprintf(cs,fbuf,
X va_arg(args,long));
X break;
X case 'h':
X cs += sprintf(cs,fbuf,
X va_arg(args,short));
X break;
X }
X break;
X case 'e': case 'E': case 'f':
X case 'g': case 'G':
X cs += sprintf(cs,fbuf,va_arg(args,double));
X break;
X case 's':
X name = va_arg(args,char *);
X cs += sprintf(cs,fbuf,name);
X break;
X }
X }
X *cs++ = '\0';
X writeitout(buf);
X va_end(args);
X return(sum);
X}
END-of-log_reason.c
exit