1
0

path.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*++
  2. Copyright (c) 2013 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. path.c
  9. Abstract:
  10. This module contains file path related functions for the C library, such
  11. as the libgen.h functions.
  12. Author:
  13. Evan Green 16-Jul-2013
  14. Environment:
  15. User Mode C Library
  16. --*/
  17. //
  18. // ------------------------------------------------------------------- Includes
  19. //
  20. #include "libcp.h"
  21. #include <assert.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. //
  25. // ---------------------------------------------------------------- Definitions
  26. //
  27. //
  28. // Define the minimum size for the path split buffer in bytes.
  29. //
  30. #define PATH_SPLIT_BUFFER_MINIMUM_SIZE 16
  31. //
  32. // ------------------------------------------------------ Data Type Definitions
  33. //
  34. //
  35. // ----------------------------------------------- Internal Function Prototypes
  36. //
  37. BOOL
  38. ClpPathSplit (
  39. PSTR Path,
  40. PSTR *DirectoryName,
  41. PSTR *BaseName
  42. );
  43. //
  44. // -------------------------------------------------------------------- Globals
  45. //
  46. //
  47. // Store the global path split buffer.
  48. //
  49. PSTR ClPathSplitBuffer = NULL;
  50. ULONG ClPathSplitBufferSize = 0;
  51. //
  52. // ------------------------------------------------------------------ Functions
  53. //
  54. LIBC_API
  55. char *
  56. basename (
  57. char *Path
  58. )
  59. /*++
  60. Routine Description:
  61. This routine takes in a path and returns a pointer to the final component
  62. of the pathname, deleting any trailing '/' characters. This routine is
  63. neither re-entrant nor thread safe.
  64. Arguments:
  65. Path - Supplies a pointer to the path to split.
  66. Return Value:
  67. Returns a pointer to the final path component name on success.
  68. NULL on failure.
  69. --*/
  70. {
  71. PSTR BaseName;
  72. BOOL Result;
  73. Result = ClpPathSplit(Path, NULL, &BaseName);
  74. if (Result == FALSE) {
  75. return NULL;
  76. }
  77. return BaseName;
  78. }
  79. LIBC_API
  80. char *
  81. dirname (
  82. char *Path
  83. )
  84. /*++
  85. Routine Description:
  86. This routine takes in a path and returns a pointer to the pathname of the
  87. parent directory of that file, deleting any trailing '/' characters. If the
  88. path does not contain a '/', or is null or empty, then this routine returns
  89. a pointer to the string ".". This routine is neither re-entrant nor thread
  90. safe.
  91. Arguments:
  92. Path - Supplies a pointer to the path to split.
  93. Return Value:
  94. Returns a pointer to the name of the directory containing that file on
  95. success.
  96. NULL on failure.
  97. --*/
  98. {
  99. PSTR DirectoryName;
  100. BOOL Result;
  101. Result = ClpPathSplit(Path, &DirectoryName, NULL);
  102. if (Result == FALSE) {
  103. return NULL;
  104. }
  105. return DirectoryName;
  106. }
  107. //
  108. // --------------------------------------------------------- Internal Functions
  109. //
  110. BOOL
  111. ClpPathSplit (
  112. PSTR Path,
  113. PSTR *DirectoryName,
  114. PSTR *BaseName
  115. )
  116. /*++
  117. Routine Description:
  118. This routine takes in a path and splits it into a directory component and
  119. a final name component. Paths that don't have any slashes in them will
  120. have a directory name of ".". Trailing slashes are not considered part of
  121. the path. This routine is neither re-entrant nor thread safe.
  122. Arguments:
  123. Path - Supplies a pointer to the path to split.
  124. DirectoryName - Supplies a pointer where the directory portion of the path
  125. will be returned. This pointer is allocated from a global buffer, and
  126. must not be modified or freed by the caller. In addition, it may be
  127. invalidated by future calls to this routine.
  128. BaseName - Supplies a pointer where the final name portion of the path
  129. will be returned. This pointer is allocated from a global buffer, and
  130. must not be modified or freed by the caller. In addition, it may be
  131. invalidated by future calls to this routine.
  132. Return Value:
  133. TRUE on success.
  134. FALSE on allocation failure.
  135. --*/
  136. {
  137. ULONG BufferSize;
  138. PSTR Directory;
  139. ULONG DirectoryEndIndex;
  140. BOOL DirectoryHadSlash;
  141. PSTR Name;
  142. ULONG NameEndIndex;
  143. ULONG NameStartIndex;
  144. PSTR NewBuffer;
  145. ULONG PathLength;
  146. BOOL Result;
  147. Directory = NULL;
  148. Name = NULL;
  149. PathLength = 0;
  150. if (Path != NULL) {
  151. PathLength = strlen(Path);
  152. }
  153. BufferSize = PathLength + sizeof(".") + 1;
  154. if (BufferSize < PATH_SPLIT_BUFFER_MINIMUM_SIZE) {
  155. BufferSize = PATH_SPLIT_BUFFER_MINIMUM_SIZE;
  156. }
  157. //
  158. // Reallocate the buffer if needed.
  159. //
  160. if (ClPathSplitBufferSize < BufferSize) {
  161. NewBuffer = malloc(BufferSize);
  162. if (NewBuffer == NULL) {
  163. Result = FALSE;
  164. goto PathSplitEnd;
  165. }
  166. if (ClPathSplitBuffer != NULL) {
  167. free(ClPathSplitBuffer);
  168. }
  169. ClPathSplitBuffer = NewBuffer;
  170. ClPathSplitBufferSize = BufferSize;
  171. }
  172. Directory = ClPathSplitBuffer;
  173. //
  174. // First handle the completely empty case.
  175. //
  176. if (PathLength == 0) {
  177. memcpy(Directory, ".", sizeof("."));
  178. Name = Directory + sizeof(".");
  179. *Name = '\0';
  180. Result = TRUE;
  181. goto PathSplitEnd;
  182. }
  183. //
  184. // Find the end of the name by backing up past trailing slashes.
  185. //
  186. NameEndIndex = PathLength - 1;
  187. while ((NameEndIndex != 0) && (Path[NameEndIndex] == '/')) {
  188. NameEndIndex -= 1;
  189. }
  190. NameStartIndex = NameEndIndex;
  191. if (Path[NameEndIndex] != '/') {
  192. NameEndIndex += 1;
  193. }
  194. //
  195. // Find the start of the name by backing up until the beginning or a slash
  196. // is found.
  197. //
  198. while ((NameStartIndex != 0) && (Path[NameStartIndex] != '/')) {
  199. NameStartIndex -= 1;
  200. }
  201. DirectoryEndIndex = NameStartIndex;
  202. if ((Path[NameStartIndex] == '/') && (NameStartIndex < NameEndIndex)) {
  203. NameStartIndex += 1;
  204. }
  205. //
  206. // Find the end of the directory by backing up over any trailing slashes.
  207. //
  208. DirectoryHadSlash = FALSE;
  209. while ((DirectoryEndIndex != 0) && (Path[DirectoryEndIndex] == '/')) {
  210. DirectoryHadSlash = TRUE;
  211. DirectoryEndIndex -= 1;
  212. }
  213. //
  214. // If there was a slash at the current index, it must be index zero to have
  215. // made the loop stop. Set the boolean to indicate there was a slash.
  216. //
  217. if (Path[DirectoryEndIndex] == '/') {
  218. DirectoryHadSlash = TRUE;
  219. }
  220. //
  221. // If there was a slash, the loop either went one too far or found a slash
  222. // at zero. Either way give it an extra one.
  223. //
  224. if (DirectoryHadSlash != FALSE) {
  225. DirectoryEndIndex += 1;
  226. }
  227. //
  228. // Ok, all the important indices are found. Copy the portions in.
  229. //
  230. if (DirectoryHadSlash == FALSE) {
  231. memcpy(Directory, ".", sizeof("."));
  232. Name = Directory + sizeof(".");
  233. } else {
  234. assert(DirectoryEndIndex != 0);
  235. memcpy(Directory, Path, DirectoryEndIndex);
  236. Directory[DirectoryEndIndex] = '\0';
  237. Name = Directory + DirectoryEndIndex + 1;
  238. }
  239. //
  240. // Copy the name.
  241. //
  242. if (NameEndIndex != NameStartIndex) {
  243. assert(NameEndIndex > NameStartIndex);
  244. memcpy(Name, Path + NameStartIndex, NameEndIndex - NameStartIndex);
  245. Name[NameEndIndex - NameStartIndex] = '\0';
  246. } else {
  247. *Name = '\0';
  248. }
  249. Result = TRUE;
  250. PathSplitEnd:
  251. if (DirectoryName != NULL) {
  252. *DirectoryName = Directory;
  253. }
  254. if (BaseName != NULL) {
  255. *BaseName = Name;
  256. }
  257. return Result;
  258. }