win32.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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(sizeof (TCHAR) * (buflen + 2));
  251. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  252. GetCurrentDirectory(buflen, retval);
  253. if (retval[buflen - 2] != '\\')
  254. strcat(retval, "\\");
  255. return((char *) retval);
  256. } /* __PHYSFS_platformCurrentDir */
  257. /* this could probably use a cleanup. */
  258. char *__PHYSFS_platformRealPath(const char *path)
  259. {
  260. char *retval = NULL;
  261. char *p = NULL;
  262. BAIL_IF_MACRO(path == NULL, ERR_INVALID_ARGUMENT, NULL);
  263. BAIL_IF_MACRO(*path == '\0', ERR_INVALID_ARGUMENT, NULL);
  264. retval = (char *) malloc(MAX_PATH);
  265. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  266. /*
  267. * If in \\server\path format, it's already an absolute path.
  268. * We'll need to check for "." and ".." dirs, though, just in case.
  269. */
  270. if ((path[0] == '\\') && (path[1] == '\\'))
  271. {
  272. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  273. strcpy(retval, path);
  274. } /* if */
  275. else
  276. {
  277. char *currentDir = __PHYSFS_platformCurrentDir();
  278. if (currentDir == NULL)
  279. {
  280. free(retval);
  281. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  282. } /* if */
  283. if (path[1] == ':') /* drive letter specified? */
  284. {
  285. /*
  286. * Apparently, "D:mypath" is the same as "D:\\mypath" if
  287. * D: is not the current drive. However, if D: is the
  288. * current drive, then "D:mypath" is a relative path. Ugh.
  289. */
  290. if (path[2] == '\\') /* maybe an absolute path? */
  291. strcpy(retval, path);
  292. else /* definitely an absolute path. */
  293. {
  294. if (path[0] == currentDir[0]) /* current drive; relative. */
  295. {
  296. strcpy(retval, currentDir);
  297. strcat(retval, path + 2);
  298. } /* if */
  299. else /* not current drive; absolute. */
  300. {
  301. retval[0] = path[0];
  302. retval[1] = ':';
  303. retval[2] = '\\';
  304. strcpy(retval + 3, path + 2);
  305. } /* else */
  306. } /* else */
  307. } /* if */
  308. else /* no drive letter specified. */
  309. {
  310. if (path[0] == '\\') /* absolute path. */
  311. {
  312. retval[0] = currentDir[0];
  313. retval[1] = ':';
  314. strcpy(retval + 2, path);
  315. } /* if */
  316. else
  317. {
  318. strcpy(retval, currentDir);
  319. strcat(retval, path);
  320. } /* else */
  321. } /* else */
  322. free(currentDir);
  323. } /* else */
  324. /* (whew.) Ok, now take out "." and ".." path entries... */
  325. p = retval;
  326. while ( (p = strstr(p, "\\.")) != NULL)
  327. {
  328. /* it's a "." entry that doesn't end the string. */
  329. if (p[2] == '\\')
  330. memmove(p + 1, p + 3, strlen(p + 3) + 1);
  331. /* it's a "." entry that ends the string. */
  332. else if (p[2] == '\0')
  333. p[0] = '\0';
  334. /* it's a ".." entry. */
  335. else if (p[2] == '.')
  336. {
  337. char *prevEntry = p - 1;
  338. while ((prevEntry != retval) && (*prevEntry != '\\'))
  339. prevEntry--;
  340. if (prevEntry == retval) /* make it look like a "." entry. */
  341. memmove(p + 1, p + 2, strlen(p + 2) + 1);
  342. else
  343. {
  344. if (p[3] != '\0') /* doesn't end string. */
  345. *prevEntry = '\0';
  346. else /* ends string. */
  347. memmove(prevEntry + 1, p + 4, strlen(p + 4) + 1);
  348. p = prevEntry;
  349. } /* else */
  350. } /* else if */
  351. else
  352. {
  353. p++; /* look past current char. */
  354. } /* else */
  355. } /* while */
  356. /* shrink the retval's memory block if possible... */
  357. p = (char *) realloc(retval, strlen(retval) + 1);
  358. if (p != NULL)
  359. retval = p;
  360. return(retval);
  361. } /* __PHYSFS_platformRealPath */
  362. int __PHYSFS_platformMkDir(const char *path)
  363. {
  364. DWORD rc = CreateDirectory(path, NULL);
  365. BAIL_IF_MACRO(rc == 0, win32strerror(), 0);
  366. return(1);
  367. } /* __PHYSFS_platformMkDir */
  368. /* end of win32.c ... */