]>
git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/lib/util/string.c
4 * Copyright (c) Intel Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "spdk/stdinc.h"
36 #include "spdk/string.h"
39 spdk_vsprintf_alloc(const char *format
, va_list args
)
46 /* Try with a small buffer first. */
49 /* Limit maximum buffer size to something reasonable so we don't loop forever. */
50 while (bufsize
<= 1024 * 1024) {
51 buf
= malloc(bufsize
);
56 va_copy(args_copy
, args
);
57 rc
= vsnprintf(buf
, bufsize
, format
, args_copy
);
61 * If vsnprintf() returned a count within our current buffer size, we are done.
62 * The count does not include the \0 terminator, so rc == bufsize is not OK.
64 if (rc
>= 0 && (size_t)rc
< bufsize
) {
69 * vsnprintf() should return the required space, but some libc versions do not
70 * implement this correctly, so just double the buffer size and try again.
72 * We don't need the data in buf, so rather than realloc(), use free() and malloc()
73 * again to avoid a copy.
83 spdk_sprintf_alloc(const char *format
, ...)
88 va_start(args
, format
);
89 ret
= spdk_vsprintf_alloc(format
, args
);
114 spdk_strsepq(char **stringp
, const char *delim
)
117 int quoted
= 0, bslash
= 0;
125 while (*q
!= '\0' && *q
!= '\n') {
126 /* eat quoted characters */
132 if (quoted
== '"' && *q
== '\\') {
136 } else if (*q
== quoted
) {
143 } else if (*q
== '\\') {
147 } else if (*q
== '"' || *q
== '\'') {
154 if (strchr(delim
, *q
) == NULL
) {
166 while (*q
!= '\0' && strchr(delim
, *q
) != NULL
) {
179 spdk_str_trim(char *s
)
189 while (*p
!= '\0' && isspace(*p
)) {
195 while (q
- 1 >= p
&& isspace(*(q
- 1))) {
200 /* if remove header, move */
213 spdk_strcpy_pad(void *dst
, const char *src
, size_t size
, int pad
)
219 memcpy(dst
, src
, len
);
220 memset((char *)dst
+ len
, pad
, size
- len
);
222 memcpy(dst
, src
, size
);
227 spdk_strlen_pad(const void *str
, size_t size
, int pad
)
229 const uint8_t *start
;
233 pad_byte
= (uint8_t)pad
;
234 start
= (const uint8_t *)str
;
240 iter
= start
+ size
- 1;
242 if (*iter
!= pad_byte
) {
243 return iter
- start
+ 1;
247 /* Hit the start of the string finding only pad_byte. */
255 spdk_parse_ip_addr(char *ip
, char **host
, char **port
)
278 } else if (*p
!= ':') {
311 spdk_str_chomp(char *s
)
313 size_t len
= strlen(s
);
317 if (s
[len
- 1] != '\r' && s
[len
- 1] != '\n') {
330 spdk_strerror_r(int errnum
, char *buf
, size_t buflen
)
334 #if defined(__USE_GNU)
336 new_buffer
= strerror_r(errnum
, buf
, buflen
);
337 if (new_buffer
!= NULL
) {
338 snprintf(buf
, buflen
, "%s", new_buffer
);
344 rc
= strerror_r(errnum
, buf
, buflen
);
348 snprintf(buf
, buflen
, "Unknown error %d", errnum
);
353 spdk_parse_capacity(const char *cap_str
, uint64_t *cap
, bool *has_prefix
)
358 rc
= sscanf(cap_str
, "%"SCNu64
"%c", cap
, &bin_prefix
);
362 } else if (rc
== 0) {
364 /* No scanf matches - the string does not start with a digit */
373 switch (bin_prefix
) {
384 *cap
*= 1024 * 1024 * 1024;
394 spdk_mem_all_zero(const void *data
, size_t size
)
396 const uint8_t *buf
= data
;