This is the ubiqx binary tree and linked list library.
[Samba.git] / source / web / cgi.c
blob56c293985d60e1ec3b362a4a33cb9067b14b6911
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 some simple CGI helper routines
5 Copyright (C) Andrew Tridgell 1997
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
25 #define MAX_VARIABLES 512
27 struct var {
28 char *name;
29 char *value;
32 static struct var variables[MAX_VARIABLES];
33 static int num_variables;
36 static int grab_line(int *cl, char *line, int maxsize)
38 int i = 0;
40 while ((*cl)) {
41 int c = fgetc(stdin);
42 (*cl)--;
44 if (c == EOF) {
45 (*cl) = 0;
46 break;
49 if (c == '+') {
50 c = ' ';
53 if (c == '\r') continue;
55 if (strchr("\n&", c)) break;
57 if (c == '%' && (*cl) >= 2) {
58 int c1, c2;
59 c1 = fgetc(stdin);
60 c2 = fgetc(stdin);
61 (*cl) -= 2;
62 if (c1 == EOF || c2 == EOF) break;
63 if (c1 >= '0' && c1 <= '9')
64 c1 = c1 - '0';
65 else if (c1 >= 'A' && c1 <= 'F')
66 c1 = 10 + c1 - 'A';
67 else if (c1 >= 'a' && c1 <= 'f')
68 c1 = 10 + c1 - 'a';
69 else break;
71 if (c2 >= '0' && c2 <= '9')
72 c2 = c2 - '0';
73 else if (c2 >= 'A' && c2 <= 'F')
74 c2 = 10 + c2 - 'A';
75 else if (c2 >= 'a' && c2 <= 'f')
76 c2 = 10 + c2 - 'a';
77 else break;
79 c = (c1<<4) | c2;
82 line[i++] = c;
84 if (i == maxsize) break;
87 /* now unescape the line */
90 line[i] = 0;
91 return 1;
95 /***************************************************************************
96 load all the variables passed to the CGI program
97 ***************************************************************************/
98 void cgi_load_variables(void)
100 static pstring line;
101 char *p;
102 int len;
104 if (!(p=getenv("CONTENT_LENGTH"))) return;
106 len = atoi(p);
108 if (len <= 0) return;
112 while (len && grab_line(&len, line, sizeof(line)-1)) {
113 p = strchr(line,'=');
114 if (!p) continue;
116 *p = 0;
118 variables[num_variables].name = strdup(line);
119 variables[num_variables].value = strdup(p+1);
121 if (!variables[num_variables].name ||
122 !variables[num_variables].value)
123 continue;
125 #if 0
126 printf("%s=%s<br>\n",
127 variables[num_variables].name,
128 variables[num_variables].value);
129 #endif
131 num_variables++;
132 if (num_variables == MAX_VARIABLES) break;
135 fclose(stdin);
139 /***************************************************************************
140 find a variable passed via CGI
141 ***************************************************************************/
142 char *cgi_variable(char *name)
144 int i;
146 for (i=0;i<num_variables;i++)
147 if (strcmp(variables[i].name, name) == 0)
148 return variables[i].value;
149 return NULL;
153 /***************************************************************************
154 return the value of a CGI boolean variable.
155 ***************************************************************************/
156 int cgi_boolean(char *name, int def)
158 char *p = cgi_variable(name);
160 if (!p) return def;
162 return strcmp(p, "1") == 0;