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