     return nil
   }
 
-  let stampSuffix = { (buffer: UnsafeMutablePointer<CChar>) in
-    let alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
-    _ = (0 ..< 6).map { index in
-        buffer[index] = CChar(alpha.shuffled().randomElement()!.utf8.first!)
-    }
-  }
-
   // Attempt to create the directory
   var retries: Int = 100
   repeat {
-    stampSuffix(template + length - 6)
+    __randname(template + length - 6)
     if _mkdir(template) == 0 {
       return template
     }

    _ template: UnsafeMutablePointer<CChar>?,
    _ suffixlen: Int32
) -> Int32 {
  guard let template = template else { return -EINVAL }
  return String(cString: template).withCString(encodedAs: UTF16.self) {
    let capacity: Int = wcslen($0) + 1
    return $0.withMemoryRebound(to: wchar_t.self, capacity: capacity) {
      guard _wmktemp_s(UnsafeMutablePointer(mutating: $0), capacity) == 0 else {
        return -EINVAL
      }

      var fd: Int32 = -1
      _wsopen_s(&fd, $0, _O_RDWR | _O_CREAT | _O_BINARY | _O_NOINHERIT,
                _SH_DENYNO, _S_IREAD | _S_IWRITE)

      String(decodingCString: $0, as: UTF16.self).utf8CString.withUnsafeBytes {
        template.assign(from: $0.bindMemory(to: CChar.self).baseAddress!,
                        count: $0.count)
      }
      return fd
    }
  }
}
#endif

  fatalError("realpath is unimplemented")
}

private func __randname(_ buffer: UnsafeMutablePointer<CChar>) {
  let alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  _ = (0 ..< 6).map { index in
      buffer[index] = CChar(alpha.shuffled().randomElement()!.utf8.first!)
  }
}

// char *mkdtemp(char *template);
// NOTE(compnerd) this is unsafe!  This assumes that the template is *ASCII*.
public func mkdtemp(
