Coverage Report

Created: 2023-05-28 06:42

/src/netcdf-c/libdispatch/dmissing.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *  Copyright 2018, University Corporation for Atmospheric Research
3
 *      See netcdf/COPYRIGHT file for copying and redistribution conditions.
4
 */
5
6
/**
7
 * Provide local alternatives for unix functions
8
 * not available on all machines.
9
 * Currently, this defines:
10
 *   strdup
11
 *   strcpy
12
 *   strlcpy
13
 *   strlcat
14
*/
15
16
#ifdef HAVE_CONFIG_H
17
#include "config.h"
18
#endif
19
#ifdef HAVE_STDLIB_H
20
#include <stdlib.h>
21
#endif
22
#ifdef HAVE_STDIO_H
23
#include <stdio.h>
24
#endif
25
#ifdef HAVE_STRING_H
26
#include <string.h>
27
#endif
28
29
/**************************************************/
30
31
#ifndef HAVE_STRDUP
32
char*
33
strdup(const char* s)
34
{
35
    char* dup;
36
    size_t len;
37
    if(s == NULL) return NULL;
38
    len = strlen(s);
39
    dup = (char*)malloc(len+1);
40
    memcpy(dup,s,len);
41
    dup[len] = '\0';
42
    return dup;
43
}
44
#endif
45
46
47
#ifndef WIN32
48
49
#ifndef HAVE_STRLCPY
50
/*
51
 * Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
52
 *
53
 * Permission to use, copy, modify, and distribute this software for any
54
 * purpose with or without fee is hereby granted, provided that the above
55
 * copyright notice and this permission notice appear in all copies.
56
 *
57
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
58
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
59
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
60
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
61
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
62
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
63
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
64
 */
65
/*
66
 * Copy string src to buffer dst of size dsize.  At most dsize-1
67
 * chars will be copied.  Always NUL terminates (unless dsize == 0).
68
 * Returns strlen(src); if retval >= dsize, truncation occurred.
69
 */
70
size_t
71
strlcpy(char *dst, const char* src, size_t dsize)
72
0
{
73
0
  const char *osrc = src;
74
0
  size_t nleft = dsize;
75
76
  /* Copy as many bytes as will fit. */
77
0
  if (nleft != 0) {
78
0
    while (--nleft != 0) {
79
0
      if ((*dst++ = *src++) == '\0')
80
0
        break;
81
0
    }
82
0
  }
83
  /* Not enough room in dst, add NUL and traverse rest of src. */
84
0
  if (nleft == 0) {
85
0
    if (dsize != 0)
86
0
      *dst = '\0';   /* NUL-terminate dst */
87
0
    while (*src++)
88
0
      ;
89
0
  }
90
0
  return(src - osrc - 1); /* count does not include NUL */
91
0
}
92
#endif
93
94
#ifndef HAVE_STRLCAT
95
/* strlcat */
96
/*
97
 * Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
98
 *
99
 * Permission to use, copy, modify, and distribute this software for any
100
 * purpose with or without fee is hereby granted, provided that the above
101
 * copyright notice and this permission notice appear in all copies.
102
 *
103
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
104
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
105
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
106
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
107
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
108
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
109
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
110
 */
111
112
/*
113
 * Appends src to string dst of size dsize (unlike strncat, dsize is the
114
 * full size of dst, not space left).  At most dsize-1 characters
115
 * will be copied.  Always NUL terminates (unless dsize <= strlen(dst)).
116
 * Returns strlen(src) + MIN(dsize, strlen(initial dst)).
117
 * If retval >= dsize, truncation occurred.
118
 */
119
size_t
120
strlcat(char* dst, const char* src, size_t dsize)
121
1
{
122
1
  const char *odst = dst;
123
1
  const char *osrc = src;
124
1
  size_t n = dsize;
125
1
  size_t dlen;
126
127
  /* Find the end of dst and adjust bytes left but don't go past end. */
128
1
  while (n-- != 0 && *dst != '\0')
129
0
    dst++;
130
1
  dlen = dst - odst;
131
1
  n = dsize - dlen;
132
133
1
  if (n-- == 0)
134
0
    return(dlen + strlen(src));
135
6
  while (*src != '\0') {
136
5
    if (n != 0) {
137
5
      *dst++ = *src;
138
5
      n--;
139
5
    }
140
5
    src++;
141
5
  }
142
1
  *dst = '\0';
143
144
1
  return(dlen + (src - osrc));  /* count does not include NUL */
145
1
}
146
#endif /*!HAVE_STRLCAT*/
147
148
#endif /*WIN32*/
149
150
#if 0
151
Not currently used
152
/* Define an version of strcasestr renamed to avoid any system definition */
153
/* See https://android.googlesource.com/platform/bionic/+/a27d2baa/libc/string/strcasestr.c */
154
/*  $OpenBSD: strcasestr.c,v 1.3 2006/03/31 05:34:55 deraadt Exp $  */
155
/*  $NetBSD: strcasestr.c,v 1.2 2005/02/09 21:35:47 kleink Exp $  */
156
/*-
157
 * Copyright (c) 1990, 1993
158
 *  The Regents of the University of California.  All rights reserved.
159
 *
160
 * This code is derived from software contributed to Berkeley by
161
 * Chris Torek.
162
 *
163
 * Redistribution and use in source and binary forms, with or without
164
 * modification, are permitted provided that the following conditions
165
 * are met:
166
 * 1. Redistributions of source code must retain the above copyright
167
 *    notice, this list of conditions and the following disclaimer.
168
 * 2. Redistributions in binary form must reproduce the above copyright
169
 *    notice, this list of conditions and the following disclaimer in the
170
 *    documentation and/or other materials provided with the distribution.
171
 * 3. Neither the name of the University nor the names of its contributors
172
 *    may be used to endorse or promote products derived from this software
173
 *    without specific prior written permission.
174
 *
175
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
176
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
177
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
178
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
179
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
180
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
181
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
182
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
183
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
184
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
185
 * SUCH DAMAGE.
186
 */
187
188
/*
189
 * Find the first occurrence of find in s, ignore case.
190
 */
191
const char *
192
NC_strcasestr(const char *s, const char *find)
193
{
194
    char c, sc;
195
    size_t len;
196
    if ((c = *find++) != 0) {
197
        c = (char)tolower((unsigned char)c);
198
        len = strlen(find);
199
        do {
200
            do {
201
                if ((sc = *s++) == 0) return (NULL);
202
            } while ((char)tolower((unsigned char)sc) != c);
203
        } while (strncasecmp(s, find, len) != 0);
204
        s--;
205
    }
206
    return ((char *)s);
207
}
208
#endif