/src/libgit2/src/util/unix/realpath.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) the libgit2 contributors. All rights reserved. |
3 | | * |
4 | | * This file is part of libgit2, distributed under the GNU GPL v2 with |
5 | | * a Linking Exception. For full terms see the included COPYING file. |
6 | | */ |
7 | | |
8 | | #include "git2_util.h" |
9 | | |
10 | | #ifndef GIT_WIN32 |
11 | | |
12 | | #include <limits.h> |
13 | | #include <stdlib.h> |
14 | | #include <fcntl.h> |
15 | | #include <unistd.h> |
16 | | |
17 | | char *p_realpath(const char *pathname, char *resolved) |
18 | 4 | { |
19 | 4 | char *result; |
20 | | |
21 | 4 | if ((result = realpath(pathname, resolved)) == NULL) |
22 | 0 | return NULL; |
23 | | |
24 | | #ifdef __OpenBSD__ |
25 | | /* The OpenBSD realpath function behaves differently, |
26 | | * figure out if the file exists */ |
27 | | if (access(result, F_OK) < 0) { |
28 | | if (!resolved) |
29 | | free(result); |
30 | | |
31 | | return NULL; |
32 | | } |
33 | | #endif |
34 | | |
35 | | /* |
36 | | * If resolved == NULL, the system has allocated the result |
37 | | * string. We need to strdup this into _our_ allocator pool |
38 | | * so that callers can free it with git__free. |
39 | | */ |
40 | 4 | if (!resolved) { |
41 | 0 | char *dup = git__strdup(result); |
42 | 0 | free(result); |
43 | |
|
44 | 0 | result = dup; |
45 | 0 | } |
46 | | |
47 | 4 | return result; |
48 | 4 | } |
49 | | |
50 | | #endif |