1 const char miscutil_rcs[] = "$Id: miscutil.c,v 1.19 2001/10/14 22:02:57 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.19 2001/10/14 22:02:57 jongfoster
40 * New function string_append() which is like strsav(), but running
41 * out of memory isn't automatically FATAL.
43 * Revision 1.18 2001/09/20 13:33:43 steudten
45 * change long to int as return value in hash_string(). Remember the wraparound
46 * for int = long = sizeof(4) - thats maybe not what we want.
48 * Revision 1.17 2001/09/13 20:51:29 jongfoster
49 * Fixing potential problems with characters >=128 in simplematch()
50 * This was also a compiler warning.
52 * Revision 1.16 2001/09/10 10:56:59 oes
53 * Silenced compiler warnings
55 * Revision 1.15 2001/07/13 14:02:24 oes
56 * Removed vim-settings
58 * Revision 1.14 2001/06/29 21:45:41 oes
59 * Indentation, CRLF->LF, Tab-> Space
61 * Revision 1.13 2001/06/29 13:32:14 oes
62 * Removed logentry from cancelled commit
64 * Revision 1.12 2001/06/09 10:55:28 jongfoster
65 * Changing BUFSIZ ==> BUFFER_SIZE
67 * Revision 1.11 2001/06/07 23:09:19 jongfoster
68 * Cosmetic indentation changes.
70 * Revision 1.10 2001/06/07 14:51:38 joergs
71 * make_path() no longer adds '/' if the dir already ends in '/'.
73 * Revision 1.9 2001/06/07 14:43:17 swa
74 * slight mistake in make_path, unix path style is /.
76 * Revision 1.8 2001/06/05 22:32:01 jongfoster
77 * New function make_path() to splice directory and file names together.
79 * Revision 1.7 2001/06/03 19:12:30 oes
82 * Revision 1.6 2001/06/01 18:14:49 jongfoster
83 * Changing the calls to strerr() to check HAVE_STRERR (which is defined
84 * in config.h if appropriate) rather than the NO_STRERR macro.
86 * Revision 1.5 2001/06/01 10:31:51 oes
87 * Added character class matching to trivimatch; renamed to simplematch
89 * Revision 1.4 2001/05/31 17:32:31 oes
91 * - Enhanced domain part globbing with infix and prefix asterisk
92 * matching and optional unanchored operation
94 * Revision 1.3 2001/05/29 23:10:09 oes
97 * - Introduced chomp()
98 * - Moved strsav() from showargs to miscutil
100 * Revision 1.2 2001/05/29 09:50:24 jongfoster
101 * Unified blocklist/imagelist/permissionslist.
102 * File format is still under discussion, but the internal changes
105 * Also modified interceptor behaviour:
106 * - We now intercept all URLs beginning with one of the following
107 * prefixes (and *only* these prefixes):
109 * * http://ijbswa.sf.net/config/
110 * * http://ijbswa.sourceforge.net/config/
111 * - New interceptors "home page" - go to http://i.j.b/ to see it.
112 * - Internal changes so that intercepted and fast redirect pages
113 * are not replaced with an image.
114 * - Interceptors now have the option to send a binary page direct
115 * to the client. (i.e. ijb-send-banner uses this)
116 * - Implemented show-url-info interceptor. (Which is why I needed
117 * the above interceptors changes - a typical URL is
118 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
119 * The previous mechanism would not have intercepted that, and
120 * if it had been intercepted then it then it would have replaced
123 * Revision 1.1.1.1 2001/05/15 13:59:00 oes
124 * Initial import of version 2.9.3 source tree
127 *********************************************************************/
140 * FIXME: Only need project.h for BUFFER_SIZE. It would be nice
141 * to remove this dependency.
144 #include "miscutil.h"
147 const char miscutil_h_rcs[] = MISCUTIL_H_VERSION;
149 /* Fix a problem with Solaris. There should be no effect on other
151 * Solaris's isspace() is a macro which uses it's argument directly
152 * as an array index. Therefore we need to make sure that high-bit
153 * characters generate +ve values, and ideally we also want to make
154 * the argument match the declared parameter type of "int".
156 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
157 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
159 /*********************************************************************
163 * Description : Malloc some memory and set it to '\0'.
164 * The way calloc() ought to be -acjc
167 * 1 : size = Size of memory chunk to return.
169 * Returns : Pointer to newly malloc'd memory chunk.
171 *********************************************************************/
172 void *zalloc(int size)
176 if ((ret = (void *)malloc(size)) != NULL)
178 memset(ret, 0, size);
185 /*********************************************************************
187 * Function : hash_string
189 * Description : Take a string and compute a (hopefuly) unique numeric
190 * integer value. This has several uses, but being able
191 * to "switch" a string the one of my favorites.
194 * 1 : s : string to be hashed.
196 * Returns : an unsigned long variable with the hashed value.
198 *********************************************************************/
199 unsigned int hash_string( const char* s )
214 /*********************************************************************
218 * Description : For some reason (which is beyond me), gcc and WIN32
219 * don't like strdup. When a "free" is executed on a
220 * strdup'd ptr, it can at times freez up! So I just
221 * replaced it and problem was solved.
224 * 1 : s = string to duplicate
226 * Returns : Pointer to newly malloc'ed copy of the string.
228 *********************************************************************/
229 char *strdup( const char *s )
231 char * result = (char *)malloc( strlen(s)+1 );
241 #endif /* def __MINGW32__ */
245 /*********************************************************************
247 * Function : safe_strerror
249 * Description : Variant of the library routine strerror() which will
250 * work on systems without the library routine, and
251 * which should never return NULL.
254 * 1 : err = the `errno' of the last operation.
256 * Returns : An "English" string of the last `errno'. Allocated
257 * with strdup(), so caller frees. May be NULL if the
258 * system is out of memory.
260 *********************************************************************/
261 char *safe_strerror(int err)
264 char buf[BUFFER_SIZE];
269 #endif /* HAVE_STRERROR */
273 sprintf(buf, "(errno = %d)", err);
282 /*********************************************************************
284 * Function : strcmpic
286 * Description : Case insensitive string comparison
289 * 1 : s1 = string 1 to compare
290 * 2 : s2 = string 2 to compare
292 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
294 *********************************************************************/
295 int strcmpic(const char *s1, const char *s2)
299 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
305 return(ijb_tolower(*s1) - ijb_tolower(*s2));
310 /*********************************************************************
312 * Function : strncmpic
314 * Description : Case insensitive string comparison (upto n characters)
317 * 1 : s1 = string 1 to compare
318 * 2 : s2 = string 2 to compare
319 * 3 : n = maximum characters to compare
321 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
323 *********************************************************************/
324 int strncmpic(const char *s1, const char *s2, size_t n)
326 if (n <= 0) return(0);
330 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
339 return(ijb_tolower(*s1) - ijb_tolower(*s2));
344 /*********************************************************************
348 * Description : In-situ-eliminate all leading and trailing whitespace
352 * 1 : s : string to be chomped.
354 * Returns : chomped string
356 *********************************************************************/
357 char *chomp(char *string)
362 * strip trailing whitespace
364 p = string + strlen(string);
365 while (p > string && ijb_isspace(*(p-1)))
372 * find end of leading whitespace
375 while (*q && ijb_isspace(*q))
381 * if there was any, move the rest forwards
396 /*********************************************************************
400 * Description : Reallocate "old" and append text to it. This makes
401 * it easier to append to malloc'd strings.
402 * Running out of memory is a FATAL error.
405 * 1 : old = Old text that is to be extended. Will be
406 * free()d by this routine. May be NULL.
407 * 2 : text_to_append = Text to be appended to old.
410 * Returns : Pointer to newly malloc'ed appended string.
411 * If there is no text to append, return old. Caller
414 *********************************************************************/
415 char *strsav(char *old, const char *text_to_append)
417 int old_len, new_len = 0;
420 if (( text_to_append == NULL) || (*text_to_append == '\0'))
427 if ((p = strdup(text_to_append)) == NULL)
429 log_error(LOG_LEVEL_FATAL, "strdup() failed!", new_len);
430 /* Never get here - LOG_LEVEL_FATAL causes program exit */
435 old_len = strlen(old);
436 new_len = old_len + strlen(text_to_append) + 1;
438 if ((p = realloc(old, new_len)) == NULL)
440 log_error(LOG_LEVEL_FATAL, "realloc(%d) bytes failed!", new_len);
441 /* Never get here - LOG_LEVEL_FATAL causes program exit */
444 strcpy(p + old_len, text_to_append);
449 /*********************************************************************
451 * Function : string_append
453 * Description : Reallocate target_string and append text to it.
454 * This makes it easier to append to malloc'd strings.
455 * This is similar to strsav(), but running out of
456 * memory isn't catastrophic.
459 * 1 : target_string = Pointer to old text that is to be
460 * extended. *target_string will be free()d by this
461 * routine. target_string must be non-NULL.
462 * If *target_string is NULL, this routine will
463 * do nothing and return with an error - this allows
464 * you to make many calls to this routine and only
465 * check for errors after the last one.
466 * 2 : text_to_append = Text to be appended to old.
469 * Returns : On success, returns 0 and sets *target_string to
470 * newly malloc'ed appended string. Caller must free().
471 * On out-of-memory, returns nonzero (and free()s
472 * *target_string and sets it to NULL).
474 *********************************************************************/
475 int string_append(char **target_string, const char *text_to_append)
480 assert(target_string);
481 assert(text_to_append);
483 if (*target_string == NULL)
488 if (*text_to_append == '\0')
493 old_len = strlen(*target_string);
495 if (NULL == (new_string = realloc(*target_string,
496 strlen(text_to_append) + old_len + 1)))
498 free(*target_string);
500 *target_string = NULL;
504 strcpy(new_string + old_len, text_to_append);
506 *target_string = new_string;
511 /*********************************************************************
513 * Function : simplematch
515 * Description : String matching, with a (greedy) '*' wildcard that
516 * stands for zero or more arbitrary characters and
517 * character classes in [], which take both enumerations
521 * 1 : pattern = pattern for matching
522 * 2 : text = text to be matched
524 * Returns : 0 if match, else nonzero
526 *********************************************************************/
527 int simplematch(char *pattern, char *text)
529 unsigned char *pat = (unsigned char *) pattern;
530 unsigned char *txt = (unsigned char *) text;
531 unsigned char *fallback = pat;
534 unsigned char lastchar = 'a';
536 unsigned char charmap[32];
542 /* EOF pattern but !EOF text? */
548 /* '*' in the pattern? */
552 /* The pattern ends afterwards? Speed up the return. */
558 /* Else, set wildcard mode and remember position after '*' */
563 /* Character range specification? */
566 memset(charmap, '\0', sizeof(charmap));
568 while (*++pat != ']')
574 else if (*pat == '-')
576 if ((*++pat == ']') || *pat == '\0')
580 for(i = lastchar; i <= *pat; i++)
582 charmap[i / 8] |= (1 << (i % 8));
587 charmap[*pat / 8] |= (1 << (*pat % 8));
591 } /* -END- if Character range specification */
594 /* Compare: Char match, or char range match*/
596 || ((*pat == ']') && (charmap[*txt / 8] & (1 << (*txt % 8)))) )
598 /* Sucess, go ahead */
603 /* In wildcard mode, just try again after failiure */
618 /* Cut off extra '*'s */
619 if(*pat == '*') pat++;
621 /* If this is the pattern's end, fine! */
627 /*********************************************************************
631 * Description : Duplicate the first n characters of a string that may
632 * contain '\0' characters.
635 * 1 : string = string to be duplicated
636 * 2 : n = number of bytes to duplicate
638 * Returns : pointer to copy, or NULL if failiure
640 *********************************************************************/
641 char *bindup(const char *string, int n)
645 if (NULL == (dup = (char *)malloc(n)))
651 memcpy(dup, string, n);
659 /*********************************************************************
661 * Function : make_path
663 * Description : Takes a directory name and a file name, returns
664 * the complete path. Handles windows/unix differences.
665 * If the file name is already an absolute path, or if
666 * the directory name is NULL or empty, it returns
670 * 1 : dir: Name of directory or NULL for none.
671 * 2 : file: Name of file. Should not be NULL or empty.
673 * Returns : "dir/file" (Or on windows, "dir\file").
674 * It allocates the string on the heap. Caller frees.
675 * Returns NULL in error (i.e. NULL file or out of
678 *********************************************************************/
679 char * make_path(const char * dir, const char * file)
686 strncpy(path,dir,512);
691 if(AddPart(path,file,512))
697 #else /* ndef AMIGA */
699 if ((file == NULL) || (*file == '\0'))
701 return NULL; /* Error */
704 if ((dir == NULL) || (*dir == '\0') /* No directory specified */
706 || (*file == '\\') || (file[1] == ':') /* Absolute path (DOS) */
707 #else /* ifndef _WIN32 */
708 || (*file == '/') /* Absolute path (U*ix) */
709 #endif /* ifndef _WIN32 */
716 char * path = malloc(strlen(dir) + strlen(file) + 2);
720 #else /* ifndef _WIN32 */
721 if(path[strlen(path)-1] != '/') strcat(path, "/");
722 #endif /* ifndef _WIN32 */
727 #endif /* ndef AMIGA */