File: utils.c

package info (click to toggle)
man2html 1.6f%2Brepack-1%2Bsqueeze1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,416 kB
  • ctags: 2,894
  • sloc: ansic: 9,558; sh: 2,301; makefile: 343; perl: 335; awk: 305; lisp: 171; cs: 170
file content (239 lines) | stat: -rw-r--r-- 4,172 bytes parent folder | download | duplicates (3)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* vim:ts=4
 *
 * utils.c - man2html utility functions
 *
 * Author: Robert Luberda <robert@debian.org>, Jan 2003
 * Copyright: GPL
 *
 * "$Id: utils.c,v 1.5 2003-02-02 21:01:39 robert Exp $"
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>

#include "utils.h"



void error_page(int status, char *s, char *t, ...) 
{
     va_list p;

     switch(status) {
		case 400:
			printf("Status: 400 Bad Request\n");
			break;
		case 403:
			printf("Status: 403 Forbidden\n");
			break;
		case 404:
			printf("Status: 404 Not Found\n");
			break;
		case 500:
			printf("Status: 500 Internal Server Error\n");
			break;
		case 0:
		default:
			break;
     }
	     
     printf(CONTENTTYPE DOCTYPE);
     printf("<HTML><HEAD><TITLE>%s</TITLE></HEAD>\n"
	    "<BODY>\n<H1>%s</H1>\n", s, s);
     va_start(p, t);
     vfprintf(stdout, t, p);
     va_end(p);
     printf("</BODY></HTML>\n");
	 if (status == 0)
	    exit(EXIT_SUCCESS);
	 else
		exit(EXIT_FAILURE);			 
}


char * xstrdup(const char *s) 
{
     char *p = strdup(s);
     if (p == NULL)
	  error_page(500, "Out of memory",
			 "Sorry, out of memory, aborting...\n");
     return p;
}

void * xmalloc(size_t size) 
{
     void *p = malloc(size);
     if (p == NULL)
	  error_page(500, "Out of memory",
			 "Sorry, out of memory, aborting...\n");
     return p;
}

void * xrealloc(void *ptr, size_t size) 
{
     void *p = realloc(ptr,size);
     if (p == NULL)
	  error_page(500, "Out of memory",
			 "Sorry, out of memory, aborting...\n");
     return p;
}


/* decode CGI urls */
char * urldecode(char *s)
{
		char *p, *q, *out;
		char buf[3];
		

		out = strdup(s);
		p = s;
	    q = out;
		while (*p)
		{
				if (*p == '+')
						*q++ = ' ';
				else if ((*p == '%') && isxdigit(*(p+1)) && isxdigit(*(p+2)))
				{
					buf[0] = *(p+1);
					buf[1] = *(p+2);
					buf[2] = '\0';
					*q++   = (char)(strtol(buf, NULL, 16));
					p      += 2;
				}
				else
					*q++ = *p;
			p++;
		};
		*q = '\0';
		return out;
}			
			
/* encode urls */
char * urlencode(char *s)
{
		char *p, *q, *out;
		char buf[3];
		size_t i, len;
		
		len = strlen(s) + 1;
		out = (char*) malloc(len * sizeof(char));
		p = s;
		q = out;
		while (*p)
		{
				if (*p == ' ')
						*q++ = '+';
				else if (isalnum(*p) || *p == '_' || *p == ':' || *p == '-'
								|| *p == '.' || *p == '/')
						*q++ = *p;
				else
				{
						sprintf(buf, "%02x", (unsigned char)*p);
						len += 2;
						i = q - out;
						out = (char*) realloc(out, len * sizeof(char));
						q = out + i;
						*q++ = '%';
						*q++ = buf[0];
						*q++ = buf[1];
				}
				p++;
		}
		*q = '\0';
		return out;
}
		
void print_html_enc(char *s, FILE *f)
{
		while (*s)
		{
				if (*s == '<')
						fputs("&lt;", f);
				else if (*s == '>')								
						fputs("&gt;", f);
				else if (*s == '&')								
						fputs("&amp;", f);
				else if (*s == '"')								
						fputs("&quot;", f);
				else
						fputc(*s, f);						
				s++;
		}
}

int querystring2argv(int *argc, char ***argv)
{
		char *t, *s, *q;
		char **new_argv;
		int new_argc;
		
		if (!(t = getenv("QUERY_STRING")))
				return 0;

		if (!(s = strstr(t, QUERY_MAGIC_STR)))
				return 0;

		s += sizeof(QUERY_MAGIC_STR) - 1;

		/* kill at first '&' */
		if ((q = strstr(s, "&")))
				*q = '\0';

		s = urldecode(s);

		new_argv = malloc(2 * sizeof(char**));
		new_argv[0] = *argv[0];
		new_argc = 1;

		q = strtok(s, " \t\n\r");
		while (q)
		{
			new_argv[new_argc++] = strdup(q);
			q = strtok(NULL, " \t\n\r");
			new_argv = realloc(new_argv, (new_argc + 1) * sizeof(char**));
		}
		*(new_argv + new_argc) = NULL;
		*argc = new_argc;
		*argv = new_argv;
		
		free(s);
		return 1;
}

/*
# Do we need lynxcgi URLs? For the moment our criterion is
# 1) HTTP_USER_AGENT=Lynx*  and 2) HTTP_HOST is unset.
*/
int is_lynx() 
{
	char *t;

	t = getenv("HTTP_HOST");
	if (t && *t)
			return 0;

	t = getenv("HTTP_USER_AGENT");
	if (!t || !*t)
			return 0;

	return !strncasecmp(t, "lynx", 4);
}
	

#if UTILS_TEST
int main(int argc, char ** argv)
{
		char * s;
		querystring2argv(&argc, &argv);
				
		while (argc)
				puts(argv[--argc]);
		return 0;
}
		
#endif