Coverage Report

Created: 2026-01-04 06:24

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