Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

142 řádky
4.0 KiB

  1. /* $LP: LPlib/source/LPdir_win.c,v 1.10 2004/08/26 13:36:05 _cvs_levitte Exp $ */
  2. /*
  3. * Copyright (c) 2004, Richard Levitte <richard@levitte.org>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "directory.h"
  28. #if defined(OPENSSL_WINDOWS)
  29. #pragma warning(push, 3)
  30. #include <windows.h>
  31. #pragma warning(pop)
  32. #include <errno.h>
  33. #include <string.h>
  34. #include <tchar.h>
  35. #ifndef NAME_MAX
  36. #define NAME_MAX 255
  37. #endif
  38. struct OPENSSL_dir_context_st {
  39. WIN32_FIND_DATA ctx;
  40. HANDLE handle;
  41. char entry_name[NAME_MAX + 1];
  42. };
  43. const char *OPENSSL_DIR_read(OPENSSL_DIR_CTX **ctx, const char *directory) {
  44. if (ctx == NULL || directory == NULL) {
  45. errno = EINVAL;
  46. return 0;
  47. }
  48. errno = 0;
  49. if (*ctx == NULL) {
  50. *ctx = malloc(sizeof(OPENSSL_DIR_CTX));
  51. if (*ctx == NULL) {
  52. errno = ENOMEM;
  53. return 0;
  54. }
  55. memset(*ctx, 0, sizeof(OPENSSL_DIR_CTX));
  56. if (sizeof(TCHAR) != sizeof(char)) {
  57. TCHAR *wdir = NULL;
  58. /* len_0 denotes string length *with* trailing 0 */
  59. size_t index = 0, len_0 = strlen(directory) + 1;
  60. wdir = (TCHAR *)malloc(len_0 * sizeof(TCHAR));
  61. if (wdir == NULL) {
  62. free(*ctx);
  63. *ctx = NULL;
  64. errno = ENOMEM;
  65. return 0;
  66. }
  67. if (!MultiByteToWideChar(CP_ACP, 0, directory, len_0, (WCHAR *)wdir,
  68. len_0)) {
  69. for (index = 0; index < len_0; index++) {
  70. wdir[index] = (TCHAR)directory[index];
  71. }
  72. }
  73. (*ctx)->handle = FindFirstFile(wdir, &(*ctx)->ctx);
  74. free(wdir);
  75. } else {
  76. (*ctx)->handle = FindFirstFile((TCHAR *)directory, &(*ctx)->ctx);
  77. }
  78. if ((*ctx)->handle == INVALID_HANDLE_VALUE) {
  79. free(*ctx);
  80. *ctx = NULL;
  81. errno = EINVAL;
  82. return 0;
  83. }
  84. } else {
  85. if (FindNextFile((*ctx)->handle, &(*ctx)->ctx) == FALSE) {
  86. return 0;
  87. }
  88. }
  89. if (sizeof(TCHAR) != sizeof(char)) {
  90. TCHAR *wdir = (*ctx)->ctx.cFileName;
  91. size_t index, len_0 = 0;
  92. while (wdir[len_0] && len_0 < (sizeof((*ctx)->entry_name) - 1)) {
  93. len_0++;
  94. }
  95. len_0++;
  96. if (!WideCharToMultiByte(CP_ACP, 0, (WCHAR *)wdir, len_0,
  97. (*ctx)->entry_name, sizeof((*ctx)->entry_name),
  98. NULL, 0)) {
  99. for (index = 0; index < len_0; index++) {
  100. (*ctx)->entry_name[index] = (char)wdir[index];
  101. }
  102. }
  103. } else {
  104. strncpy((*ctx)->entry_name, (const char *)(*ctx)->ctx.cFileName,
  105. sizeof((*ctx)->entry_name) - 1);
  106. }
  107. (*ctx)->entry_name[sizeof((*ctx)->entry_name) - 1] = '\0';
  108. return (*ctx)->entry_name;
  109. }
  110. int OPENSSL_DIR_end(OPENSSL_DIR_CTX **ctx) {
  111. if (ctx != NULL && *ctx != NULL) {
  112. FindClose((*ctx)->handle);
  113. free(*ctx);
  114. *ctx = NULL;
  115. return 1;
  116. }
  117. errno = EINVAL;
  118. return 0;
  119. }
  120. #endif /* OPENSSL_WINDOWS */