tal/path: new module
[ccan] / ccan / tal / path / test / run-is_file.c
1 #include <ccan/tal/path/path.h>
2 #include <ccan/tal/path/path.c>
3 #include <ccan/tap/tap.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7
8 int main(void)
9 {
10         char cwd[1024], *path, *ctx = tal_strdup(NULL, "ctx");
11
12         plan_tests(7);
13
14         if (!getcwd(cwd, sizeof(cwd)))
15                 abort();
16
17         unlink("run-is_file-dir-link");
18         unlink("run-is_file-file-link");
19         unlink("run-is_file-dir/file");
20         rmdir("run-is_file-dir");
21         if (mkdir("run-is_file-dir", 0700) != 0)
22                 abort();
23         if (symlink("run-is_file-dir", "run-is_file-dir-link") != 0)
24                 abort();
25         if (symlink("run-is_file-dir/file", "run-is_file-file-link") != 0)
26                 abort();
27         close(open("run-is_file-dir/file", O_WRONLY|O_CREAT, 0600));
28
29         ok1(!path_is_file("run-is_file-dir-link"));
30         ok1(path_is_file("run-is_file-file-link"));
31         ok1(path_is_file("run-is_file-dir/file"));
32         ok1(!path_is_file("run-is_file-dir"));
33         ok1(!path_is_file("run-is_file-nonexist"));
34
35         path = path_join(ctx, cwd, "run-is_file-dir/file");
36         ok1(path_is_file(path));
37         ok1(!path_is_file(cwd));
38
39         tal_free(ctx);
40
41         return exit_status();
42 }