win32.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Win32 support routines for PhysicsFS.
  3. *
  4. * Please see the file LICENSE in the source's root directory.
  5. *
  6. * This file written by Ryan C. Gordon.
  7. */
  8. #if (defined __STRICT_ANSI__)
  9. #define __PHYSFS_DOING_STRICT_ANSI__
  10. #endif
  11. #include <windows.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <ctype.h>
  15. #define __PHYSICSFS_INTERNAL__
  16. #include "physfs_internal.h"
  17. const char *__PHYSFS_platformDirSeparator = "\\";
  18. static const char *win32strerror(void)
  19. {
  20. static TCHAR msgbuf[255];
  21. FormatMessage(
  22. FORMAT_MESSAGE_FROM_SYSTEM |
  23. FORMAT_MESSAGE_IGNORE_INSERTS,
  24. NULL,
  25. GetLastError(),
  26. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
  27. msgbuf,
  28. sizeof (msgbuf) / sizeof (TCHAR),
  29. NULL
  30. );
  31. return((const char *) msgbuf);
  32. } /* win32strerror */
  33. char **__PHYSFS_platformDetectAvailableCDs(void)
  34. {
  35. char **retval = (char **) malloc(sizeof (char *));
  36. int cd_count = 1; /* We count the NULL entry. */
  37. char drive_str[4] = "x:\\";
  38. for (drive_str[0] = 'A'; drive_str[0] <= 'Z'; drive_str[0]++)
  39. {
  40. if (GetDriveType(drive_str) == DRIVE_CDROM)
  41. {
  42. char **tmp = realloc(retval, sizeof (char *) * cd_count + 1);
  43. if (tmp)
  44. {
  45. retval = tmp;
  46. retval[cd_count-1] = (char *) malloc(4);
  47. if (retval[cd_count-1])
  48. {
  49. strcpy(retval[cd_count-1], drive_str);
  50. cd_count++;
  51. } /* if */
  52. } /* if */
  53. } /* if */
  54. } /* for */
  55. retval[cd_count - 1] = NULL;
  56. return(retval);
  57. } /* __PHYSFS_detectAvailableCDs */
  58. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  59. {
  60. DWORD buflen = 0;
  61. char *retval = NULL;
  62. char *filepart = NULL;
  63. if (strchr(argv0, '\\') != NULL) /* default behaviour can handle this. */
  64. return(NULL);
  65. buflen = SearchPath(NULL, argv0, NULL, buflen, NULL, NULL);
  66. retval = (char *) malloc(buflen);
  67. BAIL_IF_MACRO(!retval, ERR_OUT_OF_MEMORY, NULL);
  68. SearchPath(NULL, argv0, NULL, buflen, retval, &filepart);
  69. return(retval);
  70. } /* __PHYSFS_platformCalcBaseDir */
  71. char *__PHYSFS_platformGetUserName(void)
  72. {
  73. DWORD bufsize = 0;
  74. LPTSTR retval = NULL;
  75. if (GetUserName(NULL, &bufsize) == 0) /* This SHOULD fail. */
  76. {
  77. retval = (LPTSTR) malloc(bufsize);
  78. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  79. if (GetUserName(retval, &bufsize) == 0) /* ?! */
  80. {
  81. free(retval);
  82. retval = NULL;
  83. } /* if */
  84. } /* if */
  85. return((char *) retval);
  86. } /* __PHYSFS_platformGetUserName */
  87. static char *copyEnvironmentVariable(const char *varname)
  88. {
  89. const char *envr = getenv(varname);
  90. char *retval = NULL;
  91. if (envr != NULL)
  92. {
  93. retval = malloc(strlen(envr) + 1);
  94. if (retval != NULL)
  95. strcpy(retval, envr);
  96. } /* if */
  97. return(retval);
  98. } /* copyEnvironmentVariable */
  99. char *__PHYSFS_platformGetUserDir(void)
  100. {
  101. char *home = copyEnvironmentVariable("HOME");
  102. const char *homedrive = getenv("HOMEDRIVE");
  103. const char *homepath = getenv("HOMEPATH");
  104. if (home != NULL)
  105. return(home);
  106. if ((homedrive != NULL) && (homepath != NULL))
  107. {
  108. char *retval = (char *) malloc(strlen(homedrive)+strlen(homepath)+2);
  109. if (retval != NULL)
  110. {
  111. strcpy(retval, homedrive);
  112. if ((homepath[0] != '\\') &&
  113. (homedrive[strlen(homedrive)-1] != '\\'))
  114. {
  115. strcat(retval, "\\");
  116. } /* if */
  117. strcat(retval, homepath);
  118. return(retval);
  119. } /* if */
  120. } /* if */
  121. return(NULL); /* fall through to default rules. */
  122. } /* __PHYSFS_platformGetUserDir */
  123. int __PHYSFS_platformGetThreadID(void)
  124. {
  125. return((int) GetCurrentThreadId());
  126. } /* __PHYSFS_platformGetThreadID */
  127. /* ...make this Cygwin AND Visual C friendly... */
  128. int __PHYSFS_platformStricmp(const char *x, const char *y)
  129. {
  130. int ux, uy;
  131. do
  132. {
  133. ux = toupper((int) *x);
  134. uy = toupper((int) *y);
  135. if (ux > uy)
  136. return(1);
  137. else if (ux < uy)
  138. return(-1);
  139. x++;
  140. y++;
  141. } while ((ux) && (uy));
  142. return(0);
  143. } /* __PHYSFS_platformStricmp */
  144. int __PHYSFS_platformExists(const char *fname)
  145. {
  146. return(GetFileAttributes(fname) != 0xffffffff);
  147. } /* __PHYSFS_platformExists */
  148. int __PHYSFS_platformIsSymLink(const char *fname)
  149. {
  150. return(0); /* no symlinks on win32. */
  151. } /* __PHYSFS_platformIsSymlink */
  152. int __PHYSFS_platformIsDirectory(const char *fname)
  153. {
  154. return((GetFileAttributes(fname) & FILE_ATTRIBUTE_DIRECTORY) != 0);
  155. } /* __PHYSFS_platformIsDirectory */
  156. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  157. const char *dirName,
  158. const char *append)
  159. {
  160. int len = ((prepend) ? strlen(prepend) : 0) +
  161. ((append) ? strlen(append) : 0) +
  162. strlen(dirName) + 1;
  163. char *retval = malloc(len);
  164. char *p;
  165. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  166. if (prepend)
  167. strcpy(retval, prepend);
  168. else
  169. retval[0] = '\0';
  170. strcat(retval, dirName);
  171. if (append)
  172. strcat(retval, append);
  173. for (p = strchr(retval, '/'); p != NULL; p = strchr(p + 1, '/'))
  174. *p = '\\';
  175. return(retval);
  176. } /* __PHYSFS_platformCvtToDependent */
  177. /* Much like my college days, try to sleep for 10 milliseconds at a time... */
  178. void __PHYSFS_platformTimeslice(void)
  179. {
  180. Sleep(10);
  181. } /* __PHYSFS_platformTimeslice */
  182. LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
  183. int omitSymLinks)
  184. {
  185. LinkedStringList *retval = NULL;
  186. LinkedStringList *l = NULL;
  187. LinkedStringList *prev = NULL;
  188. HANDLE dir;
  189. WIN32_FIND_DATA ent;
  190. dir = FindFirstFile(dirname, &ent);
  191. BAIL_IF_MACRO(dir == INVALID_HANDLE_VALUE, win32strerror(), NULL);
  192. for (; FindNextFile(dir, &ent) != 0; )
  193. {
  194. if (strcmp(ent.cFileName, ".") == 0)
  195. continue;
  196. if (strcmp(ent.cFileName, "..") == 0)
  197. continue;
  198. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  199. if (l == NULL)
  200. break;
  201. l->str = (char *) malloc(strlen(ent.cFileName) + 1);
  202. if (l->str == NULL)
  203. {
  204. free(l);
  205. break;
  206. } /* if */
  207. strcpy(l->str, ent.cFileName);
  208. if (retval == NULL)
  209. retval = l;
  210. else
  211. prev->next = l;
  212. prev = l;
  213. l->next = NULL;
  214. } /* while */
  215. FindClose(dir);
  216. return(retval);
  217. } /* __PHYSFS_platformEnumerateFiles */
  218. int __PHYSFS_platformFileLength(FILE *handle)
  219. {
  220. fpos_t curpos;
  221. int retval;
  222. fgetpos(handle, &curpos);
  223. fseek(handle, 0, SEEK_END);
  224. retval = ftell(handle);
  225. fsetpos(handle, &curpos);
  226. return(retval);
  227. } /* __PHYSFS_platformFileLength */
  228. char *__PHYSFS_platformCurrentDir(void)
  229. {
  230. LPTSTR retval;
  231. DWORD buflen = 0;
  232. buflen = GetCurrentDirectory(buflen, NULL);
  233. retval = (LPTSTR) malloc(buflen);
  234. GetCurrentDirectory(buflen, retval);
  235. return((char *) retval);
  236. } /* __PHYSFS_platformCurrentDir */
  237. char *__PHYSFS_platformRealPath(const char *path)
  238. {
  239. /* !!! FIXME: This isn't supported on CygWin! */
  240. return(_fullpath(NULL, path, _MAX_PATH));
  241. } /* __PHYSFS_platformRealPath */
  242. int __PHYSFS_platformMkDir(const char *path)
  243. {
  244. DWORD rc = CreateDirectory(path, NULL);
  245. BAIL_IF_MACRO(rc == 0, win32strerror(), 0);
  246. return(1);
  247. } /* __PHYSFS_platformMkDir */
  248. /* end of win32.c ... */