Blame


1 7b19e0f1 2017-11-05 stsp /*
2 a1fd68d8 2018-01-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 7b19e0f1 2017-11-05 stsp *
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.
7 7b19e0f1 2017-11-05 stsp *
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.
15 7b19e0f1 2017-11-05 stsp */
16 7b19e0f1 2017-11-05 stsp
17 4027f31a 2017-11-04 stsp #include <limits.h>
18 4027f31a 2017-11-04 stsp #include <stdlib.h>
19 4027f31a 2017-11-04 stsp #include <unistd.h>
20 4027f31a 2017-11-04 stsp #include <stdio.h>
21 4027f31a 2017-11-04 stsp #include <string.h>
22 4027f31a 2017-11-04 stsp
23 9d31a1d8 2018-03-11 stsp #include "got_error.h"
24 9d31a1d8 2018-03-11 stsp
25 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
26 1411938b 2018-02-12 stsp
27 4027f31a 2017-11-04 stsp int
28 4027f31a 2017-11-04 stsp got_path_is_absolute(const char *path)
29 4027f31a 2017-11-04 stsp {
30 4027f31a 2017-11-04 stsp return path[0] == '/';
31 4027f31a 2017-11-04 stsp }
32 4027f31a 2017-11-04 stsp
33 4027f31a 2017-11-04 stsp char *
34 4027f31a 2017-11-04 stsp got_path_get_absolute(const char *relpath)
35 4027f31a 2017-11-04 stsp {
36 4027f31a 2017-11-04 stsp char cwd[PATH_MAX];
37 4027f31a 2017-11-04 stsp char *abspath;
38 4027f31a 2017-11-04 stsp
39 4027f31a 2017-11-04 stsp if (getcwd(cwd, sizeof(cwd)) == NULL)
40 4027f31a 2017-11-04 stsp return NULL;
41 4027f31a 2017-11-04 stsp
42 4027f31a 2017-11-04 stsp if (asprintf(&abspath, "%s/%s/", cwd, relpath) == -1)
43 4027f31a 2017-11-04 stsp return NULL;
44 4027f31a 2017-11-04 stsp
45 4027f31a 2017-11-04 stsp return abspath;
46 4027f31a 2017-11-04 stsp }
47 4027f31a 2017-11-04 stsp
48 4027f31a 2017-11-04 stsp char *
49 4027f31a 2017-11-04 stsp got_path_normalize(const char *path)
50 4027f31a 2017-11-04 stsp {
51 4027f31a 2017-11-04 stsp char *resolved;
52 4027f31a 2017-11-04 stsp
53 4027f31a 2017-11-04 stsp resolved = realpath(path, NULL);
54 4027f31a 2017-11-04 stsp if (resolved == NULL)
55 4027f31a 2017-11-04 stsp return NULL;
56 4027f31a 2017-11-04 stsp
57 4027f31a 2017-11-04 stsp if (!got_path_is_absolute(resolved)) {
58 4027f31a 2017-11-04 stsp char *abspath = got_path_get_absolute(resolved);
59 4027f31a 2017-11-04 stsp free(resolved);
60 4027f31a 2017-11-04 stsp resolved = abspath;
61 4027f31a 2017-11-04 stsp }
62 4027f31a 2017-11-04 stsp
63 4027f31a 2017-11-04 stsp return resolved;
64 4027f31a 2017-11-04 stsp }
65 a1fd68d8 2018-01-12 stsp
66 9d31a1d8 2018-03-11 stsp const struct got_error *
67 9d31a1d8 2018-03-11 stsp got_path_segment_count(int *count, const char *path)
68 9d31a1d8 2018-03-11 stsp {
69 9d31a1d8 2018-03-11 stsp char *s = strdup(path), *p;
70 9d31a1d8 2018-03-11 stsp
71 9d31a1d8 2018-03-11 stsp *count = 0;
72 9d31a1d8 2018-03-11 stsp
73 9d31a1d8 2018-03-11 stsp if (s == NULL)
74 0a585a0d 2018-03-17 stsp return got_error_from_errno();
75 9d31a1d8 2018-03-11 stsp
76 9d31a1d8 2018-03-11 stsp do {
77 9d31a1d8 2018-03-11 stsp p = strsep(&s, "/");
78 9d31a1d8 2018-03-11 stsp if (s && *s != '/')
79 9d31a1d8 2018-03-11 stsp (*count)++;
80 9d31a1d8 2018-03-11 stsp } while (p);
81 9d31a1d8 2018-03-11 stsp
82 9d31a1d8 2018-03-11 stsp return NULL;
83 9d31a1d8 2018-03-11 stsp }