選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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