2 const char miscutil_rcs[] = "$Id: miscutil.c,v 1.10 2001/06/07 14:51:38 joergs 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.10 2001/06/07 14:51:38 joergs
41 * make_path() no longer adds '/' if the dir already ends in '/'.
43 * Revision 1.9 2001/06/07 14:43:17 swa
44 * slight mistake in make_path, unix path style is /.
46 * Revision 1.8 2001/06/05 22:32:01 jongfoster
47 * New function make_path() to splice directory and file names together.
49 * Revision 1.7 2001/06/03 19:12:30 oes
52 * Revision 1.7 2001/06/03 11:03:48 oes
59 * adapted to new enlist_unique arg format
63 * introduced confdir option
65 * filters.c filtrers.h
67 * extracted-CGI relevant stuff
75 * support for new cgi mechansim
79 * functions for new list type: "map"
80 * extended enlist_unique
87 * deleted const struct interceptors
95 * added struct http_response
96 * changes struct interceptors to struct cgi_dispatcher
97 * moved HTML stuff to cgi.h
106 * Revision 1.6 2001/06/01 18:14:49 jongfoster
107 * Changing the calls to strerr() to check HAVE_STRERR (which is defined
108 * in config.h if appropriate) rather than the NO_STRERR macro.
110 * Revision 1.5 2001/06/01 10:31:51 oes
111 * Added character class matching to trivimatch; renamed to simplematch
113 * Revision 1.4 2001/05/31 17:32:31 oes
115 * - Enhanced domain part globbing with infix and prefix asterisk
116 * matching and optional unanchored operation
118 * Revision 1.3 2001/05/29 23:10:09 oes
121 * - Introduced chomp()
122 * - Moved strsav() from showargs to miscutil
124 * Revision 1.2 2001/05/29 09:50:24 jongfoster
125 * Unified blocklist/imagelist/permissionslist.
126 * File format is still under discussion, but the internal changes
129 * Also modified interceptor behaviour:
130 * - We now intercept all URLs beginning with one of the following
131 * prefixes (and *only* these prefixes):
133 * * http://ijbswa.sf.net/config/
134 * * http://ijbswa.sourceforge.net/config/
135 * - New interceptors "home page" - go to http://i.j.b/ to see it.
136 * - Internal changes so that intercepted and fast redirect pages
137 * are not replaced with an image.
138 * - Interceptors now have the option to send a binary page direct
139 * to the client. (i.e. ijb-send-banner uses this)
140 * - Implemented show-url-info interceptor. (Which is why I needed
141 * the above interceptors changes - a typical URL is
142 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
143 * The previous mechanism would not have intercepted that, and
144 * if it had been intercepted then it then it would have replaced
147 * Revision 1.1.1.1 2001/05/15 13:59:00 oes
148 * Initial import of version 2.9.3 source tree
151 *********************************************************************/
162 #include "miscutil.h"
165 const char miscutil_h_rcs[] = MISCUTIL_H_VERSION;
167 /* Fix a problem with Solaris. There should be no effect on other
169 * Solaris's isspace() is a macro which uses it's argument directly
170 * as an array index. Therefore we need to make sure that high-bit
171 * characters generate +ve values, and ideally we also want to make
172 * the argument match the declared parameter type of "int".
174 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
175 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
177 /*********************************************************************
181 * Description : Malloc some memory and set it to '\0'.
182 * The way calloc() ought to be -acjc
185 * 1 : size = Size of memory chunk to return.
187 * Returns : Pointer to newly malloc'd memory chunk.
189 *********************************************************************/
190 void *zalloc(int size)
194 if ((ret = (void *)malloc(size)) != NULL)
196 memset(ret, 0, size);
203 /*********************************************************************
205 * Function : hash_string
207 * Description : Take a string and compute a (hopefuly) unique numeric
208 * integer value. This has several uses, but being able
209 * to "switch" a string the one of my favorites.
212 * 1 : s : string to be hashed.
214 * Returns : an unsigned long variable with the hashed value.
216 *********************************************************************/
217 unsigned long hash_string( const char* s )
219 unsigned long h = 0ul;
232 /*********************************************************************
236 * Description : For some reason (which is beyond me), gcc and WIN32
237 * don't like strdup. When a "free" is executed on a
238 * strdup'd ptr, it can at times freez up! So I just
239 * replaced it and problem was solved.
242 * 1 : s = string to duplicate
244 * Returns : Pointer to newly malloc'ed copy of the string.
246 *********************************************************************/
247 char *strdup( const char *s )
249 char * result = (char *)malloc( strlen(s)+1 );
259 #endif /* def __MINGW32__ */
263 /*********************************************************************
265 * Function : safe_strerror
267 * Description : Variant of the library routine strerror() which will
268 * work on systems without the library routine, and
269 * which should never return NULL.
272 * 1 : err = the `errno' of the last operation.
274 * Returns : An "English" string of the last `errno'. Allocated
275 * with strdup(), so caller frees. May be NULL if the
276 * system is out of memory.
278 *********************************************************************/
279 char *safe_strerror(int err)
287 #endif /* HAVE_STRERROR */
291 sprintf(buf, "(errno = %d)", err);
300 /*********************************************************************
302 * Function : strcmpic
304 * Description : Case insensitive string comparison
307 * 1 : s1 = string 1 to compare
308 * 2 : s2 = string 2 to compare
310 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
312 *********************************************************************/
313 int strcmpic(const char *s1, const char *s2)
317 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
323 return(ijb_tolower(*s1) - ijb_tolower(*s2));
328 /*********************************************************************
330 * Function : strncmpic
332 * Description : Case insensitive string comparison (upto n characters)
335 * 1 : s1 = string 1 to compare
336 * 2 : s2 = string 2 to compare
337 * 3 : n = maximum characters to compare
339 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
341 *********************************************************************/
342 int strncmpic(const char *s1, const char *s2, size_t n)
344 if (n <= 0) return(0);
348 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
357 return(ijb_tolower(*s1) - ijb_tolower(*s2));
362 /*********************************************************************
366 * Description : In-situ-eliminate all leading and trailing whitespace
370 * 1 : s : string to be chomped.
372 * Returns : chomped string
374 *********************************************************************/
375 char *chomp(char *string)
380 * strip trailing whitespace
382 p = string + strlen(string);
383 while (p > string && ijb_isspace(*(p-1)))
390 * find end of leading whitespace
393 while (*q && ijb_isspace(*q))
399 * if there was any, move the rest forwards
413 /*********************************************************************
417 * Description : Reallocate "old" and append text to it. This makes
418 * it easier to append to malloc'd strings.
421 * 1 : old = Old text that is to be extended. Will be
422 * free()d by this routine.
423 * 2 : text_to_append = Text to be appended to old.
425 * Returns : Pointer to newly malloc'ed appended string.
426 * If there is no text to append, return old. Caller
429 *********************************************************************/
430 char *strsav(char *old, const char *text_to_append)
432 int old_len, new_len;
435 if (( text_to_append == NULL) || (*text_to_append == '\0'))
442 old_len = strlen(old);
449 new_len = old_len + strlen(text_to_append) + 1;
453 if ((p = realloc(old, new_len)) == NULL)
455 log_error(LOG_LEVEL_FATAL, "realloc(%d) bytes failed!", new_len);
456 /* Never get here - LOG_LEVEL_FATAL causes program exit */
461 if ((p = (char *)malloc(new_len)) == NULL)
463 log_error(LOG_LEVEL_FATAL, "malloc(%d) bytes failed!", new_len);
464 /* Never get here - LOG_LEVEL_FATAL causes program exit */
468 strcpy(p + old_len, text_to_append);
474 /*********************************************************************
476 * Function : simplematch
478 * Description : String matching, with a (greedy) '*' wildcard that
479 * stands for zero or more arbitrary characters and
480 * character classes in [], which take both enumerations
484 * 1 : pattern = pattern for matching
485 * 2 : text = text to be matched
487 * Returns : 0 if match, else nonzero
489 *********************************************************************/
490 int simplematch(char *pattern, char *text)
499 unsigned char charmap[32];
505 /* EOF pattern but !EOF text? */
511 /* '*' in the pattern? */
515 /* The pattern ends afterwards? Speed up the return. */
521 /* Else, set wildcard mode and remember position after '*' */
526 /* Character range specification? */
529 memset(charmap, '\0', sizeof(charmap));
531 while (*++pat != ']')
537 else if (*pat == '-')
539 if ((*++pat == ']') || *pat == '\0')
543 for(i = lastchar; i <= *pat; i++)
545 charmap[i / 8] |= (1 << (i % 8));
550 charmap[*pat / 8] |= (1 << (*pat % 8));
554 } /* -END- if Character range specification */
557 /* Compare: Char match, or char range match*/
559 || ((*pat == ']') && (charmap[*txt / 8] & (1 << (*txt % 8)))) )
561 /* Sucess, go ahead */
566 /* In wildcard mode, just try again after failiure */
581 /* Cut off extra '*'s */
582 if(*pat == '*') pat++;
584 /* If this is the pattern's end, fine! */
590 /*********************************************************************
594 * Description : Duplicate the first n characters of a string that may
595 * contain '\0' characters.
598 * 1 : string = string to be duplicated
599 * 2 : n = number of bytes to duplicate
601 * Returns : pointer to copy, or NULL if failiure
603 *********************************************************************/
604 char *bindup(const char *string, int n)
608 if (NULL == (dup = (char *)malloc(n)))
614 memcpy(dup, string, n);
622 /*********************************************************************
624 * Function : make_path
626 * Description : Takes a directory name and a file name, returns
627 * the complete path. Handles windows/unix differences.
628 * If the file name is already an absolute path, or if
629 * the directory name is NULL or empty, it returns
633 * 1 : dir: Name of directory or NULL for none.
634 * 2 : file: Name of file. Should not be NULL or empty.
636 * Returns : "dir/file" (Or on windows, "dir\file").
637 * It allocates the string on the heap. Caller frees.
638 * Returns NULL in error (i.e. NULL file or out of
641 *********************************************************************/
642 char * make_path(const char * dir, const char * file)
649 strncpy(path,dir,512);
654 if(AddPart(path,file,512))
660 #else /* ndef AMIGA */
662 if ((file == NULL) || (*file == '\0'))
664 return NULL; /* Error */
667 if ((dir == NULL) || (*dir == '\0') /* No directory specified */
669 || (*file == '\\') || (file[1] == ':') /* Absolute path (DOS) */
670 #else /* ifndef _WIN32 */
671 || (*file == '/') /* Absolute path (U*ix) */
672 #endif /* ifndef _WIN32 */
679 char * path = malloc(strlen(dir) + strlen(file) + 2);
683 #else /* ifndef _WIN32 */
684 if(path[strlen(path)-1] != '/') strcat(path, "/");
685 #endif /* ifndef _WIN32 */
690 #endif /* ndef AMIGA */