1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2008, 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: strequal.c,v 1.40 2008-10-23 11:49:19 bagder Exp $
22  ***************************************************************************/
23 
24 #include "setup.h"
25 
26 #include <string.h>
27 #include <ctype.h>
28 
29 #ifdef HAVE_STRINGS_H
30 #include <strings.h>
31 #endif
32 
33 #include "strequal.h"
34 
curl_strequal(const char * first,const char * second)35 int curl_strequal(const char *first, const char *second)
36 {
37 #if defined(HAVE_STRCASECMP)
38   return !(strcasecmp)(first, second);
39 #elif defined(HAVE_STRCMPI)
40   return !(strcmpi)(first, second);
41 #elif defined(HAVE_STRICMP)
42   return !(stricmp)(first, second);
43 #else
44   while(*first && *second) {
45     if(toupper(*first) != toupper(*second)) {
46       break;
47     }
48     first++;
49     second++;
50   }
51   return toupper(*first) == toupper(*second);
52 #endif
53 }
54 
curl_strnequal(const char * first,const char * second,size_t max)55 int curl_strnequal(const char *first, const char *second, size_t max)
56 {
57 #if defined(HAVE_STRNCASECMP)
58   return !strncasecmp(first, second, max);
59 #elif defined(HAVE_STRNCMPI)
60   return !strncmpi(first, second, max);
61 #elif defined(HAVE_STRNICMP)
62   return !strnicmp(first, second, max);
63 #else
64   while(*first && *second && max) {
65     if(toupper(*first) != toupper(*second)) {
66       break;
67     }
68     max--;
69     first++;
70     second++;
71   }
72   if(0 == max)
73     return 1; /* they are equal this far */
74 
75   return toupper(*first) == toupper(*second);
76 #endif
77 }
78 
79 #ifndef HAVE_STRLCAT
80 /*
81  * The strlcat() function appends the NUL-terminated string src to the end
82  * of dst. It will append at most size - strlen(dst) - 1 bytes, NUL-termi-
83  * nating the result.
84  *
85  * The strlcpy() and strlcat() functions return the total length of the
86  * string they tried to create.  For strlcpy() that means the length of src.
87  * For strlcat() that means the initial length of dst plus the length of
88  * src. While this may seem somewhat confusing it was done to make trunca-
89  * tion detection simple.
90  *
91  *
92  */
Curl_strlcat(char * dst,const char * src,size_t siz)93 size_t Curl_strlcat(char *dst, const char *src, size_t siz)
94 {
95   char *d = dst;
96   const char *s = src;
97   size_t n = siz;
98   size_t dlen;
99 
100   /* Find the end of dst and adjust bytes left but don't go past end */
101   while(n-- != 0 && *d != '\0')
102     d++;
103   dlen = d - dst;
104   n = siz - dlen;
105 
106   if(n == 0)
107     return(dlen + strlen(s));
108   while(*s != '\0') {
109     if(n != 1) {
110       *d++ = *s;
111       n--;
112     }
113     s++;
114   }
115   *d = '\0';
116 
117   return(dlen + (s - src));     /* count does not include NUL */
118 }
119 #endif
120