Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl / strequal.c
blob4b97f1dbb3bd1224475b94ee4315a558cf8539a1
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2006, 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.4 2007/03/20 19:51:42 hoffman Exp $
22 ***************************************************************************/
24 #include "setup.h"
26 #include <string.h>
27 #include <ctype.h>
28 #ifdef HAVE_STRINGS_H
29 #include <strings.h>
30 #endif
32 #include "strequal.h"
34 #if defined(HAVE_STRCASECMP) && defined(__STRICT_ANSI__)
35 /* this is for "-ansi -Wall -pedantic" to stop complaining! */
36 extern int (strcasecmp)(const char *s1, const char *s2);
37 extern int (strncasecmp)(const char *s1, const char *s2, size_t n);
38 #endif
40 int curl_strequal(const char *first, const char *second)
42 #if defined(HAVE_STRCASECMP)
43 return !(strcasecmp)(first, second);
44 #elif defined(HAVE_STRCMPI)
45 return !(strcmpi)(first, second);
46 #elif defined(HAVE_STRICMP)
47 return !(stricmp)(first, second);
48 #else
49 while (*first && *second) {
50 if (toupper(*first) != toupper(*second)) {
51 break;
53 first++;
54 second++;
56 return toupper(*first) == toupper(*second);
57 #endif
60 int curl_strnequal(const char *first, const char *second, size_t max)
62 #if defined(HAVE_STRCASECMP)
63 return !strncasecmp(first, second, max);
64 #elif defined(HAVE_STRCMPI)
65 return !strncmpi(first, second, max);
66 #elif defined(HAVE_STRICMP)
67 return !strnicmp(first, second, max);
68 #else
69 while (*first && *second && max) {
70 if (toupper(*first) != toupper(*second)) {
71 break;
73 max--;
74 first++;
75 second++;
77 if(0 == max)
78 return 1; /* they are equal this far */
80 return toupper(*first) == toupper(*second);
81 #endif
85 * Curl_strcasestr() finds the first occurrence of the substring needle in the
86 * string haystack. The terminating `\0' characters are not compared. The
87 * matching is done CASE INSENSITIVE, which thus is the difference between
88 * this and strstr().
90 char *Curl_strcasestr(const char *haystack, const char *needle)
92 size_t nlen = strlen(needle);
93 size_t hlen = strlen(haystack);
95 while(hlen-- >= nlen) {
96 if(curl_strnequal(haystack, needle, nlen))
97 return (char *)haystack;
98 haystack++;
100 return NULL;
103 #ifndef HAVE_STRLCAT
105 * The strlcat() function appends the NUL-terminated string src to the end
106 * of dst. It will append at most size - strlen(dst) - 1 bytes, NUL-termi-
107 * nating the result.
109 * The strlcpy() and strlcat() functions return the total length of the
110 * string they tried to create. For strlcpy() that means the length of src.
111 * For strlcat() that means the initial length of dst plus the length of
112 * src. While this may seem somewhat confusing it was done to make trunca-
113 * tion detection simple.
117 size_t Curl_strlcat(char *dst, const char *src, size_t siz)
119 char *d = dst;
120 const char *s = src;
121 size_t n = siz;
122 size_t dlen;
124 /* Find the end of dst and adjust bytes left but don't go past end */
125 while (n-- != 0 && *d != '\0')
126 d++;
127 dlen = d - dst;
128 n = siz - dlen;
130 if (n == 0)
131 return(dlen + strlen(s));
132 while (*s != '\0') {
133 if (n != 1) {
134 *d++ = *s;
135 n--;
137 s++;
139 *d = '\0';
141 return(dlen + (s - src)); /* count does not include NUL */
143 #endif