*
* 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.8 1999/07/28 00:24:15 roberts Exp $";
+static const char rcsid[] = "$Id: fcgi_stdio.c,v 1.14 2001/09/01 01:09:30 robs Exp $";
#endif /* not lint */
-#include "fcgi_config.h"
-
-#ifdef _WIN32
-#define DLLAPI __declspec(dllexport)
-#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 */
+#include "fcgi_config.h"
+
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
-#define NO_FCGI_DEFINES
-#include "fcgi_stdio.h"
-#undef NO_FCGI_DEFINES
+#ifdef _WIN32
+#define DLLAPI __declspec(dllexport)
+#endif
#include "fcgiapp.h"
#include "fcgios.h"
+#include "fcgimisc.h"
+
+#define NO_FCGI_DEFINES
+#include "fcgi_stdio.h"
+#undef NO_FCGI_DEFINES
#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
+#ifdef HAVE_FILENO_PROTO
+#include <stdio.h>
+#else
extern int fileno(FILE *stream);
#endif
#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];
-\f
+
/*
*----------------------------------------------------------------------
*
}
return 0;
}
-\f
+
/*
*----------------------------------------------------------------------
*
FCGI_stderr->fcgx_stream = NULL;
environ = NULL;
}
-\f
+
/*
*----------------------------------------------------------------------
*
*
*----------------------------------------------------------------------
*/
-
int FCGI_StartFilterData(void)
{
if(FCGI_stdin->stdio_stream) {
return FCGX_StartFilterData(FCGI_stdin->fcgx_stream);
}
}
-\f
+
/*
*----------------------------------------------------------------------
*
*
*----------------------------------------------------------------------
*/
-
void FCGI_SetExitStatus(int status)
{
if(FCGI_stdin->fcgx_stream) {
FCGX_SetExitStatus(status, FCGI_stdin->fcgx_stream);
}
}
-\f
+
/*
*----------------------------------------------------------------------
*
*
*----------------------------------------------------------------------
*/
-
void FCGI_perror(const char *str)
{
FCGI_fputs(str, FCGI_stderr);
FCGI_fputs(strerror(OS_Errno), FCGI_stderr);
return;
}
-\f
+
/*
*----------------------------------------------------------------------
*
*
*----------------------------------------------------------------------
*/
-
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
+
/*
*----------------------------------------------------------------------
*
*
*----------------------------------------------------------------------
*/
-
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)
}
return NULL;
}
-\f
+
/*
*----------------------------------------------------------------------
*
if(fp->stdio_stream)
setbuf(fp->stdio_stream, buf);
}
-\f
+
/*
*----------------------------------------------------------------------
*
*
*----------------------------------------------------------------------
*/
-
int FCGI_fseek(FCGI_FILE *fp, long offset, int whence)
{
if(fp->stdio_stream)
}
}
#endif
-\f
+
/*
*----------------------------------------------------------------------
*
*
*----------------------------------------------------------------------
*/
-
int FCGI_fgetc(FCGI_FILE *fp)
{
if(fp->stdio_stream)
return FCGX_UnGetChar(c, fp->fcgx_stream);
return EOF;
}
-\f
+
/*
*----------------------------------------------------------------------
*
*
*----------------------------------------------------------------------
*/
-
char *FCGI_fgets(char *str, int size, FCGI_FILE *fp)
{
if(fp->stdio_stream)
}
/*
- * 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)
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
*----------------------------------------------------------------------
*/
-\f
/*
*----------------------------------------------------------------------
*
*
*----------------------------------------------------------------------
*/
-
int FCGI_fputc(int c, FCGI_FILE *fp)
{
if(fp->stdio_stream)
{
return FCGI_fputc(c, FCGI_stdout);
}
-\f
+
/*
*----------------------------------------------------------------------
*
*
*----------------------------------------------------------------------
*/
-
int FCGI_fputs(const char *str, FCGI_FILE *fp)
{
if(fp->stdio_stream)
}
return EOF;
}
-\f
+
/*
*----------------------------------------------------------------------
*
*
*----------------------------------------------------------------------
*/
-
int FCGI_fprintf(FCGI_FILE *fp, const char *format, ...)
{
va_list ap;
va_end(ap);
return n;
}
-\f
+
/*
*----------------------------------------------------------------------
*
*
*----------------------------------------------------------------------
*/
-
int FCGI_vfprintf(FCGI_FILE *fp, const char *format, va_list ap)
{
if(fp->stdio_stream)
return FCGX_VFPrintF(FCGI_stdout->fcgx_stream, format, ap);
return EOF;
}
-\f
+
/*
*----------------------------------------------------------------------
*
*
*----------------------------------------------------------------------
*/
-
size_t FCGI_fread(void *ptr, size_t size, size_t nmemb, FCGI_FILE *fp)
{
int n;
}
return (size_t)EOF;
}
-\f
+
/*
*----------------------------------------------------------------------
*
}
return;
}
-\f
+
/*
*----------------------------------------------------------------------
*
*/
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
/*
*----------------------------------------------------------------------
*
*
*----------------------------------------------------------------------
*/
-
int FCGI_fileno(FCGI_FILE *fp)
{
if(fp->stdio_stream)
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) {
/*
}
return n;
}
-
-/*
- *----------------------------------------------------------------------
- */