Fixed LOG_LEVEL_LOG
[privoxy.git] / miscutil.c
1 const char miscutil_rcs[] = "$Id: miscutil.c,v 1.1 2001/05/13 21:57:06 administrator Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /home/administrator/cvs/ijb/miscutil.c,v $
5  *
6  * Purpose     :  zalloc, hash_string, safe_strerror, strcmpic,
7  *                strncmpic, and MinGW32 strdup functions.  These are
8  *                each too small to deserve their own file but don't 
9  *                really fit in any other file.
10  *
11  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
12  *                IJBSWA team.  http://ijbswa.sourceforge.net
13  *
14  *                Based on the Internet Junkbuster originally written
15  *                by and Copyright (C) 1997 Anonymous Coders and 
16  *                Junkbusters Corporation.  http://www.junkbusters.com
17  *
18  *                This program is free software; you can redistribute it 
19  *                and/or modify it under the terms of the GNU General
20  *                Public License as published by the Free Software
21  *                Foundation; either version 2 of the License, or (at
22  *                your option) any later version.
23  *
24  *                This program is distributed in the hope that it will
25  *                be useful, but WITHOUT ANY WARRANTY; without even the
26  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
27  *                PARTICULAR PURPOSE.  See the GNU General Public
28  *                License for more details.
29  *
30  *                The GNU General Public License should be included with
31  *                this file.  If not, you can view it at
32  *                http://www.gnu.org/copyleft/gpl.html
33  *                or write to the Free Software Foundation, Inc., 59
34  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
35  *
36  * Revisions   :
37  *    $Log: miscutil.c,v $
38  *
39  *********************************************************************/
40 \f
41
42 #include "config.h"
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <malloc.h>
48 #include <ctype.h>
49
50 #include "miscutil.h"
51
52 const char miscutil_h_rcs[] = MISCUTIL_H_VERSION;
53
54 /* Fix a problem with Solaris.  There should be no effect on other
55  * platforms.
56  * Solaris's isspace() is a macro which uses it's argument directly
57  * as an array index.  Therefore we need to make sure that high-bit
58  * characters generate +ve values, and ideally we also want to make
59  * the argument match the declared parameter type of "int".
60  */
61 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
62
63 /*********************************************************************
64  *
65  * Function    :  zalloc
66  *
67  * Description :  Malloc some memory and set it to '\0'.
68  *                The way calloc() ought to be -acjc
69  *
70  * Parameters  :
71  *          1  :  size = Size of memory chunk to return.
72  *
73  * Returns     :  Pointer to newly malloc'd memory chunk.
74  *
75  *********************************************************************/
76 void *zalloc(int size)
77 {
78    void * ret;
79
80    if ((ret = (void *)malloc(size)) != NULL)
81    {
82       memset(ret, 0, size);
83    }
84
85    return(ret);
86 }
87
88
89 /*********************************************************************
90  *
91  * Function    :  hash_string
92  *
93  * Description :  Take a string and compute a (hopefuly) unique numeric
94  *                integer value.  This has several uses, but being able
95  *                to "switch" a string the one of my favorites.
96  *
97  * Parameters  :
98  *          1  :  s : string to be hashed.
99  *
100  * Returns     :  an unsigned long variable with the hashed value.
101  *
102  *********************************************************************/
103 unsigned long hash_string( const char* s )
104 {
105    unsigned long h = 0ul; 
106
107    for ( ; *s; ++s )
108    {
109       h = 5 * h + *s;
110    }
111
112    return (h);
113
114 }
115
116
117 #ifdef __MINGW32__
118 /*********************************************************************
119  *
120  * Function    :  strdup
121  *
122  * Description :  For some reason (which is beyond me), gcc and WIN32
123  *                don't like strdup.  When a "free" is executed on a
124  *                strdup'd ptr, it can at times freez up!  So I just
125  *                replaced it and problem was solved.
126  *
127  * Parameters  :
128  *          1  :  s = string to duplicate
129  *
130  * Returns     :  Pointer to newly malloc'ed copy of the string.
131  *
132  *********************************************************************/
133 char *strdup( const char *s )
134 {
135    char * result = (char *)malloc( strlen(s)+1 );
136
137    if (result != NULL)
138    {
139       strcpy( result, s );
140    }
141
142    return( result );
143 }
144
145 #endif /* def __MINGW32__ */
146
147
148
149 /*********************************************************************
150  *
151  * Function    :  safe_strerror
152  *
153  * Description :  Variant of the library routine strerror() which will
154  *                work on systems without the library routine, and
155  *                which should never return NULL.
156  *
157  * Parameters  :
158  *          1  :  err = the `errno' of the last operation.
159  *
160  * Returns     :  An "English" string of the last `errno'.  Allocated
161  *                with strdup(), so caller frees.  May be NULL if the
162  *                system is out of memory.
163  *
164  *********************************************************************/
165 char *safe_strerror(int err)
166 {
167    char *s = NULL;
168    char buf[BUFSIZ];
169
170
171 #ifndef NOSTRERROR
172    s = strerror(err);
173 #endif /* NOSTRERROR */
174
175    if (s == NULL)
176    {
177       sprintf(buf, "(errno = %d)", err);
178       s = buf;
179    }
180
181    return(strdup(s));
182
183 }
184
185
186 /*********************************************************************
187  *
188  * Function    :  strcmpic
189  *
190  * Description :  Case insensitive string comparison
191  *
192  * Parameters  :
193  *          1  :  s1 = string 1 to compare
194  *          2  :  s2 = string 2 to compare
195  *
196  * Returns     :  0 if s1==s2, Negative if s1<s2, Positive if s1>s2
197  *
198  *********************************************************************/
199 int strcmpic(char *s1, char *s2)
200 {
201    while (*s1 && *s2)
202    {
203       if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
204       {
205          break;
206       }
207       s1++, s2++;
208    }
209    return(ijb_tolower(*s1) - ijb_tolower(*s2));
210
211 }
212
213
214 /*********************************************************************
215  *
216  * Function    :  strncmpic
217  *
218  * Description :  Case insensitive string comparison (upto n characters)
219  *
220  * Parameters  :
221  *          1  :  s1 = string 1 to compare
222  *          2  :  s2 = string 2 to compare
223  *          3  :  n = maximum characters to compare
224  *
225  * Returns     :  0 if s1==s2, Negative if s1<s2, Positive if s1>s2
226  *
227  *********************************************************************/
228 int strncmpic(char *s1, char *s2, size_t n)
229 {
230    if (n <= 0) return(0);
231
232    while (*s1 && *s2)
233    {
234       if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
235       {
236          break;
237       }
238
239       if (--n <= 0) break;
240
241       s1++, s2++;
242    }
243    return(ijb_tolower(*s1) - ijb_tolower(*s2));
244
245 }
246
247
248 /*
249   Local Variables:
250   tab-width: 3
251   end:
252 */