1 const char miscutil_rcs[] = "$Id: miscutil.c,v 1.15 2001/07/13 14:02:24 oes 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.15 2001/07/13 14:02:24 oes
40 * Removed vim-settings
42 * Revision 1.14 2001/06/29 21:45:41 oes
43 * Indentation, CRLF->LF, Tab-> Space
45 * Revision 1.13 2001/06/29 13:32:14 oes
46 * Removed logentry from cancelled commit
48 * Revision 1.12 2001/06/09 10:55:28 jongfoster
49 * Changing BUFSIZ ==> BUFFER_SIZE
51 * Revision 1.11 2001/06/07 23:09:19 jongfoster
52 * Cosmetic indentation changes.
54 * Revision 1.10 2001/06/07 14:51:38 joergs
55 * make_path() no longer adds '/' if the dir already ends in '/'.
57 * Revision 1.9 2001/06/07 14:43:17 swa
58 * slight mistake in make_path, unix path style is /.
60 * Revision 1.8 2001/06/05 22:32:01 jongfoster
61 * New function make_path() to splice directory and file names together.
63 * Revision 1.7 2001/06/03 19:12:30 oes
66 * Revision 1.6 2001/06/01 18:14:49 jongfoster
67 * Changing the calls to strerr() to check HAVE_STRERR (which is defined
68 * in config.h if appropriate) rather than the NO_STRERR macro.
70 * Revision 1.5 2001/06/01 10:31:51 oes
71 * Added character class matching to trivimatch; renamed to simplematch
73 * Revision 1.4 2001/05/31 17:32:31 oes
75 * - Enhanced domain part globbing with infix and prefix asterisk
76 * matching and optional unanchored operation
78 * Revision 1.3 2001/05/29 23:10:09 oes
81 * - Introduced chomp()
82 * - Moved strsav() from showargs to miscutil
84 * Revision 1.2 2001/05/29 09:50:24 jongfoster
85 * Unified blocklist/imagelist/permissionslist.
86 * File format is still under discussion, but the internal changes
89 * Also modified interceptor behaviour:
90 * - We now intercept all URLs beginning with one of the following
91 * prefixes (and *only* these prefixes):
93 * * http://ijbswa.sf.net/config/
94 * * http://ijbswa.sourceforge.net/config/
95 * - New interceptors "home page" - go to http://i.j.b/ to see it.
96 * - Internal changes so that intercepted and fast redirect pages
97 * are not replaced with an image.
98 * - Interceptors now have the option to send a binary page direct
99 * to the client. (i.e. ijb-send-banner uses this)
100 * - Implemented show-url-info interceptor. (Which is why I needed
101 * the above interceptors changes - a typical URL is
102 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
103 * The previous mechanism would not have intercepted that, and
104 * if it had been intercepted then it then it would have replaced
107 * Revision 1.1.1.1 2001/05/15 13:59:00 oes
108 * Initial import of version 2.9.3 source tree
111 *********************************************************************/
123 * FIXME: Only need project.h for BUFFER_SIZE. It would be nice
124 * to remove this dependency.
127 #include "miscutil.h"
130 const char miscutil_h_rcs[] = MISCUTIL_H_VERSION;
132 /* Fix a problem with Solaris. There should be no effect on other
134 * Solaris's isspace() is a macro which uses it's argument directly
135 * as an array index. Therefore we need to make sure that high-bit
136 * characters generate +ve values, and ideally we also want to make
137 * the argument match the declared parameter type of "int".
139 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
140 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
142 /*********************************************************************
146 * Description : Malloc some memory and set it to '\0'.
147 * The way calloc() ought to be -acjc
150 * 1 : size = Size of memory chunk to return.
152 * Returns : Pointer to newly malloc'd memory chunk.
154 *********************************************************************/
155 void *zalloc(int size)
159 if ((ret = (void *)malloc(size)) != NULL)
161 memset(ret, 0, size);
168 /*********************************************************************
170 * Function : hash_string
172 * Description : Take a string and compute a (hopefuly) unique numeric
173 * integer value. This has several uses, but being able
174 * to "switch" a string the one of my favorites.
177 * 1 : s : string to be hashed.
179 * Returns : an unsigned long variable with the hashed value.
181 *********************************************************************/
182 unsigned long hash_string( const char* s )
184 unsigned long h = 0ul;
197 /*********************************************************************
201 * Description : For some reason (which is beyond me), gcc and WIN32
202 * don't like strdup. When a "free" is executed on a
203 * strdup'd ptr, it can at times freez up! So I just
204 * replaced it and problem was solved.
207 * 1 : s = string to duplicate
209 * Returns : Pointer to newly malloc'ed copy of the string.
211 *********************************************************************/
212 char *strdup( const char *s )
214 char * result = (char *)malloc( strlen(s)+1 );
224 #endif /* def __MINGW32__ */
228 /*********************************************************************
230 * Function : safe_strerror
232 * Description : Variant of the library routine strerror() which will
233 * work on systems without the library routine, and
234 * which should never return NULL.
237 * 1 : err = the `errno' of the last operation.
239 * Returns : An "English" string of the last `errno'. Allocated
240 * with strdup(), so caller frees. May be NULL if the
241 * system is out of memory.
243 *********************************************************************/
244 char *safe_strerror(int err)
247 char buf[BUFFER_SIZE];
252 #endif /* HAVE_STRERROR */
256 sprintf(buf, "(errno = %d)", err);
265 /*********************************************************************
267 * Function : strcmpic
269 * Description : Case insensitive string comparison
272 * 1 : s1 = string 1 to compare
273 * 2 : s2 = string 2 to compare
275 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
277 *********************************************************************/
278 int strcmpic(const char *s1, const char *s2)
282 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
288 return(ijb_tolower(*s1) - ijb_tolower(*s2));
293 /*********************************************************************
295 * Function : strncmpic
297 * Description : Case insensitive string comparison (upto n characters)
300 * 1 : s1 = string 1 to compare
301 * 2 : s2 = string 2 to compare
302 * 3 : n = maximum characters to compare
304 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
306 *********************************************************************/
307 int strncmpic(const char *s1, const char *s2, size_t n)
309 if (n <= 0) return(0);
313 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
322 return(ijb_tolower(*s1) - ijb_tolower(*s2));
327 /*********************************************************************
331 * Description : In-situ-eliminate all leading and trailing whitespace
335 * 1 : s : string to be chomped.
337 * Returns : chomped string
339 *********************************************************************/
340 char *chomp(char *string)
345 * strip trailing whitespace
347 p = string + strlen(string);
348 while (p > string && ijb_isspace(*(p-1)))
355 * find end of leading whitespace
358 while (*q && ijb_isspace(*q))
364 * if there was any, move the rest forwards
378 /*********************************************************************
382 * Description : Reallocate "old" and append text to it. This makes
383 * it easier to append to malloc'd strings.
386 * 1 : old = Old text that is to be extended. Will be
387 * free()d by this routine.
388 * 2 : text_to_append = Text to be appended to old.
390 * Returns : Pointer to newly malloc'ed appended string.
391 * If there is no text to append, return old. Caller
394 *********************************************************************/
395 char *strsav(char *old, const char *text_to_append)
397 int old_len, new_len;
400 if (( text_to_append == NULL) || (*text_to_append == '\0'))
407 old_len = strlen(old);
414 new_len = old_len + strlen(text_to_append) + 1;
418 if ((p = realloc(old, new_len)) == NULL)
420 log_error(LOG_LEVEL_FATAL, "realloc(%d) bytes failed!", new_len);
421 /* Never get here - LOG_LEVEL_FATAL causes program exit */
426 if ((p = (char *)malloc(new_len)) == NULL)
428 log_error(LOG_LEVEL_FATAL, "malloc(%d) bytes failed!", new_len);
429 /* Never get here - LOG_LEVEL_FATAL causes program exit */
433 strcpy(p + old_len, text_to_append);
439 /*********************************************************************
441 * Function : simplematch
443 * Description : String matching, with a (greedy) '*' wildcard that
444 * stands for zero or more arbitrary characters and
445 * character classes in [], which take both enumerations
449 * 1 : pattern = pattern for matching
450 * 2 : text = text to be matched
452 * Returns : 0 if match, else nonzero
454 *********************************************************************/
455 int simplematch(char *pattern, char *text)
457 char *fallback = pattern;
464 unsigned char charmap[32];
470 /* EOF pattern but !EOF text? */
476 /* '*' in the pattern? */
480 /* The pattern ends afterwards? Speed up the return. */
486 /* Else, set wildcard mode and remember position after '*' */
491 /* Character range specification? */
494 memset(charmap, '\0', sizeof(charmap));
496 while (*++pat != ']')
502 else if (*pat == '-')
504 if ((*++pat == ']') || *pat == '\0')
508 for(i = lastchar; i <= *pat; i++)
510 charmap[i / 8] |= (1 << (i % 8));
515 charmap[*pat / 8] |= (1 << (*pat % 8));
519 } /* -END- if Character range specification */
522 /* Compare: Char match, or char range match*/
524 || ((*pat == ']') && (charmap[*txt / 8] & (1 << (*txt % 8)))) )
526 /* Sucess, go ahead */
531 /* In wildcard mode, just try again after failiure */
546 /* Cut off extra '*'s */
547 if(*pat == '*') pat++;
549 /* If this is the pattern's end, fine! */
555 /*********************************************************************
559 * Description : Duplicate the first n characters of a string that may
560 * contain '\0' characters.
563 * 1 : string = string to be duplicated
564 * 2 : n = number of bytes to duplicate
566 * Returns : pointer to copy, or NULL if failiure
568 *********************************************************************/
569 char *bindup(const char *string, int n)
573 if (NULL == (dup = (char *)malloc(n)))
579 memcpy(dup, string, n);
587 /*********************************************************************
589 * Function : make_path
591 * Description : Takes a directory name and a file name, returns
592 * the complete path. Handles windows/unix differences.
593 * If the file name is already an absolute path, or if
594 * the directory name is NULL or empty, it returns
598 * 1 : dir: Name of directory or NULL for none.
599 * 2 : file: Name of file. Should not be NULL or empty.
601 * Returns : "dir/file" (Or on windows, "dir\file").
602 * It allocates the string on the heap. Caller frees.
603 * Returns NULL in error (i.e. NULL file or out of
606 *********************************************************************/
607 char * make_path(const char * dir, const char * file)
614 strncpy(path,dir,512);
619 if(AddPart(path,file,512))
625 #else /* ndef AMIGA */
627 if ((file == NULL) || (*file == '\0'))
629 return NULL; /* Error */
632 if ((dir == NULL) || (*dir == '\0') /* No directory specified */
634 || (*file == '\\') || (file[1] == ':') /* Absolute path (DOS) */
635 #else /* ifndef _WIN32 */
636 || (*file == '/') /* Absolute path (U*ix) */
637 #endif /* ifndef _WIN32 */
644 char * path = malloc(strlen(dir) + strlen(file) + 2);
648 #else /* ifndef _WIN32 */
649 if(path[strlen(path)-1] != '/') strcat(path, "/");
650 #endif /* ifndef _WIN32 */
655 #endif /* ndef AMIGA */