1 const char miscutil_rcs[] = "$Id: miscutil.c,v 1.1.1.1 2001/05/15 13:59:00 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/miscutil.c,v $
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.
11 * Copyright : Written by and Copyright (C) 2001 the SourceForge
12 * IJBSWA team. http://ijbswa.sourceforge.net
14 * Based on the Internet Junkbuster originally written
15 * by and Copyright (C) 1997 Anonymous Coders and
16 * Junkbusters Corporation. http://www.junkbusters.com
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.
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.
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.
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
42 *********************************************************************/
55 const char miscutil_h_rcs[] = MISCUTIL_H_VERSION;
57 /* Fix a problem with Solaris. There should be no effect on other
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".
64 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
66 /*********************************************************************
70 * Description : Malloc some memory and set it to '\0'.
71 * The way calloc() ought to be -acjc
74 * 1 : size = Size of memory chunk to return.
76 * Returns : Pointer to newly malloc'd memory chunk.
78 *********************************************************************/
79 void *zalloc(int size)
83 if ((ret = (void *)malloc(size)) != NULL)
92 /*********************************************************************
94 * Function : hash_string
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.
101 * 1 : s : string to be hashed.
103 * Returns : an unsigned long variable with the hashed value.
105 *********************************************************************/
106 unsigned long hash_string( const char* s )
108 unsigned long h = 0ul;
121 /*********************************************************************
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.
131 * 1 : s = string to duplicate
133 * Returns : Pointer to newly malloc'ed copy of the string.
135 *********************************************************************/
136 char *strdup( const char *s )
138 char * result = (char *)malloc( strlen(s)+1 );
148 #endif /* def __MINGW32__ */
152 /*********************************************************************
154 * Function : safe_strerror
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.
161 * 1 : err = the `errno' of the last operation.
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.
167 *********************************************************************/
168 char *safe_strerror(int err)
176 #endif /* NOSTRERROR */
180 sprintf(buf, "(errno = %d)", err);
189 /*********************************************************************
191 * Function : strcmpic
193 * Description : Case insensitive string comparison
196 * 1 : s1 = string 1 to compare
197 * 2 : s2 = string 2 to compare
199 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
201 *********************************************************************/
202 int strcmpic(const char *s1, const char *s2)
206 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
212 return(ijb_tolower(*s1) - ijb_tolower(*s2));
217 /*********************************************************************
219 * Function : strncmpic
221 * Description : Case insensitive string comparison (upto n characters)
224 * 1 : s1 = string 1 to compare
225 * 2 : s2 = string 2 to compare
226 * 3 : n = maximum characters to compare
228 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
230 *********************************************************************/
231 int strncmpic(const char *s1, const char *s2, size_t n)
233 if (n <= 0) return(0);
237 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
246 return(ijb_tolower(*s1) - ijb_tolower(*s2));