2 const char miscutil_rcs[] = "$Id: miscutil.c,v 1.9 2001/06/07 14:43:17 swa Exp $";
3 /*********************************************************************
5 * File : $Source: /cvsroot/ijbswa/current/miscutil.c,v $
7 * Purpose : zalloc, hash_string, safe_strerror, strcmpic,
8 * strncmpic, strsav, chomp, and MinGW32 strdup
10 * These are each too small to deserve their own file
11 * but don't really fit in any other file.
13 * Copyright : Written by and Copyright (C) 2001 the SourceForge
14 * IJBSWA team. http://ijbswa.sourceforge.net
16 * Based on the Internet Junkbuster originally written
17 * by and Copyright (C) 1997 Anonymous Coders and
18 * Junkbusters Corporation. http://www.junkbusters.com
20 * This program is free software; you can redistribute it
21 * and/or modify it under the terms of the GNU General
22 * Public License as published by the Free Software
23 * Foundation; either version 2 of the License, or (at
24 * your option) any later version.
26 * This program is distributed in the hope that it will
27 * be useful, but WITHOUT ANY WARRANTY; without even the
28 * implied warranty of MERCHANTABILITY or FITNESS FOR A
29 * PARTICULAR PURPOSE. See the GNU General Public
30 * License for more details.
32 * The GNU General Public License should be included with
33 * this file. If not, you can view it at
34 * http://www.gnu.org/copyleft/gpl.html
35 * or write to the Free Software Foundation, Inc., 59
36 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
39 * $Log: miscutil.c,v $
40 * Revision 1.9 2001/06/07 14:43:17 swa
41 * slight mistake in make_path, unix path style is /.
43 * Revision 1.8 2001/06/05 22:32:01 jongfoster
44 * New function make_path() to splice directory and file names together.
46 * Revision 1.7 2001/06/03 19:12:30 oes
49 * Revision 1.7 2001/06/03 11:03:48 oes
56 * adapted to new enlist_unique arg format
60 * introduced confdir option
62 * filters.c filtrers.h
64 * extracted-CGI relevant stuff
72 * support for new cgi mechansim
76 * functions for new list type: "map"
77 * extended enlist_unique
84 * deleted const struct interceptors
92 * added struct http_response
93 * changes struct interceptors to struct cgi_dispatcher
94 * moved HTML stuff to cgi.h
103 * Revision 1.6 2001/06/01 18:14:49 jongfoster
104 * Changing the calls to strerr() to check HAVE_STRERR (which is defined
105 * in config.h if appropriate) rather than the NO_STRERR macro.
107 * Revision 1.5 2001/06/01 10:31:51 oes
108 * Added character class matching to trivimatch; renamed to simplematch
110 * Revision 1.4 2001/05/31 17:32:31 oes
112 * - Enhanced domain part globbing with infix and prefix asterisk
113 * matching and optional unanchored operation
115 * Revision 1.3 2001/05/29 23:10:09 oes
118 * - Introduced chomp()
119 * - Moved strsav() from showargs to miscutil
121 * Revision 1.2 2001/05/29 09:50:24 jongfoster
122 * Unified blocklist/imagelist/permissionslist.
123 * File format is still under discussion, but the internal changes
126 * Also modified interceptor behaviour:
127 * - We now intercept all URLs beginning with one of the following
128 * prefixes (and *only* these prefixes):
130 * * http://ijbswa.sf.net/config/
131 * * http://ijbswa.sourceforge.net/config/
132 * - New interceptors "home page" - go to http://i.j.b/ to see it.
133 * - Internal changes so that intercepted and fast redirect pages
134 * are not replaced with an image.
135 * - Interceptors now have the option to send a binary page direct
136 * to the client. (i.e. ijb-send-banner uses this)
137 * - Implemented show-url-info interceptor. (Which is why I needed
138 * the above interceptors changes - a typical URL is
139 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
140 * The previous mechanism would not have intercepted that, and
141 * if it had been intercepted then it then it would have replaced
144 * Revision 1.1.1.1 2001/05/15 13:59:00 oes
145 * Initial import of version 2.9.3 source tree
148 *********************************************************************/
159 #include "miscutil.h"
162 const char miscutil_h_rcs[] = MISCUTIL_H_VERSION;
164 /* Fix a problem with Solaris. There should be no effect on other
166 * Solaris's isspace() is a macro which uses it's argument directly
167 * as an array index. Therefore we need to make sure that high-bit
168 * characters generate +ve values, and ideally we also want to make
169 * the argument match the declared parameter type of "int".
171 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
172 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
174 /*********************************************************************
178 * Description : Malloc some memory and set it to '\0'.
179 * The way calloc() ought to be -acjc
182 * 1 : size = Size of memory chunk to return.
184 * Returns : Pointer to newly malloc'd memory chunk.
186 *********************************************************************/
187 void *zalloc(int size)
191 if ((ret = (void *)malloc(size)) != NULL)
193 memset(ret, 0, size);
200 /*********************************************************************
202 * Function : hash_string
204 * Description : Take a string and compute a (hopefuly) unique numeric
205 * integer value. This has several uses, but being able
206 * to "switch" a string the one of my favorites.
209 * 1 : s : string to be hashed.
211 * Returns : an unsigned long variable with the hashed value.
213 *********************************************************************/
214 unsigned long hash_string( const char* s )
216 unsigned long h = 0ul;
229 /*********************************************************************
233 * Description : For some reason (which is beyond me), gcc and WIN32
234 * don't like strdup. When a "free" is executed on a
235 * strdup'd ptr, it can at times freez up! So I just
236 * replaced it and problem was solved.
239 * 1 : s = string to duplicate
241 * Returns : Pointer to newly malloc'ed copy of the string.
243 *********************************************************************/
244 char *strdup( const char *s )
246 char * result = (char *)malloc( strlen(s)+1 );
256 #endif /* def __MINGW32__ */
260 /*********************************************************************
262 * Function : safe_strerror
264 * Description : Variant of the library routine strerror() which will
265 * work on systems without the library routine, and
266 * which should never return NULL.
269 * 1 : err = the `errno' of the last operation.
271 * Returns : An "English" string of the last `errno'. Allocated
272 * with strdup(), so caller frees. May be NULL if the
273 * system is out of memory.
275 *********************************************************************/
276 char *safe_strerror(int err)
284 #endif /* HAVE_STRERROR */
288 sprintf(buf, "(errno = %d)", err);
297 /*********************************************************************
299 * Function : strcmpic
301 * Description : Case insensitive string comparison
304 * 1 : s1 = string 1 to compare
305 * 2 : s2 = string 2 to compare
307 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
309 *********************************************************************/
310 int strcmpic(const char *s1, const char *s2)
314 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
320 return(ijb_tolower(*s1) - ijb_tolower(*s2));
325 /*********************************************************************
327 * Function : strncmpic
329 * Description : Case insensitive string comparison (upto n characters)
332 * 1 : s1 = string 1 to compare
333 * 2 : s2 = string 2 to compare
334 * 3 : n = maximum characters to compare
336 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
338 *********************************************************************/
339 int strncmpic(const char *s1, const char *s2, size_t n)
341 if (n <= 0) return(0);
345 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
354 return(ijb_tolower(*s1) - ijb_tolower(*s2));
359 /*********************************************************************
363 * Description : In-situ-eliminate all leading and trailing whitespace
367 * 1 : s : string to be chomped.
369 * Returns : chomped string
371 *********************************************************************/
372 char *chomp(char *string)
377 * strip trailing whitespace
379 p = string + strlen(string);
380 while (p > string && ijb_isspace(*(p-1)))
387 * find end of leading whitespace
390 while (*q && ijb_isspace(*q))
396 * if there was any, move the rest forwards
410 /*********************************************************************
414 * Description : Reallocate "old" and append text to it. This makes
415 * it easier to append to malloc'd strings.
418 * 1 : old = Old text that is to be extended. Will be
419 * free()d by this routine.
420 * 2 : text_to_append = Text to be appended to old.
422 * Returns : Pointer to newly malloc'ed appended string.
423 * If there is no text to append, return old. Caller
426 *********************************************************************/
427 char *strsav(char *old, const char *text_to_append)
429 int old_len, new_len;
432 if (( text_to_append == NULL) || (*text_to_append == '\0'))
439 old_len = strlen(old);
446 new_len = old_len + strlen(text_to_append) + 1;
450 if ((p = realloc(old, new_len)) == NULL)
452 log_error(LOG_LEVEL_FATAL, "realloc(%d) bytes failed!", new_len);
453 /* Never get here - LOG_LEVEL_FATAL causes program exit */
458 if ((p = (char *)malloc(new_len)) == NULL)
460 log_error(LOG_LEVEL_FATAL, "malloc(%d) bytes failed!", new_len);
461 /* Never get here - LOG_LEVEL_FATAL causes program exit */
465 strcpy(p + old_len, text_to_append);
471 /*********************************************************************
473 * Function : simplematch
475 * Description : String matching, with a (greedy) '*' wildcard that
476 * stands for zero or more arbitrary characters and
477 * character classes in [], which take both enumerations
481 * 1 : pattern = pattern for matching
482 * 2 : text = text to be matched
484 * Returns : 0 if match, else nonzero
486 *********************************************************************/
487 int simplematch(char *pattern, char *text)
496 unsigned char charmap[32];
502 /* EOF pattern but !EOF text? */
508 /* '*' in the pattern? */
512 /* The pattern ends afterwards? Speed up the return. */
518 /* Else, set wildcard mode and remember position after '*' */
523 /* Character range specification? */
526 memset(charmap, '\0', sizeof(charmap));
528 while (*++pat != ']')
534 else if (*pat == '-')
536 if ((*++pat == ']') || *pat == '\0')
540 for(i = lastchar; i <= *pat; i++)
542 charmap[i / 8] |= (1 << (i % 8));
547 charmap[*pat / 8] |= (1 << (*pat % 8));
551 } /* -END- if Character range specification */
554 /* Compare: Char match, or char range match*/
556 || ((*pat == ']') && (charmap[*txt / 8] & (1 << (*txt % 8)))) )
558 /* Sucess, go ahead */
563 /* In wildcard mode, just try again after failiure */
578 /* Cut off extra '*'s */
579 if(*pat == '*') pat++;
581 /* If this is the pattern's end, fine! */
587 /*********************************************************************
591 * Description : Duplicate the first n characters of a string that may
592 * contain '\0' characters.
595 * 1 : string = string to be duplicated
596 * 2 : n = number of bytes to duplicate
598 * Returns : pointer to copy, or NULL if failiure
600 *********************************************************************/
601 char *bindup(const char *string, int n)
605 if (NULL == (dup = (char *)malloc(n)))
611 memcpy(dup, string, n);
619 /*********************************************************************
621 * Function : make_path
623 * Description : Takes a directory name and a file name, returns
624 * the complete path. Handles windows/unix differences.
625 * If the file name is already an absolute path, or if
626 * the directory name is NULL or empty, it returns
630 * 1 : dir: Name of directory or NULL for none.
631 * 2 : file: Name of file. Should not be NULL or empty.
633 * Returns : "dir/file" (Or on windows, "dir\file").
634 * It allocates the string on the heap. Caller frees.
635 * Returns NULL in error (i.e. NULL file or out of
638 *********************************************************************/
639 char * make_path(const char * dir, const char * file)
646 strncpy(path,dir,512);
651 if(AddPart(path,file,512))
657 #else /* ndef AMIGA */
659 if ((file == NULL) || (*file == '\0'))
661 return NULL; /* Error */
664 if ((dir == NULL) || (*dir == '\0') /* No directory specified */
666 || (*file == '\\') || (file[1] == ':') /* Absolute path (DOS) */
667 #else /* ifndef _WIN32 */
668 || (*file == '/') /* Absolute path (U*ix) */
669 #endif /* ifndef _WIN32 */
676 char * path = malloc(strlen(dir) + strlen(file) + 2);
680 #else /* ifndef _WIN32 */
681 if(path[strlen(path)-1] != '/') strcat(path, "/");
682 #endif /* ifndef _WIN32 */
687 #endif /* ndef AMIGA */