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