summaryrefslogtreecommitdiff
path: root/libgcompat/realpath.c
blob: ab68cc4b68686f8542092097c22f0a89677d96ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifndef LINKER
#error LINKER must be defined
#endif

static char *(*real_realpath)(const char *restrict, char *restrict);

char *realpath(const char *restrict path, char *restrict resolved)
{
	if (real_realpath == NULL) {
		real_realpath = dlsym(RTLD_NEXT, "realpath");
		if (real_realpath == NULL) {
			errno = ENOSYS;
			return NULL;
		}
	}

	if (!strcmp(path, "/proc/self/exe")) {
		char *fixed = resolved;

		if (fixed == NULL) {
			fixed = malloc(PATH_MAX);
			if (fixed == NULL) {
				errno = ENOMEM;
				return NULL;
			}
		}

		/* If passed in, the buffer is at least PATH_MAX per POSIX. */
		ssize_t written = readlink(path, fixed, PATH_MAX - 1);
		if (written == -1) {
			/* Free the buffer iff we allocated it. */
			if (fixed != resolved)
				free(fixed);
			return NULL;
		}
		fixed[written] = '\0';

		return fixed;
	}

	return real_realpath(path, resolved);
}