1 const char miscutil_rcs[] = "$Id: miscutil.c,v 1.17 2001/09/13 20:51:29 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.17 2001/09/13 20:51:29 jongfoster
40 * Fixing potential problems with characters >=128 in simplematch()
41 * This was also a compiler warning.
43 * Revision 1.16 2001/09/10 10:56:59 oes
44 * Silenced compiler warnings
46 * Revision 1.15 2001/07/13 14:02:24 oes
47 * Removed vim-settings
49 * Revision 1.14 2001/06/29 21:45:41 oes
50 * Indentation, CRLF->LF, Tab-> Space
52 * Revision 1.13 2001/06/29 13:32:14 oes
53 * Removed logentry from cancelled commit
55 * Revision 1.12 2001/06/09 10:55:28 jongfoster
56 * Changing BUFSIZ ==> BUFFER_SIZE
58 * Revision 1.11 2001/06/07 23:09:19 jongfoster
59 * Cosmetic indentation changes.
61 * Revision 1.10 2001/06/07 14:51:38 joergs
62 * make_path() no longer adds '/' if the dir already ends in '/'.
64 * Revision 1.9 2001/06/07 14:43:17 swa
65 * slight mistake in make_path, unix path style is /.
67 * Revision 1.8 2001/06/05 22:32:01 jongfoster
68 * New function make_path() to splice directory and file names together.
70 * Revision 1.7 2001/06/03 19:12:30 oes
73 * Revision 1.6 2001/06/01 18:14:49 jongfoster
74 * Changing the calls to strerr() to check HAVE_STRERR (which is defined
75 * in config.h if appropriate) rather than the NO_STRERR macro.
77 * Revision 1.5 2001/06/01 10:31:51 oes
78 * Added character class matching to trivimatch; renamed to simplematch
80 * Revision 1.4 2001/05/31 17:32:31 oes
82 * - Enhanced domain part globbing with infix and prefix asterisk
83 * matching and optional unanchored operation
85 * Revision 1.3 2001/05/29 23:10:09 oes
88 * - Introduced chomp()
89 * - Moved strsav() from showargs to miscutil
91 * Revision 1.2 2001/05/29 09:50:24 jongfoster
92 * Unified blocklist/imagelist/permissionslist.
93 * File format is still under discussion, but the internal changes
96 * Also modified interceptor behaviour:
97 * - We now intercept all URLs beginning with one of the following
98 * prefixes (and *only* these prefixes):
100 * * http://ijbswa.sf.net/config/
101 * * http://ijbswa.sourceforge.net/config/
102 * - New interceptors "home page" - go to http://i.j.b/ to see it.
103 * - Internal changes so that intercepted and fast redirect pages
104 * are not replaced with an image.
105 * - Interceptors now have the option to send a binary page direct
106 * to the client. (i.e. ijb-send-banner uses this)
107 * - Implemented show-url-info interceptor. (Which is why I needed
108 * the above interceptors changes - a typical URL is
109 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
110 * The previous mechanism would not have intercepted that, and
111 * if it had been intercepted then it then it would have replaced
114 * Revision 1.1.1.1 2001/05/15 13:59:00 oes
115 * Initial import of version 2.9.3 source tree
118 *********************************************************************/
130 * FIXME: Only need project.h for BUFFER_SIZE. It would be nice
131 * to remove this dependency.
134 #include "miscutil.h"
137 const char miscutil_h_rcs[] = MISCUTIL_H_VERSION;
139 /* Fix a problem with Solaris. There should be no effect on other
141 * Solaris's isspace() is a macro which uses it's argument directly
142 * as an array index. Therefore we need to make sure that high-bit
143 * characters generate +ve values, and ideally we also want to make
144 * the argument match the declared parameter type of "int".
146 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
147 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
149 /*********************************************************************
153 * Description : Malloc some memory and set it to '\0'.
154 * The way calloc() ought to be -acjc
157 * 1 : size = Size of memory chunk to return.
159 * Returns : Pointer to newly malloc'd memory chunk.
161 *********************************************************************/
162 void *zalloc(int size)
166 if ((ret = (void *)malloc(size)) != NULL)
168 memset(ret, 0, size);
175 /*********************************************************************
177 * Function : hash_string
179 * Description : Take a string and compute a (hopefuly) unique numeric
180 * integer value. This has several uses, but being able
181 * to "switch" a string the one of my favorites.
184 * 1 : s : string to be hashed.
186 * Returns : an unsigned long variable with the hashed value.
188 *********************************************************************/
189 unsigned int hash_string( const char* s )
204 /*********************************************************************
208 * Description : For some reason (which is beyond me), gcc and WIN32
209 * don't like strdup. When a "free" is executed on a
210 * strdup'd ptr, it can at times freez up! So I just
211 * replaced it and problem was solved.
214 * 1 : s = string to duplicate
216 * Returns : Pointer to newly malloc'ed copy of the string.
218 *********************************************************************/
219 char *strdup( const char *s )
221 char * result = (char *)malloc( strlen(s)+1 );
231 #endif /* def __MINGW32__ */
235 /*********************************************************************
237 * Function : safe_strerror
239 * Description : Variant of the library routine strerror() which will
240 * work on systems without the library routine, and
241 * which should never return NULL.
244 * 1 : err = the `errno' of the last operation.
246 * Returns : An "English" string of the last `errno'. Allocated
247 * with strdup(), so caller frees. May be NULL if the
248 * system is out of memory.
250 *********************************************************************/
251 char *safe_strerror(int err)
254 char buf[BUFFER_SIZE];
259 #endif /* HAVE_STRERROR */
263 sprintf(buf, "(errno = %d)", err);
272 /*********************************************************************
274 * Function : strcmpic
276 * Description : Case insensitive string comparison
279 * 1 : s1 = string 1 to compare
280 * 2 : s2 = string 2 to compare
282 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
284 *********************************************************************/
285 int strcmpic(const char *s1, const char *s2)
289 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
295 return(ijb_tolower(*s1) - ijb_tolower(*s2));
300 /*********************************************************************
302 * Function : strncmpic
304 * Description : Case insensitive string comparison (upto n characters)
307 * 1 : s1 = string 1 to compare
308 * 2 : s2 = string 2 to compare
309 * 3 : n = maximum characters to compare
311 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
313 *********************************************************************/
314 int strncmpic(const char *s1, const char *s2, size_t n)
316 if (n <= 0) return(0);
320 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
329 return(ijb_tolower(*s1) - ijb_tolower(*s2));
334 /*********************************************************************
338 * Description : In-situ-eliminate all leading and trailing whitespace
342 * 1 : s : string to be chomped.
344 * Returns : chomped string
346 *********************************************************************/
347 char *chomp(char *string)
352 * strip trailing whitespace
354 p = string + strlen(string);
355 while (p > string && ijb_isspace(*(p-1)))
362 * find end of leading whitespace
365 while (*q && ijb_isspace(*q))
371 * if there was any, move the rest forwards
385 /*********************************************************************
389 * Description : Reallocate "old" and append text to it. This makes
390 * it easier to append to malloc'd strings.
393 * 1 : old = Old text that is to be extended. Will be
394 * free()d by this routine.
395 * 2 : text_to_append = Text to be appended to old.
397 * Returns : Pointer to newly malloc'ed appended string.
398 * If there is no text to append, return old. Caller
401 *********************************************************************/
402 char *strsav(char *old, const char *text_to_append)
404 int old_len, new_len;
407 if (( text_to_append == NULL) || (*text_to_append == '\0'))
414 old_len = strlen(old);
421 new_len = old_len + strlen(text_to_append) + 1;
425 if ((p = realloc(old, new_len)) == NULL)
427 log_error(LOG_LEVEL_FATAL, "realloc(%d) bytes failed!", new_len);
428 /* Never get here - LOG_LEVEL_FATAL causes program exit */
433 if ((p = (char *)malloc(new_len)) == NULL)
435 log_error(LOG_LEVEL_FATAL, "malloc(%d) bytes failed!", new_len);
436 /* Never get here - LOG_LEVEL_FATAL causes program exit */
440 strcpy(p + old_len, text_to_append);
446 /*********************************************************************
448 * Function : simplematch
450 * Description : String matching, with a (greedy) '*' wildcard that
451 * stands for zero or more arbitrary characters and
452 * character classes in [], which take both enumerations
456 * 1 : pattern = pattern for matching
457 * 2 : text = text to be matched
459 * Returns : 0 if match, else nonzero
461 *********************************************************************/
462 int simplematch(char *pattern, char *text)
464 unsigned char *pat = (unsigned char *) pattern;
465 unsigned char *txt = (unsigned char *) text;
466 unsigned char *fallback = pat;
469 unsigned char lastchar = 'a';
471 unsigned char charmap[32];
477 /* EOF pattern but !EOF text? */
483 /* '*' in the pattern? */
487 /* The pattern ends afterwards? Speed up the return. */
493 /* Else, set wildcard mode and remember position after '*' */
498 /* Character range specification? */
501 memset(charmap, '\0', sizeof(charmap));
503 while (*++pat != ']')
509 else if (*pat == '-')
511 if ((*++pat == ']') || *pat == '\0')
515 for(i = lastchar; i <= *pat; i++)
517 charmap[i / 8] |= (1 << (i % 8));
522 charmap[*pat / 8] |= (1 << (*pat % 8));
526 } /* -END- if Character range specification */
529 /* Compare: Char match, or char range match*/
531 || ((*pat == ']') && (charmap[*txt / 8] & (1 << (*txt % 8)))) )
533 /* Sucess, go ahead */
538 /* In wildcard mode, just try again after failiure */
553 /* Cut off extra '*'s */
554 if(*pat == '*') pat++;
556 /* If this is the pattern's end, fine! */
562 /*********************************************************************
566 * Description : Duplicate the first n characters of a string that may
567 * contain '\0' characters.
570 * 1 : string = string to be duplicated
571 * 2 : n = number of bytes to duplicate
573 * Returns : pointer to copy, or NULL if failiure
575 *********************************************************************/
576 char *bindup(const char *string, int n)
580 if (NULL == (dup = (char *)malloc(n)))
586 memcpy(dup, string, n);
594 /*********************************************************************
596 * Function : make_path
598 * Description : Takes a directory name and a file name, returns
599 * the complete path. Handles windows/unix differences.
600 * If the file name is already an absolute path, or if
601 * the directory name is NULL or empty, it returns
605 * 1 : dir: Name of directory or NULL for none.
606 * 2 : file: Name of file. Should not be NULL or empty.
608 * Returns : "dir/file" (Or on windows, "dir\file").
609 * It allocates the string on the heap. Caller frees.
610 * Returns NULL in error (i.e. NULL file or out of
613 *********************************************************************/
614 char * make_path(const char * dir, const char * file)
621 strncpy(path,dir,512);
626 if(AddPart(path,file,512))
632 #else /* ndef AMIGA */
634 if ((file == NULL) || (*file == '\0'))
636 return NULL; /* Error */
639 if ((dir == NULL) || (*dir == '\0') /* No directory specified */
641 || (*file == '\\') || (file[1] == ':') /* Absolute path (DOS) */
642 #else /* ifndef _WIN32 */
643 || (*file == '/') /* Absolute path (U*ix) */
644 #endif /* ifndef _WIN32 */
651 char * path = malloc(strlen(dir) + strlen(file) + 2);
655 #else /* ifndef _WIN32 */
656 if(path[strlen(path)-1] != '/') strcat(path, "/");
657 #endif /* ifndef _WIN32 */
662 #endif /* ndef AMIGA */