From: Tom C. <tom...@us...> - 2004-11-10 05:24:21
|
Update of /cvsroot/qmailadmin/qmailadmin In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15244 Modified Files: Tag: stable-1_2 ChangeLog Makefile.am Makefile.in Added Files: Tag: stable-1_2 printh.c printh.h Log Message: Add printh.c, new routines for generating HTML-safe and CGI-safe strings. Index: Makefile.in =================================================================== RCS file: /cvsroot/qmailadmin/qmailadmin/Makefile.in,v retrieving revision 1.17.2.1 retrieving revision 1.17.2.2 diff -u -d -r1.17.2.1 -r1.17.2.2 --- Makefile.in 7 Oct 2004 20:06:34 -0000 1.17.2.1 +++ Makefile.in 10 Nov 2004 05:24:11 -0000 1.17.2.2 @@ -107,7 +107,7 @@ qmailadmin_DEPENDENCIES = $(COMMONDEPENDENCIES) COMMONSOURCES = qmailadmin.c alias.c autorespond.c forward.c mailinglist.c \ - user.c util.c auth.c template.c \ + user.c util.c printh.c auth.c template.c \ command.c show.c cgi.c limits.c dotqmail.c \ qmailadmin.h qmailadminx.h @@ -127,9 +127,9 @@ am__objects_1 = qmailadmin.$(OBJEXT) alias.$(OBJEXT) \ autorespond.$(OBJEXT) forward.$(OBJEXT) mailinglist.$(OBJEXT) \ - user.$(OBJEXT) util.$(OBJEXT) auth.$(OBJEXT) template.$(OBJEXT) \ - command.$(OBJEXT) show.$(OBJEXT) cgi.$(OBJEXT) limits.$(OBJEXT) \ - dotqmail.$(OBJEXT) + user.$(OBJEXT) util.$(OBJEXT) printh.$(OBJEXT) auth.$(OBJEXT) \ + template.$(OBJEXT) command.$(OBJEXT) show.$(OBJEXT) \ + cgi.$(OBJEXT) limits.$(OBJEXT) dotqmail.$(OBJEXT) am_qmailadmin_OBJECTS = $(am__objects_1) qmailadmin_OBJECTS = $(am_qmailadmin_OBJECTS) qmailadmin_LDFLAGS = @@ -143,7 +143,7 @@ @AMDEP_TRUE@ ./$(DEPDIR)/autorespond.Po ./$(DEPDIR)/cgi.Po \ @AMDEP_TRUE@ ./$(DEPDIR)/command.Po ./$(DEPDIR)/dotqmail.Po \ @AMDEP_TRUE@ ./$(DEPDIR)/forward.Po ./$(DEPDIR)/limits.Po \ -@AMDEP_TRUE@ ./$(DEPDIR)/mailinglist.Po \ +@AMDEP_TRUE@ ./$(DEPDIR)/mailinglist.Po ./$(DEPDIR)/printh.Po \ @AMDEP_TRUE@ ./$(DEPDIR)/qmailadmin.Po ./$(DEPDIR)/show.Po \ @AMDEP_TRUE@ ./$(DEPDIR)/template.Po ./$(DEPDIR)/user.Po \ @AMDEP_TRUE@ ./$(DEPDIR)/util.Po @@ -240,6 +240,7 @@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/forward.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/limits.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mailinglist.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/printh.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/qmailadmin.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/show.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/template.Po@am__quote@ @@ -480,6 +481,7 @@ mailinglist.o: config.h qmailadmin.h qmailadminx.h user.o: config.h qmailadmin.h qmailadminx.h util.o: config.h qmailadmin.h qmailadminx.h +printh.o: printh.h auth.o: config.h qmailadmin.h qmailadminx.h template.o: config.h qmailadmin.h qmailadminx.h command.o: qmailadmin.h qmailadminx.h --- NEW FILE: printh.c --- /* * $Id: printh.c,v 1.1.2.1 2004/11/10 05:24:11 tomcollins Exp $ * Copyright (C) 2004 Tom Logic LLC * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ #include "printh.h" /* included from printh.h: #include <stdlib.h> #include <stdarg.h> */ #include <string.h> #include <stdio.h> #include <ctype.h> /* vsnprinth - Custom version of sprintf for escaping strings for use on HTML * pages and in cgi script parameters. * * (based on qnprintf from vpopmail) * * int vsnprinth (char *buffer, size_t size, const char *format, va_list ap) * int snprinth (char *buffer, size_t size, const char *format, ...); * int printh (const char *format, ...); * * buffer - buffer to print string to * size - size of buffer * format - a printf-style format string* * ... - variable arguments for the format string * * NOTE: Currently supported formats: %%, %s, %d/%i, %u, %ld/%li, %lu * Since this function was designed to escape strings for use on HTML pages, * the formats don't support any extended options. * * Extra formats: %C (like %s, but converts string to cgi-parameter safe * version) and %H (like %s, but converts to HTML-safe version) * * Returns the number of characters that would have been printed to buffer * if it was big enough. (i.e., if return value is larger than (size-1), * buffer received an incomplete copy of the formatted string). * * It is possible to call snprinth with a NULL buffer of 0 bytes to determine * how large the buffer needs to be. This is inefficient, as snprinth has * to run twice. * * snprinth written November 2004 by Tom Collins <to...@to...> * qnprintf written February 2004 by Tom Collins <to...@to...> */ int vsnprinth (char *buffer, size_t size, const char *format, va_list ap) { const char hex[] = "0123456789abcdef"; int printed; /* number of characters that would have been printed */ const char *f; /* current position in format string */ char *b; /* current position in output buffer */ char n[20]; /* buffer to hold string representation of number */ char *s; /* pointer to string to insert */ char *copy; /* pointer to replacement string for special char */ int stringtype; #define SPRINTH_NORMAL 0 #define SPRINTH_HTML 1 #define SPRINTH_CGI 2 if (buffer == NULL && size > 0) return -1; printed = 0; b = buffer; for (f = format; *f != '\0'; f++) { if (*f != '%') { if (++printed < size) *b++ = *f; } else { f++; s = n; stringtype = SPRINTH_NORMAL; switch (*f) { case '%': strcpy (n, "%"); break; case 'd': case 'i': snprintf (n, sizeof(n), "%d", va_arg (ap, int)); break; case 'u': snprintf (n, sizeof(n), "%u", va_arg (ap, unsigned int)); break; case 'l': f++; switch (*f) { case 'd': case 'i': snprintf (n, sizeof(n), "%ld", va_arg (ap, long)); break; case 'u': snprintf (n, sizeof(n), "%lu", va_arg (ap, unsigned long)); break; default: strcpy (n, "*"); } break; case 's': s = va_arg (ap, char *); break; case 'H': s = va_arg (ap, char *); stringtype = SPRINTH_HTML; break; case 'C': s = va_arg (ap, char *); stringtype = SPRINTH_CGI; break; default: strcpy (n, "*"); } /* now copy the string parameter into the buffer */ while (*s != '\0') { copy = NULL; /* default to no special handling */ if (stringtype == SPRINTH_HTML) { switch (*s) { case '"': copy = """; break; case '<': copy = "<"; break; case '>': copy = ">"; break; case '&': copy = "&"; break; } } else if (stringtype == SPRINTH_CGI) { if (*s == ' ') copy = strcpy (n, "+"); else if (! isalnum(*s)) { copy = n; sprintf (n, "%%%c%c", hex[*s >> 4 & 0x0F], hex[*s & 0x0F]); } } if (copy == NULL) { if (++printed < size) *b++ = *s; } else { /* replace *s with buffer pointed to by copy */ while (*copy != '\0') { if (++printed < size) *b++ = *copy; copy++; } } s++; } } } *b = '\0'; /* If the formatted string doesn't fit in the buffer, zero out the buffer. */ if (printed >= size) { memset (buffer, '\0', size); } return printed; } int snprinth (char *buffer, size_t size, const char *format, ...) { int ret; va_list argp; va_start (argp, format); ret = vsnprinth (buffer, size, format, argp); va_end (argp); return ret; } int printh (const char *format, ...) { int ret; char buffer[1024]; va_list argp; va_start (argp, format); ret = vsnprinth (buffer, sizeof(buffer), format, argp); va_end (argp); printf ("%s", buffer); return ret; } Index: Makefile.am =================================================================== RCS file: /cvsroot/qmailadmin/qmailadmin/Makefile.am,v retrieving revision 1.12 retrieving revision 1.12.2.1 diff -u -d -r1.12 -r1.12.2.1 --- Makefile.am 22 Jan 2004 04:43:12 -0000 1.12 +++ Makefile.am 10 Nov 2004 05:24:11 -0000 1.12.2.1 @@ -24,7 +24,7 @@ qmailadmin_DEPENDENCIES=$(COMMONDEPENDENCIES) COMMONSOURCES=qmailadmin.c alias.c autorespond.c forward.c mailinglist.c \ - user.c util.c auth.c template.c \ + user.c util.c printh.c auth.c template.c \ command.c show.c cgi.c limits.c dotqmail.c \ qmailadmin.h qmailadminx.h @@ -38,6 +38,7 @@ mailinglist.o: config.h qmailadmin.h qmailadminx.h user.o: config.h qmailadmin.h qmailadminx.h util.o: config.h qmailadmin.h qmailadminx.h +printh.o: printh.h auth.o: config.h qmailadmin.h qmailadminx.h template.o: config.h qmailadmin.h qmailadminx.h command.o: qmailadmin.h qmailadminx.h Index: ChangeLog =================================================================== RCS file: /cvsroot/qmailadmin/qmailadmin/ChangeLog,v retrieving revision 1.15.2.27 retrieving revision 1.15.2.28 diff -u -d -r1.15.2.27 -r1.15.2.28 --- ChangeLog 23 Oct 2004 20:49:56 -0000 1.15.2.27 +++ ChangeLog 10 Nov 2004 05:24:11 -0000 1.15.2.28 @@ -11,6 +11,8 @@ - Better detect .qmail-alias files that are tied to mailing lists. (Aliases that end in "-owner" but aren't tied to ezmlm lists will now display properly.) + - Add printh.c, new routines for generating HTML-safe and CGI-safe + strings. 1.2.3 - released 7-Oct-04 --- NEW FILE: printh.h --- /* * $Id: printh.h,v 1.1.2.1 2004/11/10 05:24:11 tomcollins Exp $ * Copyright (C) 2004 Tom Logic LLC * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ #include <stdlib.h> #include <stdarg.h> int vsnprinth (char *buffer, size_t size, const char *format, va_list ap); int snprinth (char *buffer, size_t size, const char *format, ...); int printh (const char *format, ...); |