/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 | | #if !defined(_MSC_VER) && !defined(WIN32) |
47 | | |
48 | | #ifndef HAVE_STRLCAT |
49 | | /* strlcat */ |
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 | | /* |
67 | | * Appends src to string dst of size dsize (unlike strncat, dsize is the |
68 | | * full size of dst, not space left). At most dsize-1 characters |
69 | | * will be copied. Always NUL terminates (unless dsize <= strlen(dst)). |
70 | | * Returns strlen(src) + MIN(dsize, strlen(initial dst)). |
71 | | * If retval >= dsize, truncation occurred. |
72 | | */ |
73 | | size_t |
74 | | nc_strlcat(char* dst, const char* src, size_t dsize) |
75 | 1 | { |
76 | 1 | const char *odst = dst; |
77 | 1 | const char *osrc = src; |
78 | 1 | size_t n = dsize; |
79 | 1 | size_t dlen; |
80 | | |
81 | | /* Find the end of dst and adjust bytes left but don't go past end. */ |
82 | 1 | while (n-- != 0 && *dst != '\0') |
83 | 0 | dst++; |
84 | 1 | dlen = dst - odst; |
85 | 1 | n = dsize - dlen; |
86 | | |
87 | 1 | if (n-- == 0) |
88 | 0 | return(dlen + strlen(src)); |
89 | 6 | while (*src != '\0') { |
90 | 5 | if (n != 0) { |
91 | 5 | *dst++ = *src; |
92 | 5 | n--; |
93 | 5 | } |
94 | 5 | src++; |
95 | 5 | } |
96 | 1 | *dst = '\0'; |
97 | | |
98 | 1 | return(dlen + (src - osrc)); /* count does not include NUL */ |
99 | 1 | } |
100 | | #endif /*!HAVE_STRLCAT*/ |
101 | | |
102 | | #endif /*WIN32*/ |
103 | | |
104 | | #if 0 |
105 | | Not currently used |
106 | | /* Define an version of strcasestr renamed to avoid any system definition */ |
107 | | /* See https://android.googlesource.com/platform/bionic/+/a27d2baa/libc/string/strcasestr.c */ |
108 | | /* $OpenBSD: strcasestr.c,v 1.3 2006/03/31 05:34:55 deraadt Exp $ */ |
109 | | /* $NetBSD: strcasestr.c,v 1.2 2005/02/09 21:35:47 kleink Exp $ */ |
110 | | /*- |
111 | | * Copyright (c) 1990, 1993 |
112 | | * The Regents of the University of California. All rights reserved. |
113 | | * |
114 | | * This code is derived from software contributed to Berkeley by |
115 | | * Chris Torek. |
116 | | * |
117 | | * Redistribution and use in source and binary forms, with or without |
118 | | * modification, are permitted provided that the following conditions |
119 | | * are met: |
120 | | * 1. Redistributions of source code must retain the above copyright |
121 | | * notice, this list of conditions and the following disclaimer. |
122 | | * 2. Redistributions in binary form must reproduce the above copyright |
123 | | * notice, this list of conditions and the following disclaimer in the |
124 | | * documentation and/or other materials provided with the distribution. |
125 | | * 3. Neither the name of the University nor the names of its contributors |
126 | | * may be used to endorse or promote products derived from this software |
127 | | * without specific prior written permission. |
128 | | * |
129 | | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
130 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
131 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
132 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
133 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
134 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
135 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
136 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
137 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
138 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
139 | | * SUCH DAMAGE. |
140 | | */ |
141 | | |
142 | | /* |
143 | | * Find the first occurrence of find in s, ignore case. |
144 | | */ |
145 | | const char * |
146 | | NC_strcasestr(const char *s, const char *find) |
147 | | { |
148 | | char c, sc; |
149 | | size_t len; |
150 | | if ((c = *find++) != 0) { |
151 | | c = (char)tolower((unsigned char)c); |
152 | | len = strlen(find); |
153 | | do { |
154 | | do { |
155 | | if ((sc = *s++) == 0) return (NULL); |
156 | | } while ((char)tolower((unsigned char)sc) != c); |
157 | | } while (strncasecmp(s, find, len) != 0); |
158 | | s--; |
159 | | } |
160 | | return ((char *)s); |
161 | | } |
162 | | #endif |