2 const char miscutil_rcs[] = "$Id: miscutil.c,v 1.11 2001/06/07 23:09:19 jongfoster 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.11 2001/06/07 23:09:19 jongfoster
41 * Cosmetic indentation changes.
43 * Revision 1.10 2001/06/07 14:51:38 joergs
44 * make_path() no longer adds '/' if the dir already ends in '/'.
46 * Revision 1.9 2001/06/07 14:43:17 swa
47 * slight mistake in make_path, unix path style is /.
49 * Revision 1.8 2001/06/05 22:32:01 jongfoster
50 * New function make_path() to splice directory and file names together.
52 * Revision 1.7 2001/06/03 19:12:30 oes
55 * Revision 1.7 2001/06/03 11:03:48 oes
62 * adapted to new enlist_unique arg format
66 * introduced confdir option
68 * filters.c filtrers.h
70 * extracted-CGI relevant stuff
78 * support for new cgi mechansim
82 * functions for new list type: "map"
83 * extended enlist_unique
90 * deleted const struct interceptors
98 * added struct http_response
99 * changes struct interceptors to struct cgi_dispatcher
100 * moved HTML stuff to cgi.h
109 * Revision 1.6 2001/06/01 18:14:49 jongfoster
110 * Changing the calls to strerr() to check HAVE_STRERR (which is defined
111 * in config.h if appropriate) rather than the NO_STRERR macro.
113 * Revision 1.5 2001/06/01 10:31:51 oes
114 * Added character class matching to trivimatch; renamed to simplematch
116 * Revision 1.4 2001/05/31 17:32:31 oes
118 * - Enhanced domain part globbing with infix and prefix asterisk
119 * matching and optional unanchored operation
121 * Revision 1.3 2001/05/29 23:10:09 oes
124 * - Introduced chomp()
125 * - Moved strsav() from showargs to miscutil
127 * Revision 1.2 2001/05/29 09:50:24 jongfoster
128 * Unified blocklist/imagelist/permissionslist.
129 * File format is still under discussion, but the internal changes
132 * Also modified interceptor behaviour:
133 * - We now intercept all URLs beginning with one of the following
134 * prefixes (and *only* these prefixes):
136 * * http://ijbswa.sf.net/config/
137 * * http://ijbswa.sourceforge.net/config/
138 * - New interceptors "home page" - go to http://i.j.b/ to see it.
139 * - Internal changes so that intercepted and fast redirect pages
140 * are not replaced with an image.
141 * - Interceptors now have the option to send a binary page direct
142 * to the client. (i.e. ijb-send-banner uses this)
143 * - Implemented show-url-info interceptor. (Which is why I needed
144 * the above interceptors changes - a typical URL is
145 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
146 * The previous mechanism would not have intercepted that, and
147 * if it had been intercepted then it then it would have replaced
150 * Revision 1.1.1.1 2001/05/15 13:59:00 oes
151 * Initial import of version 2.9.3 source tree
154 *********************************************************************/
166 * FIXME: Only need project.h for BUFFER_SIZE. It would be nice
\r
167 * to remove this dependency.
\r
169 #include "project.h"
\r
170 #include "miscutil.h"
\r
173 const char miscutil_h_rcs[] = MISCUTIL_H_VERSION;
175 /* Fix a problem with Solaris. There should be no effect on other
177 * Solaris's isspace() is a macro which uses it's argument directly
178 * as an array index. Therefore we need to make sure that high-bit
179 * characters generate +ve values, and ideally we also want to make
180 * the argument match the declared parameter type of "int".
182 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
183 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
185 /*********************************************************************
189 * Description : Malloc some memory and set it to '\0'.
190 * The way calloc() ought to be -acjc
193 * 1 : size = Size of memory chunk to return.
195 * Returns : Pointer to newly malloc'd memory chunk.
197 *********************************************************************/
198 void *zalloc(int size)
202 if ((ret = (void *)malloc(size)) != NULL)
204 memset(ret, 0, size);
211 /*********************************************************************
213 * Function : hash_string
215 * Description : Take a string and compute a (hopefuly) unique numeric
216 * integer value. This has several uses, but being able
217 * to "switch" a string the one of my favorites.
220 * 1 : s : string to be hashed.
222 * Returns : an unsigned long variable with the hashed value.
224 *********************************************************************/
225 unsigned long hash_string( const char* s )
227 unsigned long h = 0ul;
240 /*********************************************************************
244 * Description : For some reason (which is beyond me), gcc and WIN32
245 * don't like strdup. When a "free" is executed on a
246 * strdup'd ptr, it can at times freez up! So I just
247 * replaced it and problem was solved.
250 * 1 : s = string to duplicate
252 * Returns : Pointer to newly malloc'ed copy of the string.
254 *********************************************************************/
255 char *strdup( const char *s )
257 char * result = (char *)malloc( strlen(s)+1 );
267 #endif /* def __MINGW32__ */
271 /*********************************************************************
273 * Function : safe_strerror
275 * Description : Variant of the library routine strerror() which will
276 * work on systems without the library routine, and
277 * which should never return NULL.
280 * 1 : err = the `errno' of the last operation.
282 * Returns : An "English" string of the last `errno'. Allocated
283 * with strdup(), so caller frees. May be NULL if the
284 * system is out of memory.
286 *********************************************************************/
287 char *safe_strerror(int err)
290 char buf[BUFFER_SIZE];
295 #endif /* HAVE_STRERROR */
299 sprintf(buf, "(errno = %d)", err);
308 /*********************************************************************
310 * Function : strcmpic
312 * Description : Case insensitive string comparison
315 * 1 : s1 = string 1 to compare
316 * 2 : s2 = string 2 to compare
318 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
320 *********************************************************************/
321 int strcmpic(const char *s1, const char *s2)
325 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
331 return(ijb_tolower(*s1) - ijb_tolower(*s2));
336 /*********************************************************************
338 * Function : strncmpic
340 * Description : Case insensitive string comparison (upto n characters)
343 * 1 : s1 = string 1 to compare
344 * 2 : s2 = string 2 to compare
345 * 3 : n = maximum characters to compare
347 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
349 *********************************************************************/
350 int strncmpic(const char *s1, const char *s2, size_t n)
352 if (n <= 0) return(0);
356 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
365 return(ijb_tolower(*s1) - ijb_tolower(*s2));
370 /*********************************************************************
374 * Description : In-situ-eliminate all leading and trailing whitespace
378 * 1 : s : string to be chomped.
380 * Returns : chomped string
382 *********************************************************************/
383 char *chomp(char *string)
388 * strip trailing whitespace
390 p = string + strlen(string);
391 while (p > string && ijb_isspace(*(p-1)))
398 * find end of leading whitespace
401 while (*q && ijb_isspace(*q))
407 * if there was any, move the rest forwards
421 /*********************************************************************
425 * Description : Reallocate "old" and append text to it. This makes
426 * it easier to append to malloc'd strings.
429 * 1 : old = Old text that is to be extended. Will be
430 * free()d by this routine.
431 * 2 : text_to_append = Text to be appended to old.
433 * Returns : Pointer to newly malloc'ed appended string.
434 * If there is no text to append, return old. Caller
437 *********************************************************************/
438 char *strsav(char *old, const char *text_to_append)
440 int old_len, new_len;
443 if (( text_to_append == NULL) || (*text_to_append == '\0'))
450 old_len = strlen(old);
457 new_len = old_len + strlen(text_to_append) + 1;
461 if ((p = realloc(old, new_len)) == NULL)
463 log_error(LOG_LEVEL_FATAL, "realloc(%d) bytes failed!", new_len);
464 /* Never get here - LOG_LEVEL_FATAL causes program exit */
469 if ((p = (char *)malloc(new_len)) == NULL)
471 log_error(LOG_LEVEL_FATAL, "malloc(%d) bytes failed!", new_len);
472 /* Never get here - LOG_LEVEL_FATAL causes program exit */
476 strcpy(p + old_len, text_to_append);
482 /*********************************************************************
484 * Function : simplematch
486 * Description : String matching, with a (greedy) '*' wildcard that
487 * stands for zero or more arbitrary characters and
488 * character classes in [], which take both enumerations
492 * 1 : pattern = pattern for matching
493 * 2 : text = text to be matched
495 * Returns : 0 if match, else nonzero
497 *********************************************************************/
498 int simplematch(char *pattern, char *text)
507 unsigned char charmap[32];
513 /* EOF pattern but !EOF text? */
519 /* '*' in the pattern? */
523 /* The pattern ends afterwards? Speed up the return. */
529 /* Else, set wildcard mode and remember position after '*' */
534 /* Character range specification? */
537 memset(charmap, '\0', sizeof(charmap));
539 while (*++pat != ']')
545 else if (*pat == '-')
547 if ((*++pat == ']') || *pat == '\0')
551 for(i = lastchar; i <= *pat; i++)
553 charmap[i / 8] |= (1 << (i % 8));
558 charmap[*pat / 8] |= (1 << (*pat % 8));
562 } /* -END- if Character range specification */
565 /* Compare: Char match, or char range match*/
567 || ((*pat == ']') && (charmap[*txt / 8] & (1 << (*txt % 8)))) )
569 /* Sucess, go ahead */
574 /* In wildcard mode, just try again after failiure */
589 /* Cut off extra '*'s */
590 if(*pat == '*') pat++;
592 /* If this is the pattern's end, fine! */
598 /*********************************************************************
602 * Description : Duplicate the first n characters of a string that may
603 * contain '\0' characters.
606 * 1 : string = string to be duplicated
607 * 2 : n = number of bytes to duplicate
609 * Returns : pointer to copy, or NULL if failiure
611 *********************************************************************/
612 char *bindup(const char *string, int n)
616 if (NULL == (dup = (char *)malloc(n)))
622 memcpy(dup, string, n);
630 /*********************************************************************
632 * Function : make_path
634 * Description : Takes a directory name and a file name, returns
635 * the complete path. Handles windows/unix differences.
636 * If the file name is already an absolute path, or if
637 * the directory name is NULL or empty, it returns
641 * 1 : dir: Name of directory or NULL for none.
642 * 2 : file: Name of file. Should not be NULL or empty.
644 * Returns : "dir/file" (Or on windows, "dir\file").
645 * It allocates the string on the heap. Caller frees.
646 * Returns NULL in error (i.e. NULL file or out of
649 *********************************************************************/
650 char * make_path(const char * dir, const char * file)
657 strncpy(path,dir,512);
662 if(AddPart(path,file,512))
668 #else /* ndef AMIGA */
670 if ((file == NULL) || (*file == '\0'))
672 return NULL; /* Error */
675 if ((dir == NULL) || (*dir == '\0') /* No directory specified */
677 || (*file == '\\') || (file[1] == ':') /* Absolute path (DOS) */
678 #else /* ifndef _WIN32 */
679 || (*file == '/') /* Absolute path (U*ix) */
680 #endif /* ifndef _WIN32 */
687 char * path = malloc(strlen(dir) + strlen(file) + 2);
691 #else /* ifndef _WIN32 */
692 if(path[strlen(path)-1] != '/') strcat(path, "/");
693 #endif /* ifndef _WIN32 */
698 #endif /* ndef AMIGA */