From: Joey Schulze Date: Sun, 25 Nov 2007 15:46:53 +0000 (+0000) Subject: Auxilliary routines X-Git-Tag: cgilib_0-6~67 X-Git-Url: https://git.infodrom.org/?p=infodrom%2Fcgilib;a=commitdiff_plain;h=b0e6354a8769ae38bd5a8d3308cd1a56d1c24a14 Auxilliary routines --- diff --git a/aux.c b/aux.c new file mode 100644 index 0000000..e54895c --- /dev/null +++ b/aux.c @@ -0,0 +1,87 @@ +/* + aux.c - Auxilliary code for CGI library + Copyright (C) 2007 by Martin Schulze + + 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 +#include + +char *cgiEscape (char *string) +{ + char *cp, *np; + char *buf; + size_t len; + + for (cp=string, len=0; *cp; cp++) { + switch (*cp) { + case '&': + len+=5; + break; + case '<': + case '>': + len+=4; + break; + default: + len++; + break; + } + } + + if (len == strlen(string)) + return strdup(string); + + if ((buf = (char *)malloc(len+1)) == NULL) + return NULL; + + for (cp=string, np=buf; *cp; cp++) { + switch (*cp) { + case '&': + *np++ = '&'; + *np++ = 'a'; + *np++ = 'm'; + *np++ = 'p'; + *np++ = ';'; + break; + case '<': + *np++ = '&'; + *np++ = 'l'; + *np++ = 't'; + *np++ = ';'; + break; + case '>': + *np++ = '&'; + *np++ = 'g'; + *np++ = 't'; + *np++ = ';'; + break; + default: + *np++ = *cp; + break; + } + } + *np = '\0'; + + return buf; +} + +/* + * Local variables: + * c-indent-level: 4 + * c-basic-offset: 4 + * tab-width: 8 + * End: + */ diff --git a/cgi.h b/cgi.h index 64d0444..c663ef4 100644 --- a/cgi.h +++ b/cgi.h @@ -1,6 +1,6 @@ /* - cgi.h - Some simple routines for cgi programming - Copyright (c) 1996-8 Martin Schulze + cgi.h - Routines for CGI programming + Copyright (c) 1996-8,2007 Martin Schulze 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 @@ -112,4 +112,10 @@ void cgiFreeList (char **list); */ void cgiFree (s_cgi *parms); +/* cgiEscape + * + * Escapes <&> in a string + */ +char *cgiEscape (char *string); + #endif /* _CGI_H_ */