Auxilliary routines
authorJoey Schulze <joey@infodrom.org>
Sun, 25 Nov 2007 15:46:53 +0000 (15:46 +0000)
committerJoey Schulze <joey@infodrom.org>
Sun, 25 Nov 2007 15:46:53 +0000 (15:46 +0000)
aux.c [new file with mode: 0644]
cgi.h

diff --git a/aux.c b/aux.c
new file mode 100644 (file)
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 <joey@infodrom.org>
+     
+    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 <string.h>
+
+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 (file)
--- a/cgi.h
+++ b/cgi.h
@@ -1,6 +1,6 @@
 /*
 /*
-    cgi.h - Some simple routines for cgi programming
-    Copyright (c) 1996-8  Martin Schulze <joey@infodrom.org>
+    cgi.h - Routines for CGI programming
+    Copyright (c) 1996-8,2007  Martin Schulze <joey@infodrom.org>
 
     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
 
     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);
 
  */
 void cgiFree (s_cgi *parms);
 
+/* cgiEscape
+ *
+ * Escapes <&> in a string
+ */
+char *cgiEscape (char *string);
+
 #endif /* _CGI_H_ */
 #endif /* _CGI_H_ */