Update to the new FSF address
[infodrom/cgilib] / aux.c
diff --git a/aux.c b/aux.c
index e54895c..a808b9d 100644 (file)
--- a/aux.c
+++ b/aux.c
@@ -1,6 +1,6 @@
 /*
     aux.c - Auxilliary code for CGI library
-    Copyright (C) 2007 by Martin Schulze <joey@infodrom.org>
+    Copyright (C) 2007,8 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
     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.
+    along with this program; if not, write to the Free Software Foundation
+    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
 #include <stdlib.h>
 #include <string.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <syslog.h>
+
+int cgiDebugLevel = 0;
+int cgiDebugType = 1;
 
 char *cgiEscape (char *string)
 {
@@ -78,6 +84,73 @@ char *cgiEscape (char *string)
     return buf;
 }
 
+void cgiDebugOutput (int level, char *format, ...)
+{
+  va_list args;
+
+  if (level <= cgiDebugLevel) {
+
+      va_start (args, format);
+
+      switch (cgiDebugType) {
+      case 0:
+         printf ("<pre>\n");
+         vprintf (format, args);
+         printf ("\n</pre>\n");
+         break;
+      case 1:
+         vfprintf (stderr, format, args);
+         printf ("\n");
+         break;
+      case 2:
+         vsyslog (LOG_DEBUG, format, args);
+         break;
+      }
+
+      va_end (args);
+  }
+}
+
+#define BUFSIZE 128
+
+char *cgiGetLine (FILE *stream)
+{
+    static char *line = NULL;
+    static size_t size = 0;
+    char buf[BUFSIZE];
+    char *cp;
+
+    if (!line) {
+      if ((line = (char *)malloc (BUFSIZE)) == NULL)
+       return NULL;
+      size = BUFSIZE;
+    }
+    line[0] = '\0';
+
+    while (!feof (stream)) {
+      if ((cp = fgets (buf, sizeof (buf), stream)) == NULL)
+       return NULL;
+
+      if (strlen(line)+strlen(buf)+1 > size) {
+       if ((cp = (char *)realloc (line, size + BUFSIZE)) == NULL)
+         return line;
+       size += BUFSIZE;
+       line = cp;
+      }
+
+      strcat (line, buf);
+      if (line[strlen(line)-1] == '\n') {
+       line[strlen(line)-1] = '\0';
+       if (line[strlen(line)-1] == '\r')
+         line[strlen(line)-1] = '\0';
+       cgiDebugOutput (4, "Read line '%s'", line);
+       return line;
+      }
+    }
+
+    return NULL;
+}
+
 /*
  * Local variables:
  *  c-indent-level: 4