X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=libfcgi%2Ffcgi_stdio.c;h=a4f395ac51a94cbf85758bcce240403f8e0e21ed;hb=020897044f17e4d1acfb64d389df06fe19920e6f;hp=befd500aa22ae28ef4bffee10c21e84ad227badc;hpb=ed728efb4171b6205476c97d17b94dadde4bfaa1;p=catagits%2Ffcgi2.git diff --git a/libfcgi/fcgi_stdio.c b/libfcgi/fcgi_stdio.c index befd500..a4f395a 100644 --- a/libfcgi/fcgi_stdio.c +++ b/libfcgi/fcgi_stdio.c @@ -1,4 +1,4 @@ -/* +/* * fcgi_stdio.c -- * * FastCGI-stdio compatibility package @@ -12,51 +12,63 @@ */ #ifndef lint -static const char rcsid[] = "$Id: fcgi_stdio.c,v 1.4 1999/01/30 22:24:09 roberts Exp $"; +static const char rcsid[] = "$Id: fcgi_stdio.c,v 1.9 2000/10/30 22:52:24 robs Exp $"; #endif /* not lint */ +#include "fcgi_config.h" + #ifdef _WIN32 #define DLLAPI __declspec(dllexport) +#include #endif -#define NO_FCGI_DEFINES -#include "fcgi_stdio.h" -#undef NO_FCGI_DEFINES +#include /* for errno */ +#include /* for va_arg */ +#include /* for malloc */ +#include /* for strerror */ -#ifdef _WIN32 -#include -#endif -#include - /* for errno */ -#include - /* for va_arg */ -#include - /* for malloc */ -#include - /* for strerror */ #ifdef HAVE_UNISTD_H #include #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 -#ifdef _WIN32 FCGI_FILE _fcgi_sF[3]; -#else -FCGI_FILE _fcgi_sF[3] = {{stdin, NULL}, {stdout, NULL}, {stderr, NULL}}; -#endif -#ifdef _WIN32 -#define popen _popen -#endif /* *---------------------------------------------------------------------- @@ -105,9 +117,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 +127,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 @@ -192,16 +202,16 @@ void FCGI_Finish(void) * * 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 * *---------------------------------------------------------------------- */ @@ -267,18 +277,22 @@ 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; } @@ -294,7 +308,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) @@ -396,7 +416,7 @@ int FCGI_ftell(FCGI_FILE *fp) else { OS_SetErrno(ESPIPE); return -1; - } + } } void FCGI_rewind(FCGI_FILE *fp) @@ -415,7 +435,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,7 +445,7 @@ int FCGI_fsetpos(FCGI_FILE *fp, const fpos_t *pos) else { OS_SetErrno(ESPIPE); return -1; - } + } } #endif @@ -496,7 +516,7 @@ char *FCGI_gets(char *str) int c; for (s = str; ((c = FCGI_getchar()) != '\n');) { if(c == EOF) { - if(s == str) + if(s == str) return NULL; else break; @@ -535,7 +555,7 @@ 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); @@ -632,7 +652,7 @@ 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,7 +661,7 @@ 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; } @@ -737,7 +757,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; } @@ -751,22 +777,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) @@ -777,25 +787,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) { /*