summaryrefslogtreecommitdiff
path: root/tools/ccan_dir.c
blob: 6d95e064a5968d463b4528505406a37bd8995029 (plain)
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
#include <ccan/err/err.h>
#include <ccan/tal/path/path.h>
#include "tools.h"
#include <assert.h>
#include <string.h>
#include <stdlib.h>

/* Walk up to find /ccan/ => ccan directory. */
static unsigned int ccan_dir_prefix(const char *fulldir)
{
	unsigned int i;

	assert(fulldir[0] == '/');
	for (i = strlen(fulldir) - 1; i > 0; i--) {
		if (strncmp(fulldir+i, "/ccan", 5) != 0)
			continue;
		if (fulldir[i+5] != '\0' && fulldir[i+5] != '/')
			continue;
		return i + 1;
	}
	return 0;
}

const char *find_ccan_dir(const char *base)
{
	static char *ccan_dir;

	if (!ccan_dir) {
		if (base[0] != '/') {
			const char *tmpctx = path_cwd(NULL);
			find_ccan_dir(path_join(tmpctx, tmpctx, base));
			tal_free(tmpctx);
		} else {
			unsigned int prefix = ccan_dir_prefix(base);
			if (prefix)
				ccan_dir = tal_strndup(NULL, base, prefix);
		}
	}
	return ccan_dir;
}