Blame


1 4027f31a 2017-11-04 stsp /* #include <sys/syslimits.h> */
2 4027f31a 2017-11-04 stsp
3 4027f31a 2017-11-04 stsp #include <limits.h>
4 4027f31a 2017-11-04 stsp #include <stdlib.h>
5 4027f31a 2017-11-04 stsp #include <unistd.h>
6 4027f31a 2017-11-04 stsp #include <stdio.h>
7 4027f31a 2017-11-04 stsp #include <string.h>
8 4027f31a 2017-11-04 stsp
9 4027f31a 2017-11-04 stsp int
10 4027f31a 2017-11-04 stsp got_path_is_absolute(const char *path)
11 4027f31a 2017-11-04 stsp {
12 4027f31a 2017-11-04 stsp return path[0] == '/';
13 4027f31a 2017-11-04 stsp }
14 4027f31a 2017-11-04 stsp
15 4027f31a 2017-11-04 stsp char *
16 4027f31a 2017-11-04 stsp got_path_get_absolute(const char *relpath)
17 4027f31a 2017-11-04 stsp {
18 4027f31a 2017-11-04 stsp char cwd[PATH_MAX];
19 4027f31a 2017-11-04 stsp char *abspath;
20 4027f31a 2017-11-04 stsp
21 4027f31a 2017-11-04 stsp if (getcwd(cwd, sizeof(cwd)) == NULL)
22 4027f31a 2017-11-04 stsp return NULL;
23 4027f31a 2017-11-04 stsp
24 4027f31a 2017-11-04 stsp if (asprintf(&abspath, "%s/%s/", cwd, relpath) == -1)
25 4027f31a 2017-11-04 stsp return NULL;
26 4027f31a 2017-11-04 stsp
27 4027f31a 2017-11-04 stsp return abspath;
28 4027f31a 2017-11-04 stsp }
29 4027f31a 2017-11-04 stsp
30 4027f31a 2017-11-04 stsp char *
31 4027f31a 2017-11-04 stsp got_path_normalize(const char *path)
32 4027f31a 2017-11-04 stsp {
33 4027f31a 2017-11-04 stsp char *resolved;
34 4027f31a 2017-11-04 stsp
35 4027f31a 2017-11-04 stsp resolved = realpath(path, NULL);
36 4027f31a 2017-11-04 stsp if (resolved == NULL)
37 4027f31a 2017-11-04 stsp return NULL;
38 4027f31a 2017-11-04 stsp
39 4027f31a 2017-11-04 stsp if (!got_path_is_absolute(resolved)) {
40 4027f31a 2017-11-04 stsp char *abspath = got_path_get_absolute(resolved);
41 4027f31a 2017-11-04 stsp free(resolved);
42 4027f31a 2017-11-04 stsp resolved = abspath;
43 4027f31a 2017-11-04 stsp }
44 4027f31a 2017-11-04 stsp
45 4027f31a 2017-11-04 stsp return resolved;
46 4027f31a 2017-11-04 stsp }
47 4027f31a 2017-11-04 stsp
48 4027f31a 2017-11-04 stsp int
49 4027f31a 2017-11-04 stsp got_path_is_normalized(const char *path)
50 4027f31a 2017-11-04 stsp {
51 4027f31a 2017-11-04 stsp char *normpath;
52 4027f31a 2017-11-04 stsp int ret;
53 4027f31a 2017-11-04 stsp
54 4027f31a 2017-11-04 stsp normpath = got_path_normalize(path);
55 4027f31a 2017-11-04 stsp ret = (strcmp(normpath, path) == 0);
56 4027f31a 2017-11-04 stsp free(normpath);
57 4027f31a 2017-11-04 stsp
58 4027f31a 2017-11-04 stsp return ret;
59 4027f31a 2017-11-04 stsp }