Misc. updates to get a clean make on Linux. A bit of
[catagits/fcgi2.git] / examples / echo2.c
1 /* 
2  * echo2.c --
3  *
4  *      Produce a page containing all the inputs (fcgiapp version)
5  *
6  *
7  * Copyright (c) 1996 Open Market, Inc.
8  *
9  * See the file "LICENSE.TERMS" for information on usage and redistribution
10  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11  *
12  */
13
14 #ifndef lint
15 static const char rcsid[] = "$Id: echo2.c,v 1.2 1999/01/30 22:27:32 roberts Exp $";
16 #endif /* not lint */
17
18 #if defined HAVE_UNISTD_H || __linux__
19 #include <unistd.h>
20 #endif
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include "fcgiapp.h"
25
26 #ifdef _WIN32
27 #include <process.h>
28 #endif
29
30 static void PrintEnv(FCGX_Stream *out, char *label, char **envp)
31 {
32     printf("%s:<br>\n<pre>\n", label);
33     FCGX_FPrintF(out, "%s:<br>\n<pre>\n", label);
34     for(; *envp != NULL; envp++) {
35         FCGX_FPrintF(out, "%s\n", *envp);
36     }
37     FCGX_FPrintF(out, "</pre><p>\n");
38 }
39
40 #ifndef _WIN32
41 extern char **environ;
42 #endif
43
44 void main ()
45 {
46     FCGX_Stream *in, *out, *err;
47     FCGX_ParamArray envp;
48     int count = 0;
49     while (FCGX_Accept(&in, &out, &err, &envp) >= 0) {
50         char *contentLength = FCGX_GetParam("CONTENT_LENGTH", envp);
51         int len;
52         FCGX_FPrintF(out,
53                "Content-type: text/html\r\n"
54                "\r\n"
55                "<title>FastCGI echo (fcgiapp version)</title>"
56                "<h1>FastCGI echo (fcgiapp version)</h1>\n"
57                "Request number %d,  Process ID: %d<p>\n", ++count, 
58                      getpid());
59         if(contentLength != NULL) {
60             len = strtol(contentLength, NULL, 10);
61         } else {
62             len = 0;
63         }
64         if(len <= 0) {
65             FCGX_FPrintF(out, "No data from standard input.<p>\n");
66         } else {
67             int i, ch;
68             FCGX_FPrintF(out, "Standard input:<br>\n<pre>\n");
69             for(i = 0; i < len; i++) {
70                 if((ch = FCGX_GetChar(in)) < 0) {
71                     FCGX_FPrintF(out, "Error: Not enough bytes received "
72                                       "on standard input<p>\n");
73                     break;
74                 }
75                 FCGX_PutChar(ch, out);
76             }
77             FCGX_FPrintF(out, "\n</pre><p>\n");
78         }
79         PrintEnv(out, "Request environment", envp);
80         PrintEnv(out, "Initial environment", environ);
81     } /* while */
82 }