File: cgitool.c

package info (click to toggle)
berolist 2.5.5-9
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 436 kB
  • ctags: 100
  • sloc: ansic: 2,770; sh: 1,513; makefile: 97
file content (94 lines) | stat: -rw-r--r-- 1,823 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "tool.h"
#include "cgitool.h"

char *cgi_scan(char *setting) 
{
	char *par, *loc, *cont;
	char *buf;
	char *ascii;
	register int i;
	
	par=getenv("QUERY_STRING");
	if(par==NULL) return NULL;
	buf=salloc(slen(par)+1);
	strcpy(buf,par);
	
	loc=strtok(buf,"&");
	while (loc) {
		cont=strstr(loc,setting);
		if(cont==loc) {
			loc=strchr(cont,'=')+1;
			loc=strdup(loc);
			for(i=0;i<slen(loc);i++) {
				if(loc[i]=='+') loc[i]=' '; /* CGI: spaces are replaced by + */
				if(loc[i]=='%') {		/* special chars... %2F = / */
					if(loc[i+1]!='%') {
						ascii=salloc(3);
						ascii[0]=loc[i+1];
						ascii[1]=loc[i+2];
						loc[i]=(char) strtol(ascii,NULL,16);
						strcpy(loc+i+1,loc+i+3);
					} else
						i++;
				}
			}
			free(buf);
			if(slen(loc)==0) /* Treat empty fields as nonexistant */
				return NULL;
			return loc;
		} else
			loc=strtok(NULL,"&");
	}
	free(buf);
	return NULL;
}

unsigned int cgi_count()
{
	char *par, *loc;
	char *buf;
	register int count=0;
	
	par=getenv("QUERY_STRING");
	if(par==NULL) return 0;
	buf=salloc(slen(par)+1);
	strcpy(buf,par);
	
	loc=strtok(buf,"&");
	while (loc) {
		count++;
		loc=strtok(NULL,"&");
	}
	free(buf);
	return count;
}

char *to_cgi(char *t)
{
	char *cgi;
	register int i;
	int loc=0;
	
	cgi=salloc(3*slen(t));
	for(i=0;i<slen(t);i++) {
		if((t[i]>='a' && t[i]<='z') || (t[i]>='A' && t[i]<='Z'))
			cgi[loc++]=t[i];
		else if(t[i]==' ')
			cgi[loc++]='+';
		else if(t[i]!='\r') {
			sprintf(cgi+loc,"%%%2x",t[i]);
			loc+=3;
		}
	}
	cgi=(char *) realloc(cgi,slen(cgi)+1);
	return cgi;
}

void html_head(char *title)
{
	puts("Content-Type: text/html\n\n");
	puts("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">");
	printf("<HTML><HEAD><TITLE>%s: %s</TITLE>",VERSION,title);
	printf("<META NAME=Generator CONTENT=%s>",VERSION);
	puts("<BODY>");
}