1 #include "config_ast.h"  // IWYU pragma: keep
2 
3 #include <fcntl.h>
4 #include <limits.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8 
9 #include "ast.h"
10 #include "terror.h"
11 
12 char test_path[PATH_MAX] = "foo";
13 
tmain()14 tmain() {
15     UNUSED(argc);
16     UNUSED(argv);
17 
18     char path[PATH_MAX];
19     char absolute_path[PATH_MAX];
20     char absolute_test_path[PATH_MAX];
21 
22     getcwd(absolute_test_path, sizeof(absolute_test_path));
23     strcat(absolute_test_path, "/");
24     strcat(absolute_test_path, "foo");
25 
26     int fd = open(test_path, O_CREAT, 0666);
27     if (fd == -1) terror("Failed to create test file");
28 
29     // Search this path in current directory and expand it
30     pathpath("foo", NULL, PATH_ABSOLUTE, path, sizeof(path));
31     if (strcmp(path, absolute_test_path))
32         terror("Failed to expand path of file in current directory");
33 
34     if (!pathpath("cat", NULL, PATH_EXECUTE, absolute_path, sizeof(absolute_path)))
35         terror("Failed to find `cat` in current $PATH");
36 
37     if (!pathpath(absolute_path, NULL, PATH_EXECUTE, path, sizeof(path)))
38         terror("Failed to find `%s` with absolute path", path);
39 
40     // Tests for serching paths through `FPATH`
41     setenv("PATH", "", 1);
42     setenv("FPATH", "/bin:/usr/bin:/usr/local/bin", 1);
43     if (!pathpath("cat", NULL, PATH_EXECUTE, path, sizeof(path)))
44         terror("Failed to search path through `FPATH`");
45 
46     texit(0);
47 }
48