win32.c 12 KB

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