HTTP_REDIRECT_TEMPLATE fixed.
[privoxy.git] / miscutil.c
1 const char miscutil_rcs[] = "$Id: miscutil.c,v 1.1.1.1 2001/05/15 13:59:00 oes Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/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  *    Revision 1.1.1.1  2001/05/15 13:59:00  oes
39  *    Initial import of version 2.9.3 source tree
40  *
41  *
42  *********************************************************************/
43 \f
44
45 #include "config.h"
46
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <malloc.h>
51 #include <ctype.h>
52
53 #include "miscutil.h"
54
55 const char miscutil_h_rcs[] = MISCUTIL_H_VERSION;
56
57 /* Fix a problem with Solaris.  There should be no effect on other
58  * platforms.
59  * Solaris's isspace() is a macro which uses it's argument directly
60  * as an array index.  Therefore we need to make sure that high-bit
61  * characters generate +ve values, and ideally we also want to make
62  * the argument match the declared parameter type of "int".
63  */
64 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
65
66 /*********************************************************************
67  *
68  * Function    :  zalloc
69  *
70  * Description :  Malloc some memory and set it to '\0'.
71  *                The way calloc() ought to be -acjc
72  *
73  * Parameters  :
74  *          1  :  size = Size of memory chunk to return.
75  *
76  * Returns     :  Pointer to newly malloc'd memory chunk.
77  *
78  *********************************************************************/
79 void *zalloc(int size)
80 {
81    void * ret;
82
83    if ((ret = (void *)malloc(size)) != NULL)
84    {
85       memset(ret, 0, size);
86    }
87
88    return(ret);
89 }
90
91
92 /*********************************************************************
93  *
94  * Function    :  hash_string
95  *
96  * Description :  Take a string and compute a (hopefuly) unique numeric
97  *                integer value.  This has several uses, but being able
98  *                to "switch" a string the one of my favorites.
99  *
100  * Parameters  :
101  *          1  :  s : string to be hashed.
102  *
103  * Returns     :  an unsigned long variable with the hashed value.
104  *
105  *********************************************************************/
106 unsigned long hash_string( const char* s )
107 {
108    unsigned long h = 0ul; 
109
110    for ( ; *s; ++s )
111    {
112       h = 5 * h + *s;
113    }
114
115    return (h);
116
117 }
118
119
120 #ifdef __MINGW32__
121 /*********************************************************************
122  *
123  * Function    :  strdup
124  *
125  * Description :  For some reason (which is beyond me), gcc and WIN32
126  *                don't like strdup.  When a "free" is executed on a
127  *                strdup'd ptr, it can at times freez up!  So I just
128  *                replaced it and problem was solved.
129  *
130  * Parameters  :
131  *          1  :  s = string to duplicate
132  *
133  * Returns     :  Pointer to newly malloc'ed copy of the string.
134  *
135  *********************************************************************/
136 char *strdup( const char *s )
137 {
138    char * result = (char *)malloc( strlen(s)+1 );
139
140    if (result != NULL)
141    {
142       strcpy( result, s );
143    }
144
145    return( result );
146 }
147
148 #endif /* def __MINGW32__ */
149
150
151
152 /*********************************************************************
153  *
154  * Function    :  safe_strerror
155  *
156  * Description :  Variant of the library routine strerror() which will
157  *                work on systems without the library routine, and
158  *                which should never return NULL.
159  *
160  * Parameters  :
161  *          1  :  err = the `errno' of the last operation.
162  *
163  * Returns     :  An "English" string of the last `errno'.  Allocated
164  *                with strdup(), so caller frees.  May be NULL if the
165  *                system is out of memory.
166  *
167  *********************************************************************/
168 char *safe_strerror(int err)
169 {
170    char *s = NULL;
171    char buf[BUFSIZ];
172
173
174 #ifndef NOSTRERROR
175    s = strerror(err);
176 #endif /* NOSTRERROR */
177
178    if (s == NULL)
179    {
180       sprintf(buf, "(errno = %d)", err);
181       s = buf;
182    }
183
184    return(strdup(s));
185
186 }
187
188
189 /*********************************************************************
190  *
191  * Function    :  strcmpic
192  *
193  * Description :  Case insensitive string comparison
194  *
195  * Parameters  :
196  *          1  :  s1 = string 1 to compare
197  *          2  :  s2 = string 2 to compare
198  *
199  * Returns     :  0 if s1==s2, Negative if s1<s2, Positive if s1>s2
200  *
201  *********************************************************************/
202 int strcmpic(const char *s1, const char *s2)
203 {
204    while (*s1 && *s2)
205    {
206       if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
207       {
208          break;
209       }
210       s1++, s2++;
211    }
212    return(ijb_tolower(*s1) - ijb_tolower(*s2));
213
214 }
215
216
217 /*********************************************************************
218  *
219  * Function    :  strncmpic
220  *
221  * Description :  Case insensitive string comparison (upto n characters)
222  *
223  * Parameters  :
224  *          1  :  s1 = string 1 to compare
225  *          2  :  s2 = string 2 to compare
226  *          3  :  n = maximum characters to compare
227  *
228  * Returns     :  0 if s1==s2, Negative if s1<s2, Positive if s1>s2
229  *
230  *********************************************************************/
231 int strncmpic(const char *s1, const char *s2, size_t n)
232 {
233    if (n <= 0) return(0);
234
235    while (*s1 && *s2)
236    {
237       if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
238       {
239          break;
240       }
241
242       if (--n <= 0) break;
243
244       s1++, s2++;
245    }
246    return(ijb_tolower(*s1) - ijb_tolower(*s2));
247
248 }
249
250
251 /*
252   Local Variables:
253   tab-width: 3
254   end:
255 */