LCOV - code coverage report
Current view: top level - lib/eal/unix - rte_basename.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 8 8 100.0 %
Date: 2025-11-01 17:50:34 Functions: 1 1 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 8 8 100.0 %

           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                 :            : }

Generated by: LCOV version 1.14