win32.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. retval = (char *) malloc(sizeof (TCHAR) * (MAX_PATH + 1));
  66. buflen = GetModuleFileName(NULL, retval, MAX_PATH + 1);
  67. retval[buflen] = '\0'; /* does API always null-terminate the string? */
  68. /* make sure the string was not truncated. */
  69. if (__PHYSFS_platformStricmp(&retval[buflen - 4], ".exe") == 0)
  70. {
  71. char *ptr = strrchr(retval, '\\');
  72. if (ptr != NULL)
  73. {
  74. *(ptr + 1) = '\0'; /* chop off filename. */
  75. /* free up the bytes we didn't actually use. */
  76. retval = (char *) realloc(retval, strlen(retval) + 1);
  77. if (retval != NULL)
  78. return(retval);
  79. } /* if */
  80. } /* if */
  81. /* if any part of the previous approach failed, try SearchPath()... */
  82. buflen = SearchPath(NULL, argv0, NULL, buflen, NULL, NULL);
  83. retval = (char *) realloc(retval, buflen);
  84. BAIL_IF_MACRO(!retval, ERR_OUT_OF_MEMORY, NULL);
  85. SearchPath(NULL, argv0, NULL, buflen, retval, &filepart);
  86. return(retval);
  87. } /* __PHYSFS_platformCalcBaseDir */
  88. char *__PHYSFS_platformGetUserName(void)
  89. {
  90. DWORD bufsize = 0;
  91. LPTSTR retval = NULL;
  92. if (GetUserName(NULL, &bufsize) == 0) /* This SHOULD fail. */
  93. {
  94. retval = (LPTSTR) malloc(bufsize);
  95. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  96. if (GetUserName(retval, &bufsize) == 0) /* ?! */
  97. {
  98. free(retval);
  99. retval = NULL;
  100. } /* if */
  101. } /* if */
  102. return((char *) retval);
  103. } /* __PHYSFS_platformGetUserName */
  104. static char *copyEnvironmentVariable(const char *varname)
  105. {
  106. const char *envr = getenv(varname);
  107. char *retval = NULL;
  108. if (envr != NULL)
  109. {
  110. retval = malloc(strlen(envr) + 1);
  111. if (retval != NULL)
  112. strcpy(retval, envr);
  113. } /* if */
  114. return(retval);
  115. } /* copyEnvironmentVariable */
  116. char *__PHYSFS_platformGetUserDir(void)
  117. {
  118. char *home = copyEnvironmentVariable("HOME");
  119. const char *homedrive = getenv("HOMEDRIVE");
  120. const char *homepath = getenv("HOMEPATH");
  121. if (home != NULL)
  122. return(home);
  123. if ((homedrive != NULL) && (homepath != NULL))
  124. {
  125. char *retval = (char *) malloc(strlen(homedrive)+strlen(homepath)+2);
  126. if (retval != NULL)
  127. {
  128. strcpy(retval, homedrive);
  129. if ((homepath[0] != '\\') &&
  130. (homedrive[strlen(homedrive)-1] != '\\'))
  131. {
  132. strcat(retval, "\\");
  133. } /* if */
  134. strcat(retval, homepath);
  135. return(retval);
  136. } /* if */
  137. } /* if */
  138. return(NULL); /* fall through to default rules. */
  139. } /* __PHYSFS_platformGetUserDir */
  140. int __PHYSFS_platformGetThreadID(void)
  141. {
  142. return((int) GetCurrentThreadId());
  143. } /* __PHYSFS_platformGetThreadID */
  144. /* ...make this Cygwin AND Visual C friendly... */
  145. int __PHYSFS_platformStricmp(const char *x, const char *y)
  146. {
  147. int ux, uy;
  148. do
  149. {
  150. ux = toupper((int) *x);
  151. uy = toupper((int) *y);
  152. if (ux > uy)
  153. return(1);
  154. else if (ux < uy)
  155. return(-1);
  156. x++;
  157. y++;
  158. } while ((ux) && (uy));
  159. return(0);
  160. } /* __PHYSFS_platformStricmp */
  161. int __PHYSFS_platformExists(const char *fname)
  162. {
  163. return(GetFileAttributes(fname) != 0xffffffff);
  164. } /* __PHYSFS_platformExists */
  165. int __PHYSFS_platformIsSymLink(const char *fname)
  166. {
  167. return(0); /* no symlinks on win32. */
  168. } /* __PHYSFS_platformIsSymlink */
  169. int __PHYSFS_platformIsDirectory(const char *fname)
  170. {
  171. return((GetFileAttributes(fname) & FILE_ATTRIBUTE_DIRECTORY) != 0);
  172. } /* __PHYSFS_platformIsDirectory */
  173. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  174. const char *dirName,
  175. const char *append)
  176. {
  177. int len = ((prepend) ? strlen(prepend) : 0) +
  178. ((append) ? strlen(append) : 0) +
  179. strlen(dirName) + 1;
  180. char *retval = malloc(len);
  181. char *p;
  182. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  183. if (prepend)
  184. strcpy(retval, prepend);
  185. else
  186. retval[0] = '\0';
  187. strcat(retval, dirName);
  188. if (append)
  189. strcat(retval, append);
  190. for (p = strchr(retval, '/'); p != NULL; p = strchr(p + 1, '/'))
  191. *p = '\\';
  192. return(retval);
  193. } /* __PHYSFS_platformCvtToDependent */
  194. /* Much like my college days, try to sleep for 10 milliseconds at a time... */
  195. void __PHYSFS_platformTimeslice(void)
  196. {
  197. Sleep(10);
  198. } /* __PHYSFS_platformTimeslice */
  199. LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
  200. int omitSymLinks)
  201. {
  202. LinkedStringList *retval = NULL;
  203. LinkedStringList *l = NULL;
  204. LinkedStringList *prev = NULL;
  205. HANDLE dir;
  206. WIN32_FIND_DATA ent;
  207. dir = FindFirstFile(dirname, &ent);
  208. BAIL_IF_MACRO(dir == INVALID_HANDLE_VALUE, win32strerror(), NULL);
  209. for (; FindNextFile(dir, &ent) != 0; )
  210. {
  211. if (strcmp(ent.cFileName, ".") == 0)
  212. continue;
  213. if (strcmp(ent.cFileName, "..") == 0)
  214. continue;
  215. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  216. if (l == NULL)
  217. break;
  218. l->str = (char *) malloc(strlen(ent.cFileName) + 1);
  219. if (l->str == NULL)
  220. {
  221. free(l);
  222. break;
  223. } /* if */
  224. strcpy(l->str, ent.cFileName);
  225. if (retval == NULL)
  226. retval = l;
  227. else
  228. prev->next = l;
  229. prev = l;
  230. l->next = NULL;
  231. } /* while */
  232. FindClose(dir);
  233. return(retval);
  234. } /* __PHYSFS_platformEnumerateFiles */
  235. int __PHYSFS_platformFileLength(FILE *handle)
  236. {
  237. fpos_t curpos;
  238. int retval;
  239. fgetpos(handle, &curpos);
  240. fseek(handle, 0, SEEK_END);
  241. retval = ftell(handle);
  242. fsetpos(handle, &curpos);
  243. return(retval);
  244. } /* __PHYSFS_platformFileLength */
  245. char *__PHYSFS_platformCurrentDir(void)
  246. {
  247. LPTSTR retval;
  248. DWORD buflen = 0;
  249. buflen = GetCurrentDirectory(buflen, NULL);
  250. retval = (LPTSTR) malloc(buflen);
  251. GetCurrentDirectory(buflen, retval);
  252. return((char *) retval);
  253. } /* __PHYSFS_platformCurrentDir */
  254. char *__PHYSFS_platformRealPath(const char *path)
  255. {
  256. /* !!! FIXME: This isn't supported on CygWin! */
  257. return(_fullpath(NULL, path, MAX_PATH));
  258. } /* __PHYSFS_platformRealPath */
  259. int __PHYSFS_platformMkDir(const char *path)
  260. {
  261. DWORD rc = CreateDirectory(path, NULL);
  262. BAIL_IF_MACRO(rc == 0, win32strerror(), 0);
  263. return(1);
  264. } /* __PHYSFS_platformMkDir */
  265. /* end of win32.c ... */