Removed genereating of FCGI.xs
[catagits/fcgi2.git] / examples / echo.c
1 /*
2  * echo.c --
3  *
4  *      Produce a page containing all FastCGI inputs
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 #ifndef lint
14 static const char rcsid[] = "$Id: echo.c,v 1.5 1999/07/28 00:29:37 roberts Exp $";
15 #endif /* not lint */
16
17 #include "fcgi_config.h"
18
19 #include <stdlib.h>
20
21 #ifdef HAVE_UNISTD_H
22 #include <unistd.h>
23 #endif
24
25 #ifdef _WIN32
26 #include <process.h>
27 #else
28 extern char **environ;
29 #endif
30
31 #include "fcgi_stdio.h"
32
33
34 static void PrintEnv(char *label, char **envp)
35 {
36     printf("%s:<br>\n<pre>\n", label);
37     for ( ; *envp != NULL; envp++) {
38         printf("%s\n", *envp);
39     }
40     printf("</pre><p>\n");
41 }
42
43 int main ()
44 {
45     char **initialEnv = environ;
46     int count = 0;
47
48     while (FCGI_Accept() >= 0) {
49         char *contentLength = getenv("CONTENT_LENGTH");
50         int len;
51
52         printf("Content-type: text/html\r\n"
53             "\r\n"
54             "<title>FastCGI echo</title>"
55             "<h1>FastCGI echo</h1>\n"
56             "Request number %d,  Process ID: %d<p>\n", ++count, getpid());
57
58         if (contentLength != NULL) {
59             len = strtol(contentLength, NULL, 10);
60         }
61         else {
62             len = 0;
63         }
64
65         if (len <= 0) {
66             printf("No data from standard input.<p>\n");
67         }
68         else {
69             int i, ch;
70
71             printf("Standard input:<br>\n<pre>\n");
72             for (i = 0; i < len; i++) {
73                 if ((ch = getchar()) < 0) {
74                     printf("Error: Not enough bytes received on standard input<p>\n");
75                     break;
76                 }
77                 putchar(ch);
78             }
79             printf("\n</pre><p>\n");
80         }
81
82         PrintEnv("Request environment", environ);
83         PrintEnv("Initial environment", initialEnv);
84     } /* while */
85
86     return 0;
87 }