win32.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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 <userenv.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <ctype.h>
  16. #define __PHYSICSFS_INTERNAL__
  17. #include "physfs_internal.h"
  18. const char *__PHYSFS_platformDirSeparator = "\\";
  19. static HANDLE ProcessHandle = NULL; /* Current process handle */
  20. static HANDLE AccessTokenHandle = NULL; /* Security handle to process */
  21. static DWORD ProcessID; /* ID assigned to current process */
  22. static int runningNT; /* TRUE if NT derived OS */
  23. static OSVERSIONINFO OSVersionInfo; /* Information about the OS */
  24. /* NT specific information */
  25. static char *ProfileDirectory = NULL; /* User profile folder */
  26. static const char *win32strerror(void)
  27. {
  28. static TCHAR msgbuf[255];
  29. FormatMessage(
  30. FORMAT_MESSAGE_FROM_SYSTEM |
  31. FORMAT_MESSAGE_IGNORE_INSERTS,
  32. NULL,
  33. GetLastError(),
  34. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
  35. msgbuf,
  36. sizeof (msgbuf) / sizeof (TCHAR),
  37. NULL
  38. );
  39. return((const char *) msgbuf);
  40. } /* win32strerror */
  41. char **__PHYSFS_platformDetectAvailableCDs(void)
  42. {
  43. char **retval = (char **) malloc(sizeof (char *));
  44. int cd_count = 1; /* We count the NULL entry. */
  45. char drive_str[4] = "x:\\";
  46. for (drive_str[0] = 'A'; drive_str[0] <= 'Z'; drive_str[0]++)
  47. {
  48. if (GetDriveType(drive_str) == DRIVE_CDROM)
  49. {
  50. char **tmp = realloc(retval, sizeof (char *) * cd_count + 1);
  51. if (tmp)
  52. {
  53. retval = tmp;
  54. retval[cd_count-1] = (char *) malloc(4);
  55. if (retval[cd_count-1])
  56. {
  57. strcpy(retval[cd_count-1], drive_str);
  58. cd_count++;
  59. } /* if */
  60. } /* if */
  61. } /* if */
  62. } /* for */
  63. retval[cd_count - 1] = NULL;
  64. return(retval);
  65. } /* __PHYSFS_detectAvailableCDs */
  66. static char *getExePath(const char *argv0)
  67. {
  68. char *filepart = NULL;
  69. char *retval = (char *) malloc(sizeof (TCHAR) * (MAX_PATH + 1));
  70. DWORD buflen = GetModuleFileName(NULL, retval, MAX_PATH + 1);
  71. retval[buflen] = '\0'; /* does API always null-terminate the string? */
  72. /* make sure the string was not truncated. */
  73. if (__PHYSFS_platformStricmp(&retval[buflen - 4], ".exe") == 0)
  74. {
  75. char *ptr = strrchr(retval, '\\');
  76. if (ptr != NULL)
  77. {
  78. *(ptr + 1) = '\0'; /* chop off filename. */
  79. /* free up the bytes we didn't actually use. */
  80. retval = (char *) realloc(retval, strlen(retval) + 1);
  81. if (retval != NULL)
  82. return(retval);
  83. } /* if */
  84. } /* if */
  85. /* if any part of the previous approach failed, try SearchPath()... */
  86. buflen = SearchPath(NULL, argv0, NULL, buflen, NULL, NULL);
  87. retval = (char *) realloc(retval, buflen);
  88. BAIL_IF_MACRO(!retval, ERR_OUT_OF_MEMORY, NULL);
  89. SearchPath(NULL, argv0, NULL, buflen, retval, &filepart);
  90. return(retval);
  91. } /* getExePath */
  92. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  93. {
  94. if (strchr(argv0, '\\') != NULL) /* default behaviour can handle this. */
  95. return(NULL);
  96. return(getExePath(argv0));
  97. } /* __PHYSFS_platformCalcBaseDir */
  98. char *__PHYSFS_platformGetUserName(void)
  99. {
  100. DWORD bufsize = 0;
  101. LPTSTR retval = NULL;
  102. if (GetUserName(NULL, &bufsize) == 0) /* This SHOULD fail. */
  103. {
  104. retval = (LPTSTR) malloc(bufsize);
  105. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  106. if (GetUserName(retval, &bufsize) == 0) /* ?! */
  107. {
  108. free(retval);
  109. retval = NULL;
  110. } /* if */
  111. } /* if */
  112. return((char *) retval);
  113. } /* __PHYSFS_platformGetUserName */
  114. static char *copyEnvironmentVariable(const char *varname)
  115. {
  116. const char *envr = getenv(varname);
  117. char *retval = NULL;
  118. if (envr != NULL)
  119. {
  120. retval = malloc(strlen(envr) + 1);
  121. if (retval != NULL)
  122. strcpy(retval, envr);
  123. } /* if */
  124. return(retval);
  125. } /* copyEnvironmentVariable */
  126. char *__PHYSFS_platformGetUserDir(void)
  127. {
  128. char *userdir = NULL;
  129. /*!!!TODO - Need to get the userdir for non-NT based OSes */
  130. if(runningNT)
  131. {
  132. userdir = ProfileDirectory;
  133. }
  134. return userdir;
  135. } /* __PHYSFS_platformGetUserDir */
  136. int __PHYSFS_platformGetThreadID(void)
  137. {
  138. return((int) GetCurrentThreadId());
  139. } /* __PHYSFS_platformGetThreadID */
  140. /* ...make this Cygwin AND Visual C friendly... */
  141. int __PHYSFS_platformStricmp(const char *x, const char *y)
  142. {
  143. int ux, uy;
  144. do
  145. {
  146. ux = toupper((int) *x);
  147. uy = toupper((int) *y);
  148. if (ux > uy)
  149. return(1);
  150. else if (ux < uy)
  151. return(-1);
  152. x++;
  153. y++;
  154. } while ((ux) && (uy));
  155. return(0);
  156. } /* __PHYSFS_platformStricmp */
  157. int __PHYSFS_platformExists(const char *fname)
  158. {
  159. return(GetFileAttributes(fname) != 0xffffffff);
  160. } /* __PHYSFS_platformExists */
  161. int __PHYSFS_platformIsSymLink(const char *fname)
  162. {
  163. return(0); /* no symlinks on win32. */
  164. } /* __PHYSFS_platformIsSymlink */
  165. int __PHYSFS_platformIsDirectory(const char *fname)
  166. {
  167. return((GetFileAttributes(fname) & FILE_ATTRIBUTE_DIRECTORY) != 0);
  168. } /* __PHYSFS_platformIsDirectory */
  169. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  170. const char *dirName,
  171. const char *append)
  172. {
  173. int len = ((prepend) ? strlen(prepend) : 0) +
  174. ((append) ? strlen(append) : 0) +
  175. strlen(dirName) + 1;
  176. char *retval = malloc(len);
  177. char *p;
  178. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  179. if (prepend)
  180. strcpy(retval, prepend);
  181. else
  182. retval[0] = '\0';
  183. strcat(retval, dirName);
  184. if (append)
  185. strcat(retval, append);
  186. for (p = strchr(retval, '/'); p != NULL; p = strchr(p + 1, '/'))
  187. *p = '\\';
  188. return(retval);
  189. } /* __PHYSFS_platformCvtToDependent */
  190. /* Much like my college days, try to sleep for 10 milliseconds at a time... */
  191. void __PHYSFS_platformTimeslice(void)
  192. {
  193. Sleep(10);
  194. } /* __PHYSFS_platformTimeslice */
  195. LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
  196. int omitSymLinks)
  197. {
  198. LinkedStringList *retval = NULL;
  199. LinkedStringList *l = NULL;
  200. LinkedStringList *prev = NULL;
  201. HANDLE dir;
  202. WIN32_FIND_DATA ent;
  203. dir = FindFirstFile(dirname, &ent);
  204. BAIL_IF_MACRO(dir == INVALID_HANDLE_VALUE, win32strerror(), NULL);
  205. while (FindNextFile(dir, &ent) != 0)
  206. {
  207. if (strcmp(ent.cFileName, ".") == 0)
  208. continue;
  209. if (strcmp(ent.cFileName, "..") == 0)
  210. continue;
  211. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  212. if (l == NULL)
  213. break;
  214. l->str = (char *) malloc(strlen(ent.cFileName) + 1);
  215. if (l->str == NULL)
  216. {
  217. free(l);
  218. break;
  219. } /* if */
  220. strcpy(l->str, ent.cFileName);
  221. if (retval == NULL)
  222. retval = l;
  223. else
  224. prev->next = l;
  225. prev = l;
  226. l->next = NULL;
  227. } /* while */
  228. FindClose(dir);
  229. return(retval);
  230. } /* __PHYSFS_platformEnumerateFiles */
  231. int __PHYSFS_platformFileLength(FILE *handle)
  232. {
  233. fpos_t curpos;
  234. int retval;
  235. fgetpos(handle, &curpos);
  236. fseek(handle, 0, SEEK_END);
  237. retval = ftell(handle);
  238. fsetpos(handle, &curpos);
  239. return(retval);
  240. } /* __PHYSFS_platformFileLength */
  241. char *__PHYSFS_platformCurrentDir(void)
  242. {
  243. LPTSTR retval;
  244. DWORD buflen = 0;
  245. buflen = GetCurrentDirectory(buflen, NULL);
  246. retval = (LPTSTR) malloc(sizeof (TCHAR) * (buflen + 2));
  247. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  248. GetCurrentDirectory(buflen, retval);
  249. if (retval[buflen - 2] != '\\')
  250. strcat(retval, "\\");
  251. return((char *) retval);
  252. } /* __PHYSFS_platformCurrentDir */
  253. /* this could probably use a cleanup. */
  254. char *__PHYSFS_platformRealPath(const char *path)
  255. {
  256. char *retval = NULL;
  257. char *p = NULL;
  258. BAIL_IF_MACRO(path == NULL, ERR_INVALID_ARGUMENT, NULL);
  259. BAIL_IF_MACRO(*path == '\0', ERR_INVALID_ARGUMENT, NULL);
  260. retval = (char *) malloc(MAX_PATH);
  261. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  262. /*
  263. * If in \\server\path format, it's already an absolute path.
  264. * We'll need to check for "." and ".." dirs, though, just in case.
  265. */
  266. if ((path[0] == '\\') && (path[1] == '\\'))
  267. strcpy(retval, path);
  268. else
  269. {
  270. char *currentDir = __PHYSFS_platformCurrentDir();
  271. if (currentDir == NULL)
  272. {
  273. free(retval);
  274. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  275. } /* if */
  276. if (path[1] == ':') /* drive letter specified? */
  277. {
  278. /*
  279. * Apparently, "D:mypath" is the same as "D:\\mypath" if
  280. * D: is not the current drive. However, if D: is the
  281. * current drive, then "D:mypath" is a relative path. Ugh.
  282. */
  283. if (path[2] == '\\') /* maybe an absolute path? */
  284. strcpy(retval, path);
  285. else /* definitely an absolute path. */
  286. {
  287. if (path[0] == currentDir[0]) /* current drive; relative. */
  288. {
  289. strcpy(retval, currentDir);
  290. strcat(retval, path + 2);
  291. } /* if */
  292. else /* not current drive; absolute. */
  293. {
  294. retval[0] = path[0];
  295. retval[1] = ':';
  296. retval[2] = '\\';
  297. strcpy(retval + 3, path + 2);
  298. } /* else */
  299. } /* else */
  300. } /* if */
  301. else /* no drive letter specified. */
  302. {
  303. if (path[0] == '\\') /* absolute path. */
  304. {
  305. retval[0] = currentDir[0];
  306. retval[1] = ':';
  307. strcpy(retval + 2, path);
  308. } /* if */
  309. else
  310. {
  311. strcpy(retval, currentDir);
  312. strcat(retval, path);
  313. } /* else */
  314. } /* else */
  315. free(currentDir);
  316. } /* else */
  317. /* (whew.) Ok, now take out "." and ".." path entries... */
  318. p = retval;
  319. while ( (p = strstr(p, "\\.")) != NULL)
  320. {
  321. /* it's a "." entry that doesn't end the string. */
  322. if (p[2] == '\\')
  323. memmove(p + 1, p + 3, strlen(p + 3) + 1);
  324. /* it's a "." entry that ends the string. */
  325. else if (p[2] == '\0')
  326. p[0] = '\0';
  327. /* it's a ".." entry. */
  328. else if (p[2] == '.')
  329. {
  330. char *prevEntry = p - 1;
  331. while ((prevEntry != retval) && (*prevEntry != '\\'))
  332. prevEntry--;
  333. if (prevEntry == retval) /* make it look like a "." entry. */
  334. memmove(p + 1, p + 2, strlen(p + 2) + 1);
  335. else
  336. {
  337. if (p[3] != '\0') /* doesn't end string. */
  338. *prevEntry = '\0';
  339. else /* ends string. */
  340. memmove(prevEntry + 1, p + 4, strlen(p + 4) + 1);
  341. p = prevEntry;
  342. } /* else */
  343. } /* else if */
  344. else
  345. {
  346. p++; /* look past current char. */
  347. } /* else */
  348. } /* while */
  349. /* shrink the retval's memory block if possible... */
  350. p = (char *) realloc(retval, strlen(retval) + 1);
  351. if (p != NULL)
  352. retval = p;
  353. return(retval);
  354. } /* __PHYSFS_platformRealPath */
  355. int __PHYSFS_platformMkDir(const char *path)
  356. {
  357. DWORD rc = CreateDirectory(path, NULL);
  358. BAIL_IF_MACRO(rc == 0, win32strerror(), 0);
  359. return(1);
  360. } /* __PHYSFS_platformMkDir */
  361. /*
  362. * Initialize any NT specific stuff. This includes any OS based on NT.
  363. *
  364. * Return zero if there was a catastrophic failure and non-zero otherwise.
  365. */
  366. static int doNTInit()
  367. {
  368. DWORD pathsize = 0;
  369. char TempProfileDirectory[1];
  370. /* Create a process access token handle */
  371. if(!OpenProcessToken(ProcessHandle, TOKEN_QUERY, &AccessTokenHandle))
  372. {
  373. /* Access token is required by other win32 functions */
  374. return 0;
  375. }
  376. /* Should fail. Will write the size of the profile path in pathsize*/
  377. /*!!! Second parameter can't be NULL or the function fails??? */
  378. if(!GetUserProfileDirectory(AccessTokenHandle, TempProfileDirectory, &pathsize))
  379. {
  380. const char *temp;
  381. temp = win32strerror();
  382. /* Allocate memory for the profile directory */
  383. ProfileDirectory = (char *)malloc(pathsize);
  384. BAIL_IF_MACRO(ProfileDirectory == NULL, ERR_OUT_OF_MEMORY, 0);
  385. /* Try to get the profile directory */
  386. if(!GetUserProfileDirectory(AccessTokenHandle, ProfileDirectory, &pathsize))
  387. {
  388. free(ProfileDirectory);
  389. return 0;
  390. }
  391. }
  392. /* Everything initialized okay */
  393. return 1;
  394. }
  395. /*
  396. * Get OS info and save it.
  397. *
  398. * Returns non-zero if successful, otherwise it returns zero on failure.
  399. */
  400. int getOSInfo(void)
  401. {
  402. /* Get OS info */
  403. OSVersionInfo.dwOSVersionInfoSize = sizeof(OSVersionInfo);
  404. if(!GetVersionEx(&OSVersionInfo))
  405. {
  406. return 0;
  407. }
  408. /* Set to TRUE if we are runnign a WinNT based OS 4.0 or greater */
  409. runningNT = (OSVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) &&
  410. (OSVersionInfo.dwMajorVersion > 3);
  411. return 1;
  412. }
  413. int __PHYSFS_platformInit(void)
  414. {
  415. if(!getOSInfo())
  416. {
  417. return 0;
  418. }
  419. /* Get Windows ProcessID associated with the current process */
  420. ProcessID = GetCurrentProcessId();
  421. /* Create a process handle associated with the current process ID */
  422. ProcessHandle = GetCurrentProcess();
  423. if(ProcessHandle == NULL)
  424. {
  425. /* Process handle is required by other win32 functions */
  426. return 0;
  427. }
  428. /* If running an NT system (NT/Win2k/XP, etc...) */
  429. if(runningNT)
  430. {
  431. if(!doNTInit())
  432. {
  433. /* Error initializing NT stuff */
  434. return 0;
  435. }
  436. }
  437. /* It's all good */
  438. return 1;
  439. }
  440. /*
  441. * Uninitialize any NT specific stuff done in doNTInit().
  442. *
  443. * Return zero if there was a catastrophic failure and non-zero otherwise.
  444. */
  445. static int doNTDeinit()
  446. {
  447. if(CloseHandle(AccessTokenHandle) != S_OK)
  448. {
  449. return 0;
  450. }
  451. free(ProfileDirectory);
  452. /* It's all good */
  453. return 1;
  454. }
  455. int __PHYSFS_platformDeinit(void)
  456. {
  457. if(!doNTDeinit())
  458. return 0;
  459. if(CloseHandle(ProcessHandle) != S_OK)
  460. return 0;
  461. /* It's all good */
  462. return 1;
  463. }
  464. /* end of win32.c ... */