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