1 const char miscutil_rcs[] = "$Id: miscutil.c,v 1.18 2001/09/20 13:33:43 steudten 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.18 2001/09/20 13:33:43 steudten
41 * change long to int as return value in hash_string(). Remember the wraparound
42 * for int = long = sizeof(4) - thats maybe not what we want.
44 * Revision 1.17 2001/09/13 20:51:29 jongfoster
45 * Fixing potential problems with characters >=128 in simplematch()
46 * This was also a compiler warning.
48 * Revision 1.16 2001/09/10 10:56:59 oes
49 * Silenced compiler warnings
51 * Revision 1.15 2001/07/13 14:02:24 oes
52 * Removed vim-settings
54 * Revision 1.14 2001/06/29 21:45:41 oes
55 * Indentation, CRLF->LF, Tab-> Space
57 * Revision 1.13 2001/06/29 13:32:14 oes
58 * Removed logentry from cancelled commit
60 * Revision 1.12 2001/06/09 10:55:28 jongfoster
61 * Changing BUFSIZ ==> BUFFER_SIZE
63 * Revision 1.11 2001/06/07 23:09:19 jongfoster
64 * Cosmetic indentation changes.
66 * Revision 1.10 2001/06/07 14:51:38 joergs
67 * make_path() no longer adds '/' if the dir already ends in '/'.
69 * Revision 1.9 2001/06/07 14:43:17 swa
70 * slight mistake in make_path, unix path style is /.
72 * Revision 1.8 2001/06/05 22:32:01 jongfoster
73 * New function make_path() to splice directory and file names together.
75 * Revision 1.7 2001/06/03 19:12:30 oes
78 * Revision 1.6 2001/06/01 18:14:49 jongfoster
79 * Changing the calls to strerr() to check HAVE_STRERR (which is defined
80 * in config.h if appropriate) rather than the NO_STRERR macro.
82 * Revision 1.5 2001/06/01 10:31:51 oes
83 * Added character class matching to trivimatch; renamed to simplematch
85 * Revision 1.4 2001/05/31 17:32:31 oes
87 * - Enhanced domain part globbing with infix and prefix asterisk
88 * matching and optional unanchored operation
90 * Revision 1.3 2001/05/29 23:10:09 oes
93 * - Introduced chomp()
94 * - Moved strsav() from showargs to miscutil
96 * Revision 1.2 2001/05/29 09:50:24 jongfoster
97 * Unified blocklist/imagelist/permissionslist.
98 * File format is still under discussion, but the internal changes
101 * Also modified interceptor behaviour:
102 * - We now intercept all URLs beginning with one of the following
103 * prefixes (and *only* these prefixes):
105 * * http://ijbswa.sf.net/config/
106 * * http://ijbswa.sourceforge.net/config/
107 * - New interceptors "home page" - go to http://i.j.b/ to see it.
108 * - Internal changes so that intercepted and fast redirect pages
109 * are not replaced with an image.
110 * - Interceptors now have the option to send a binary page direct
111 * to the client. (i.e. ijb-send-banner uses this)
112 * - Implemented show-url-info interceptor. (Which is why I needed
113 * the above interceptors changes - a typical URL is
114 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
115 * The previous mechanism would not have intercepted that, and
116 * if it had been intercepted then it then it would have replaced
119 * Revision 1.1.1.1 2001/05/15 13:59:00 oes
120 * Initial import of version 2.9.3 source tree
123 *********************************************************************/
136 * FIXME: Only need project.h for BUFFER_SIZE. It would be nice
137 * to remove this dependency.
140 #include "miscutil.h"
143 const char miscutil_h_rcs[] = MISCUTIL_H_VERSION;
145 /* Fix a problem with Solaris. There should be no effect on other
147 * Solaris's isspace() is a macro which uses it's argument directly
148 * as an array index. Therefore we need to make sure that high-bit
149 * characters generate +ve values, and ideally we also want to make
150 * the argument match the declared parameter type of "int".
152 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
153 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
155 /*********************************************************************
159 * Description : Malloc some memory and set it to '\0'.
160 * The way calloc() ought to be -acjc
163 * 1 : size = Size of memory chunk to return.
165 * Returns : Pointer to newly malloc'd memory chunk.
167 *********************************************************************/
168 void *zalloc(int size)
172 if ((ret = (void *)malloc(size)) != NULL)
174 memset(ret, 0, size);
181 /*********************************************************************
183 * Function : hash_string
185 * Description : Take a string and compute a (hopefuly) unique numeric
186 * integer value. This has several uses, but being able
187 * to "switch" a string the one of my favorites.
190 * 1 : s : string to be hashed.
192 * Returns : an unsigned long variable with the hashed value.
194 *********************************************************************/
195 unsigned int hash_string( const char* s )
210 /*********************************************************************
214 * Description : For some reason (which is beyond me), gcc and WIN32
215 * don't like strdup. When a "free" is executed on a
216 * strdup'd ptr, it can at times freez up! So I just
217 * replaced it and problem was solved.
220 * 1 : s = string to duplicate
222 * Returns : Pointer to newly malloc'ed copy of the string.
224 *********************************************************************/
225 char *strdup( const char *s )
227 char * result = (char *)malloc( strlen(s)+1 );
237 #endif /* def __MINGW32__ */
241 /*********************************************************************
243 * Function : safe_strerror
245 * Description : Variant of the library routine strerror() which will
246 * work on systems without the library routine, and
247 * which should never return NULL.
250 * 1 : err = the `errno' of the last operation.
252 * Returns : An "English" string of the last `errno'. Allocated
253 * with strdup(), so caller frees. May be NULL if the
254 * system is out of memory.
256 *********************************************************************/
257 char *safe_strerror(int err)
260 char buf[BUFFER_SIZE];
265 #endif /* HAVE_STRERROR */
269 sprintf(buf, "(errno = %d)", err);
278 /*********************************************************************
280 * Function : strcmpic
282 * Description : Case insensitive string comparison
285 * 1 : s1 = string 1 to compare
286 * 2 : s2 = string 2 to compare
288 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
290 *********************************************************************/
291 int strcmpic(const char *s1, const char *s2)
295 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
301 return(ijb_tolower(*s1) - ijb_tolower(*s2));
306 /*********************************************************************
308 * Function : strncmpic
310 * Description : Case insensitive string comparison (upto n characters)
313 * 1 : s1 = string 1 to compare
314 * 2 : s2 = string 2 to compare
315 * 3 : n = maximum characters to compare
317 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
319 *********************************************************************/
320 int strncmpic(const char *s1, const char *s2, size_t n)
322 if (n <= 0) return(0);
326 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
335 return(ijb_tolower(*s1) - ijb_tolower(*s2));
340 /*********************************************************************
344 * Description : In-situ-eliminate all leading and trailing whitespace
348 * 1 : s : string to be chomped.
350 * Returns : chomped string
352 *********************************************************************/
353 char *chomp(char *string)
358 * strip trailing whitespace
360 p = string + strlen(string);
361 while (p > string && ijb_isspace(*(p-1)))
368 * find end of leading whitespace
371 while (*q && ijb_isspace(*q))
377 * if there was any, move the rest forwards
392 /*********************************************************************
396 * Description : Reallocate "old" and append text to it. This makes
397 * it easier to append to malloc'd strings.
398 * Running out of memory is a FATAL error.
401 * 1 : old = Old text that is to be extended. Will be
402 * free()d by this routine. May be NULL.
403 * 2 : text_to_append = Text to be appended to old.
406 * Returns : Pointer to newly malloc'ed appended string.
407 * If there is no text to append, return old. Caller
410 *********************************************************************/
411 char *strsav(char *old, const char *text_to_append)
413 int old_len, new_len;
416 if (( text_to_append == NULL) || (*text_to_append == '\0'))
423 if ((p = strdup(text_to_append)) == NULL)
425 log_error(LOG_LEVEL_FATAL, "strdup() failed!", new_len);
426 /* Never get here - LOG_LEVEL_FATAL causes program exit */
431 old_len = strlen(old);
432 new_len = old_len + strlen(text_to_append) + 1;
434 if ((p = realloc(old, new_len)) == NULL)
436 log_error(LOG_LEVEL_FATAL, "realloc(%d) bytes failed!", new_len);
437 /* Never get here - LOG_LEVEL_FATAL causes program exit */
440 strcpy(p + old_len, text_to_append);
445 /*********************************************************************
447 * Function : string_append
449 * Description : Reallocate target_string and append text to it.
450 * This makes it easier to append to malloc'd strings.
451 * This is similar to strsav(), but running out of
452 * memory isn't catastrophic.
455 * 1 : target_string = Pointer to old text that is to be
456 * extended. *target_string will be free()d by this
457 * routine. target_string must be non-NULL.
458 * If *target_string is NULL, this routine will
459 * do nothing and return with an error - this allows
460 * you to make many calls to this routine and only
461 * check for errors after the last one.
462 * 2 : text_to_append = Text to be appended to old.
465 * Returns : On success, returns 0 and sets *target_string to
466 * newly malloc'ed appended string. Caller must free().
467 * On out-of-memory, returns nonzero (and free()s
468 * *target_string and sets it to NULL).
470 *********************************************************************/
471 int string_append(char **target_string, const char *text_to_append)
476 assert(target_string);
477 assert(text_to_append);
479 if (*target_string == NULL)
484 if (*text_to_append == '\0')
489 old_len = strlen(*target_string);
491 if (NULL == (new_string = realloc(*target_string,
492 strlen(text_to_append) + old_len + 1)))
494 free(*target_string);
496 *target_string = NULL;
500 strcpy(new_string + old_len, text_to_append);
502 *target_string = new_string;
507 /*********************************************************************
509 * Function : simplematch
511 * Description : String matching, with a (greedy) '*' wildcard that
512 * stands for zero or more arbitrary characters and
513 * character classes in [], which take both enumerations
517 * 1 : pattern = pattern for matching
518 * 2 : text = text to be matched
520 * Returns : 0 if match, else nonzero
522 *********************************************************************/
523 int simplematch(char *pattern, char *text)
525 unsigned char *pat = (unsigned char *) pattern;
526 unsigned char *txt = (unsigned char *) text;
527 unsigned char *fallback = pat;
530 unsigned char lastchar = 'a';
532 unsigned char charmap[32];
538 /* EOF pattern but !EOF text? */
544 /* '*' in the pattern? */
548 /* The pattern ends afterwards? Speed up the return. */
554 /* Else, set wildcard mode and remember position after '*' */
559 /* Character range specification? */
562 memset(charmap, '\0', sizeof(charmap));
564 while (*++pat != ']')
570 else if (*pat == '-')
572 if ((*++pat == ']') || *pat == '\0')
576 for(i = lastchar; i <= *pat; i++)
578 charmap[i / 8] |= (1 << (i % 8));
583 charmap[*pat / 8] |= (1 << (*pat % 8));
587 } /* -END- if Character range specification */
590 /* Compare: Char match, or char range match*/
592 || ((*pat == ']') && (charmap[*txt / 8] & (1 << (*txt % 8)))) )
594 /* Sucess, go ahead */
599 /* In wildcard mode, just try again after failiure */
614 /* Cut off extra '*'s */
615 if(*pat == '*') pat++;
617 /* If this is the pattern's end, fine! */
623 /*********************************************************************
627 * Description : Duplicate the first n characters of a string that may
628 * contain '\0' characters.
631 * 1 : string = string to be duplicated
632 * 2 : n = number of bytes to duplicate
634 * Returns : pointer to copy, or NULL if failiure
636 *********************************************************************/
637 char *bindup(const char *string, int n)
641 if (NULL == (dup = (char *)malloc(n)))
647 memcpy(dup, string, n);
655 /*********************************************************************
657 * Function : make_path
659 * Description : Takes a directory name and a file name, returns
660 * the complete path. Handles windows/unix differences.
661 * If the file name is already an absolute path, or if
662 * the directory name is NULL or empty, it returns
666 * 1 : dir: Name of directory or NULL for none.
667 * 2 : file: Name of file. Should not be NULL or empty.
669 * Returns : "dir/file" (Or on windows, "dir\file").
670 * It allocates the string on the heap. Caller frees.
671 * Returns NULL in error (i.e. NULL file or out of
674 *********************************************************************/
675 char * make_path(const char * dir, const char * file)
682 strncpy(path,dir,512);
687 if(AddPart(path,file,512))
693 #else /* ndef AMIGA */
695 if ((file == NULL) || (*file == '\0'))
697 return NULL; /* Error */
700 if ((dir == NULL) || (*dir == '\0') /* No directory specified */
702 || (*file == '\\') || (file[1] == ':') /* Absolute path (DOS) */
703 #else /* ifndef _WIN32 */
704 || (*file == '/') /* Absolute path (U*ix) */
705 #endif /* ifndef _WIN32 */
712 char * path = malloc(strlen(dir) + strlen(file) + 2);
716 #else /* ifndef _WIN32 */
717 if(path[strlen(path)-1] != '/') strcat(path, "/");
718 #endif /* ifndef _WIN32 */
723 #endif /* ndef AMIGA */