renamed libfcgi.dsp
[catagits/fcgi2.git] / libfcgi / fcgi_stdio.c
index 79ca307..85a1bac 100644 (file)
  */
 
 #ifndef lint
-static const char rcsid[] = "$Id: fcgi_stdio.c,v 1.7 1999/07/27 15:00:16 roberts Exp $";
+static const char rcsid[] = "$Id: fcgi_stdio.c,v 1.10 2001/06/18 14:44:37 robs Exp $";
 #endif /* not lint */
 
+#include "fcgi_config.h"
+
 #ifdef _WIN32
 #define DLLAPI  __declspec(dllexport)
+#define WIN32_LEAN_AND_MEAN 
+#include <windows.h>
 #endif
 
-#define NO_FCGI_DEFINES
-#include "fcgi_stdio.h"
-#undef NO_FCGI_DEFINES
+#include <errno.h>  /* for errno */
+#include <stdarg.h> /* for va_arg */
+#include <stdlib.h> /* for malloc */
+#include <string.h> /* for strerror */
 
-#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
 
+#define NO_FCGI_DEFINES
+#include "fcgi_stdio.h"
+#undef NO_FCGI_DEFINES
+
 #include "fcgiapp.h"
 #include "fcgios.h"
 
+#ifndef _WIN32
+
+extern char **environ;
+
+/* These definitions should be supplied by stdio.h but for some
+ * reason they get lost on certain platforms. */
+#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);
+
+#else /* _WIN32 */
+
+#define popen _popen
+#define pclose _pclose
+
+#endif /* _WIN32 */
+
 #ifndef FALSE
 #define FALSE (0)
 #endif
+
 #ifndef TRUE
 #define TRUE  (1)
 #endif
 
 FCGI_FILE _fcgi_sF[3];
 
-#ifdef _WIN32
-#define popen _popen
-#endif
 \f
 /*
  *----------------------------------------------------------------------
@@ -101,9 +118,6 @@ FCGI_FILE _fcgi_sF[3];
  */
 static int acceptCalled = FALSE;
 static int isCGI = FALSE;
-#ifndef _WIN32
-extern char **environ;
-#endif
 
 int FCGI_Accept(void)
 {
@@ -269,13 +283,17 @@ void FCGI_perror(const char *str)
 static FCGI_FILE *FCGI_OpenFromFILE(FILE *stream)
 {
     FCGI_FILE *fp;
-    if(stream == NULL)
-        return NULL;
-    fp = (FCGI_FILE *)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,7 +309,13 @@ 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)
@@ -734,7 +758,13 @@ void FCGI_clearerr(FCGI_FILE *fp)
  */
 FCGI_FILE *FCGI_tmpfile(void)
 {
-       return FCGI_OpenFromFILE(tmpfile());
+    FILE * file = tmpfile();
+    FCGI_FILE * fcgi_file = FCGI_OpenFromFILE(file);
+
+    if (file && !fcgi_file)
+        fclose(file);
+
+    return fcgi_file;
 }
 
 \f
@@ -748,22 +778,6 @@ FCGI_FILE *FCGI_tmpfile(void)
  *----------------------------------------------------------------------
  */
 
-/*
- * 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
- */
-#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)
@@ -774,25 +788,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) {
         /*