Coverage Report

Created: 2026-01-09 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/pistache/src/common/ps_basename.cc
Line
Count
Source
1
/*
2
 * SPDX-FileCopyrightText: 2024 Duncan Greatwood
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5
 */
6
7
// Defines a ps_basename_r for OS that do not have basename_r natively
8
9
#include <pistache/ps_basename.h>
10
11
/* ------------------------------------------------------------------------- */
12
13
14
#ifndef __APPLE__
15
16
/* ------------------------------------------------------------------------- */
17
18
#ifdef _IS_WINDOWS
19
20
#include <stdlib.h> // for _splitpath_s
21
#include <string.h> // strlen
22
#include <pistache/ps_strl.h> // PS_STRLCPY and PS_STRLCAT
23
24
#include <algorithm> // std::max
25
#include <vector>
26
27
/* ------------------------------------------------------------------------- */
28
29
extern "C" char * ps_basename_r(const char * path, char * bname)
30
{
31
    int tmp_errno = errno; // preserve errno
32
33
    if (!bname)
34
        return(nullptr);
35
    bname[0] = 0;
36
37
    if (!path)
38
        return(bname);
39
    size_t path_len = strlen(&(path[0]));
40
    if (!path_len)
41
        return(bname);
42
43
    char drive[24]; drive[0] = 0;
44
45
    const size_t mpl = std::max<size_t>(path_len+16, PST_MAXPATHLEN+16);
46
47
    std::vector<char> dirname_buff(mpl);
48
    dirname_buff[0] = 0;
49
50
    std::vector<char> fname_buff(mpl);
51
    fname_buff[0] = 0;
52
53
    std::vector<char> ext_buff(mpl);
54
    ext_buff[0] = 0;
55
56
    errno_t sp_res = _splitpath_s(path,
57
                                  &(drive[0]), sizeof(drive)-8,
58
                                  dirname_buff.data(), mpl-8,
59
                                  fname_buff.data(), mpl-8,
60
                                  ext_buff.data(), mpl-8);
61
62
    if (sp_res)
63
    {
64
        // sp_res is an error code
65
        // Don't log, since this function called by logging
66
67
        errno = tmp_errno;
68
        return(nullptr);
69
    }
70
71
    PS_STRLCPY(bname, fname_buff.data(), PST_MAXPATHLEN);
72
    PS_STRLCAT(bname, ext_buff.data(), PST_MAXPATHLEN);
73
74
    errno = tmp_errno;
75
    return(bname);
76
}
77
78
79
/* ------------------------------------------------------------------------- */
80
81
#else
82
83
/* ------------------------------------------------------------------------- */
84
// Linux or BSD
85
86
#include <mutex>
87
#include <libgen.h> // for basename
88
#include <string.h> // strlen
89
#include <stdlib.h> // malloc
90
91
/* ------------------------------------------------------------------------- */
92
93
#define PS_BASENAME_R ps_basename_r
94
static std::mutex ps_basename_r_mutex;
95
extern "C" char * ps_basename_r(const char * path, char * bname)
96
0
{
97
0
    if (!bname)
98
0
        return(nullptr);
99
100
0
    bname[0] = 0;
101
102
0
    std::lock_guard<std::mutex> l_guard(ps_basename_r_mutex);
103
104
0
    char * path_copy =
105
0
        reinterpret_cast<char *>(malloc((path ? strlen(path) : 0) + 6));
106
0
    strcpy(path_copy, path); // since basename may change path contents
107
108
0
    char * bname_res = basename(path_copy);
109
110
0
    if (bname_res)
111
0
       strcpy(&(bname[0]), bname_res);
112
113
0
    free(path_copy);
114
0
    return(bname);
115
0
}
116
117
118
/* ------------------------------------------------------------------------- */
119
120
#endif // of ifdef _IS_WINDOWS... else...
121
122
/* ------------------------------------------------------------------------- */
123
124
#endif // of ifndef __APPLE__