LPdir_win.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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(FindFirstFile)
  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. struct dirent *direntry = NULL;
  53. if (ctx == NULL || directory == NULL) {
  54. errno = EINVAL;
  55. return 0;
  56. }
  57. errno = 0;
  58. if (*ctx == NULL) {
  59. const char *extdir = directory;
  60. char *extdirbuf = NULL;
  61. size_t dirlen = strlen(directory);
  62. if (dirlen == 0) {
  63. errno = ENOENT;
  64. return 0;
  65. }
  66. *ctx = (LP_DIR_CTX *)malloc(sizeof(LP_DIR_CTX));
  67. if (*ctx == NULL) {
  68. errno = ENOMEM;
  69. return 0;
  70. }
  71. memset(*ctx, '\0', sizeof(LP_DIR_CTX));
  72. if (directory[dirlen - 1] != '*') {
  73. extdirbuf = (char *)malloc(dirlen + 3);
  74. if (extdirbuf == NULL) {
  75. free(*ctx);
  76. *ctx = NULL;
  77. errno = ENOMEM;
  78. return 0;
  79. }
  80. if (directory[dirlen - 1] != '/' && directory[dirlen - 1] != '\\')
  81. extdir = strcat(strcpy(extdirbuf, directory), "/*");
  82. else
  83. extdir = strcat(strcpy(extdirbuf, directory), "*");
  84. }
  85. if (sizeof(TCHAR) != sizeof(char)) {
  86. TCHAR *wdir = NULL;
  87. /* len_0 denotes string length *with* trailing 0 */
  88. size_t index = 0, len_0 = strlen(extdir) + 1;
  89. wdir = (TCHAR *)calloc(len_0, sizeof(TCHAR));
  90. if (wdir == NULL) {
  91. if (extdirbuf != NULL) {
  92. free(extdirbuf);
  93. }
  94. free(*ctx);
  95. *ctx = NULL;
  96. errno = ENOMEM;
  97. return 0;
  98. }
  99. #ifdef LP_MULTIBYTE_AVAILABLE
  100. if (!MultiByteToWideChar
  101. (CP_ACP, 0, extdir, len_0, (WCHAR *)wdir, len_0))
  102. #endif
  103. for (index = 0; index < len_0; index++)
  104. wdir[index] = (TCHAR)extdir[index];
  105. (*ctx)->handle = FindFirstFile(wdir, &(*ctx)->ctx);
  106. free(wdir);
  107. } else {
  108. (*ctx)->handle = FindFirstFile((TCHAR *)extdir, &(*ctx)->ctx);
  109. }
  110. if (extdirbuf != NULL) {
  111. free(extdirbuf);
  112. }
  113. if ((*ctx)->handle == INVALID_HANDLE_VALUE) {
  114. free(*ctx);
  115. *ctx = NULL;
  116. errno = EINVAL;
  117. return 0;
  118. }
  119. } else {
  120. if (FindNextFile((*ctx)->handle, &(*ctx)->ctx) == FALSE) {
  121. return 0;
  122. }
  123. }
  124. if (sizeof(TCHAR) != sizeof(char)) {
  125. TCHAR *wdir = (*ctx)->ctx.cFileName;
  126. size_t index, len_0 = 0;
  127. while (wdir[len_0] && len_0 < (sizeof((*ctx)->entry_name) - 1))
  128. len_0++;
  129. len_0++;
  130. #ifdef LP_MULTIBYTE_AVAILABLE
  131. if (!WideCharToMultiByte
  132. (CP_ACP, 0, (WCHAR *)wdir, len_0, (*ctx)->entry_name,
  133. sizeof((*ctx)->entry_name), NULL, 0))
  134. #endif
  135. for (index = 0; index < len_0; index++)
  136. (*ctx)->entry_name[index] = (char)wdir[index];
  137. } else
  138. strncpy((*ctx)->entry_name, (const char *)(*ctx)->ctx.cFileName,
  139. sizeof((*ctx)->entry_name) - 1);
  140. (*ctx)->entry_name[sizeof((*ctx)->entry_name) - 1] = '\0';
  141. return (*ctx)->entry_name;
  142. }
  143. int LP_find_file_end(LP_DIR_CTX **ctx)
  144. {
  145. if (ctx != NULL && *ctx != NULL) {
  146. FindClose((*ctx)->handle);
  147. free(*ctx);
  148. *ctx = NULL;
  149. return 1;
  150. }
  151. errno = EINVAL;
  152. return 0;
  153. }