1 const char miscutil_rcs[] = "$Id: miscutil.c,v 1.21 2001/10/23 21:27:50 jongfoster 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.21 2001/10/23 21:27:50 jongfoster
40 * Standardising error codes in string_append
41 * make_path() no longer adds '\\' if the dir already ends in '\\' (this
42 * is just copying a UNIX-specific fix to the Windows-specific part)
44 * Revision 1.20 2001/10/22 15:33:56 david__schmidt
45 * Special-cased OS/2 out of the Netscape-abort-on-404-in-js problem in
46 * filters.c. Added a FIXME in front of the offending code. I'll gladly
47 * put in a better/more robust fix for all parties if one is presented...
48 * It seems that just returning 200 instead of 404 would pretty much fix
49 * it for everyone, but I don't know all the history of the problem.
51 * Revision 1.19 2001/10/14 22:02:57 jongfoster
52 * New function string_append() which is like strsav(), but running
53 * out of memory isn't automatically FATAL.
55 * Revision 1.18 2001/09/20 13:33:43 steudten
57 * change long to int as return value in hash_string(). Remember the wraparound
58 * for int = long = sizeof(4) - thats maybe not what we want.
60 * Revision 1.17 2001/09/13 20:51:29 jongfoster
61 * Fixing potential problems with characters >=128 in simplematch()
62 * This was also a compiler warning.
64 * Revision 1.16 2001/09/10 10:56:59 oes
65 * Silenced compiler warnings
67 * Revision 1.15 2001/07/13 14:02:24 oes
68 * Removed vim-settings
70 * Revision 1.14 2001/06/29 21:45:41 oes
71 * Indentation, CRLF->LF, Tab-> Space
73 * Revision 1.13 2001/06/29 13:32:14 oes
74 * Removed logentry from cancelled commit
76 * Revision 1.12 2001/06/09 10:55:28 jongfoster
77 * Changing BUFSIZ ==> BUFFER_SIZE
79 * Revision 1.11 2001/06/07 23:09:19 jongfoster
80 * Cosmetic indentation changes.
82 * Revision 1.10 2001/06/07 14:51:38 joergs
83 * make_path() no longer adds '/' if the dir already ends in '/'.
85 * Revision 1.9 2001/06/07 14:43:17 swa
86 * slight mistake in make_path, unix path style is /.
88 * Revision 1.8 2001/06/05 22:32:01 jongfoster
89 * New function make_path() to splice directory and file names together.
91 * Revision 1.7 2001/06/03 19:12:30 oes
94 * Revision 1.6 2001/06/01 18:14:49 jongfoster
95 * Changing the calls to strerr() to check HAVE_STRERR (which is defined
96 * in config.h if appropriate) rather than the NO_STRERR macro.
98 * Revision 1.5 2001/06/01 10:31:51 oes
99 * Added character class matching to trivimatch; renamed to simplematch
101 * Revision 1.4 2001/05/31 17:32:31 oes
103 * - Enhanced domain part globbing with infix and prefix asterisk
104 * matching and optional unanchored operation
106 * Revision 1.3 2001/05/29 23:10:09 oes
109 * - Introduced chomp()
110 * - Moved strsav() from showargs to miscutil
112 * Revision 1.2 2001/05/29 09:50:24 jongfoster
113 * Unified blocklist/imagelist/permissionslist.
114 * File format is still under discussion, but the internal changes
117 * Also modified interceptor behaviour:
118 * - We now intercept all URLs beginning with one of the following
119 * prefixes (and *only* these prefixes):
121 * * http://ijbswa.sf.net/config/
122 * * http://ijbswa.sourceforge.net/config/
123 * - New interceptors "home page" - go to http://i.j.b/ to see it.
124 * - Internal changes so that intercepted and fast redirect pages
125 * are not replaced with an image.
126 * - Interceptors now have the option to send a binary page direct
127 * to the client. (i.e. ijb-send-banner uses this)
128 * - Implemented show-url-info interceptor. (Which is why I needed
129 * the above interceptors changes - a typical URL is
130 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
131 * The previous mechanism would not have intercepted that, and
132 * if it had been intercepted then it then it would have replaced
135 * Revision 1.1.1.1 2001/05/15 13:59:00 oes
136 * Initial import of version 2.9.3 source tree
139 *********************************************************************/
152 #include "miscutil.h"
155 const char miscutil_h_rcs[] = MISCUTIL_H_VERSION;
157 /*********************************************************************
161 * Description : Malloc some memory and set it to '\0'.
162 * The way calloc() ought to be -acjc
165 * 1 : size = Size of memory chunk to return.
167 * Returns : Pointer to newly malloc'd memory chunk.
169 *********************************************************************/
170 void *zalloc(int size)
174 if ((ret = (void *)malloc(size)) != NULL)
176 memset(ret, 0, size);
183 /*********************************************************************
185 * Function : hash_string
187 * Description : Take a string and compute a (hopefuly) unique numeric
188 * integer value. This has several uses, but being able
189 * to "switch" a string the one of my favorites.
192 * 1 : s : string to be hashed.
194 * Returns : an unsigned long variable with the hashed value.
196 *********************************************************************/
197 unsigned int hash_string( const char* s )
212 /*********************************************************************
216 * Description : For some reason (which is beyond me), gcc and WIN32
217 * don't like strdup. When a "free" is executed on a
218 * strdup'd ptr, it can at times freez up! So I just
219 * replaced it and problem was solved.
222 * 1 : s = string to duplicate
224 * Returns : Pointer to newly malloc'ed copy of the string.
226 *********************************************************************/
227 char *strdup( const char *s )
229 char * result = (char *)malloc( strlen(s)+1 );
239 #endif /* def __MINGW32__ */
243 /*********************************************************************
245 * Function : safe_strerror
247 * Description : Variant of the library routine strerror() which will
248 * work on systems without the library routine, and
249 * which should never return NULL.
252 * 1 : err = the `errno' of the last operation.
254 * Returns : An "English" string of the last `errno'. Allocated
255 * with strdup(), so caller frees. May be NULL if the
256 * system is out of memory.
258 *********************************************************************/
259 char *safe_strerror(int err)
262 char buf[BUFFER_SIZE];
267 #endif /* HAVE_STRERROR */
271 sprintf(buf, "(errno = %d)", err);
280 /*********************************************************************
282 * Function : strcmpic
284 * Description : Case insensitive string comparison
287 * 1 : s1 = string 1 to compare
288 * 2 : s2 = string 2 to compare
290 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
292 *********************************************************************/
293 int strcmpic(const char *s1, const char *s2)
297 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
303 return(ijb_tolower(*s1) - ijb_tolower(*s2));
308 /*********************************************************************
310 * Function : strncmpic
312 * Description : Case insensitive string comparison (upto n characters)
315 * 1 : s1 = string 1 to compare
316 * 2 : s2 = string 2 to compare
317 * 3 : n = maximum characters to compare
319 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
321 *********************************************************************/
322 int strncmpic(const char *s1, const char *s2, size_t n)
324 if (n <= 0) return(0);
328 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
337 return(ijb_tolower(*s1) - ijb_tolower(*s2));
342 /*********************************************************************
346 * Description : In-situ-eliminate all leading and trailing whitespace
350 * 1 : s : string to be chomped.
352 * Returns : chomped string
354 *********************************************************************/
355 char *chomp(char *string)
360 * strip trailing whitespace
362 p = string + strlen(string);
363 while (p > string && ijb_isspace(*(p-1)))
370 * find end of leading whitespace
373 while (*q && ijb_isspace(*q))
379 * if there was any, move the rest forwards
394 /*********************************************************************
398 * Description : Reallocate "old" and append text to it. This makes
399 * it easier to append to malloc'd strings.
400 * Running out of memory is a FATAL error.
403 * 1 : old = Old text that is to be extended. Will be
404 * free()d by this routine. May be NULL.
405 * 2 : text_to_append = Text to be appended to old.
408 * Returns : Pointer to newly malloc'ed appended string.
409 * If there is no text to append, return old. Caller
412 *********************************************************************/
413 char *strsav(char *old, const char *text_to_append)
415 int old_len, new_len = 0;
418 if (( text_to_append == NULL) || (*text_to_append == '\0'))
425 if ((p = strdup(text_to_append)) == NULL)
427 log_error(LOG_LEVEL_FATAL, "strdup() failed!", new_len);
428 /* Never get here - LOG_LEVEL_FATAL causes program exit */
433 old_len = strlen(old);
434 new_len = old_len + strlen(text_to_append) + 1;
436 if ((p = realloc(old, new_len)) == NULL)
438 log_error(LOG_LEVEL_FATAL, "realloc(%d) bytes failed!", new_len);
439 /* Never get here - LOG_LEVEL_FATAL causes program exit */
442 strcpy(p + old_len, text_to_append);
447 /*********************************************************************
449 * Function : string_append
451 * Description : Reallocate target_string and append text to it.
452 * This makes it easier to append to malloc'd strings.
453 * This is similar to strsav(), but running out of
454 * memory isn't catastrophic.
458 * The following style provides sufficient error
459 * checking for this routine, with minimal clutter
460 * in the source code. It is recommended if you
461 * have many calls to this function:
463 * char * s = strdup(...); // don't check for error
464 * string_append(&s, ...); // don't check for error
465 * string_append(&s, ...); // don't check for error
466 * string_append(&s, ...); // don't check for error
467 * if (NULL == s) { ... handle error ... }
471 * char * s = strdup(...); // don't check for error
472 * string_append(&s, ...); // don't check for error
473 * string_append(&s, ...); // don't check for error
474 * if (string_append(&s, ...)) {... handle error ...}
477 * 1 : target_string = Pointer to old text that is to be
478 * extended. *target_string will be free()d by this
479 * routine. target_string must be non-NULL.
480 * If *target_string is NULL, this routine will
481 * do nothing and return with an error - this allows
482 * you to make many calls to this routine and only
483 * check for errors after the last one.
484 * 2 : text_to_append = Text to be appended to old.
487 * Returns : JB_ERR_OK on success, and sets *target_string
488 * to newly malloc'ed appended string. Caller
489 * must free(*target_string).
490 * JB_ERR_MEMORY on out-of-memory. (And free()s
491 * *target_string and sets it to NULL).
492 * JB_ERR_MEMORY if *target_string is NULL.
494 *********************************************************************/
495 jb_err string_append(char **target_string, const char *text_to_append)
500 assert(target_string);
501 assert(text_to_append);
503 if (*target_string == NULL)
505 return JB_ERR_MEMORY;
508 if (*text_to_append == '\0')
513 old_len = strlen(*target_string);
515 if (NULL == (new_string = realloc(*target_string,
516 strlen(text_to_append) + old_len + 1)))
518 free(*target_string);
520 *target_string = NULL;
521 return JB_ERR_MEMORY;
524 strcpy(new_string + old_len, text_to_append);
526 *target_string = new_string;
531 /*********************************************************************
533 * Function : simplematch
535 * Description : String matching, with a (greedy) '*' wildcard that
536 * stands for zero or more arbitrary characters and
537 * character classes in [], which take both enumerations
541 * 1 : pattern = pattern for matching
542 * 2 : text = text to be matched
544 * Returns : 0 if match, else nonzero
546 *********************************************************************/
547 int simplematch(char *pattern, char *text)
549 unsigned char *pat = (unsigned char *) pattern;
550 unsigned char *txt = (unsigned char *) text;
551 unsigned char *fallback = pat;
554 unsigned char lastchar = 'a';
556 unsigned char charmap[32];
562 /* EOF pattern but !EOF text? */
568 /* '*' in the pattern? */
572 /* The pattern ends afterwards? Speed up the return. */
578 /* Else, set wildcard mode and remember position after '*' */
583 /* Character range specification? */
586 memset(charmap, '\0', sizeof(charmap));
588 while (*++pat != ']')
594 else if (*pat == '-')
596 if ((*++pat == ']') || *pat == '\0')
600 for(i = lastchar; i <= *pat; i++)
602 charmap[i / 8] |= (1 << (i % 8));
607 charmap[*pat / 8] |= (1 << (*pat % 8));
611 } /* -END- if Character range specification */
614 /* Compare: Char match, or char range match*/
616 || ((*pat == ']') && (charmap[*txt / 8] & (1 << (*txt % 8)))) )
618 /* Sucess, go ahead */
623 /* In wildcard mode, just try again after failiure */
638 /* Cut off extra '*'s */
639 if(*pat == '*') pat++;
641 /* If this is the pattern's end, fine! */
647 /*********************************************************************
651 * Description : Duplicate the first n characters of a string that may
652 * contain '\0' characters.
655 * 1 : string = string to be duplicated
656 * 2 : n = number of bytes to duplicate
658 * Returns : pointer to copy, or NULL if failiure
660 *********************************************************************/
661 char *bindup(const char *string, int n)
665 if (NULL == (dup = (char *)malloc(n)))
671 memcpy(dup, string, n);
679 /*********************************************************************
681 * Function : make_path
683 * Description : Takes a directory name and a file name, returns
684 * the complete path. Handles windows/unix differences.
685 * If the file name is already an absolute path, or if
686 * the directory name is NULL or empty, it returns
690 * 1 : dir: Name of directory or NULL for none.
691 * 2 : file: Name of file. Should not be NULL or empty.
693 * Returns : "dir/file" (Or on windows, "dir\file").
694 * It allocates the string on the heap. Caller frees.
695 * Returns NULL in error (i.e. NULL file or out of
698 *********************************************************************/
699 char * make_path(const char * dir, const char * file)
706 strncpy(path,dir,512);
711 if(AddPart(path,file,512))
717 #else /* ndef AMIGA */
719 if ((file == NULL) || (*file == '\0'))
721 return NULL; /* Error */
724 if ((dir == NULL) || (*dir == '\0') /* No directory specified */
726 || (*file == '\\') || (file[1] == ':') /* Absolute path (DOS) */
727 #else /* ifndef _WIN32 */
728 || (*file == '/') /* Absolute path (U*ix) */
729 #endif /* ifndef _WIN32 */
736 char * path = malloc(strlen(dir) + strlen(file) + 2);
739 if(path[strlen(path)-1] != '\\')
743 #else /* ifndef _WIN32 */
744 if(path[strlen(path)-1] != '/')
748 #endif /* ifndef _WIN32 */
753 #endif /* ndef AMIGA */