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 1411938b 2018-02-12 stsp #include "got_path_priv.h"
24 1411938b 2018-02-12 stsp
25 4027f31a 2017-11-04 stsp int
26 4027f31a 2017-11-04 stsp got_path_is_absolute(const char *path)
27 4027f31a 2017-11-04 stsp {
28 4027f31a 2017-11-04 stsp return path[0] == '/';
29 4027f31a 2017-11-04 stsp }
30 4027f31a 2017-11-04 stsp
31 4027f31a 2017-11-04 stsp char *
32 4027f31a 2017-11-04 stsp got_path_get_absolute(const char *relpath)
33 4027f31a 2017-11-04 stsp {
34 4027f31a 2017-11-04 stsp char cwd[PATH_MAX];
35 4027f31a 2017-11-04 stsp char *abspath;
36 4027f31a 2017-11-04 stsp
37 4027f31a 2017-11-04 stsp if (getcwd(cwd, sizeof(cwd)) == NULL)
38 4027f31a 2017-11-04 stsp return NULL;
39 4027f31a 2017-11-04 stsp
40 4027f31a 2017-11-04 stsp if (asprintf(&abspath, "%s/%s/", cwd, relpath) == -1)
41 4027f31a 2017-11-04 stsp return NULL;
42 4027f31a 2017-11-04 stsp
43 4027f31a 2017-11-04 stsp return abspath;
44 4027f31a 2017-11-04 stsp }
45 4027f31a 2017-11-04 stsp
46 4027f31a 2017-11-04 stsp char *
47 4027f31a 2017-11-04 stsp got_path_normalize(const char *path)
48 4027f31a 2017-11-04 stsp {
49 4027f31a 2017-11-04 stsp char *resolved;
50 4027f31a 2017-11-04 stsp
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;
54 4027f31a 2017-11-04 stsp
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;
59 4027f31a 2017-11-04 stsp }
60 4027f31a 2017-11-04 stsp
61 4027f31a 2017-11-04 stsp return resolved;
62 4027f31a 2017-11-04 stsp }
63 a1fd68d8 2018-01-12 stsp
64 a1fd68d8 2018-01-12 stsp FILE *
65 a1fd68d8 2018-01-12 stsp got_opentemp(void)
66 a1fd68d8 2018-01-12 stsp {
67 a1fd68d8 2018-01-12 stsp char name[PATH_MAX];
68 a1fd68d8 2018-01-12 stsp int fd;
69 a1fd68d8 2018-01-12 stsp FILE *f;
70 a1fd68d8 2018-01-12 stsp
71 a1fd68d8 2018-01-12 stsp if (strlcpy(name, "/tmp/got.XXXXXXXX", sizeof(name)) >= sizeof(name))
72 a1fd68d8 2018-01-12 stsp return NULL;
73 a1fd68d8 2018-01-12 stsp
74 a1fd68d8 2018-01-12 stsp fd = mkstemp(name);
75 a1fd68d8 2018-01-12 stsp if (fd < 0)
76 a1fd68d8 2018-01-12 stsp return NULL;
77 a1fd68d8 2018-01-12 stsp
78 0465ef11 2018-01-13 stsp unlink(name);
79 a1fd68d8 2018-01-12 stsp f = fdopen(fd, "w+");
80 a1fd68d8 2018-01-12 stsp if (f == NULL) {
81 a1fd68d8 2018-01-12 stsp close(fd);
82 a1fd68d8 2018-01-12 stsp return NULL;
83 a1fd68d8 2018-01-12 stsp }
84 a1fd68d8 2018-01-12 stsp
85 a1fd68d8 2018-01-12 stsp return f;
86 a1fd68d8 2018-01-12 stsp }