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