Initial commit of visual studio 9 git build superproject.
[git-build-vc9.git] / curl / lib / strequal.c
blob4c85143120cd26d593fb56f8d273e69b5eed1534
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * $Id: strequal.c,v 1.33 2007-11-07 09:21:36 bagder Exp $
22 ***************************************************************************/
24 #ifndef _GNU_SOURCE
25 /* glibc needs this to define the prototype for strcasestr */
26 #define _GNU_SOURCE 1
27 #endif
29 #include "setup.h"
31 #include <string.h>
32 #include <ctype.h>
34 #ifdef HAVE_STRINGS_H
35 #include <strings.h>
36 #endif
38 #include "strequal.h"
40 #if defined(HAVE_STRCASECMP) && defined(__STRICT_ANSI__)
41 /* this is for "-ansi -Wall -pedantic" to stop complaining! */
42 extern int (strcasecmp)(const char *s1, const char *s2);
43 extern int (strncasecmp)(const char *s1, const char *s2, size_t n);
44 #endif
46 int curl_strequal(const char *first, const char *second)
48 #if defined(HAVE_STRCASECMP)
49 return !(strcasecmp)(first, second);
50 #elif defined(HAVE_STRCMPI)
51 return !(strcmpi)(first, second);
52 #elif defined(HAVE_STRICMP)
53 return !(stricmp)(first, second);
54 #else
55 while(*first && *second) {
56 if(toupper(*first) != toupper(*second)) {
57 break;
59 first++;
60 second++;
62 return toupper(*first) == toupper(*second);
63 #endif
66 int curl_strnequal(const char *first, const char *second, size_t max)
68 #if defined(HAVE_STRCASECMP)
69 return !strncasecmp(first, second, max);
70 #elif defined(HAVE_STRCMPI)
71 return !strncmpi(first, second, max);
72 #elif defined(HAVE_STRICMP)
73 return !strnicmp(first, second, max);
74 #else
75 while(*first && *second && max) {
76 if(toupper(*first) != toupper(*second)) {
77 break;
79 max--;
80 first++;
81 second++;
83 if(0 == max)
84 return 1; /* they are equal this far */
86 return toupper(*first) == toupper(*second);
87 #endif
91 * Curl_strcasestr() finds the first occurrence of the substring needle in the
92 * string haystack. The terminating `\0' characters are not compared. The
93 * matching is done CASE INSENSITIVE, which thus is the difference between
94 * this and strstr().
96 char *Curl_strcasestr(const char *haystack, const char *needle)
98 #if defined(HAVE_STRCASESTR)
99 return strcasestr(haystack, needle);
100 #else
101 size_t nlen = strlen(needle);
102 size_t hlen = strlen(haystack);
104 while(hlen-- >= nlen) {
105 if(curl_strnequal(haystack, needle, nlen))
106 return (char *)haystack;
107 haystack++;
109 return NULL;
110 #endif
113 #ifndef HAVE_STRLCAT
115 * The strlcat() function appends the NUL-terminated string src to the end
116 * of dst. It will append at most size - strlen(dst) - 1 bytes, NUL-termi-
117 * nating the result.
119 * The strlcpy() and strlcat() functions return the total length of the
120 * string they tried to create. For strlcpy() that means the length of src.
121 * For strlcat() that means the initial length of dst plus the length of
122 * src. While this may seem somewhat confusing it was done to make trunca-
123 * tion detection simple.
127 size_t Curl_strlcat(char *dst, const char *src, size_t siz)
129 char *d = dst;
130 const char *s = src;
131 size_t n = siz;
132 size_t dlen;
134 /* Find the end of dst and adjust bytes left but don't go past end */
135 while(n-- != 0 && *d != '\0')
136 d++;
137 dlen = d - dst;
138 n = siz - dlen;
140 if(n == 0)
141 return(dlen + strlen(s));
142 while(*s != '\0') {
143 if(n != 1) {
144 *d++ = *s;
145 n--;
147 s++;
149 *d = '\0';
151 return(dlen + (s - src)); /* count does not include NUL */
153 #endif