]> jfr.im git - irc/thales.git/blob - src/compat.c
even more cosmetic updates
[irc/thales.git] / src / compat.c
1 /* GNU Thales - IRC to Relational Database Gateway
2 * Copyright (C) 2002 Lucas Nussbaum <lucas@lucas-nussbaum.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 #include "thales.h"
20
21 /*************************************************************************/
22
23
24 #if !HAVE_STRICMP && !HAVE_STRCASECMP
25
26 /* stricmp, strnicmp: Case-insensitive versions of strcmp() and
27 * strncmp().
28 */
29
30 int stricmp(const char *s1, const char *s2)
31 {
32 register int c;
33
34 while ((c = tolower(*s1)) == tolower(*s2))
35 {
36 if (c == 0)
37 return 0;
38 s1++;
39 s2++;
40 }
41 if (c < tolower(*s2))
42 return -1;
43 return 1;
44 }
45
46 int strnicmp(const char *s1, const char *s2, size_t len)
47 {
48 register int c;
49
50 if (!len)
51 return 0;
52 while ((c = tolower(*s1)) == tolower(*s2) && len > 0)
53 {
54 if (c == 0 || --len == 0)
55 return 0;
56 s1++;
57 s2++;
58 }
59 if (c < tolower(*s2))
60 return -1;
61 return 1;
62 }
63 #endif
64
65 /*************************************************************************/
66
67 #if !HAVE_STRDUP
68 char *strdup(const char *s)
69 {
70 char *new = calloc(strlen(s) + 1, 1);
71 if (new)
72 strcpy(new, s);
73 return new;
74 }
75 #endif
76
77 /*************************************************************************/
78
79 #if !HAVE_STRSPN
80 size_t strspn(const char *s, const char *accept)
81 {
82 size_t i = 0;
83
84 while (*s && strchr(accept, *s))
85 ++i, ++s;
86 return i;
87 }
88 #endif
89
90 /*************************************************************************/
91
92 #if !HAVE_STRERROR
93 # if HAVE_SYS_ERRLIST
94 extern char *sys_errlist[];
95 # endif
96
97 char *strerror(int errnum)
98 {
99 # if HAVE_SYS_ERRLIST
100 return sys_errlist[errnum];
101 # else
102 static char buf[20];
103 snprintf(buf, sizeof(buf), "Error %d", errnum);
104 return buf;
105 # endif
106 }
107 #endif