LPdir_win.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2004, Richard Levitte <richard@levitte.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <windows.h>
  27. #include <tchar.h>
  28. #ifndef LPDIR_H
  29. # include "LPdir.h"
  30. #endif
  31. /*
  32. * We're most likely overcautious here, but let's reserve for broken WinCE
  33. * headers and explicitly opt for UNICODE call. Keep in mind that our WinCE
  34. * builds are compiled with -DUNICODE [as well as -D_UNICODE].
  35. */
  36. #if defined(LP_SYS_WINCE) && !defined(FindFirstFile)
  37. # define FindFirstFile FindFirstFileW
  38. #endif
  39. #if defined(LP_SYS_WINCE) && !defined(FindNextFile)
  40. # define FindNextFile FindNextFileW
  41. #endif
  42. #ifndef NAME_MAX
  43. # define NAME_MAX 255
  44. #endif
  45. struct LP_dir_context_st {
  46. WIN32_FIND_DATA ctx;
  47. HANDLE handle;
  48. char entry_name[NAME_MAX + 1];
  49. };
  50. const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
  51. {
  52. if (ctx == NULL || directory == NULL) {
  53. errno = EINVAL;
  54. return 0;
  55. }
  56. errno = 0;
  57. if (*ctx == NULL) {
  58. const char *extdir = directory;
  59. char *extdirbuf = NULL;
  60. size_t dirlen = strlen(directory);
  61. if (dirlen == 0) {
  62. errno = ENOENT;
  63. return 0;
  64. }
  65. *ctx = malloc(sizeof(**ctx));
  66. if (*ctx == NULL) {
  67. errno = ENOMEM;
  68. return 0;
  69. }
  70. memset(*ctx, 0, sizeof(**ctx));
  71. if (directory[dirlen - 1] != '*') {
  72. extdirbuf = (char *)malloc(dirlen + 3);
  73. if (extdirbuf == NULL) {
  74. free(*ctx);
  75. *ctx = NULL;
  76. errno = ENOMEM;
  77. return 0;
  78. }
  79. if (directory[dirlen - 1] != '/' && directory[dirlen - 1] != '\\')
  80. extdir = strcat(strcpy(extdirbuf, directory), "/*");
  81. else
  82. extdir = strcat(strcpy(extdirbuf, directory), "*");
  83. }
  84. if (sizeof(TCHAR) != sizeof(char)) {
  85. TCHAR *wdir = NULL;
  86. /* len_0 denotes string length *with* trailing 0 */
  87. size_t index = 0, len_0 = strlen(extdir) + 1;
  88. wdir = (TCHAR *)calloc(len_0, sizeof(TCHAR));
  89. if (wdir == NULL) {
  90. if (extdirbuf != NULL) {
  91. free(extdirbuf);
  92. }
  93. free(*ctx);
  94. *ctx = NULL;
  95. errno = ENOMEM;
  96. return 0;
  97. }
  98. #ifdef LP_MULTIBYTE_AVAILABLE
  99. if (!MultiByteToWideChar
  100. (CP_ACP, 0, extdir, len_0, (WCHAR *)wdir, len_0))
  101. #endif
  102. for (index = 0; index < len_0; index++)
  103. wdir[index] = (TCHAR)extdir[index];
  104. (*ctx)->handle = FindFirstFile(wdir, &(*ctx)->ctx);
  105. free(wdir);
  106. } else {
  107. (*ctx)->handle = FindFirstFile((TCHAR *)extdir, &(*ctx)->ctx);
  108. }
  109. if (extdirbuf != NULL) {
  110. free(extdirbuf);
  111. }
  112. if ((*ctx)->handle == INVALID_HANDLE_VALUE) {
  113. free(*ctx);
  114. *ctx = NULL;
  115. errno = EINVAL;
  116. return 0;
  117. }
  118. } else {
  119. if (FindNextFile((*ctx)->handle, &(*ctx)->ctx) == FALSE) {
  120. return 0;
  121. }
  122. }
  123. if (sizeof(TCHAR) != sizeof(char)) {
  124. TCHAR *wdir = (*ctx)->ctx.cFileName;
  125. size_t index, len_0 = 0;
  126. while (wdir[len_0] && len_0 < (sizeof((*ctx)->entry_name) - 1))
  127. len_0++;
  128. len_0++;
  129. #ifdef LP_MULTIBYTE_AVAILABLE
  130. if (!WideCharToMultiByte
  131. (CP_ACP, 0, (WCHAR *)wdir, len_0, (*ctx)->entry_name,
  132. sizeof((*ctx)->entry_name), NULL, 0))
  133. #endif
  134. for (index = 0; index < len_0; index++)
  135. (*ctx)->entry_name[index] = (char)wdir[index];
  136. } else
  137. strncpy((*ctx)->entry_name, (const char *)(*ctx)->ctx.cFileName,
  138. sizeof((*ctx)->entry_name) - 1);
  139. (*ctx)->entry_name[sizeof((*ctx)->entry_name) - 1] = '\0';
  140. return (*ctx)->entry_name;
  141. }
  142. int LP_find_file_end(LP_DIR_CTX **ctx)
  143. {
  144. if (ctx != NULL && *ctx != NULL) {
  145. FindClose((*ctx)->handle);
  146. free(*ctx);
  147. *ctx = NULL;
  148. return 1;
  149. }
  150. errno = EINVAL;
  151. return 0;
  152. }