LPdir_win.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) 2004, Richard Levitte <[email protected]>
  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. /* We're most likely overcautious here, but let's reserve for
  32. broken WinCE headers and explicitly opt for UNICODE call.
  33. Keep in mind that our WinCE builds are compiled with -DUNICODE
  34. [as well as -D_UNICODE]. */
  35. #if defined(LP_SYS_WINCE) && !defined(FindFirstFile)
  36. # define FindFirstFile FindFirstFileW
  37. #endif
  38. #if defined(LP_SYS_WINCE) && !defined(FindNextFile)
  39. # define FindNextFile FindNextFileW
  40. #endif
  41. #ifndef NAME_MAX
  42. #define NAME_MAX 255
  43. #endif
  44. struct LP_dir_context_st
  45. {
  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. {
  54. errno = EINVAL;
  55. return 0;
  56. }
  57. errno = 0;
  58. if (*ctx == NULL)
  59. {
  60. const char *extdir = directory;
  61. char *extdirbuf = NULL;
  62. size_t dirlen = strlen (directory);
  63. if (dirlen == 0)
  64. {
  65. errno = ENOENT;
  66. return 0;
  67. }
  68. *ctx = (LP_DIR_CTX *)malloc(sizeof(LP_DIR_CTX));
  69. if (*ctx == NULL)
  70. {
  71. errno = ENOMEM;
  72. return 0;
  73. }
  74. memset(*ctx, '\0', sizeof(LP_DIR_CTX));
  75. if (directory[dirlen-1] != '*')
  76. {
  77. extdirbuf = (char *)malloc(dirlen + 3);
  78. if (extdirbuf == NULL)
  79. {
  80. free(*ctx);
  81. *ctx = NULL;
  82. errno = ENOMEM;
  83. return 0;
  84. }
  85. if (directory[dirlen-1] != '/' && directory[dirlen-1] != '\\')
  86. extdir = strcat(strcpy (extdirbuf,directory),"/*");
  87. else
  88. extdir = strcat(strcpy (extdirbuf,directory),"*");
  89. }
  90. if (sizeof(TCHAR) != sizeof(char))
  91. {
  92. TCHAR *wdir = NULL;
  93. /* len_0 denotes string length *with* trailing 0 */
  94. size_t index = 0,len_0 = strlen(extdir) + 1;
  95. wdir = (TCHAR *)calloc(len_0, sizeof(TCHAR));
  96. if (wdir == NULL)
  97. {
  98. if (extdirbuf != NULL)
  99. {
  100. free (extdirbuf);
  101. }
  102. free(*ctx);
  103. *ctx = NULL;
  104. errno = ENOMEM;
  105. return 0;
  106. }
  107. #ifdef LP_MULTIBYTE_AVAILABLE
  108. if (!MultiByteToWideChar(CP_ACP, 0, extdir, len_0, (WCHAR *)wdir, len_0))
  109. #endif
  110. for (index = 0; index < len_0; index++)
  111. wdir[index] = (TCHAR)extdir[index];
  112. (*ctx)->handle = FindFirstFile(wdir, &(*ctx)->ctx);
  113. free(wdir);
  114. }
  115. else
  116. {
  117. (*ctx)->handle = FindFirstFile((TCHAR *)extdir, &(*ctx)->ctx);
  118. }
  119. if (extdirbuf != NULL)
  120. {
  121. free (extdirbuf);
  122. }
  123. if ((*ctx)->handle == INVALID_HANDLE_VALUE)
  124. {
  125. free(*ctx);
  126. *ctx = NULL;
  127. errno = EINVAL;
  128. return 0;
  129. }
  130. }
  131. else
  132. {
  133. if (FindNextFile((*ctx)->handle, &(*ctx)->ctx) == FALSE)
  134. {
  135. return 0;
  136. }
  137. }
  138. if (sizeof(TCHAR) != sizeof(char))
  139. {
  140. TCHAR *wdir = (*ctx)->ctx.cFileName;
  141. size_t index, len_0 = 0;
  142. while (wdir[len_0] && len_0 < (sizeof((*ctx)->entry_name) - 1)) len_0++;
  143. len_0++;
  144. #ifdef LP_MULTIBYTE_AVAILABLE
  145. if (!WideCharToMultiByte(CP_ACP, 0, (WCHAR *)wdir, len_0, (*ctx)->entry_name,
  146. sizeof((*ctx)->entry_name), NULL, 0))
  147. #endif
  148. for (index = 0; index < len_0; index++)
  149. (*ctx)->entry_name[index] = (char)wdir[index];
  150. }
  151. else
  152. strncpy((*ctx)->entry_name, (const char *)(*ctx)->ctx.cFileName,
  153. sizeof((*ctx)->entry_name)-1);
  154. (*ctx)->entry_name[sizeof((*ctx)->entry_name)-1] = '\0';
  155. return (*ctx)->entry_name;
  156. }
  157. int LP_find_file_end(LP_DIR_CTX **ctx)
  158. {
  159. if (ctx != NULL && *ctx != NULL)
  160. {
  161. FindClose((*ctx)->handle);
  162. free(*ctx);
  163. *ctx = NULL;
  164. return 1;
  165. }
  166. errno = EINVAL;
  167. return 0;
  168. }