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#include <string.h>
26#include <ctype.h>
27
28#ifdef HAVE_STRINGS_H
29#include <strings.h>
30#endif
31
32#include "strequal.h"
33
34/*
35 * @unittest: 1301
36 */
37int 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/*
58 * @unittest: 1301
59 */
60int curl_strnequal(const char *first, const char *second, size_t max)
61{
62#if defined(HAVE_STRNCASECMP)
63  return !strncasecmp(first, second, max);
64#elif defined(HAVE_STRNCMPI)
65  return !strncmpi(first, second, max);
66#elif defined(HAVE_STRNICMP)
67  return !strnicmp(first, second, max);
68#else
69  while(*first && *second && max) {
70    if(toupper(*first) != toupper(*second)) {
71      break;
72    }
73    max--;
74    first++;
75    second++;
76  }
77  if(0 == max)
78    return 1; /* they are equal this far */
79
80  return toupper(*first) == toupper(*second);
81#endif
82}
83
84#ifndef HAVE_STRLCAT
85/*
86 * The strlcat() function appends the NUL-terminated string src to the end
87 * of dst. It will append at most size - strlen(dst) - 1 bytes, NUL-termi-
88 * nating the result.
89 *
90 * The strlcpy() and strlcat() functions return the total length of the
91 * string they tried to create.  For strlcpy() that means the length of src.
92 * For strlcat() that means the initial length of dst plus the length of
93 * src. While this may seem somewhat confusing it was done to make trunca-
94 * tion detection simple.
95 *
96 *
97 */
98size_t Curl_strlcat(char *dst, const char *src, size_t siz)
99{
100  char *d = dst;
101  const char *s = src;
102  size_t n = siz;
103  union {
104    ssize_t sig;
105     size_t uns;
106  } dlen;
107
108  /* Find the end of dst and adjust bytes left but don't go past end */
109  while(n-- != 0 && *d != '\0')
110    d++;
111  dlen.sig = d - dst;
112  n = siz - dlen.uns;
113
114  if(n == 0)
115    return(dlen.uns + strlen(s));
116  while(*s != '\0') {
117    if(n != 1) {
118      *d++ = *s;
119      n--;
120    }
121    s++;
122  }
123  *d = '\0';
124
125  return(dlen.uns + (s - src));     /* count does not include NUL */
126}
127#endif
128