Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause 2 : : * Copyright(c) 2025 Intel Corporation 3 : : */ 4 : : 5 : : #include <string.h> 6 : : #include <stdlib.h> 7 : : #include <libgen.h> 8 : : #include <limits.h> 9 : : 10 : : #include <eal_export.h> 11 : : #include <rte_string_fns.h> 12 : : 13 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_basename, 25.11) 14 : : size_t 15 : 217 : rte_basename(const char *path, char *buf, size_t buflen) 16 : : { 17 : : char copy[PATH_MAX + 1]; 18 : : size_t retval = 0; 19 : : 20 [ + + ]: 217 : if (path == NULL) 21 [ + + ]: 2 : return (buf == NULL) ? strlen(".") : strlcpy(buf, ".", buflen); 22 : : 23 : : /* basename is on the end, so if path is too long, use only last PATH_MAX bytes */ 24 : 215 : const size_t pathlen = strlen(path); 25 [ + + ]: 215 : if (pathlen > PATH_MAX) 26 : 1 : path = &path[pathlen - PATH_MAX]; 27 : : 28 : : /* make a copy of buffer since basename may modify it */ 29 : : strlcpy(copy, path, sizeof(copy)); 30 : : 31 : : /* if passed a null buffer, just return length of basename, otherwise strlcpy it */ 32 : : retval = (buf == NULL) ? 33 [ + + ]: 215 : strlen(basename(copy)) : 34 : 204 : strlcpy(buf, basename(copy), buflen); 35 : : 36 : : return retval; 37 : : }