+// Converts forward slashes to backslashes, removes duplicate slashes, and
+// removes slashes from both the start and end of the pathname.
+// Necessary because some (buggy?) EFI implementations produce "\/" strings
+// in pathnames, because some user inputs can produce duplicate directory
+// separators, and because we want consistent start and end slashes for
+// directory comparisons. A special case: If the PathName refers to root,
+// return "/", since some firmware implementations flake out if this
+// isn't present.
+VOID CleanUpPathNameSlashes(IN OUT CHAR16 *PathName) {
+ CHAR16 *NewName;
+ UINTN i, FinalChar = 0;
+ BOOLEAN LastWasSlash = FALSE;
+
+ NewName = AllocateZeroPool(sizeof(CHAR16) * (StrLen(PathName) + 2));
+ if (NewName != NULL) {
+ for (i = 0; i < StrLen(PathName); i++) {
+ if ((PathName[i] == L'/') || (PathName[i] == L'\\')) {
+ if ((!LastWasSlash) && (FinalChar != 0))
+ NewName[FinalChar++] = L'\\';
+ LastWasSlash = TRUE;
+ } else {
+ NewName[FinalChar++] = PathName[i];
+ LastWasSlash = FALSE;
+ } // if/else
+ } // for
+ NewName[FinalChar] = 0;
+ if ((FinalChar > 0) && (NewName[FinalChar - 1] == L'\\'))
+ NewName[--FinalChar] = 0;
+ if (FinalChar == 0) {
+ NewName[0] = L'\\';
+ NewName[1] = 0;
+ }
+ // Copy the transformed name back....
+ StrCpy(PathName, NewName);
+ FreePool(NewName);
+ } // if allocation OK
+} // CleanUpPathNameSlashes()
+