1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2014 Intel Corporation 3 */ 4 5 #include <stdio.h> 6 #include <errno.h> 7 8 #include <rte_string_fns.h> 9 #include <rte_errno.h> 10 11 /* split string into tokens */ 12 int 13 rte_strsplit(char *string, int stringlen, 14 char **tokens, int maxtokens, char delim) 15 { 16 int i, tok = 0; 17 int tokstart = 1; /* first token is right at start of string */ 18 19 if (string == NULL || tokens == NULL) 20 goto einval_error; 21 22 for (i = 0; i < stringlen; i++) { 23 if (string[i] == '\0' || tok >= maxtokens) 24 break; 25 if (tokstart) { 26 tokstart = 0; 27 tokens[tok++] = &string[i]; 28 } 29 if (string[i] == delim) { 30 string[i] = '\0'; 31 tokstart = 1; 32 } 33 } 34 return tok; 35 36 einval_error: 37 errno = EINVAL; 38 return -1; 39 } 40 41 /* Copy src string into dst. 42 * 43 * Return negative value and NUL-terminate if dst is too short, 44 * Otherwise return number of bytes copied. 45 */ 46 ssize_t 47 rte_strscpy(char *dst, const char *src, size_t dsize) 48 { 49 size_t nleft = dsize; 50 size_t res = 0; 51 52 /* Copy as many bytes as will fit. */ 53 while (nleft != 0) { 54 dst[res] = src[res]; 55 if (src[res] == '\0') 56 return res; 57 res++; 58 nleft--; 59 } 60 61 /* Not enough room in dst, set NUL and return error. */ 62 if (res != 0) 63 dst[res - 1] = '\0'; 64 rte_errno = E2BIG; 65 return -rte_errno; 66 } 67