Blob


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