2 * Copyright (C) 1998-1999,2003 Ian Jackson
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.
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.
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.
31 static void outerror(void) {
33 exit(debugmode ? 0 : -1);
36 void syserror(const char *m) {
37 if (printf("Content-Type: text/plain\n\n"
38 "ucgi: system call error:\n"
40 m,strerror(errno))==EOF || fflush(stdout)) outerror();
44 void error(const char *m) {
45 if (printf("Content-Type: text/plain\n\n"
48 m)==EOF || fflush(stdout)) outerror();
52 void *xmalloc(size_t sz) {
56 if (!r) syserror("malloc failed");
60 void *xrealloc(void *ptr, size_t sz) {
64 if (!r) syserror("realloc failed");
68 void xsetenv(const char *en, const char *ev, int overwrite) {
69 if (setenv(en,ev,overwrite)) syserror("setenv");
72 void filter_environment(unsigned flags, const char *prefix_in,
73 const char *const *patv,
74 void (*foundone)(const char *fulln,
75 const char *en, const char *ev,
80 const char *const *patp;
81 const char *en, *ev, *pat, *q;
82 char enbuf[MAX_ENVVAR_NAME];
83 size_t n, pn = strlen(prefix_in);
86 D( if (debugmode) printf(";; filter_environment...\n"); )
87 for (ep= environ; (en= *ep); ep++) {
88 D( if (debugmode) printf(";; consider env-var `%s'\n", en); )
89 if (strncmp(en, prefix_in, pn) != 0 || !en[pn]) {
90 D( if (debugmode) printf(";; doesn't match prefix\n"); )
93 for (patp= patv; (pat= *patp); patp++) {
96 if (*pat == '!' && (flags & FILTF_WILDCARD)) {
103 printf(";; mismatch `%s' (prefix)\n", *patp); )
106 D( if (debugmode) printf(";; matched `%s'\n", *patp); )
109 } else if (*pat == '*' && (flags & FILTF_WILDCARD)) {
113 printf(";; mismatch `%s' (discard: no `=')\n", *patp); )
117 printf(";; wildcard match for `%s'\n", *patp); )
121 if (*pat++ != *q++) {
122 D( if (debugmode) printf(";; mismatch `%s'\n", *patp); )
128 if (n >= sizeof(enbuf))
129 error("environment variable name too long");
130 memcpy(enbuf, en, n);
133 printf(";; full = `%s'; tail = `%s'; value = `%s'\n",
134 enbuf, enbuf + pn, ev); )
135 foundone(enbuf, enbuf + pn, ev, p);
136 } D( else if (debugmode)
137 printf(";; matched negated pattern\n"); )