chiark / gitweb /
@@ -1,6 +1,7 @@
[userv-utils.git] / www-cgi / ucgicommon.c
1 /*
2  * Copyright (C) 1998-1999 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_USER_AGENT",
41   "PATH_INFO",
42   "PATH_TRANSLATED",
43   "QUERY_STRING",
44   "REMOTE_ADDR",
45   "REMOTE_HOST",
46   "REMOTE_USER",
47   "REMOTE_IDENT",
48   "REQUEST_METHOD",
49   "SCRIPT_FILENAME",
50   "SCRIPT_NAME",
51   "SCRIPT_URI",
52   "SCRIPT_URL",
53   "SERVER_ADMIN",
54   "SERVER_NAME",
55   "SERVER_PORT",
56   "SERVER_PROTOCOL",
57   "SERVER_SOFTWARE",
58   0
59 };
60 const int nenvok= sizeof(envok)/sizeof(envok[0]);
61
62 int debugmode= 0;
63
64 static void outerror(void) {
65   perror("stdout");
66   exit(debugmode ? 0 : -1);
67 }
68
69 void syserror(const char *m) {
70   if (printf("Content-Type: text/plain\n\n"
71              "ucgi: system call error:\n"
72              "%s: %s\n",
73              m,strerror(errno))==EOF || fflush(stdout)) outerror();
74   exit(0);
75 }
76
77 void error(const char *m) {
78   if (printf("Content-Type: text/plain\n\n"
79              "ucgi: error:\n"
80              "%s\n",
81              m)==EOF || fflush(stdout)) outerror();
82   exit(0);
83 }
84
85 void *xmalloc(size_t sz) {
86   void *r;
87
88   r= malloc(sz);
89   if (!r) syserror("malloc failed");
90   return r;
91 }
92
93 void xsetenv(const char *en, const char *ev, int overwrite) {
94   if (setenv(en,ev,overwrite)) syserror("setenv");
95 }