www-cgi/: Move `xrealloc' to `ucgicommon'.
[userv-utils] / www-cgi / ucgicommon.c
CommitLineData
a33962ba 1/*
711a0748 2 * Copyright (C) 1998-1999,2003 Ian Jackson
a33962ba 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 */
6a580c17 20
21#include <stdio.h>
22#include <string.h>
6aaa2ce3 23#include <errno.h>
6a580c17 24
25#include "ucgi.h"
26
27const char *const envok[]= {
957b3b21 28 "AUTH_TYPE",
6a580c17 29 "CONTENT_LENGTH",
30 "CONTENT_TYPE",
31 "DOCUMENT_ROOT",
32 "GATEWAY_INTERFACE",
33 "HTTP_ACCEPT",
957b3b21 34 "HTTP_ACCEPT_CHARSET",
6a580c17 35 "HTTP_ACCEPT_ENCODING",
36 "HTTP_ACCEPT_LANGUAGE",
37 "HTTP_CACHE_CONTROL",
957b3b21
IJ
38 "HTTP_CONNECTION",
39 "HTTP_CONTENT_ENCODING",
05aecda5 40 "HTTP_COOKIE",
957b3b21 41 "HTTP_DNT",
6a580c17 42 "HTTP_HOST",
957b3b21 43 "HTTP_KEEP_ALIVE",
6a580c17 44 "HTTP_NEGOTIATE",
45 "HTTP_PRAGMA",
50689b02 46 "HTTP_REFERER",
6a580c17 47 "HTTP_USER_AGENT",
957b3b21
IJ
48 "HTTP_VIA",
49 "HTTP_X_FORWARDED_FOR",
50 "HTTPS",
6a580c17 51 "PATH_INFO",
52 "PATH_TRANSLATED",
53 "QUERY_STRING",
54 "REMOTE_ADDR",
55 "REMOTE_HOST",
56 "REMOTE_USER",
57 "REMOTE_IDENT",
58 "REQUEST_METHOD",
957b3b21 59 "REQUEST_URI",
6a580c17 60 "SCRIPT_FILENAME",
61 "SCRIPT_NAME",
62 "SCRIPT_URI",
63 "SCRIPT_URL",
957b3b21 64 "SERVER_ADDR",
6a580c17 65 "SERVER_ADMIN",
66 "SERVER_NAME",
67 "SERVER_PORT",
68 "SERVER_PROTOCOL",
957b3b21 69 "SERVER_SIGNATURE",
6a580c17 70 "SERVER_SOFTWARE",
71 0
72};
73const int nenvok= sizeof(envok)/sizeof(envok[0]);
74
75int debugmode= 0;
76
77static void outerror(void) {
78 perror("stdout");
79 exit(debugmode ? 0 : -1);
80}
81
82void syserror(const char *m) {
83 if (printf("Content-Type: text/plain\n\n"
84 "ucgi: system call error:\n"
85 "%s: %s\n",
86 m,strerror(errno))==EOF || fflush(stdout)) outerror();
87 exit(0);
88}
89
90void error(const char *m) {
91 if (printf("Content-Type: text/plain\n\n"
92 "ucgi: error:\n"
93 "%s\n",
94 m)==EOF || fflush(stdout)) outerror();
95 exit(0);
96}
97
98void *xmalloc(size_t sz) {
99 void *r;
100
101 r= malloc(sz);
102 if (!r) syserror("malloc failed");
103 return r;
104}
105
44a77f48
MW
106void *xrealloc(void *ptr, size_t sz) {
107 void *r;
108
109 r= realloc(ptr,sz);
110 if (!r) syserror("realloc failed");
111 return r;
112}
113
6a580c17 114void xsetenv(const char *en, const char *ev, int overwrite) {
115 if (setenv(en,ev,overwrite)) syserror("setenv");
116}