GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: fx_directory_short_name_get_extended.c Lines: 25 25 100.0 %
Date: 2024-03-11 05:15:45 Branches: 24 24 100.0 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 *
4
 * This program and the accompanying materials are made available under the
5
 * terms of the MIT License which is available at
6
 * https://opensource.org/licenses/MIT.
7
 *
8
 * SPDX-License-Identifier: MIT
9
 **************************************************************************/
10
11
12
/**************************************************************************/
13
/**************************************************************************/
14
/**                                                                       */
15
/** FileX Component                                                       */
16
/**                                                                       */
17
/**   Directory                                                           */
18
/**                                                                       */
19
/**************************************************************************/
20
/**************************************************************************/
21
22
#define FX_SOURCE_CODE
23
24
25
/* Include necessary system files.  */
26
27
#include "fx_api.h"
28
#include "fx_directory.h"
29
30
31
/**************************************************************************/
32
/*                                                                        */
33
/*  FUNCTION                                               RELEASE        */
34
/*                                                                        */
35
/*    _fx_directory_short_name_get_extended               PORTABLE C      */
36
/*                                                           6.1          */
37
/*  AUTHOR                                                                */
38
/*                                                                        */
39
/*    William E. Lamie, Microsoft Corporation                             */
40
/*                                                                        */
41
/*  DESCRIPTION                                                           */
42
/*                                                                        */
43
/*    This function gets the short file name via the supplied long file   */
44
/*    name. If the long file name is really a short file name, the short  */
45
/*    file name will be returned.                                         */
46
/*                                                                        */
47
/*  INPUT                                                                 */
48
/*                                                                        */
49
/*    media_ptr                             Media control block pointer   */
50
/*    long_file_name                        Pointer to long (max 255) name*/
51
/*    short_file_name                       Pointer to short (8.3) name   */
52
/*    short_file_name_length                Buffer length for short name  */
53
/*                                                                        */
54
/*  OUTPUT                                                                */
55
/*                                                                        */
56
/*    return status                                                       */
57
/*                                                                        */
58
/*  CALLS                                                                 */
59
/*                                                                        */
60
/*    _fx_directory_search                  Search for the file name in   */
61
/*                                                                        */
62
/*  CALLED BY                                                             */
63
/*                                                                        */
64
/*    Application Code                                                    */
65
/*                                                                        */
66
/*  RELEASE HISTORY                                                       */
67
/*                                                                        */
68
/*    DATE              NAME                      DESCRIPTION             */
69
/*                                                                        */
70
/*  05-19-2020     William E. Lamie         Initial Version 6.0           */
71
/*  09-30-2020     William E. Lamie         Modified comment(s),          */
72
/*                                            resulting in version 6.1    */
73
/*                                                                        */
74
/**************************************************************************/
75
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)
76
{
77
78
UINT         status;
79
UINT         i;
80
FX_DIR_ENTRY dir_entry;
81
82
83
    /* Setup pointer to media name buffer.  */
84
47
    dir_entry.fx_dir_entry_name =  media_ptr -> fx_media_name_buffer + FX_MAX_LONG_NAME_LEN;
85
86
    /* Clear the short name string.  */
87
47
    dir_entry.fx_dir_entry_short_name[0] =  0;
88
89
    /* If trace is enabled, insert this event into the trace buffer.  */
90
    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)
91
92
    /* Protect against other threads accessing the media.  */
93
47
    FX_PROTECT
94
95
    /* Search the system for the supplied short directory name.  */
96
47
    status =  _fx_directory_search(media_ptr, long_file_name, &dir_entry, FX_NULL, FX_NULL);
97
98
    /* Determine if the search was successful.  */
99
47
    if (status != FX_SUCCESS)
100
    {
101
102
        /* Release media protection.  */
103
1
        FX_UNPROTECT
104
105
        /* Return the error code.  */
106
1
        return(status);
107
    }
108
109
    /* Determine if there is a short name.  */
110
46
    if (dir_entry.fx_dir_entry_short_name[0])
111
    {
112
113
        /* Yes, there is a short name. Copy the short file name portion into the destination string.  */
114
43
        i =  0;
115

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

43
        if ((i == (short_file_name_length - 1)) && (dir_entry.fx_dir_entry_short_name[i]))
130
        {
131
132
            /* Buffer too short, return error.  */
133
1
            status =  FX_BUFFER_ERROR;
134
        }
135
    }
136
    else
137
    {
138
139
        /* There is no long file name, simply copy the long file name into the short file name destination.  */
140
3
        i =  0;
141

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

3
        if ((i == (short_file_name_length - 1)) && (dir_entry.fx_dir_entry_name[i]))
156
        {
157
158
            /* Buffer too short, return error.  */
159
1
            status =  FX_BUFFER_ERROR;
160
        }
161
    }
162
163
    /* Release media protection.  */
164
46
    FX_UNPROTECT
165
166
    /* Return the completion status.  */
167
46
    return(status);
168
}
169