1 const char miscutil_rcs[] = "$Id: miscutil.c,v 1.7 2001/06/03 19:12:30 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/miscutil.c,v $
6 * Purpose : zalloc, hash_string, safe_strerror, strcmpic,
7 * strncmpic, strsav, chomp, and MinGW32 strdup
9 * These are each too small to deserve their own file
10 * but don't really fit in any other file.
12 * Copyright : Written by and Copyright (C) 2001 the SourceForge
13 * IJBSWA team. http://ijbswa.sourceforge.net
15 * Based on the Internet Junkbuster originally written
16 * by and Copyright (C) 1997 Anonymous Coders and
17 * Junkbusters Corporation. http://www.junkbusters.com
19 * This program is free software; you can redistribute it
20 * and/or modify it under the terms of the GNU General
21 * Public License as published by the Free Software
22 * Foundation; either version 2 of the License, or (at
23 * your option) any later version.
25 * This program is distributed in the hope that it will
26 * be useful, but WITHOUT ANY WARRANTY; without even the
27 * implied warranty of MERCHANTABILITY or FITNESS FOR A
28 * PARTICULAR PURPOSE. See the GNU General Public
29 * License for more details.
31 * The GNU General Public License should be included with
32 * this file. If not, you can view it at
33 * http://www.gnu.org/copyleft/gpl.html
34 * or write to the Free Software Foundation, Inc., 59
35 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38 * $Log: miscutil.c,v $
39 * Revision 1.7 2001/06/03 19:12:30 oes
42 * Revision 1.7 2001/06/03 11:03:48 oes
49 * adapted to new enlist_unique arg format
53 * introduced confdir option
55 * filters.c filtrers.h
57 * extracted-CGI relevant stuff
65 * support for new cgi mechansim
69 * functions for new list type: "map"
70 * extended enlist_unique
77 * deleted const struct interceptors
85 * added struct http_response
86 * changes struct interceptors to struct cgi_dispatcher
87 * moved HTML stuff to cgi.h
96 * Revision 1.6 2001/06/01 18:14:49 jongfoster
97 * Changing the calls to strerr() to check HAVE_STRERR (which is defined
98 * in config.h if appropriate) rather than the NO_STRERR macro.
100 * Revision 1.5 2001/06/01 10:31:51 oes
101 * Added character class matching to trivimatch; renamed to simplematch
103 * Revision 1.4 2001/05/31 17:32:31 oes
105 * - Enhanced domain part globbing with infix and prefix asterisk
106 * matching and optional unanchored operation
108 * Revision 1.3 2001/05/29 23:10:09 oes
111 * - Introduced chomp()
112 * - Moved strsav() from showargs to miscutil
114 * Revision 1.2 2001/05/29 09:50:24 jongfoster
115 * Unified blocklist/imagelist/permissionslist.
116 * File format is still under discussion, but the internal changes
119 * Also modified interceptor behaviour:
120 * - We now intercept all URLs beginning with one of the following
121 * prefixes (and *only* these prefixes):
123 * * http://ijbswa.sf.net/config/
124 * * http://ijbswa.sourceforge.net/config/
125 * - New interceptors "home page" - go to http://i.j.b/ to see it.
126 * - Internal changes so that intercepted and fast redirect pages
127 * are not replaced with an image.
128 * - Interceptors now have the option to send a binary page direct
129 * to the client. (i.e. ijb-send-banner uses this)
130 * - Implemented show-url-info interceptor. (Which is why I needed
131 * the above interceptors changes - a typical URL is
132 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
133 * The previous mechanism would not have intercepted that, and
134 * if it had been intercepted then it then it would have replaced
137 * Revision 1.1.1.1 2001/05/15 13:59:00 oes
138 * Initial import of version 2.9.3 source tree
141 *********************************************************************/
152 #include "miscutil.h"
155 const char miscutil_h_rcs[] = MISCUTIL_H_VERSION;
157 /* Fix a problem with Solaris. There should be no effect on other
159 * Solaris's isspace() is a macro which uses it's argument directly
160 * as an array index. Therefore we need to make sure that high-bit
161 * characters generate +ve values, and ideally we also want to make
162 * the argument match the declared parameter type of "int".
164 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
165 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
167 /*********************************************************************
171 * Description : Malloc some memory and set it to '\0'.
172 * The way calloc() ought to be -acjc
175 * 1 : size = Size of memory chunk to return.
177 * Returns : Pointer to newly malloc'd memory chunk.
179 *********************************************************************/
180 void *zalloc(int size)
184 if ((ret = (void *)malloc(size)) != NULL)
186 memset(ret, 0, size);
193 /*********************************************************************
195 * Function : hash_string
197 * Description : Take a string and compute a (hopefuly) unique numeric
198 * integer value. This has several uses, but being able
199 * to "switch" a string the one of my favorites.
202 * 1 : s : string to be hashed.
204 * Returns : an unsigned long variable with the hashed value.
206 *********************************************************************/
207 unsigned long hash_string( const char* s )
209 unsigned long h = 0ul;
222 /*********************************************************************
226 * Description : For some reason (which is beyond me), gcc and WIN32
227 * don't like strdup. When a "free" is executed on a
228 * strdup'd ptr, it can at times freez up! So I just
229 * replaced it and problem was solved.
232 * 1 : s = string to duplicate
234 * Returns : Pointer to newly malloc'ed copy of the string.
236 *********************************************************************/
237 char *strdup( const char *s )
239 char * result = (char *)malloc( strlen(s)+1 );
249 #endif /* def __MINGW32__ */
253 /*********************************************************************
255 * Function : safe_strerror
257 * Description : Variant of the library routine strerror() which will
258 * work on systems without the library routine, and
259 * which should never return NULL.
262 * 1 : err = the `errno' of the last operation.
264 * Returns : An "English" string of the last `errno'. Allocated
265 * with strdup(), so caller frees. May be NULL if the
266 * system is out of memory.
268 *********************************************************************/
269 char *safe_strerror(int err)
277 #endif /* HAVE_STRERROR */
281 sprintf(buf, "(errno = %d)", err);
290 /*********************************************************************
292 * Function : strcmpic
294 * Description : Case insensitive string comparison
297 * 1 : s1 = string 1 to compare
298 * 2 : s2 = string 2 to compare
300 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
302 *********************************************************************/
303 int strcmpic(const char *s1, const char *s2)
307 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
313 return(ijb_tolower(*s1) - ijb_tolower(*s2));
318 /*********************************************************************
320 * Function : strncmpic
322 * Description : Case insensitive string comparison (upto n characters)
325 * 1 : s1 = string 1 to compare
326 * 2 : s2 = string 2 to compare
327 * 3 : n = maximum characters to compare
329 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
331 *********************************************************************/
332 int strncmpic(const char *s1, const char *s2, size_t n)
334 if (n <= 0) return(0);
338 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
347 return(ijb_tolower(*s1) - ijb_tolower(*s2));
352 /*********************************************************************
356 * Description : In-situ-eliminate all leading and trailing whitespace
360 * 1 : s : string to be chomped.
362 * Returns : chomped string
364 *********************************************************************/
365 char *chomp(char *string)
370 * strip trailing whitespace
372 p = string + strlen(string);
373 while (p > string && ijb_isspace(*(p-1)))
380 * find end of leading whitespace
383 while (*q && ijb_isspace(*q))
389 * if there was any, move the rest forwards
403 /*********************************************************************
407 * Description : Reallocate "old" and append text to it. This makes
408 * it easier to append to malloc'd strings.
411 * 1 : old = Old text that is to be extended. Will be
412 * free()d by this routine.
413 * 2 : text_to_append = Text to be appended to old.
415 * Returns : Pointer to newly malloc'ed appended string.
416 * If there is no text to append, return old. Caller
419 *********************************************************************/
420 char *strsav(char *old, const char *text_to_append)
422 int old_len, new_len;
425 if (( text_to_append == NULL) || (*text_to_append == '\0'))
432 old_len = strlen(old);
439 new_len = old_len + strlen(text_to_append) + 1;
443 if ((p = realloc(old, new_len)) == NULL)
445 log_error(LOG_LEVEL_FATAL, "realloc(%d) bytes failed!", new_len);
446 /* Never get here - LOG_LEVEL_FATAL causes program exit */
451 if ((p = (char *)malloc(new_len)) == NULL)
453 log_error(LOG_LEVEL_FATAL, "malloc(%d) bytes failed!", new_len);
454 /* Never get here - LOG_LEVEL_FATAL causes program exit */
458 strcpy(p + old_len, text_to_append);
464 /*********************************************************************
466 * Function : simplematch
468 * Description : String matching, with a (greedy) '*' wildcard that
469 * stands for zero or more arbitrary characters and
470 * character classes in [], which take both enumerations
474 * 1 : pattern = pattern for matching
475 * 2 : text = text to be matched
477 * Returns : 0 if match, else nonzero
479 *********************************************************************/
480 int simplematch(char *pattern, char *text)
489 unsigned char charmap[32];
495 /* EOF pattern but !EOF text? */
501 /* '*' in the pattern? */
505 /* The pattern ends afterwards? Speed up the return. */
511 /* Else, set wildcard mode and remember position after '*' */
516 /* Character range specification? */
519 memset(charmap, '\0', sizeof(charmap));
521 while (*++pat != ']')
527 else if (*pat == '-')
529 if ((*++pat == ']') || *pat == '\0')
533 for(i = lastchar; i <= *pat; i++)
535 charmap[i / 8] |= (1 << (i % 8));
540 charmap[*pat / 8] |= (1 << (*pat % 8));
544 } /* -END- if Character range specification */
547 /* Compare: Char match, or char range match*/
549 || ((*pat == ']') && (charmap[*txt / 8] & (1 << (*txt % 8)))) )
551 /* Sucess, go ahead */
556 /* In wildcard mode, just try again after failiure */
571 /* Cut off extra '*'s */
572 if(*pat == '*') pat++;
574 /* If this is the pattern's end, fine! */
580 /*********************************************************************
584 * Description : Duplicate the first n characters of a string that may
585 * contain '\0' characters.
588 * 1 : string = string to be duplicated
589 * 2 : n = number of bytes to duplicate
591 * Returns : pointer to copy, or NULL if failiure
593 *********************************************************************/
594 char *bindup(const char *string, int n)
598 if (NULL == (dup = (char *)malloc(n)))
604 memcpy(dup, string, n);
612 /*********************************************************************
614 * Function : make_path
616 * Description : Takes a directory name and a file name, returns
617 * the complete path. Handles windows/unix differences.
618 * If the file name is already an absolute path, or if
619 * the directory name is NULL or empty, it returns
623 * 1 : dir: Name of directory or NULL for none.
624 * 2 : file: Name of file. Should not be NULL or empty.
626 * Returns : "dir/file" (Or on windows, "dir\file").
627 * It allocates the string on the heap. Caller frees.
628 * Returns NULL in error (i.e. NULL file or out of
631 *********************************************************************/
632 char * make_path(const char * dir, const char * file)
634 if ((file == NULL) || (*file == '\0'))
636 return NULL; /* Error */
639 if ((dir == NULL) || (*dir == '\0') /* No directory specified */
641 || (*file == '\\') || (file[1] == ':') /* Absolute path (DOS) */
642 #else /* ifndef _WIN32 */
643 || (*file == '/') /* Absolute path (U*ix) */
644 #endif /* ifndef _WIN32 */
651 char * path = malloc(strlen(dir) + strlen(file) + 2);
655 #else /* ifndef _WIN32 */
657 #endif /* ifndef _WIN32 */