2 const char miscutil_rcs[] = "$Id: miscutil.c,v 1.13 2001/06/29 13:32:14 oes Exp $";
3 /*********************************************************************
5 * File : $Source: /cvsroot/ijbswa/current/miscutil.c,v $
7 * Purpose : zalloc, hash_string, safe_strerror, strcmpic,
8 * strncmpic, strsav, chomp, and MinGW32 strdup
10 * These are each too small to deserve their own file
11 * but don't really fit in any other file.
13 * Copyright : Written by and Copyright (C) 2001 the SourceForge
14 * IJBSWA team. http://ijbswa.sourceforge.net
16 * Based on the Internet Junkbuster originally written
17 * by and Copyright (C) 1997 Anonymous Coders and
18 * Junkbusters Corporation. http://www.junkbusters.com
20 * This program is free software; you can redistribute it
21 * and/or modify it under the terms of the GNU General
22 * Public License as published by the Free Software
23 * Foundation; either version 2 of the License, or (at
24 * your option) any later version.
26 * This program is distributed in the hope that it will
27 * be useful, but WITHOUT ANY WARRANTY; without even the
28 * implied warranty of MERCHANTABILITY or FITNESS FOR A
29 * PARTICULAR PURPOSE. See the GNU General Public
30 * License for more details.
32 * The GNU General Public License should be included with
33 * this file. If not, you can view it at
34 * http://www.gnu.org/copyleft/gpl.html
35 * or write to the Free Software Foundation, Inc., 59
36 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
39 * $Log: miscutil.c,v $
40 * Revision 1.13 2001/06/29 13:32:14 oes
41 * Removed logentry from cancelled commit
43 * Revision 1.12 2001/06/09 10:55:28 jongfoster
44 * Changing BUFSIZ ==> BUFFER_SIZE
46 * Revision 1.11 2001/06/07 23:09:19 jongfoster
47 * Cosmetic indentation changes.
49 * Revision 1.10 2001/06/07 14:51:38 joergs
50 * make_path() no longer adds '/' if the dir already ends in '/'.
52 * Revision 1.9 2001/06/07 14:43:17 swa
53 * slight mistake in make_path, unix path style is /.
55 * Revision 1.8 2001/06/05 22:32:01 jongfoster
56 * New function make_path() to splice directory and file names together.
58 * Revision 1.7 2001/06/03 19:12:30 oes
61 * Revision 1.6 2001/06/01 18:14:49 jongfoster
62 * Changing the calls to strerr() to check HAVE_STRERR (which is defined
63 * in config.h if appropriate) rather than the NO_STRERR macro.
65 * Revision 1.5 2001/06/01 10:31:51 oes
66 * Added character class matching to trivimatch; renamed to simplematch
68 * Revision 1.4 2001/05/31 17:32:31 oes
70 * - Enhanced domain part globbing with infix and prefix asterisk
71 * matching and optional unanchored operation
73 * Revision 1.3 2001/05/29 23:10:09 oes
76 * - Introduced chomp()
77 * - Moved strsav() from showargs to miscutil
79 * Revision 1.2 2001/05/29 09:50:24 jongfoster
80 * Unified blocklist/imagelist/permissionslist.
81 * File format is still under discussion, but the internal changes
84 * Also modified interceptor behaviour:
85 * - We now intercept all URLs beginning with one of the following
86 * prefixes (and *only* these prefixes):
88 * * http://ijbswa.sf.net/config/
89 * * http://ijbswa.sourceforge.net/config/
90 * - New interceptors "home page" - go to http://i.j.b/ to see it.
91 * - Internal changes so that intercepted and fast redirect pages
92 * are not replaced with an image.
93 * - Interceptors now have the option to send a binary page direct
94 * to the client. (i.e. ijb-send-banner uses this)
95 * - Implemented show-url-info interceptor. (Which is why I needed
96 * the above interceptors changes - a typical URL is
97 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
98 * The previous mechanism would not have intercepted that, and
99 * if it had been intercepted then it then it would have replaced
102 * Revision 1.1.1.1 2001/05/15 13:59:00 oes
103 * Initial import of version 2.9.3 source tree
106 *********************************************************************/
118 * FIXME: Only need project.h for BUFFER_SIZE. It would be nice
119 * to remove this dependency.
122 #include "miscutil.h"
125 const char miscutil_h_rcs[] = MISCUTIL_H_VERSION;
127 /* Fix a problem with Solaris. There should be no effect on other
129 * Solaris's isspace() is a macro which uses it's argument directly
130 * as an array index. Therefore we need to make sure that high-bit
131 * characters generate +ve values, and ideally we also want to make
132 * the argument match the declared parameter type of "int".
134 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
135 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
137 /*********************************************************************
141 * Description : Malloc some memory and set it to '\0'.
142 * The way calloc() ought to be -acjc
145 * 1 : size = Size of memory chunk to return.
147 * Returns : Pointer to newly malloc'd memory chunk.
149 *********************************************************************/
150 void *zalloc(int size)
154 if ((ret = (void *)malloc(size)) != NULL)
156 memset(ret, 0, size);
163 /*********************************************************************
165 * Function : hash_string
167 * Description : Take a string and compute a (hopefuly) unique numeric
168 * integer value. This has several uses, but being able
169 * to "switch" a string the one of my favorites.
172 * 1 : s : string to be hashed.
174 * Returns : an unsigned long variable with the hashed value.
176 *********************************************************************/
177 unsigned long hash_string( const char* s )
179 unsigned long h = 0ul;
192 /*********************************************************************
196 * Description : For some reason (which is beyond me), gcc and WIN32
197 * don't like strdup. When a "free" is executed on a
198 * strdup'd ptr, it can at times freez up! So I just
199 * replaced it and problem was solved.
202 * 1 : s = string to duplicate
204 * Returns : Pointer to newly malloc'ed copy of the string.
206 *********************************************************************/
207 char *strdup( const char *s )
209 char * result = (char *)malloc( strlen(s)+1 );
219 #endif /* def __MINGW32__ */
223 /*********************************************************************
225 * Function : safe_strerror
227 * Description : Variant of the library routine strerror() which will
228 * work on systems without the library routine, and
229 * which should never return NULL.
232 * 1 : err = the `errno' of the last operation.
234 * Returns : An "English" string of the last `errno'. Allocated
235 * with strdup(), so caller frees. May be NULL if the
236 * system is out of memory.
238 *********************************************************************/
239 char *safe_strerror(int err)
242 char buf[BUFFER_SIZE];
247 #endif /* HAVE_STRERROR */
251 sprintf(buf, "(errno = %d)", err);
260 /*********************************************************************
262 * Function : strcmpic
264 * Description : Case insensitive string comparison
267 * 1 : s1 = string 1 to compare
268 * 2 : s2 = string 2 to compare
270 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
272 *********************************************************************/
273 int strcmpic(const char *s1, const char *s2)
277 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
283 return(ijb_tolower(*s1) - ijb_tolower(*s2));
288 /*********************************************************************
290 * Function : strncmpic
292 * Description : Case insensitive string comparison (upto n characters)
295 * 1 : s1 = string 1 to compare
296 * 2 : s2 = string 2 to compare
297 * 3 : n = maximum characters to compare
299 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
301 *********************************************************************/
302 int strncmpic(const char *s1, const char *s2, size_t n)
304 if (n <= 0) return(0);
308 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
317 return(ijb_tolower(*s1) - ijb_tolower(*s2));
322 /*********************************************************************
326 * Description : In-situ-eliminate all leading and trailing whitespace
330 * 1 : s : string to be chomped.
332 * Returns : chomped string
334 *********************************************************************/
335 char *chomp(char *string)
340 * strip trailing whitespace
342 p = string + strlen(string);
343 while (p > string && ijb_isspace(*(p-1)))
350 * find end of leading whitespace
353 while (*q && ijb_isspace(*q))
359 * if there was any, move the rest forwards
373 /*********************************************************************
377 * Description : Reallocate "old" and append text to it. This makes
378 * it easier to append to malloc'd strings.
381 * 1 : old = Old text that is to be extended. Will be
382 * free()d by this routine.
383 * 2 : text_to_append = Text to be appended to old.
385 * Returns : Pointer to newly malloc'ed appended string.
386 * If there is no text to append, return old. Caller
389 *********************************************************************/
390 char *strsav(char *old, const char *text_to_append)
392 int old_len, new_len;
395 if (( text_to_append == NULL) || (*text_to_append == '\0'))
402 old_len = strlen(old);
409 new_len = old_len + strlen(text_to_append) + 1;
413 if ((p = realloc(old, new_len)) == NULL)
415 log_error(LOG_LEVEL_FATAL, "realloc(%d) bytes failed!", new_len);
416 /* Never get here - LOG_LEVEL_FATAL causes program exit */
421 if ((p = (char *)malloc(new_len)) == NULL)
423 log_error(LOG_LEVEL_FATAL, "malloc(%d) bytes failed!", new_len);
424 /* Never get here - LOG_LEVEL_FATAL causes program exit */
428 strcpy(p + old_len, text_to_append);
434 /*********************************************************************
436 * Function : simplematch
438 * Description : String matching, with a (greedy) '*' wildcard that
439 * stands for zero or more arbitrary characters and
440 * character classes in [], which take both enumerations
444 * 1 : pattern = pattern for matching
445 * 2 : text = text to be matched
447 * Returns : 0 if match, else nonzero
449 *********************************************************************/
450 int simplematch(char *pattern, char *text)
459 unsigned char charmap[32];
465 /* EOF pattern but !EOF text? */
471 /* '*' in the pattern? */
475 /* The pattern ends afterwards? Speed up the return. */
481 /* Else, set wildcard mode and remember position after '*' */
486 /* Character range specification? */
489 memset(charmap, '\0', sizeof(charmap));
491 while (*++pat != ']')
497 else if (*pat == '-')
499 if ((*++pat == ']') || *pat == '\0')
503 for(i = lastchar; i <= *pat; i++)
505 charmap[i / 8] |= (1 << (i % 8));
510 charmap[*pat / 8] |= (1 << (*pat % 8));
514 } /* -END- if Character range specification */
517 /* Compare: Char match, or char range match*/
519 || ((*pat == ']') && (charmap[*txt / 8] & (1 << (*txt % 8)))) )
521 /* Sucess, go ahead */
526 /* In wildcard mode, just try again after failiure */
541 /* Cut off extra '*'s */
542 if(*pat == '*') pat++;
544 /* If this is the pattern's end, fine! */
550 /*********************************************************************
554 * Description : Duplicate the first n characters of a string that may
555 * contain '\0' characters.
558 * 1 : string = string to be duplicated
559 * 2 : n = number of bytes to duplicate
561 * Returns : pointer to copy, or NULL if failiure
563 *********************************************************************/
564 char *bindup(const char *string, int n)
568 if (NULL == (dup = (char *)malloc(n)))
574 memcpy(dup, string, n);
582 /*********************************************************************
584 * Function : make_path
586 * Description : Takes a directory name and a file name, returns
587 * the complete path. Handles windows/unix differences.
588 * If the file name is already an absolute path, or if
589 * the directory name is NULL or empty, it returns
593 * 1 : dir: Name of directory or NULL for none.
594 * 2 : file: Name of file. Should not be NULL or empty.
596 * Returns : "dir/file" (Or on windows, "dir\file").
597 * It allocates the string on the heap. Caller frees.
598 * Returns NULL in error (i.e. NULL file or out of
601 *********************************************************************/
602 char * make_path(const char * dir, const char * file)
609 strncpy(path,dir,512);
614 if(AddPart(path,file,512))
620 #else /* ndef AMIGA */
622 if ((file == NULL) || (*file == '\0'))
624 return NULL; /* Error */
627 if ((dir == NULL) || (*dir == '\0') /* No directory specified */
629 || (*file == '\\') || (file[1] == ':') /* Absolute path (DOS) */
630 #else /* ifndef _WIN32 */
631 || (*file == '/') /* Absolute path (U*ix) */
632 #endif /* ifndef _WIN32 */
639 char * path = malloc(strlen(dir) + strlen(file) + 2);
643 #else /* ifndef _WIN32 */
644 if(path[strlen(path)-1] != '/') strcat(path, "/");
645 #endif /* ifndef _WIN32 */
650 #endif /* ndef AMIGA */