2 7b19e0f1 2017-11-05 stsp * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
4 7b19e0f1 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 7b19e0f1 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 7b19e0f1 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
8 7b19e0f1 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7b19e0f1 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7b19e0f1 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7b19e0f1 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7b19e0f1 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7b19e0f1 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7b19e0f1 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 4027f31a 2017-11-04 stsp /* #include <sys/syslimits.h> */
19 4027f31a 2017-11-04 stsp #include <limits.h>
20 4027f31a 2017-11-04 stsp #include <stdlib.h>
21 4027f31a 2017-11-04 stsp #include <unistd.h>
22 4027f31a 2017-11-04 stsp #include <stdio.h>
23 4027f31a 2017-11-04 stsp #include <string.h>
26 4027f31a 2017-11-04 stsp got_path_is_absolute(const char *path)
28 4027f31a 2017-11-04 stsp return path[0] == '/';
32 4027f31a 2017-11-04 stsp got_path_get_absolute(const char *relpath)
34 4027f31a 2017-11-04 stsp char cwd[PATH_MAX];
35 4027f31a 2017-11-04 stsp char *abspath;
37 4027f31a 2017-11-04 stsp if (getcwd(cwd, sizeof(cwd)) == NULL)
38 4027f31a 2017-11-04 stsp return NULL;
40 4027f31a 2017-11-04 stsp if (asprintf(&abspath, "%s/%s/", cwd, relpath) == -1)
41 4027f31a 2017-11-04 stsp return NULL;
43 4027f31a 2017-11-04 stsp return abspath;
47 4027f31a 2017-11-04 stsp got_path_normalize(const char *path)
49 4027f31a 2017-11-04 stsp char *resolved;
51 4027f31a 2017-11-04 stsp resolved = realpath(path, NULL);
52 4027f31a 2017-11-04 stsp if (resolved == NULL)
53 4027f31a 2017-11-04 stsp return NULL;
55 4027f31a 2017-11-04 stsp if (!got_path_is_absolute(resolved)) {
56 4027f31a 2017-11-04 stsp char *abspath = got_path_get_absolute(resolved);
57 4027f31a 2017-11-04 stsp free(resolved);
58 4027f31a 2017-11-04 stsp resolved = abspath;
61 4027f31a 2017-11-04 stsp return resolved;
65 4027f31a 2017-11-04 stsp got_path_is_normalized(const char *path)
67 4027f31a 2017-11-04 stsp char *normpath;
70 4027f31a 2017-11-04 stsp normpath = got_path_normalize(path);
71 4027f31a 2017-11-04 stsp ret = (strcmp(normpath, path) == 0);
72 4027f31a 2017-11-04 stsp free(normpath);