FreeRDP
TestPathMakePath.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <time.h>
4 
5 #include <winpr/crt.h>
6 #include <winpr/crypto.h>
7 #include <winpr/file.h>
8 #include <winpr/path.h>
9 
10 static UINT32 prand(UINT32 max)
11 {
12  UINT32 tmp = 0;
13  if (max <= 1)
14  return 1;
15  winpr_RAND(&tmp, sizeof(tmp));
16  return tmp % (max - 1) + 1;
17 }
18 
19 int TestPathMakePath(int argc, char* argv[])
20 {
21  size_t baseLen = 0;
22  BOOL success = 0;
23  char tmp[64] = { 0 };
24  char* path = NULL;
25  char* cur = NULL;
26  char delim = PathGetSeparatorA(0);
27  char* base = GetKnownPath(KNOWN_PATH_TEMP);
28 
29  WINPR_UNUSED(argc);
30  WINPR_UNUSED(argv);
31 
32  if (!base)
33  {
34  (void)fprintf(stderr, "Failed to get temporary directory!\n");
35  return -1;
36  }
37 
38  baseLen = strlen(base);
39 
40  for (int x = 0; x < 5; x++)
41  {
42  (void)sprintf_s(tmp, ARRAYSIZE(tmp), "%08" PRIX32, prand(UINT32_MAX));
43  path = GetCombinedPath(base, tmp);
44  free(base);
45 
46  if (!path)
47  {
48  (void)fprintf(stderr, "GetCombinedPath failed!\n");
49  return -1;
50  }
51 
52  base = path;
53  }
54 
55  printf("Creating path %s\n", path);
56  success = winpr_PathMakePath(path, NULL);
57 
58  if (!success)
59  {
60  (void)fprintf(stderr, "MakePath failed!\n");
61  free(path);
62  return -1;
63  }
64 
65  success = winpr_PathFileExists(path);
66 
67  if (!success)
68  {
69  (void)fprintf(stderr, "MakePath lied about success!\n");
70  free(path);
71  return -1;
72  }
73 
74  while (strlen(path) > baseLen)
75  {
76  if (!winpr_RemoveDirectory(path))
77  {
78  (void)fprintf(stderr, "winpr_RemoveDirectory %s failed!\n", path);
79  free(path);
80  return -1;
81  }
82 
83  cur = strrchr(path, delim);
84 
85  if (cur)
86  *cur = '\0';
87  }
88 
89  free(path);
90  printf("%s success!\n", __func__);
91  return 0;
92 }