1 const char miscutil_rcs[] = "$Id: miscutil.c,v 1.20 2001/10/22 15:33:56 david__schmidt 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.20 2001/10/22 15:33:56 david__schmidt
40 * Special-cased OS/2 out of the Netscape-abort-on-404-in-js problem in
41 * filters.c. Added a FIXME in front of the offending code. I'll gladly
42 * put in a better/more robust fix for all parties if one is presented...
43 * It seems that just returning 200 instead of 404 would pretty much fix
44 * it for everyone, but I don't know all the history of the problem.
46 * Revision 1.19 2001/10/14 22:02:57 jongfoster
47 * New function string_append() which is like strsav(), but running
48 * out of memory isn't automatically FATAL.
50 * Revision 1.18 2001/09/20 13:33:43 steudten
52 * change long to int as return value in hash_string(). Remember the wraparound
53 * for int = long = sizeof(4) - thats maybe not what we want.
55 * Revision 1.17 2001/09/13 20:51:29 jongfoster
56 * Fixing potential problems with characters >=128 in simplematch()
57 * This was also a compiler warning.
59 * Revision 1.16 2001/09/10 10:56:59 oes
60 * Silenced compiler warnings
62 * Revision 1.15 2001/07/13 14:02:24 oes
63 * Removed vim-settings
65 * Revision 1.14 2001/06/29 21:45:41 oes
66 * Indentation, CRLF->LF, Tab-> Space
68 * Revision 1.13 2001/06/29 13:32:14 oes
69 * Removed logentry from cancelled commit
71 * Revision 1.12 2001/06/09 10:55:28 jongfoster
72 * Changing BUFSIZ ==> BUFFER_SIZE
74 * Revision 1.11 2001/06/07 23:09:19 jongfoster
75 * Cosmetic indentation changes.
77 * Revision 1.10 2001/06/07 14:51:38 joergs
78 * make_path() no longer adds '/' if the dir already ends in '/'.
80 * Revision 1.9 2001/06/07 14:43:17 swa
81 * slight mistake in make_path, unix path style is /.
83 * Revision 1.8 2001/06/05 22:32:01 jongfoster
84 * New function make_path() to splice directory and file names together.
86 * Revision 1.7 2001/06/03 19:12:30 oes
89 * Revision 1.6 2001/06/01 18:14:49 jongfoster
90 * Changing the calls to strerr() to check HAVE_STRERR (which is defined
91 * in config.h if appropriate) rather than the NO_STRERR macro.
93 * Revision 1.5 2001/06/01 10:31:51 oes
94 * Added character class matching to trivimatch; renamed to simplematch
96 * Revision 1.4 2001/05/31 17:32:31 oes
98 * - Enhanced domain part globbing with infix and prefix asterisk
99 * matching and optional unanchored operation
101 * Revision 1.3 2001/05/29 23:10:09 oes
104 * - Introduced chomp()
105 * - Moved strsav() from showargs to miscutil
107 * Revision 1.2 2001/05/29 09:50:24 jongfoster
108 * Unified blocklist/imagelist/permissionslist.
109 * File format is still under discussion, but the internal changes
112 * Also modified interceptor behaviour:
113 * - We now intercept all URLs beginning with one of the following
114 * prefixes (and *only* these prefixes):
116 * * http://ijbswa.sf.net/config/
117 * * http://ijbswa.sourceforge.net/config/
118 * - New interceptors "home page" - go to http://i.j.b/ to see it.
119 * - Internal changes so that intercepted and fast redirect pages
120 * are not replaced with an image.
121 * - Interceptors now have the option to send a binary page direct
122 * to the client. (i.e. ijb-send-banner uses this)
123 * - Implemented show-url-info interceptor. (Which is why I needed
124 * the above interceptors changes - a typical URL is
125 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
126 * The previous mechanism would not have intercepted that, and
127 * if it had been intercepted then it then it would have replaced
130 * Revision 1.1.1.1 2001/05/15 13:59:00 oes
131 * Initial import of version 2.9.3 source tree
134 *********************************************************************/
147 #include "miscutil.h"
150 const char miscutil_h_rcs[] = MISCUTIL_H_VERSION;
152 /* Fix a problem with Solaris. There should be no effect on other
154 * Solaris's isspace() is a macro which uses it's argument directly
155 * as an array index. Therefore we need to make sure that high-bit
156 * characters generate +ve values, and ideally we also want to make
157 * the argument match the declared parameter type of "int".
159 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
160 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
162 /*********************************************************************
166 * Description : Malloc some memory and set it to '\0'.
167 * The way calloc() ought to be -acjc
170 * 1 : size = Size of memory chunk to return.
172 * Returns : Pointer to newly malloc'd memory chunk.
174 *********************************************************************/
175 void *zalloc(int size)
179 if ((ret = (void *)malloc(size)) != NULL)
181 memset(ret, 0, size);
188 /*********************************************************************
190 * Function : hash_string
192 * Description : Take a string and compute a (hopefuly) unique numeric
193 * integer value. This has several uses, but being able
194 * to "switch" a string the one of my favorites.
197 * 1 : s : string to be hashed.
199 * Returns : an unsigned long variable with the hashed value.
201 *********************************************************************/
202 unsigned int hash_string( const char* s )
217 /*********************************************************************
221 * Description : For some reason (which is beyond me), gcc and WIN32
222 * don't like strdup. When a "free" is executed on a
223 * strdup'd ptr, it can at times freez up! So I just
224 * replaced it and problem was solved.
227 * 1 : s = string to duplicate
229 * Returns : Pointer to newly malloc'ed copy of the string.
231 *********************************************************************/
232 char *strdup( const char *s )
234 char * result = (char *)malloc( strlen(s)+1 );
244 #endif /* def __MINGW32__ */
248 /*********************************************************************
250 * Function : safe_strerror
252 * Description : Variant of the library routine strerror() which will
253 * work on systems without the library routine, and
254 * which should never return NULL.
257 * 1 : err = the `errno' of the last operation.
259 * Returns : An "English" string of the last `errno'. Allocated
260 * with strdup(), so caller frees. May be NULL if the
261 * system is out of memory.
263 *********************************************************************/
264 char *safe_strerror(int err)
267 char buf[BUFFER_SIZE];
272 #endif /* HAVE_STRERROR */
276 sprintf(buf, "(errno = %d)", err);
285 /*********************************************************************
287 * Function : strcmpic
289 * Description : Case insensitive string comparison
292 * 1 : s1 = string 1 to compare
293 * 2 : s2 = string 2 to compare
295 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
297 *********************************************************************/
298 int strcmpic(const char *s1, const char *s2)
302 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
308 return(ijb_tolower(*s1) - ijb_tolower(*s2));
313 /*********************************************************************
315 * Function : strncmpic
317 * Description : Case insensitive string comparison (upto n characters)
320 * 1 : s1 = string 1 to compare
321 * 2 : s2 = string 2 to compare
322 * 3 : n = maximum characters to compare
324 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
326 *********************************************************************/
327 int strncmpic(const char *s1, const char *s2, size_t n)
329 if (n <= 0) return(0);
333 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
342 return(ijb_tolower(*s1) - ijb_tolower(*s2));
347 /*********************************************************************
351 * Description : In-situ-eliminate all leading and trailing whitespace
355 * 1 : s : string to be chomped.
357 * Returns : chomped string
359 *********************************************************************/
360 char *chomp(char *string)
365 * strip trailing whitespace
367 p = string + strlen(string);
368 while (p > string && ijb_isspace(*(p-1)))
375 * find end of leading whitespace
378 while (*q && ijb_isspace(*q))
384 * if there was any, move the rest forwards
399 /*********************************************************************
403 * Description : Reallocate "old" and append text to it. This makes
404 * it easier to append to malloc'd strings.
405 * Running out of memory is a FATAL error.
408 * 1 : old = Old text that is to be extended. Will be
409 * free()d by this routine. May be NULL.
410 * 2 : text_to_append = Text to be appended to old.
413 * Returns : Pointer to newly malloc'ed appended string.
414 * If there is no text to append, return old. Caller
417 *********************************************************************/
418 char *strsav(char *old, const char *text_to_append)
420 int old_len, new_len = 0;
423 if (( text_to_append == NULL) || (*text_to_append == '\0'))
430 if ((p = strdup(text_to_append)) == NULL)
432 log_error(LOG_LEVEL_FATAL, "strdup() failed!", new_len);
433 /* Never get here - LOG_LEVEL_FATAL causes program exit */
438 old_len = strlen(old);
439 new_len = old_len + strlen(text_to_append) + 1;
441 if ((p = realloc(old, new_len)) == NULL)
443 log_error(LOG_LEVEL_FATAL, "realloc(%d) bytes failed!", new_len);
444 /* Never get here - LOG_LEVEL_FATAL causes program exit */
447 strcpy(p + old_len, text_to_append);
452 /*********************************************************************
454 * Function : string_append
456 * Description : Reallocate target_string and append text to it.
457 * This makes it easier to append to malloc'd strings.
458 * This is similar to strsav(), but running out of
459 * memory isn't catastrophic.
463 * The following style provides sufficient error
464 * checking for this routine, with minimal clutter
465 * in the source code. It is recommended if you
466 * have many calls to this function:
468 * char * s = strdup(...); // don't check for error
469 * string_append(&s, ...); // don't check for error
470 * string_append(&s, ...); // don't check for error
471 * string_append(&s, ...); // don't check for error
472 * if (NULL == s) { ... handle error ... }
476 * char * s = strdup(...); // don't check for error
477 * string_append(&s, ...); // don't check for error
478 * string_append(&s, ...); // don't check for error
479 * if (string_append(&s, ...)) {... handle error ...}
482 * 1 : target_string = Pointer to old text that is to be
483 * extended. *target_string will be free()d by this
484 * routine. target_string must be non-NULL.
485 * If *target_string is NULL, this routine will
486 * do nothing and return with an error - this allows
487 * you to make many calls to this routine and only
488 * check for errors after the last one.
489 * 2 : text_to_append = Text to be appended to old.
492 * Returns : JB_ERR_OK on success, and sets *target_string
493 * to newly malloc'ed appended string. Caller
494 * must free(*target_string).
495 * JB_ERR_MEMORY on out-of-memory. (And free()s
496 * *target_string and sets it to NULL).
497 * JB_ERR_MEMORY if *target_string is NULL.
499 *********************************************************************/
500 jb_err string_append(char **target_string, const char *text_to_append)
505 assert(target_string);
506 assert(text_to_append);
508 if (*target_string == NULL)
510 return JB_ERR_MEMORY;
513 if (*text_to_append == '\0')
518 old_len = strlen(*target_string);
520 if (NULL == (new_string = realloc(*target_string,
521 strlen(text_to_append) + old_len + 1)))
523 free(*target_string);
525 *target_string = NULL;
526 return JB_ERR_MEMORY;
529 strcpy(new_string + old_len, text_to_append);
531 *target_string = new_string;
536 /*********************************************************************
538 * Function : simplematch
540 * Description : String matching, with a (greedy) '*' wildcard that
541 * stands for zero or more arbitrary characters and
542 * character classes in [], which take both enumerations
546 * 1 : pattern = pattern for matching
547 * 2 : text = text to be matched
549 * Returns : 0 if match, else nonzero
551 *********************************************************************/
552 int simplematch(char *pattern, char *text)
554 unsigned char *pat = (unsigned char *) pattern;
555 unsigned char *txt = (unsigned char *) text;
556 unsigned char *fallback = pat;
559 unsigned char lastchar = 'a';
561 unsigned char charmap[32];
567 /* EOF pattern but !EOF text? */
573 /* '*' in the pattern? */
577 /* The pattern ends afterwards? Speed up the return. */
583 /* Else, set wildcard mode and remember position after '*' */
588 /* Character range specification? */
591 memset(charmap, '\0', sizeof(charmap));
593 while (*++pat != ']')
599 else if (*pat == '-')
601 if ((*++pat == ']') || *pat == '\0')
605 for(i = lastchar; i <= *pat; i++)
607 charmap[i / 8] |= (1 << (i % 8));
612 charmap[*pat / 8] |= (1 << (*pat % 8));
616 } /* -END- if Character range specification */
619 /* Compare: Char match, or char range match*/
621 || ((*pat == ']') && (charmap[*txt / 8] & (1 << (*txt % 8)))) )
623 /* Sucess, go ahead */
628 /* In wildcard mode, just try again after failiure */
643 /* Cut off extra '*'s */
644 if(*pat == '*') pat++;
646 /* If this is the pattern's end, fine! */
652 /*********************************************************************
656 * Description : Duplicate the first n characters of a string that may
657 * contain '\0' characters.
660 * 1 : string = string to be duplicated
661 * 2 : n = number of bytes to duplicate
663 * Returns : pointer to copy, or NULL if failiure
665 *********************************************************************/
666 char *bindup(const char *string, int n)
670 if (NULL == (dup = (char *)malloc(n)))
676 memcpy(dup, string, n);
684 /*********************************************************************
686 * Function : make_path
688 * Description : Takes a directory name and a file name, returns
689 * the complete path. Handles windows/unix differences.
690 * If the file name is already an absolute path, or if
691 * the directory name is NULL or empty, it returns
695 * 1 : dir: Name of directory or NULL for none.
696 * 2 : file: Name of file. Should not be NULL or empty.
698 * Returns : "dir/file" (Or on windows, "dir\file").
699 * It allocates the string on the heap. Caller frees.
700 * Returns NULL in error (i.e. NULL file or out of
703 *********************************************************************/
704 char * make_path(const char * dir, const char * file)
711 strncpy(path,dir,512);
716 if(AddPart(path,file,512))
722 #else /* ndef AMIGA */
724 if ((file == NULL) || (*file == '\0'))
726 return NULL; /* Error */
729 if ((dir == NULL) || (*dir == '\0') /* No directory specified */
731 || (*file == '\\') || (file[1] == ':') /* Absolute path (DOS) */
732 #else /* ifndef _WIN32 */
733 || (*file == '/') /* Absolute path (U*ix) */
734 #endif /* ifndef _WIN32 */
741 char * path = malloc(strlen(dir) + strlen(file) + 2);
744 if(path[strlen(path)-1] != '\\')
748 #else /* ifndef _WIN32 */
749 if(path[strlen(path)-1] != '/')
753 #endif /* ifndef _WIN32 */
758 #endif /* ndef AMIGA */