1 const char miscutil_rcs[] = "$Id: miscutil.c,v 1.1 2001/05/13 21:57:06 administrator Exp $";
2 /*********************************************************************
4 * File : $Source: /home/administrator/cvs/ijb/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 $
39 *********************************************************************/
52 const char miscutil_h_rcs[] = MISCUTIL_H_VERSION;
54 /* Fix a problem with Solaris. There should be no effect on other
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".
61 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
63 /*********************************************************************
67 * Description : Malloc some memory and set it to '\0'.
68 * The way calloc() ought to be -acjc
71 * 1 : size = Size of memory chunk to return.
73 * Returns : Pointer to newly malloc'd memory chunk.
75 *********************************************************************/
76 void *zalloc(int size)
80 if ((ret = (void *)malloc(size)) != NULL)
89 /*********************************************************************
91 * Function : hash_string
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.
98 * 1 : s : string to be hashed.
100 * Returns : an unsigned long variable with the hashed value.
102 *********************************************************************/
103 unsigned long hash_string( const char* s )
105 unsigned long h = 0ul;
118 /*********************************************************************
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.
128 * 1 : s = string to duplicate
130 * Returns : Pointer to newly malloc'ed copy of the string.
132 *********************************************************************/
133 char *strdup( const char *s )
135 char * result = (char *)malloc( strlen(s)+1 );
145 #endif /* def __MINGW32__ */
149 /*********************************************************************
151 * Function : safe_strerror
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.
158 * 1 : err = the `errno' of the last operation.
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.
164 *********************************************************************/
165 char *safe_strerror(int err)
173 #endif /* NOSTRERROR */
177 sprintf(buf, "(errno = %d)", err);
186 /*********************************************************************
188 * Function : strcmpic
190 * Description : Case insensitive string comparison
193 * 1 : s1 = string 1 to compare
194 * 2 : s2 = string 2 to compare
196 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
198 *********************************************************************/
199 int strcmpic(char *s1, char *s2)
203 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
209 return(ijb_tolower(*s1) - ijb_tolower(*s2));
214 /*********************************************************************
216 * Function : strncmpic
218 * Description : Case insensitive string comparison (upto n characters)
221 * 1 : s1 = string 1 to compare
222 * 2 : s2 = string 2 to compare
223 * 3 : n = maximum characters to compare
225 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
227 *********************************************************************/
228 int strncmpic(char *s1, char *s2, size_t n)
230 if (n <= 0) return(0);
234 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
243 return(ijb_tolower(*s1) - ijb_tolower(*s2));