TestPathMakePath.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <time.h>
  4. #include <winpr/crt.h>
  5. #include <winpr/file.h>
  6. #include <winpr/path.h>
  7. int TestPathMakePath(int argc, char* argv[])
  8. {
  9. int x;
  10. size_t baseLen;
  11. BOOL success;
  12. char tmp[64];
  13. char* path;
  14. char* cur;
  15. char delim = PathGetSeparatorA(0);
  16. char* base = GetKnownPath(KNOWN_PATH_TEMP);
  17. if (!base)
  18. {
  19. fprintf(stderr, "Failed to get temporary directory!\n");
  20. return -1;
  21. }
  22. baseLen = strlen(base);
  23. srand(time(NULL));
  24. for (x = 0; x < 5; x++)
  25. {
  26. sprintf_s(tmp, ARRAYSIZE(tmp), "%08X", rand());
  27. path = GetCombinedPath(base, tmp);
  28. free(base);
  29. if (!path)
  30. {
  31. fprintf(stderr, "GetCombinedPath failed!\n");
  32. return -1;
  33. }
  34. base = path;
  35. }
  36. printf("Creating path %s\n", path);
  37. success = PathMakePathA(path, NULL);
  38. if (!success)
  39. {
  40. fprintf(stderr, "MakePath failed!\n");
  41. free(path);
  42. return -1;
  43. }
  44. success = PathFileExistsA(path);
  45. if (!success)
  46. {
  47. fprintf(stderr, "MakePath lied about success!\n");
  48. free(path);
  49. return -1;
  50. }
  51. while (strlen(path) > baseLen)
  52. {
  53. if (!RemoveDirectoryA(path))
  54. {
  55. fprintf(stderr, "RemoveDirectoryA %s failed!\n", path);
  56. free(path);
  57. return -1;
  58. }
  59. cur = strrchr(path, delim);
  60. if (cur)
  61. *cur = '\0';
  62. }
  63. free(path);
  64. printf("%s success!\n", __FUNCTION__);
  65. return 0;
  66. }