GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: fx_directory_name_extract.c Lines: 22 22 100.0 %
Date: 2026-03-06 18:49:02 Branches: 20 20 100.0 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 * Copyright (c) 2026-present Eclipse ThreadX contributors
4
 *
5
 * This program and the accompanying materials are made available under the
6
 * terms of the MIT License which is available at
7
 * https://opensource.org/licenses/MIT.
8
 *
9
 * SPDX-License-Identifier: MIT
10
 **************************************************************************/
11
12
13
/**************************************************************************/
14
/**************************************************************************/
15
/**                                                                       */
16
/** FileX Component                                                       */
17
/**                                                                       */
18
/**   Directory                                                           */
19
/**                                                                       */
20
/**************************************************************************/
21
/**************************************************************************/
22
23
#define FX_SOURCE_CODE
24
25
26
/* Include necessary system files.  */
27
28
#include "fx_api.h"
29
#include "fx_system.h"
30
#include "fx_directory.h"
31
#include "fx_utility.h"
32
33
34
/**************************************************************************/
35
/*                                                                        */
36
/*  FUNCTION                                               RELEASE        */
37
/*                                                                        */
38
/*    _fx_directory_name_extract                          PORTABLE C      */
39
/*                                                           6.1          */
40
/*  AUTHOR                                                                */
41
/*                                                                        */
42
/*    William E. Lamie, Microsoft Corporation                             */
43
/*                                                                        */
44
/*  DESCRIPTION                                                           */
45
/*                                                                        */
46
/*    This function extracts the file name from the supplied input        */
47
/*    string.  If there is nothing left after the extracted name, a NULL  */
48
/*    is returned to the caller.  Otherwise, if something is left, a      */
49
/*    pointer to it is returned.                                          */
50
/*                                                                        */
51
/*  INPUT                                                                 */
52
/*                                                                        */
53
/*    source_ptr                           Source string pointer          */
54
/*    dest_ptr                             Destination string pointer     */
55
/*                                                                        */
56
/*  OUTPUT                                                                */
57
/*                                                                        */
58
/*    Pointer to Next Name                 (if multiple directories)      */
59
/*                                                                        */
60
/*  CALLS                                                                 */
61
/*                                                                        */
62
/*    None                                                                */
63
/*                                                                        */
64
/*  CALLED BY                                                             */
65
/*                                                                        */
66
/*    FileX System Functions                                              */
67
/*                                                                        */
68
/**************************************************************************/
69
261818
CHAR  *_fx_directory_name_extract(CHAR *source_ptr, CHAR *dest_ptr)
70
{
71
72
UINT i;
73
74
75
    /* Set the destination string to NULL.  */
76
261818
    dest_ptr[0] = 0;
77
78
    /* Is a backslash present?  */
79

261818
    if ((*source_ptr == '\\') || (*source_ptr == '/'))
80
    {
81
82
        /* Advance the string pointer.  */
83
58666
        source_ptr++;
84
    }
85
86
    /* Loop to remove any leading spaces.  */
87
262026
    while (*source_ptr == ' ')
88
    {
89
90
        /* Position past leading space.  */
91
208
        source_ptr++;
92
    }
93
94
    /* Loop to extract the name.  */
95
261818
    i = 0;
96
2584437
    while (*source_ptr)
97
    {
98
99
        /* If another backslash is present, break the loop.  */
100

2337800
        if ((*source_ptr == '\\') || (*source_ptr == '/'))
101
        {
102
            break;
103
        }
104
105
        /* Long name can be at most 255 characters, but are further limited by the
106
           FX_MAX_LONG_NAME_LEN define.  */
107
2322620
        if (i == FX_MAX_LONG_NAME_LEN - 1)
108
        {
109
1
            break;
110
        }
111
112
        /* Store the character.  */
113
2322619
        dest_ptr[i] =  *source_ptr++;
114
115
        /* Increment the character counter.  */
116
2322619
        i++;
117
    }
118
119
    /* NULL-terminate the string.  */
120
261818
    dest_ptr[i] =  0;
121
122
    /* Determine if we can backup to the previous character.  */
123
261818
    if (i)
124
    {
125
126
        /* Yes, we can move backwards.  */
127
261817
        i--;
128
    }
129
130
    /* Get rid of trailing blanks in the destination string.  */
131
261869
    while (dest_ptr[i] == ' ')
132
    {
133
134
        /* Set this entry to NULL.  */
135
51
        dest_ptr[i] =  0;
136
137
        /* Backup to the next character. Since leading spaces have been removed,
138
           we know that the index is always greater than 1.  */
139
51
        i--;
140
    }
141
142
    /* Determine if the source string is now at the end.  */
143
261818
    if (*source_ptr == 0)
144
    {
145
146
        /* Yes, return a NULL pointer.  */
147
246637
        source_ptr = FX_NULL;
148
    }
149
150
    /* Return the last pointer position in the source.  */
151
261818
    return(source_ptr);
152
}
153