xref: /dpdk/lib/eal/common/eal_common_string_fns.c (revision dbba7c9efbb9ed5cfc88c192213f54b1f9942c00)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #include <string.h>
699a2dd95SBruce Richardson #include <stdio.h>
799a2dd95SBruce Richardson #include <stdarg.h>
899a2dd95SBruce Richardson #include <errno.h>
999a2dd95SBruce Richardson 
1099a2dd95SBruce Richardson #include <rte_string_fns.h>
11*dbba7c9eSThomas Monjalon #include <rte_errno.h>
1299a2dd95SBruce Richardson 
1399a2dd95SBruce Richardson /* split string into tokens */
1499a2dd95SBruce Richardson int
1599a2dd95SBruce Richardson rte_strsplit(char *string, int stringlen,
1699a2dd95SBruce Richardson 	     char **tokens, int maxtokens, char delim)
1799a2dd95SBruce Richardson {
1899a2dd95SBruce Richardson 	int i, tok = 0;
1999a2dd95SBruce Richardson 	int tokstart = 1; /* first token is right at start of string */
2099a2dd95SBruce Richardson 
2199a2dd95SBruce Richardson 	if (string == NULL || tokens == NULL)
2299a2dd95SBruce Richardson 		goto einval_error;
2399a2dd95SBruce Richardson 
2499a2dd95SBruce Richardson 	for (i = 0; i < stringlen; i++) {
2599a2dd95SBruce Richardson 		if (string[i] == '\0' || tok >= maxtokens)
2699a2dd95SBruce Richardson 			break;
2799a2dd95SBruce Richardson 		if (tokstart) {
2899a2dd95SBruce Richardson 			tokstart = 0;
2999a2dd95SBruce Richardson 			tokens[tok++] = &string[i];
3099a2dd95SBruce Richardson 		}
3199a2dd95SBruce Richardson 		if (string[i] == delim) {
3299a2dd95SBruce Richardson 			string[i] = '\0';
3399a2dd95SBruce Richardson 			tokstart = 1;
3499a2dd95SBruce Richardson 		}
3599a2dd95SBruce Richardson 	}
3699a2dd95SBruce Richardson 	return tok;
3799a2dd95SBruce Richardson 
3899a2dd95SBruce Richardson einval_error:
3999a2dd95SBruce Richardson 	errno = EINVAL;
4099a2dd95SBruce Richardson 	return -1;
4199a2dd95SBruce Richardson }
4299a2dd95SBruce Richardson 
4399a2dd95SBruce Richardson /* Copy src string into dst.
4499a2dd95SBruce Richardson  *
4599a2dd95SBruce Richardson  * Return negative value and NUL-terminate if dst is too short,
4699a2dd95SBruce Richardson  * Otherwise return number of bytes copied.
4799a2dd95SBruce Richardson  */
4899a2dd95SBruce Richardson ssize_t
4999a2dd95SBruce Richardson rte_strscpy(char *dst, const char *src, size_t dsize)
5099a2dd95SBruce Richardson {
5199a2dd95SBruce Richardson 	size_t nleft = dsize;
5299a2dd95SBruce Richardson 	size_t res = 0;
5399a2dd95SBruce Richardson 
5499a2dd95SBruce Richardson 	/* Copy as many bytes as will fit. */
5599a2dd95SBruce Richardson 	while (nleft != 0) {
5699a2dd95SBruce Richardson 		dst[res] = src[res];
5799a2dd95SBruce Richardson 		if (src[res] == '\0')
5899a2dd95SBruce Richardson 			return res;
5999a2dd95SBruce Richardson 		res++;
6099a2dd95SBruce Richardson 		nleft--;
6199a2dd95SBruce Richardson 	}
6299a2dd95SBruce Richardson 
6399a2dd95SBruce Richardson 	/* Not enough room in dst, set NUL and return error. */
6499a2dd95SBruce Richardson 	if (res != 0)
6599a2dd95SBruce Richardson 		dst[res - 1] = '\0';
66*dbba7c9eSThomas Monjalon 	rte_errno = E2BIG;
67*dbba7c9eSThomas Monjalon 	return -rte_errno;
6899a2dd95SBruce Richardson }
69