2 const char miscutil_rcs[] = "$Id: miscutil.c,v 1.12 2001/06/09 10:55:28 jongfoster 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.12 2001/06/09 10:55:28 jongfoster
41 * Changing BUFSIZ ==> BUFFER_SIZE
43 * Revision 1.11 2001/06/07 23:09:19 jongfoster
44 * Cosmetic indentation changes.
46 * Revision 1.10 2001/06/07 14:51:38 joergs
47 * make_path() no longer adds '/' if the dir already ends in '/'.
49 * Revision 1.9 2001/06/07 14:43:17 swa
50 * slight mistake in make_path, unix path style is /.
52 * Revision 1.8 2001/06/05 22:32:01 jongfoster
53 * New function make_path() to splice directory and file names together.
55 * Revision 1.7 2001/06/03 19:12:30 oes
58 * Revision 1.6 2001/06/01 18:14:49 jongfoster
59 * Changing the calls to strerr() to check HAVE_STRERR (which is defined
60 * in config.h if appropriate) rather than the NO_STRERR macro.
62 * Revision 1.5 2001/06/01 10:31:51 oes
63 * Added character class matching to trivimatch; renamed to simplematch
65 * Revision 1.4 2001/05/31 17:32:31 oes
67 * - Enhanced domain part globbing with infix and prefix asterisk
68 * matching and optional unanchored operation
70 * Revision 1.3 2001/05/29 23:10:09 oes
73 * - Introduced chomp()
74 * - Moved strsav() from showargs to miscutil
76 * Revision 1.2 2001/05/29 09:50:24 jongfoster
77 * Unified blocklist/imagelist/permissionslist.
78 * File format is still under discussion, but the internal changes
81 * Also modified interceptor behaviour:
82 * - We now intercept all URLs beginning with one of the following
83 * prefixes (and *only* these prefixes):
85 * * http://ijbswa.sf.net/config/
86 * * http://ijbswa.sourceforge.net/config/
87 * - New interceptors "home page" - go to http://i.j.b/ to see it.
88 * - Internal changes so that intercepted and fast redirect pages
89 * are not replaced with an image.
90 * - Interceptors now have the option to send a binary page direct
91 * to the client. (i.e. ijb-send-banner uses this)
92 * - Implemented show-url-info interceptor. (Which is why I needed
93 * the above interceptors changes - a typical URL is
94 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
95 * The previous mechanism would not have intercepted that, and
96 * if it had been intercepted then it then it would have replaced
99 * Revision 1.1.1.1 2001/05/15 13:59:00 oes
100 * Initial import of version 2.9.3 source tree
103 *********************************************************************/
115 * FIXME: Only need project.h for BUFFER_SIZE. It would be nice
\r
116 * to remove this dependency.
\r
118 #include "project.h"
\r
119 #include "miscutil.h"
\r
122 const char miscutil_h_rcs[] = MISCUTIL_H_VERSION;
124 /* Fix a problem with Solaris. There should be no effect on other
126 * Solaris's isspace() is a macro which uses it's argument directly
127 * as an array index. Therefore we need to make sure that high-bit
128 * characters generate +ve values, and ideally we also want to make
129 * the argument match the declared parameter type of "int".
131 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
132 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
134 /*********************************************************************
138 * Description : Malloc some memory and set it to '\0'.
139 * The way calloc() ought to be -acjc
142 * 1 : size = Size of memory chunk to return.
144 * Returns : Pointer to newly malloc'd memory chunk.
146 *********************************************************************/
147 void *zalloc(int size)
151 if ((ret = (void *)malloc(size)) != NULL)
153 memset(ret, 0, size);
160 /*********************************************************************
162 * Function : hash_string
164 * Description : Take a string and compute a (hopefuly) unique numeric
165 * integer value. This has several uses, but being able
166 * to "switch" a string the one of my favorites.
169 * 1 : s : string to be hashed.
171 * Returns : an unsigned long variable with the hashed value.
173 *********************************************************************/
174 unsigned long hash_string( const char* s )
176 unsigned long h = 0ul;
189 /*********************************************************************
193 * Description : For some reason (which is beyond me), gcc and WIN32
194 * don't like strdup. When a "free" is executed on a
195 * strdup'd ptr, it can at times freez up! So I just
196 * replaced it and problem was solved.
199 * 1 : s = string to duplicate
201 * Returns : Pointer to newly malloc'ed copy of the string.
203 *********************************************************************/
204 char *strdup( const char *s )
206 char * result = (char *)malloc( strlen(s)+1 );
216 #endif /* def __MINGW32__ */
220 /*********************************************************************
222 * Function : safe_strerror
224 * Description : Variant of the library routine strerror() which will
225 * work on systems without the library routine, and
226 * which should never return NULL.
229 * 1 : err = the `errno' of the last operation.
231 * Returns : An "English" string of the last `errno'. Allocated
232 * with strdup(), so caller frees. May be NULL if the
233 * system is out of memory.
235 *********************************************************************/
236 char *safe_strerror(int err)
239 char buf[BUFFER_SIZE];
244 #endif /* HAVE_STRERROR */
248 sprintf(buf, "(errno = %d)", err);
257 /*********************************************************************
259 * Function : strcmpic
261 * Description : Case insensitive string comparison
264 * 1 : s1 = string 1 to compare
265 * 2 : s2 = string 2 to compare
267 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
269 *********************************************************************/
270 int strcmpic(const char *s1, const char *s2)
274 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
280 return(ijb_tolower(*s1) - ijb_tolower(*s2));
285 /*********************************************************************
287 * Function : strncmpic
289 * Description : Case insensitive string comparison (upto n characters)
292 * 1 : s1 = string 1 to compare
293 * 2 : s2 = string 2 to compare
294 * 3 : n = maximum characters to compare
296 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
298 *********************************************************************/
299 int strncmpic(const char *s1, const char *s2, size_t n)
301 if (n <= 0) return(0);
305 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
314 return(ijb_tolower(*s1) - ijb_tolower(*s2));
319 /*********************************************************************
323 * Description : In-situ-eliminate all leading and trailing whitespace
327 * 1 : s : string to be chomped.
329 * Returns : chomped string
331 *********************************************************************/
332 char *chomp(char *string)
337 * strip trailing whitespace
339 p = string + strlen(string);
340 while (p > string && ijb_isspace(*(p-1)))
347 * find end of leading whitespace
350 while (*q && ijb_isspace(*q))
356 * if there was any, move the rest forwards
370 /*********************************************************************
374 * Description : Reallocate "old" and append text to it. This makes
375 * it easier to append to malloc'd strings.
378 * 1 : old = Old text that is to be extended. Will be
379 * free()d by this routine.
380 * 2 : text_to_append = Text to be appended to old.
382 * Returns : Pointer to newly malloc'ed appended string.
383 * If there is no text to append, return old. Caller
386 *********************************************************************/
387 char *strsav(char *old, const char *text_to_append)
389 int old_len, new_len;
392 if (( text_to_append == NULL) || (*text_to_append == '\0'))
399 old_len = strlen(old);
406 new_len = old_len + strlen(text_to_append) + 1;
410 if ((p = realloc(old, new_len)) == NULL)
412 log_error(LOG_LEVEL_FATAL, "realloc(%d) bytes failed!", new_len);
413 /* Never get here - LOG_LEVEL_FATAL causes program exit */
418 if ((p = (char *)malloc(new_len)) == NULL)
420 log_error(LOG_LEVEL_FATAL, "malloc(%d) bytes failed!", new_len);
421 /* Never get here - LOG_LEVEL_FATAL causes program exit */
425 strcpy(p + old_len, text_to_append);
431 /*********************************************************************
433 * Function : simplematch
435 * Description : String matching, with a (greedy) '*' wildcard that
436 * stands for zero or more arbitrary characters and
437 * character classes in [], which take both enumerations
441 * 1 : pattern = pattern for matching
442 * 2 : text = text to be matched
444 * Returns : 0 if match, else nonzero
446 *********************************************************************/
447 int simplematch(char *pattern, char *text)
456 unsigned char charmap[32];
462 /* EOF pattern but !EOF text? */
468 /* '*' in the pattern? */
472 /* The pattern ends afterwards? Speed up the return. */
478 /* Else, set wildcard mode and remember position after '*' */
483 /* Character range specification? */
486 memset(charmap, '\0', sizeof(charmap));
488 while (*++pat != ']')
494 else if (*pat == '-')
496 if ((*++pat == ']') || *pat == '\0')
500 for(i = lastchar; i <= *pat; i++)
502 charmap[i / 8] |= (1 << (i % 8));
507 charmap[*pat / 8] |= (1 << (*pat % 8));
511 } /* -END- if Character range specification */
514 /* Compare: Char match, or char range match*/
516 || ((*pat == ']') && (charmap[*txt / 8] & (1 << (*txt % 8)))) )
518 /* Sucess, go ahead */
523 /* In wildcard mode, just try again after failiure */
538 /* Cut off extra '*'s */
539 if(*pat == '*') pat++;
541 /* If this is the pattern's end, fine! */
547 /*********************************************************************
551 * Description : Duplicate the first n characters of a string that may
552 * contain '\0' characters.
555 * 1 : string = string to be duplicated
556 * 2 : n = number of bytes to duplicate
558 * Returns : pointer to copy, or NULL if failiure
560 *********************************************************************/
561 char *bindup(const char *string, int n)
565 if (NULL == (dup = (char *)malloc(n)))
571 memcpy(dup, string, n);
579 /*********************************************************************
581 * Function : make_path
583 * Description : Takes a directory name and a file name, returns
584 * the complete path. Handles windows/unix differences.
585 * If the file name is already an absolute path, or if
586 * the directory name is NULL or empty, it returns
590 * 1 : dir: Name of directory or NULL for none.
591 * 2 : file: Name of file. Should not be NULL or empty.
593 * Returns : "dir/file" (Or on windows, "dir\file").
594 * It allocates the string on the heap. Caller frees.
595 * Returns NULL in error (i.e. NULL file or out of
598 *********************************************************************/
599 char * make_path(const char * dir, const char * file)
606 strncpy(path,dir,512);
611 if(AddPart(path,file,512))
617 #else /* ndef AMIGA */
619 if ((file == NULL) || (*file == '\0'))
621 return NULL; /* Error */
624 if ((dir == NULL) || (*dir == '\0') /* No directory specified */
626 || (*file == '\\') || (file[1] == ':') /* Absolute path (DOS) */
627 #else /* ifndef _WIN32 */
628 || (*file == '/') /* Absolute path (U*ix) */
629 #endif /* ifndef _WIN32 */
636 char * path = malloc(strlen(dir) + strlen(file) + 2);
640 #else /* ifndef _WIN32 */
641 if(path[strlen(path)-1] != '/') strcat(path, "/");
642 #endif /* ifndef _WIN32 */
647 #endif /* ndef AMIGA */