GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: fx_directory_short_name_get_extended.c Lines: 25 25 100.0 %
Date: 2026-03-06 18:49:02 Branches: 24 24 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_directory.h"
30
31
32
/**************************************************************************/
33
/*                                                                        */
34
/*  FUNCTION                                               RELEASE        */
35
/*                                                                        */
36
/*    _fx_directory_short_name_get_extended               PORTABLE C      */
37
/*                                                           6.1          */
38
/*  AUTHOR                                                                */
39
/*                                                                        */
40
/*    William E. Lamie, Microsoft Corporation                             */
41
/*                                                                        */
42
/*  DESCRIPTION                                                           */
43
/*                                                                        */
44
/*    This function gets the short file name via the supplied long file   */
45
/*    name. If the long file name is really a short file name, the short  */
46
/*    file name will be returned.                                         */
47
/*                                                                        */
48
/*  INPUT                                                                 */
49
/*                                                                        */
50
/*    media_ptr                             Media control block pointer   */
51
/*    long_file_name                        Pointer to long (max 255) name*/
52
/*    short_file_name                       Pointer to short (8.3) name   */
53
/*    short_file_name_length                Buffer length for short name  */
54
/*                                                                        */
55
/*  OUTPUT                                                                */
56
/*                                                                        */
57
/*    return status                                                       */
58
/*                                                                        */
59
/*  CALLS                                                                 */
60
/*                                                                        */
61
/*    _fx_directory_search                  Search for the file name in   */
62
/*                                                                        */
63
/*  CALLED BY                                                             */
64
/*                                                                        */
65
/*    Application Code                                                    */
66
/*                                                                        */
67
/**************************************************************************/
68
47
UINT  _fx_directory_short_name_get_extended(FX_MEDIA *media_ptr, CHAR *long_file_name, CHAR *short_file_name, UINT short_file_name_length)
69
{
70
71
UINT         status;
72
UINT         i;
73
FX_DIR_ENTRY dir_entry;
74
75
76
    /* Setup pointer to media name buffer.  */
77
47
    dir_entry.fx_dir_entry_name =  media_ptr -> fx_media_name_buffer + FX_MAX_LONG_NAME_LEN;
78
79
    /* Clear the short name string.  */
80
47
    dir_entry.fx_dir_entry_short_name[0] =  0;
81
82
    /* If trace is enabled, insert this event into the trace buffer.  */
83
    FX_TRACE_IN_LINE_INSERT(FX_TRACE_DIRECTORY_SHORT_NAME_GET, media_ptr, long_file_name, short_file_name, 0, FX_TRACE_DIRECTORY_EVENTS, 0, 0)
84
85
    /* Protect against other threads accessing the media.  */
86
47
    FX_PROTECT
87
88
    /* Search the system for the supplied short directory name.  */
89
47
    status =  _fx_directory_search(media_ptr, long_file_name, &dir_entry, FX_NULL, FX_NULL);
90
91
    /* Determine if the search was successful.  */
92
47
    if (status != FX_SUCCESS)
93
    {
94
95
        /* Release media protection.  */
96
1
        FX_UNPROTECT
97
98
        /* Return the error code.  */
99
1
        return(status);
100
    }
101
102
    /* Determine if there is a short name.  */
103
46
    if (dir_entry.fx_dir_entry_short_name[0])
104
    {
105
106
        /* Yes, there is a short name. Copy the short file name portion into the destination string.  */
107
43
        i =  0;
108

289
        while ((i < (FX_MAX_SHORT_NAME_LEN - 1)) && (dir_entry.fx_dir_entry_short_name[i]) && (i < (short_file_name_length - 1)))
109
        {
110
111
            /* Copy a character of the long file name into the destination name.  */
112
246
            short_file_name[i] =   dir_entry.fx_dir_entry_short_name[i];
113
114
            /* Move to next character.  */
115
246
            i++;
116
        }
117
118
        /* Ensure the short file name is NULL terminated.  */
119
43
        short_file_name[i] =  FX_NULL;
120
121
        /* Check if the buffer is too short for the name.  */
122

43
        if ((i == (short_file_name_length - 1)) && (dir_entry.fx_dir_entry_short_name[i]))
123
        {
124
125
            /* Buffer too short, return error.  */
126
1
            status =  FX_BUFFER_ERROR;
127
        }
128
    }
129
    else
130
    {
131
132
        /* There is no long file name, simply copy the long file name into the short file name destination.  */
133
3
        i =  0;
134

25
        while ((i < (FX_MAX_SHORT_NAME_LEN - 1)) && (dir_entry.fx_dir_entry_name[i]) && (i < (short_file_name_length - 1)))
135
        {
136
137
            /* Copy a character of the file name into the destination name.  */
138
22
            short_file_name[i] =   dir_entry.fx_dir_entry_name[i];
139
140
            /* Move to next character.  */
141
22
            i++;
142
        }
143
144
        /* Ensure the short file name is NULL terminated.  */
145
3
        short_file_name[i] =  FX_NULL;
146
147
        /* Check if the buffer is too short for the name.  */
148

3
        if ((i == (short_file_name_length - 1)) && (dir_entry.fx_dir_entry_name[i]))
149
        {
150
151
            /* Buffer too short, return error.  */
152
1
            status =  FX_BUFFER_ERROR;
153
        }
154
    }
155
156
    /* Release media protection.  */
157
46
    FX_UNPROTECT
158
159
    /* Return the completion status.  */
160
46
    return(status);
161
}
162