25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

139 lines
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. #include <windows.h>
  30. #include <tchar.h>
  31. #include <errno.h>
  32. #ifndef NAME_MAX
  33. #define NAME_MAX 255
  34. #endif
  35. struct OPENSSL_dir_context_st {
  36. WIN32_FIND_DATA ctx;
  37. HANDLE handle;
  38. char entry_name[NAME_MAX + 1];
  39. };
  40. const char *OPENSSL_DIR_read(OPENSSL_DIR_CTX **ctx, const char *directory) {
  41. if (ctx == NULL || directory == NULL) {
  42. errno = EINVAL;
  43. return 0;
  44. }
  45. errno = 0;
  46. if (*ctx == NULL) {
  47. *ctx = malloc(sizeof(OPENSSL_DIR_CTX));
  48. if (*ctx == NULL) {
  49. errno = ENOMEM;
  50. return 0;
  51. }
  52. memset(*ctx, 0, sizeof(OPENSSL_DIR_CTX));
  53. if (sizeof(TCHAR) != sizeof(char)) {
  54. TCHAR *wdir = NULL;
  55. /* len_0 denotes string length *with* trailing 0 */
  56. size_t index = 0, len_0 = strlen(directory) + 1;
  57. wdir = (TCHAR *)malloc(len_0 * sizeof(TCHAR));
  58. if (wdir == NULL) {
  59. free(*ctx);
  60. *ctx = NULL;
  61. errno = ENOMEM;
  62. return 0;
  63. }
  64. if (!MultiByteToWideChar(CP_ACP, 0, directory, len_0, (WCHAR *)wdir,
  65. len_0)) {
  66. for (index = 0; index < len_0; index++) {
  67. wdir[index] = (TCHAR)directory[index];
  68. }
  69. }
  70. (*ctx)->handle = FindFirstFile(wdir, &(*ctx)->ctx);
  71. free(wdir);
  72. } else {
  73. (*ctx)->handle = FindFirstFile((TCHAR *)directory, &(*ctx)->ctx);
  74. }
  75. if ((*ctx)->handle == INVALID_HANDLE_VALUE) {
  76. free(*ctx);
  77. *ctx = NULL;
  78. errno = EINVAL;
  79. return 0;
  80. }
  81. } else {
  82. if (FindNextFile((*ctx)->handle, &(*ctx)->ctx) == FALSE) {
  83. return 0;
  84. }
  85. }
  86. if (sizeof(TCHAR) != sizeof(char)) {
  87. TCHAR *wdir = (*ctx)->ctx.cFileName;
  88. size_t index, len_0 = 0;
  89. while (wdir[len_0] && len_0 < (sizeof((*ctx)->entry_name) - 1)) {
  90. len_0++;
  91. }
  92. len_0++;
  93. if (!WideCharToMultiByte(CP_ACP, 0, (WCHAR *)wdir, len_0,
  94. (*ctx)->entry_name, sizeof((*ctx)->entry_name),
  95. NULL, 0)) {
  96. for (index = 0; index < len_0; index++) {
  97. (*ctx)->entry_name[index] = (char)wdir[index];
  98. }
  99. }
  100. } else {
  101. strncpy((*ctx)->entry_name, (const char *)(*ctx)->ctx.cFileName,
  102. sizeof((*ctx)->entry_name) - 1);
  103. }
  104. (*ctx)->entry_name[sizeof((*ctx)->entry_name) - 1] = '\0';
  105. return (*ctx)->entry_name;
  106. }
  107. int OPENSSL_DIR_end(OPENSSL_DIR_CTX **ctx) {
  108. if (ctx != NULL && *ctx != NULL) {
  109. FindClose((*ctx)->handle);
  110. free(*ctx);
  111. *ctx = NULL;
  112. return 1;
  113. }
  114. errno = EINVAL;
  115. return 0;
  116. }
  117. #endif /* OPENSSL_WINDOWS */