finalise 0.4
[userv-utils] / www-cgi / ucgicommon.c
1 /*
2 * Copyright (C) 1998-1999,2003 Ian Jackson
3 *
4 * This is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with userv-utils; if not, write to the Free Software
16 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * $Id$
19 */
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <errno.h>
24
25 #include "ucgi.h"
26
27 const char *const envok[]= {
28 "CONTENT_LENGTH",
29 "CONTENT_TYPE",
30 "DOCUMENT_ROOT",
31 "GATEWAY_INTERFACE",
32 "HTTP_ACCEPT",
33 "HTTP_ACCEPT_ENCODING",
34 "HTTP_ACCEPT_LANGUAGE",
35 "HTTP_CACHE_CONTROL",
36 "HTTP_COOKIE",
37 "HTTP_HOST",
38 "HTTP_NEGOTIATE",
39 "HTTP_PRAGMA",
40 "HTTP_REFERER",
41 "HTTP_USER_AGENT",
42 "PATH_INFO",
43 "PATH_TRANSLATED",
44 "QUERY_STRING",
45 "REMOTE_ADDR",
46 "REMOTE_HOST",
47 "REMOTE_USER",
48 "REMOTE_IDENT",
49 "REQUEST_METHOD",
50 "SCRIPT_FILENAME",
51 "SCRIPT_NAME",
52 "SCRIPT_URI",
53 "SCRIPT_URL",
54 "SERVER_ADMIN",
55 "SERVER_NAME",
56 "SERVER_PORT",
57 "SERVER_PROTOCOL",
58 "SERVER_SOFTWARE",
59 0
60 };
61 const int nenvok= sizeof(envok)/sizeof(envok[0]);
62
63 int debugmode= 0;
64
65 static void outerror(void) {
66 perror("stdout");
67 exit(debugmode ? 0 : -1);
68 }
69
70 void syserror(const char *m) {
71 if (printf("Content-Type: text/plain\n\n"
72 "ucgi: system call error:\n"
73 "%s: %s\n",
74 m,strerror(errno))==EOF || fflush(stdout)) outerror();
75 exit(0);
76 }
77
78 void error(const char *m) {
79 if (printf("Content-Type: text/plain\n\n"
80 "ucgi: error:\n"
81 "%s\n",
82 m)==EOF || fflush(stdout)) outerror();
83 exit(0);
84 }
85
86 void *xmalloc(size_t sz) {
87 void *r;
88
89 r= malloc(sz);
90 if (!r) syserror("malloc failed");
91 return r;
92 }
93
94 void xsetenv(const char *en, const char *ev, int overwrite) {
95 if (setenv(en,ev,overwrite)) syserror("setenv");
96 }