1 4027f31a 2017-11-04 stsp /* #include <sys/syslimits.h> */
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>
10 4027f31a 2017-11-04 stsp got_path_is_absolute(const char *path)
12 4027f31a 2017-11-04 stsp return path[0] == '/';
16 4027f31a 2017-11-04 stsp got_path_get_absolute(const char *relpath)
18 4027f31a 2017-11-04 stsp char cwd[PATH_MAX];
19 4027f31a 2017-11-04 stsp char *abspath;
21 4027f31a 2017-11-04 stsp if (getcwd(cwd, sizeof(cwd)) == NULL)
22 4027f31a 2017-11-04 stsp return NULL;
24 4027f31a 2017-11-04 stsp if (asprintf(&abspath, "%s/%s/", cwd, relpath) == -1)
25 4027f31a 2017-11-04 stsp return NULL;
27 4027f31a 2017-11-04 stsp return abspath;
31 4027f31a 2017-11-04 stsp got_path_normalize(const char *path)
33 4027f31a 2017-11-04 stsp char *resolved;
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;
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;
45 4027f31a 2017-11-04 stsp return resolved;
49 4027f31a 2017-11-04 stsp got_path_is_normalized(const char *path)
51 4027f31a 2017-11-04 stsp char *normpath;
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);