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 /**************************************************************************/
_fx_directory_short_name_get_extended(FX_MEDIA * media_ptr,CHAR * long_file_name,CHAR * short_file_name,UINT short_file_name_length)75 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     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     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     FX_PROTECT
94 
95     /* Search the system for the supplied short directory name.  */
96     status =  _fx_directory_search(media_ptr, long_file_name, &dir_entry, FX_NULL, FX_NULL);
97 
98     /* Determine if the search was successful.  */
99     if (status != FX_SUCCESS)
100     {
101 
102         /* Release media protection.  */
103         FX_UNPROTECT
104 
105         /* Return the error code.  */
106         return(status);
107     }
108 
109     /* Determine if there is a short name.  */
110     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         i =  0;
115         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             short_file_name[i] =   dir_entry.fx_dir_entry_short_name[i];
120 
121             /* Move to next character.  */
122             i++;
123         }
124 
125         /* Ensure the short file name is NULL terminated.  */
126         short_file_name[i] =  FX_NULL;
127 
128         /* Check if the buffer is too short for the name.  */
129         if ((i == (short_file_name_length - 1)) && (dir_entry.fx_dir_entry_short_name[i]))
130         {
131 
132             /* Buffer too short, return error.  */
133             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         i =  0;
141         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             short_file_name[i] =   dir_entry.fx_dir_entry_name[i];
146 
147             /* Move to next character.  */
148             i++;
149         }
150 
151         /* Ensure the short file name is NULL terminated.  */
152         short_file_name[i] =  FX_NULL;
153 
154         /* Check if the buffer is too short for the name.  */
155         if ((i == (short_file_name_length - 1)) && (dir_entry.fx_dir_entry_name[i]))
156         {
157 
158             /* Buffer too short, return error.  */
159             status =  FX_BUFFER_ERROR;
160         }
161     }
162 
163     /* Release media protection.  */
164     FX_UNPROTECT
165 
166     /* Return the completion status.  */
167     return(status);
168 }
169 
170