rename this file so it is visible by metacpan et al
[catagits/fcgi2.git] / libfcgi / fcgi_stdio.c
index 5c2577f..47dcc48 100644 (file)
@@ -1,4 +1,4 @@
-/* 
+/*
  * fcgi_stdio.c --
  *
  *      FastCGI-stdio compatibility package
@@ -6,58 +6,62 @@
  *
  * Copyright (c) 1996 Open Market, Inc.
  *
- * See the file "LICENSE.TERMS" for information on usage and redistribution
+ * See the file "LICENSE" for information on usage and redistribution
  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  *
  */
 
 #ifndef lint
-static const char rcsid[] = "$Id: fcgi_stdio.c,v 1.2 1998/03/09 15:47:09 roberts Exp $";
+static const char rcsid[] = "$Id: fcgi_stdio.c,v 1.14 2001/09/01 01:09:30 robs Exp $";
 #endif /* not lint */
 
-#ifdef _WIN32
-#define DLLAPI  __declspec(dllexport)
-#endif
+#include <errno.h>  /* for errno */
+#include <stdarg.h> /* for va_arg */
+#include <stdlib.h> /* for malloc */
+#include <string.h> /* for strerror */
 
-#define NO_FCGI_DEFINES
-#include "fcgi_stdio.h"
-#undef NO_FCGI_DEFINES
+#include "fcgi_config.h"
 
-#ifdef _WIN32
-#include <windows.h>
-#endif
-#include <errno.h>
-    /* for errno */
-#include <stdarg.h>
-    /* for va_arg */
-#include <stdlib.h>
-    /* for malloc */
-#include <string.h>
-    /* for strerror */
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
 
+#ifdef _WIN32
+#define DLLAPI  __declspec(dllexport)
+#endif
+
 #include "fcgiapp.h"
 #include "fcgios.h"
+#include "fcgimisc.h"
 
-#ifndef FALSE
-#define FALSE (0)
-#endif
-#ifndef TRUE
-#define TRUE  (1)
-#endif
+#define NO_FCGI_DEFINES
+#include "fcgi_stdio.h"
+#undef NO_FCGI_DEFINES
 
-#ifdef _WIN32
-FCGI_FILE _fcgi_sF[3];
+#ifndef _WIN32
+
+extern char **environ;
+
+#ifdef HAVE_FILENO_PROTO
+#include <stdio.h>
 #else
-FCGI_FILE _fcgi_sF[3] = {{stdin, NULL}, {stdout, NULL}, {stderr, NULL}};
+extern int fileno(FILE *stream);
 #endif
 
-#ifdef _WIN32
+extern FILE *fdopen(int fildes, const char *type);
+extern FILE *popen(const char *command, const char *type);
+extern int pclose(FILE *stream);
+
+#else /* _WIN32 */
+
 #define popen _popen
-#endif
-\f
+#define pclose _pclose
+
+#endif /* _WIN32 */
+
+FCGI_FILE _fcgi_sF[3];
+
+
 /*
  *----------------------------------------------------------------------
  *
@@ -105,9 +109,6 @@ FCGI_FILE _fcgi_sF[3] = {{stdin, NULL}, {stdout, NULL}, {stderr, NULL}};
  */
 static int acceptCalled = FALSE;
 static int isCGI = FALSE;
-#ifndef _WIN32
-extern char **environ;
-#endif
 
 int FCGI_Accept(void)
 {
@@ -118,6 +119,7 @@ int FCGI_Accept(void)
          */
         isCGI = FCGX_IsCGI();
         acceptCalled = TRUE;
+        atexit(&FCGI_Finish);
     } else if(isCGI) {
         /*
          * Not first call to FCGI_Accept and running as CGI means
@@ -149,7 +151,7 @@ int FCGI_Accept(void)
     }
     return 0;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -186,26 +188,25 @@ void FCGI_Finish(void)
     FCGI_stderr->fcgx_stream = NULL;
     environ = NULL;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
  * FCGI_StartFilterData --
  *
- *      
+ *
  *      The current request is for the filter role, and stdin is
- *      positioned at EOF of FCGI_STDIN.  The call repositions 
+ *      positioned at EOF of FCGI_STDIN.  The call repositions
  *      stdin to the start of FCGI_DATA.
  *      If the preconditions are not met (e.g. FCGI_STDIN has not
  *      been read to EOF), the call sets the stream error code to
  *      FCGX_CALL_SEQ_ERROR.
  *
  * Results:
- *      0 for a normal return, < 0 for error 
+ *      0 for a normal return, < 0 for error
  *
  *----------------------------------------------------------------------
  */
-
 int FCGI_StartFilterData(void)
 {
     if(FCGI_stdin->stdio_stream) {
@@ -214,7 +215,7 @@ int FCGI_StartFilterData(void)
         return FCGX_StartFilterData(FCGI_stdin->fcgx_stream);
     }
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -229,14 +230,13 @@ int FCGI_StartFilterData(void)
  *
  *----------------------------------------------------------------------
  */
-
 void FCGI_SetExitStatus(int status)
 {
     if(FCGI_stdin->fcgx_stream) {
         FCGX_SetExitStatus(status, FCGI_stdin->fcgx_stream);
     }
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -246,7 +246,6 @@ void FCGI_SetExitStatus(int status)
  *
  *----------------------------------------------------------------------
  */
-
 void FCGI_perror(const char *str)
 {
     FCGI_fputs(str, FCGI_stderr);
@@ -254,7 +253,7 @@ void FCGI_perror(const char *str)
     FCGI_fputs(strerror(OS_Errno), FCGI_stderr);
     return;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -267,21 +266,24 @@ void FCGI_perror(const char *str)
  *      otherwise the new FCGI_FILE *.
  *
  *----------------------------------------------------------------------
- */ 
-
+ */
 static FCGI_FILE *FCGI_OpenFromFILE(FILE *stream)
 {
     FCGI_FILE *fp;
-    if(stream == NULL)
-        return NULL;
-    fp = malloc(sizeof(FCGI_FILE));
-    if(fp == NULL)
+
+    if (stream == NULL)
         return NULL;
-    fp->stdio_stream = stream;
-    fp->fcgx_stream = NULL;
+
+    fp = (FCGI_FILE *) malloc(sizeof(FCGI_FILE));
+    if (fp != NULL)
+    {
+        fp->stdio_stream = stream;
+        fp->fcgx_stream = NULL;
+    }
+
     return fp;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -291,10 +293,15 @@ static FCGI_FILE *FCGI_OpenFromFILE(FILE *stream)
  *
  *----------------------------------------------------------------------
  */
-
 FCGI_FILE *FCGI_fopen(const char *path, const char *mode)
 {
-    return FCGI_OpenFromFILE(fopen(path, mode));
+    FILE * file = fopen(path, mode);
+    FCGI_FILE * fcgi_file = FCGI_OpenFromFILE(file);
+
+    if (file && !fcgi_file)
+        fclose(file);
+
+    return fcgi_file;
 }
 
 int FCGI_fclose(FCGI_FILE *fp)
@@ -315,8 +322,8 @@ int FCGI_fclose(FCGI_FILE *fp)
 
 int FCGI_fflush(FCGI_FILE *fp)
 {
-    if(fp == null)\r
-       return fflush(NULL);\r
+    if(fp == NULL)
+       return fflush(NULL);
     if(fp->stdio_stream)
         return fflush(fp->stdio_stream);
     else if(fp->fcgx_stream)
@@ -344,7 +351,7 @@ FCGI_FILE *FCGI_freopen(const char *path, const char *mode,
     }
     return NULL;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -368,7 +375,7 @@ void FCGI_setbuf(FCGI_FILE *fp, char *buf)
     if(fp->stdio_stream)
         setbuf(fp->stdio_stream, buf);
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -378,7 +385,6 @@ void FCGI_setbuf(FCGI_FILE *fp, char *buf)
  *
  *----------------------------------------------------------------------
  */
-
 int FCGI_fseek(FCGI_FILE *fp, long offset, int whence)
 {
     if(fp->stdio_stream)
@@ -396,7 +402,7 @@ int FCGI_ftell(FCGI_FILE *fp)
     else {
         OS_SetErrno(ESPIPE);
         return -1;
-    } 
+    }
 }
 
 void FCGI_rewind(FCGI_FILE *fp)
@@ -415,7 +421,7 @@ int FCGI_fgetpos(FCGI_FILE *fp, fpos_t *pos)
     else {
         OS_SetErrno(ESPIPE);
         return -1;
-    } 
+    }
 }
 
 int FCGI_fsetpos(FCGI_FILE *fp, const fpos_t *pos)
@@ -425,10 +431,10 @@ int FCGI_fsetpos(FCGI_FILE *fp, const fpos_t *pos)
     else {
         OS_SetErrno(ESPIPE);
         return -1;
-    } 
+    }
 }
 #endif
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -441,7 +447,6 @@ int FCGI_fsetpos(FCGI_FILE *fp, const fpos_t *pos)
  *
  *----------------------------------------------------------------------
  */
-
 int FCGI_fgetc(FCGI_FILE *fp)
 {
     if(fp->stdio_stream)
@@ -464,7 +469,7 @@ int FCGI_ungetc(int c, FCGI_FILE *fp)
         return FCGX_UnGetChar(c, fp->fcgx_stream);
     return EOF;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -474,7 +479,6 @@ int FCGI_ungetc(int c, FCGI_FILE *fp)
  *
  *----------------------------------------------------------------------
  */
-
 char *FCGI_fgets(char *str, int size, FCGI_FILE *fp)
 {
     if(fp->stdio_stream)
@@ -485,33 +489,32 @@ char *FCGI_fgets(char *str, int size, FCGI_FILE *fp)
 }
 
 /*
- * There is no standard equivalent of gets that takes an explicit
- * FILE * argument, so the following "wrapper" implements
- * gets for both FILE * and FCGX_Stream * in terms of FCGI_getchar.
+ * The gets() function reads characters from the standard input stream
+ * into the array pointed to by str until a newline character is read
+ * or an end-of-file condition is encountered.  The newline character
+ * is discarded and the string is terminated with a null character. 
  */
-
 char *FCGI_gets(char *str)
 {
     char *s;
     int c;
+
     for (s = str; ((c = FCGI_getchar()) != '\n');) {
         if(c == EOF) {
-            if(s == str) 
+            if(s == str)
                 return NULL;
             else
                 break;
         } else
-            *s++ = c;
+            *s++ = (char) c;
     }
     *s = 0;
     return str;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
- * --
- *
  *       Wrappers for functions defined in H&S Section 15.8
  *
  *       XXX: missing: fscanf, scanf
@@ -519,7 +522,6 @@ char *FCGI_gets(char *str)
  *----------------------------------------------------------------------
  */
 
-\f
 /*
  *----------------------------------------------------------------------
  *
@@ -532,10 +534,9 @@ char *FCGI_gets(char *str)
  *
  *----------------------------------------------------------------------
  */
-
 int FCGI_fputc(int c, FCGI_FILE *fp)
 {
-    if(fp->stdio_stream) 
+    if(fp->stdio_stream)
         return fputc(c, fp->stdio_stream);
     else if(fp->fcgx_stream)
         return FCGX_PutChar(c, fp->fcgx_stream);
@@ -546,7 +547,7 @@ int FCGI_putchar(int c)
 {
     return FCGI_fputc(c, FCGI_stdout);
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -556,7 +557,6 @@ int FCGI_putchar(int c)
  *
  *----------------------------------------------------------------------
  */
-
 int FCGI_fputs(const char *str, FCGI_FILE *fp)
 {
     if(fp->stdio_stream)
@@ -584,7 +584,7 @@ int FCGI_puts(const char *str)
     }
     return EOF;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -594,7 +594,6 @@ int FCGI_puts(const char *str)
  *
  *----------------------------------------------------------------------
  */
-
 int FCGI_fprintf(FCGI_FILE *fp, const char *format, ...)
 {
     va_list ap;
@@ -617,7 +616,7 @@ int FCGI_printf(const char *format, ...)
     va_end(ap);
     return n;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -627,12 +626,11 @@ int FCGI_printf(const char *format, ...)
  *
  *----------------------------------------------------------------------
  */
-
 int FCGI_vfprintf(FCGI_FILE *fp, const char *format, va_list ap)
 {
     if(fp->stdio_stream)
         return vfprintf(fp->stdio_stream, format, ap);
-    else if(fp->fcgx_stream) 
+    else if(fp->fcgx_stream)
         return FCGX_VFPrintF(fp->fcgx_stream, format, ap);
     return EOF;
 }
@@ -641,11 +639,11 @@ int FCGI_vprintf(const char *format, va_list ap)
 {
     if(FCGI_stdout->stdio_stream)
         return vfprintf(FCGI_stdout->stdio_stream, format, ap);
-    else if(FCGI_stdout->fcgx_stream) 
+    else if(FCGI_stdout->fcgx_stream)
         return FCGX_VFPrintF(FCGI_stdout->fcgx_stream, format, ap);
     return EOF;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -655,7 +653,6 @@ int FCGI_vprintf(const char *format, va_list ap)
  *
  *----------------------------------------------------------------------
  */
-
 size_t FCGI_fread(void *ptr, size_t size, size_t nmemb, FCGI_FILE *fp)
 {
     int n;
@@ -685,7 +682,7 @@ size_t FCGI_fwrite(void *ptr, size_t size, size_t nmemb, FCGI_FILE *fp)
     }
     return (size_t)EOF;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -725,33 +722,36 @@ void FCGI_clearerr(FCGI_FILE *fp)
     }
     return;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
- * FCGI_fileno, FCGI_fdopen, FCGI_popen, FCGI_pclose --
+ * FCGI_tmpfile --
  *
- *       Wrappers for POSIX, X/OPEN functions not in ISO C
+ *       Wrappers for function defined in H&S Section 15.16
  *
  *----------------------------------------------------------------------
  */
+FCGI_FILE *FCGI_tmpfile(void)
+{
+    FILE * file = tmpfile();
+    FCGI_FILE * fcgi_file = FCGI_OpenFromFILE(file);
+
+    if (file && !fcgi_file)
+        fclose(file);
+
+    return fcgi_file;
+}
 
 /*
- * These definitions should be supplied by stdio.h but for some
- * reason they get lost on certain platforms.
- */
-/*
- * XXX: Need to find the right way to handle this for NT
+ *----------------------------------------------------------------------
+ *
+ * FCGI_fileno, FCGI_fdopen, FCGI_popen, FCGI_pclose --
+ *
+ *       Wrappers for POSIX, X/OPEN functions not in ISO C
+ *
+ *----------------------------------------------------------------------
  */
-#ifndef _WIN32
-#ifndef fileno
-extern int fileno(FILE *stream);
-#endif
-extern FILE *fdopen(int fildes, const char *type);
-extern FILE *popen(const char *command, const char *type);
-extern int pclose(FILE *stream);
-#endif
-
 int FCGI_fileno(FCGI_FILE *fp)
 {
     if(fp->stdio_stream)
@@ -762,25 +762,31 @@ int FCGI_fileno(FCGI_FILE *fp)
 
 FCGI_FILE *FCGI_fdopen(int fd, const char *mode)
 {
-#ifndef _WIN32
-    return FCGI_OpenFromFILE(fdopen(fd, mode));
-#else
-    return NULL;
-#endif
+    FILE * file = fdopen(fd, mode);
+    FCGI_FILE * fcgi_file = FCGI_OpenFromFILE(file);
+
+    if (file && !fcgi_file)
+        fclose(file);
+
+    return fcgi_file;
 }
 
 FCGI_FILE *FCGI_popen(const char *cmd, const char *type)
 {
-    return FCGI_OpenFromFILE(popen(cmd, type));
+    FILE * file = popen(cmd, type);
+    FCGI_FILE * fcgi_file = FCGI_OpenFromFILE(file);
+
+    if (file && !fcgi_file)
+        pclose(file);
+
+    return fcgi_file;
 }
 
 int FCGI_pclose(FCGI_FILE *fp)
 {
     int n = EOF;
-    if(fp->stdio_stream) {
-#ifndef _WIN32
+    if (fp->stdio_stream) {
         n = pclose(fp->stdio_stream);
-#endif
         fp->stdio_stream = NULL;
     } else if(fp->fcgx_stream) {
         /*
@@ -793,7 +799,3 @@ int FCGI_pclose(FCGI_FILE *fp)
     }
     return n;
 }
-
-/*
- *----------------------------------------------------------------------
- */