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. static char *getExePath(const char *argv0)
  59. {
  60. char *filepart = NULL;
  61. char *retval = (char *) malloc(sizeof (TCHAR) * (MAX_PATH + 1));
  62. DWORD buflen = GetModuleFileName(NULL, retval, MAX_PATH + 1);
  63. retval[buflen] = '\0'; /* does API always null-terminate the string? */
  64. /* make sure the string was not truncated. */
  65. if (__PHYSFS_platformStricmp(&retval[buflen - 4], ".exe") == 0)
  66. {
  67. char *ptr = strrchr(retval, '\\');
  68. if (ptr != NULL)
  69. {
  70. *(ptr + 1) = '\0'; /* chop off filename. */
  71. /* free up the bytes we didn't actually use. */
  72. retval = (char *) realloc(retval, strlen(retval) + 1);
  73. if (retval != NULL)
  74. return(retval);
  75. } /* if */
  76. } /* if */
  77. /* if any part of the previous approach failed, try SearchPath()... */
  78. buflen = SearchPath(NULL, argv0, NULL, buflen, NULL, NULL);
  79. retval = (char *) realloc(retval, buflen);
  80. BAIL_IF_MACRO(!retval, ERR_OUT_OF_MEMORY, NULL);
  81. SearchPath(NULL, argv0, NULL, buflen, retval, &filepart);
  82. return(retval);
  83. } /* getExePath */
  84. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  85. {
  86. if (strchr(argv0, '\\') != NULL) /* default behaviour can handle this. */
  87. return(NULL);
  88. return(getExePath(argv0));
  89. } /* __PHYSFS_platformCalcBaseDir */
  90. char *__PHYSFS_platformGetUserName(void)
  91. {
  92. DWORD bufsize = 0;
  93. LPTSTR retval = NULL;
  94. if (GetUserName(NULL, &bufsize) == 0) /* This SHOULD fail. */
  95. {
  96. retval = (LPTSTR) malloc(bufsize);
  97. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  98. if (GetUserName(retval, &bufsize) == 0) /* ?! */
  99. {
  100. free(retval);
  101. retval = NULL;
  102. } /* if */
  103. } /* if */
  104. return((char *) retval);
  105. } /* __PHYSFS_platformGetUserName */
  106. static char *copyEnvironmentVariable(const char *varname)
  107. {
  108. const char *envr = getenv(varname);
  109. char *retval = NULL;
  110. if (envr != NULL)
  111. {
  112. retval = malloc(strlen(envr) + 1);
  113. if (retval != NULL)
  114. strcpy(retval, envr);
  115. } /* if */
  116. return(retval);
  117. } /* copyEnvironmentVariable */
  118. char *__PHYSFS_platformGetUserDir(void)
  119. {
  120. char *home = copyEnvironmentVariable("HOME");
  121. const char *homedrive = getenv("HOMEDRIVE");
  122. const char *homepath = getenv("HOMEPATH");
  123. if (home != NULL)
  124. return(home);
  125. if ((homedrive != NULL) && (homepath != NULL))
  126. {
  127. char *retval = (char *) malloc(strlen(homedrive)+strlen(homepath)+2);
  128. if (retval != NULL)
  129. {
  130. strcpy(retval, homedrive);
  131. if ((homepath[0] != '\\') &&
  132. (homedrive[strlen(homedrive)-1] != '\\'))
  133. {
  134. strcat(retval, "\\");
  135. } /* if */
  136. strcat(retval, homepath);
  137. return(retval);
  138. } /* if */
  139. } /* if */
  140. return(NULL);
  141. } /* __PHYSFS_platformGetUserDir */
  142. int __PHYSFS_platformGetThreadID(void)
  143. {
  144. return((int) GetCurrentThreadId());
  145. } /* __PHYSFS_platformGetThreadID */
  146. /* ...make this Cygwin AND Visual C friendly... */
  147. int __PHYSFS_platformStricmp(const char *x, const char *y)
  148. {
  149. int ux, uy;
  150. do
  151. {
  152. ux = toupper((int) *x);
  153. uy = toupper((int) *y);
  154. if (ux > uy)
  155. return(1);
  156. else if (ux < uy)
  157. return(-1);
  158. x++;
  159. y++;
  160. } while ((ux) && (uy));
  161. return(0);
  162. } /* __PHYSFS_platformStricmp */
  163. int __PHYSFS_platformExists(const char *fname)
  164. {
  165. return(GetFileAttributes(fname) != 0xffffffff);
  166. } /* __PHYSFS_platformExists */
  167. int __PHYSFS_platformIsSymLink(const char *fname)
  168. {
  169. return(0); /* no symlinks on win32. */
  170. } /* __PHYSFS_platformIsSymlink */
  171. int __PHYSFS_platformIsDirectory(const char *fname)
  172. {
  173. return((GetFileAttributes(fname) & FILE_ATTRIBUTE_DIRECTORY) != 0);
  174. } /* __PHYSFS_platformIsDirectory */
  175. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  176. const char *dirName,
  177. const char *append)
  178. {
  179. int len = ((prepend) ? strlen(prepend) : 0) +
  180. ((append) ? strlen(append) : 0) +
  181. strlen(dirName) + 1;
  182. char *retval = malloc(len);
  183. char *p;
  184. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  185. if (prepend)
  186. strcpy(retval, prepend);
  187. else
  188. retval[0] = '\0';
  189. strcat(retval, dirName);
  190. if (append)
  191. strcat(retval, append);
  192. for (p = strchr(retval, '/'); p != NULL; p = strchr(p + 1, '/'))
  193. *p = '\\';
  194. return(retval);
  195. } /* __PHYSFS_platformCvtToDependent */
  196. /* Much like my college days, try to sleep for 10 milliseconds at a time... */
  197. void __PHYSFS_platformTimeslice(void)
  198. {
  199. Sleep(10);
  200. } /* __PHYSFS_platformTimeslice */
  201. LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
  202. int omitSymLinks)
  203. {
  204. LinkedStringList *retval = NULL;
  205. LinkedStringList *l = NULL;
  206. LinkedStringList *prev = NULL;
  207. HANDLE dir;
  208. WIN32_FIND_DATA ent;
  209. dir = FindFirstFile(dirname, &ent);
  210. BAIL_IF_MACRO(dir == INVALID_HANDLE_VALUE, win32strerror(), NULL);
  211. while (FindNextFile(dir, &ent) != 0)
  212. {
  213. if (strcmp(ent.cFileName, ".") == 0)
  214. continue;
  215. if (strcmp(ent.cFileName, "..") == 0)
  216. continue;
  217. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  218. if (l == NULL)
  219. break;
  220. l->str = (char *) malloc(strlen(ent.cFileName) + 1);
  221. if (l->str == NULL)
  222. {
  223. free(l);
  224. break;
  225. } /* if */
  226. strcpy(l->str, ent.cFileName);
  227. if (retval == NULL)
  228. retval = l;
  229. else
  230. prev->next = l;
  231. prev = l;
  232. l->next = NULL;
  233. } /* while */
  234. FindClose(dir);
  235. return(retval);
  236. } /* __PHYSFS_platformEnumerateFiles */
  237. int __PHYSFS_platformFileLength(FILE *handle)
  238. {
  239. fpos_t curpos;
  240. int retval;
  241. fgetpos(handle, &curpos);
  242. fseek(handle, 0, SEEK_END);
  243. retval = ftell(handle);
  244. fsetpos(handle, &curpos);
  245. return(retval);
  246. } /* __PHYSFS_platformFileLength */
  247. char *__PHYSFS_platformCurrentDir(void)
  248. {
  249. LPTSTR retval;
  250. DWORD buflen = 0;
  251. buflen = GetCurrentDirectory(buflen, NULL);
  252. retval = (LPTSTR) malloc(sizeof (TCHAR) * (buflen + 2));
  253. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  254. GetCurrentDirectory(buflen, retval);
  255. if (retval[buflen - 2] != '\\')
  256. strcat(retval, "\\");
  257. return((char *) retval);
  258. } /* __PHYSFS_platformCurrentDir */
  259. /* this could probably use a cleanup. */
  260. char *__PHYSFS_platformRealPath(const char *path)
  261. {
  262. char *retval = NULL;
  263. char *p = NULL;
  264. BAIL_IF_MACRO(path == NULL, ERR_INVALID_ARGUMENT, NULL);
  265. BAIL_IF_MACRO(*path == '\0', ERR_INVALID_ARGUMENT, NULL);
  266. retval = (char *) malloc(MAX_PATH);
  267. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  268. /*
  269. * If in \\server\path format, it's already an absolute path.
  270. * We'll need to check for "." and ".." dirs, though, just in case.
  271. */
  272. if ((path[0] == '\\') && (path[1] == '\\'))
  273. strcpy(retval, path);
  274. else
  275. {
  276. char *currentDir = __PHYSFS_platformCurrentDir();
  277. if (currentDir == NULL)
  278. {
  279. free(retval);
  280. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  281. } /* if */
  282. if (path[1] == ':') /* drive letter specified? */
  283. {
  284. /*
  285. * Apparently, "D:mypath" is the same as "D:\\mypath" if
  286. * D: is not the current drive. However, if D: is the
  287. * current drive, then "D:mypath" is a relative path. Ugh.
  288. */
  289. if (path[2] == '\\') /* maybe an absolute path? */
  290. strcpy(retval, path);
  291. else /* definitely an absolute path. */
  292. {
  293. if (path[0] == currentDir[0]) /* current drive; relative. */
  294. {
  295. strcpy(retval, currentDir);
  296. strcat(retval, path + 2);
  297. } /* if */
  298. else /* not current drive; absolute. */
  299. {
  300. retval[0] = path[0];
  301. retval[1] = ':';
  302. retval[2] = '\\';
  303. strcpy(retval + 3, path + 2);
  304. } /* else */
  305. } /* else */
  306. } /* if */
  307. else /* no drive letter specified. */
  308. {
  309. if (path[0] == '\\') /* absolute path. */
  310. {
  311. retval[0] = currentDir[0];
  312. retval[1] = ':';
  313. strcpy(retval + 2, path);
  314. } /* if */
  315. else
  316. {
  317. strcpy(retval, currentDir);
  318. strcat(retval, path);
  319. } /* else */
  320. } /* else */
  321. free(currentDir);
  322. } /* else */
  323. /* (whew.) Ok, now take out "." and ".." path entries... */
  324. p = retval;
  325. while ( (p = strstr(p, "\\.")) != NULL)
  326. {
  327. /* it's a "." entry that doesn't end the string. */
  328. if (p[2] == '\\')
  329. memmove(p + 1, p + 3, strlen(p + 3) + 1);
  330. /* it's a "." entry that ends the string. */
  331. else if (p[2] == '\0')
  332. p[0] = '\0';
  333. /* it's a ".." entry. */
  334. else if (p[2] == '.')
  335. {
  336. char *prevEntry = p - 1;
  337. while ((prevEntry != retval) && (*prevEntry != '\\'))
  338. prevEntry--;
  339. if (prevEntry == retval) /* make it look like a "." entry. */
  340. memmove(p + 1, p + 2, strlen(p + 2) + 1);
  341. else
  342. {
  343. if (p[3] != '\0') /* doesn't end string. */
  344. *prevEntry = '\0';
  345. else /* ends string. */
  346. memmove(prevEntry + 1, p + 4, strlen(p + 4) + 1);
  347. p = prevEntry;
  348. } /* else */
  349. } /* else if */
  350. else
  351. {
  352. p++; /* look past current char. */
  353. } /* else */
  354. } /* while */
  355. /* shrink the retval's memory block if possible... */
  356. p = (char *) realloc(retval, strlen(retval) + 1);
  357. if (p != NULL)
  358. retval = p;
  359. return(retval);
  360. } /* __PHYSFS_platformRealPath */
  361. int __PHYSFS_platformMkDir(const char *path)
  362. {
  363. DWORD rc = CreateDirectory(path, NULL);
  364. BAIL_IF_MACRO(rc == 0, win32strerror(), 0);
  365. return(1);
  366. } /* __PHYSFS_platformMkDir */
  367. /* end of win32.c ... */