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_system.h"
29 #include "fx_directory.h"
30 #include "fx_utility.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _fx_directory_name_extract                          PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    William E. Lamie, Microsoft Corporation                             */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function extracts the file name from the supplied input        */
46 /*    string.  If there is nothing left after the extracted name, a NULL  */
47 /*    is returned to the caller.  Otherwise, if something is left, a      */
48 /*    pointer to it is returned.                                          */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    source_ptr                           Source string pointer          */
53 /*    dest_ptr                             Destination string pointer     */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    Pointer to Next Name                 (if multiple directories)      */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    None                                                                */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    FileX System Functions                                              */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
72 /*  09-30-2020     William E. Lamie         Modified comment(s),          */
73 /*                                            resulting in version 6.1    */
74 /*                                                                        */
75 /**************************************************************************/
_fx_directory_name_extract(CHAR * source_ptr,CHAR * dest_ptr)76 CHAR  *_fx_directory_name_extract(CHAR *source_ptr, CHAR *dest_ptr)
77 {
78 
79 UINT i;
80 
81 
82     /* Set the destination string to NULL.  */
83     dest_ptr[0] = 0;
84 
85     /* Is a backslash present?  */
86     if ((*source_ptr == '\\') || (*source_ptr == '/'))
87     {
88 
89         /* Advance the string pointer.  */
90         source_ptr++;
91     }
92 
93     /* Loop to remove any leading spaces.  */
94     while (*source_ptr == ' ')
95     {
96 
97         /* Position past leading space.  */
98         source_ptr++;
99     }
100 
101     /* Loop to extract the name.  */
102     i = 0;
103     while (*source_ptr)
104     {
105 
106         /* If another backslash is present, break the loop.  */
107         if ((*source_ptr == '\\') || (*source_ptr == '/'))
108         {
109             break;
110         }
111 
112         /* Long name can be at most 255 characters, but are further limited by the
113            FX_MAX_LONG_NAME_LEN define.  */
114         if (i == FX_MAX_LONG_NAME_LEN - 1)
115         {
116             break;
117         }
118 
119         /* Store the character.  */
120         dest_ptr[i] =  *source_ptr++;
121 
122         /* Increment the character counter.  */
123         i++;
124     }
125 
126     /* NULL-terminate the string.  */
127     dest_ptr[i] =  0;
128 
129     /* Determine if we can backup to the previous character.  */
130     if (i)
131     {
132 
133         /* Yes, we can move backwards.  */
134         i--;
135     }
136 
137     /* Get rid of trailing blanks in the destination string.  */
138     while (dest_ptr[i] == ' ')
139     {
140 
141         /* Set this entry to NULL.  */
142         dest_ptr[i] =  0;
143 
144         /* Backup to the next character. Since leading spaces have been removed,
145            we know that the index is always greater than 1.  */
146         i--;
147     }
148 
149     /* Determine if the source string is now at the end.  */
150     if (*source_ptr == 0)
151     {
152 
153         /* Yes, return a NULL pointer.  */
154         source_ptr = FX_NULL;
155     }
156 
157     /* Return the last pointer position in the source.  */
158     return(source_ptr);
159 }
160 
161