Update copyright year, since the file has been modified
[platform/upstream/curl.git] / lib / strequal.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2006, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
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.
13  *
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.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * $Id$
22  ***************************************************************************/
23
24 #include "setup.h"
25
26 #include <string.h>
27 #include <ctype.h>
28
29 #include "strequal.h"
30
31 #if defined(HAVE_STRCASECMP) && defined(__STRICT_ANSI__)
32 /* this is for "-ansi -Wall -pedantic" to stop complaining! */
33 extern int (strcasecmp)(const char *s1, const char *s2);
34 extern int (strncasecmp)(const char *s1, const char *s2, size_t n);
35 #endif
36
37 int curl_strequal(const char *first, const char *second)
38 {
39 #if defined(HAVE_STRCASECMP)
40   return !(strcasecmp)(first, second);
41 #elif defined(HAVE_STRCMPI)
42   return !(strcmpi)(first, second);
43 #elif defined(HAVE_STRICMP)
44   return !(stricmp)(first, second);
45 #else
46   while (*first && *second) {
47     if (toupper(*first) != toupper(*second)) {
48       break;
49     }
50     first++;
51     second++;
52   }
53   return toupper(*first) == toupper(*second);
54 #endif
55 }
56
57 int curl_strnequal(const char *first, const char *second, size_t max)
58 {
59 #if defined(HAVE_STRCASECMP)
60   return !strncasecmp(first, second, max);
61 #elif defined(HAVE_STRCMPI)
62   return !strncmpi(first, second, max);
63 #elif defined(HAVE_STRICMP)
64   return !strnicmp(first, second, max);
65 #else
66   while (*first && *second && max) {
67     if (toupper(*first) != toupper(*second)) {
68       break;
69     }
70     max--;
71     first++;
72     second++;
73   }
74   if(0 == max)
75     return 1; /* they are equal this far */
76
77   return toupper(*first) == toupper(*second);
78 #endif
79 }
80
81 /*
82  * Curl_strcasestr() finds the first occurrence of the substring needle in the
83  * string haystack.  The terminating `\0' characters are not compared. The
84  * matching is done CASE INSENSITIVE, which thus is the difference between
85  * this and strstr().
86  */
87 char *Curl_strcasestr(const char *haystack, const char *needle)
88 {
89   size_t nlen = strlen(needle);
90   size_t hlen = strlen(haystack);
91
92   while(hlen-- >= nlen) {
93     if(curl_strnequal(haystack, needle, nlen))
94       return (char *)haystack;
95     haystack++;
96   }
97   return NULL;
98 }
99
100 #ifndef HAVE_STRLCAT
101 /*
102  * The strlcat() function appends the NUL-terminated string src to the end
103  * of dst. It will append at most size - strlen(dst) - 1 bytes, NUL-termi-
104  * nating the result.
105  *
106  * The strlcpy() and strlcat() functions return the total length of the
107  * string they tried to create.  For strlcpy() that means the length of src.
108  * For strlcat() that means the initial length of dst plus the length of
109  * src. While this may seem somewhat confusing it was done to make trunca-
110  * tion detection simple.
111  *
112  *
113  */
114 size_t Curl_strlcat(char *dst, const char *src, size_t siz)
115 {
116   char *d = dst;
117   const char *s = src;
118   size_t n = siz;
119   size_t dlen;
120
121   /* Find the end of dst and adjust bytes left but don't go past end */
122   while (n-- != 0 && *d != '\0')
123     d++;
124   dlen = d - dst;
125   n = siz - dlen;
126
127   if (n == 0)
128     return(dlen + strlen(s));
129   while (*s != '\0') {
130     if (n != 1) {
131       *d++ = *s;
132       n--;
133     }
134     s++;
135   }
136   *d = '\0';
137
138   return(dlen + (s - src));     /* count does not include NUL */
139 }
140 #endif