Found in davenant:/usr/local/src/misc
[userv-utils] / www-cgi / ucgicommon.c
1 /**/
2
3 #include <stdio.h>
4 #include <string.h>
5
6 #include "ucgi.h"
7
8 const char *const envok[]= {
9 "CONTENT_LENGTH",
10 "CONTENT_TYPE",
11 "DOCUMENT_ROOT",
12 "GATEWAY_INTERFACE",
13 "HTTP_ACCEPT",
14 "HTTP_ACCEPT_ENCODING",
15 "HTTP_ACCEPT_LANGUAGE",
16 "HTTP_CACHE_CONTROL",
17 "HTTP_HOST",
18 "HTTP_NEGOTIATE",
19 "HTTP_PRAGMA",
20 "HTTP_USER_AGENT",
21 "PATH_INFO",
22 "PATH_TRANSLATED",
23 "QUERY_STRING",
24 "REMOTE_ADDR",
25 "REMOTE_HOST",
26 "REMOTE_USER",
27 "REMOTE_IDENT",
28 "REQUEST_METHOD",
29 "SCRIPT_FILENAME",
30 "SCRIPT_NAME",
31 "SCRIPT_URI",
32 "SCRIPT_URL",
33 "SERVER_ADMIN",
34 "SERVER_NAME",
35 "SERVER_PORT",
36 "SERVER_PROTOCOL",
37 "SERVER_SOFTWARE",
38 0
39 };
40 const int nenvok= sizeof(envok)/sizeof(envok[0]);
41
42 int debugmode= 0;
43
44 static void outerror(void) {
45 perror("stdout");
46 exit(debugmode ? 0 : -1);
47 }
48
49 void syserror(const char *m) {
50 if (printf("Content-Type: text/plain\n\n"
51 "ucgi: system call error:\n"
52 "%s: %s\n",
53 m,strerror(errno))==EOF || fflush(stdout)) outerror();
54 exit(0);
55 }
56
57 void error(const char *m) {
58 if (printf("Content-Type: text/plain\n\n"
59 "ucgi: error:\n"
60 "%s\n",
61 m)==EOF || fflush(stdout)) outerror();
62 exit(0);
63 }
64
65 void *xmalloc(size_t sz) {
66 void *r;
67
68 r= malloc(sz);
69 if (!r) syserror("malloc failed");
70 return r;
71 }
72
73 void xsetenv(const char *en, const char *ev, int overwrite) {
74 if (setenv(en,ev,overwrite)) syserror("setenv");
75 }