xref: /freebsd-14.2/libexec/rtld-elf/libmap.c (revision 68f1db20)
129ade362SMatthew N. Dodd /*
229ade362SMatthew N. Dodd  * $FreeBSD$
329ade362SMatthew N. Dodd  */
429ade362SMatthew N. Dodd 
529ade362SMatthew N. Dodd #include <sys/param.h>
6*68f1db20SKonstantin Belousov #include <sys/fcntl.h>
7*68f1db20SKonstantin Belousov #include <sys/mman.h>
8*68f1db20SKonstantin Belousov #include <sys/queue.h>
9*68f1db20SKonstantin Belousov #include <sys/stat.h>
10*68f1db20SKonstantin Belousov #include <errno.h>
11*68f1db20SKonstantin Belousov #include <stdlib.h>
12*68f1db20SKonstantin Belousov #include <string.h>
1329ade362SMatthew N. Dodd 
143467e8b8SMatthew N. Dodd #include "debug.h"
153467e8b8SMatthew N. Dodd #include "rtld.h"
167227105fSMatthew N. Dodd #include "libmap.h"
173467e8b8SMatthew N. Dodd 
1829ade362SMatthew N. Dodd #ifndef _PATH_LIBMAP_CONF
1929ade362SMatthew N. Dodd #define	_PATH_LIBMAP_CONF	"/etc/libmap.conf"
2029ade362SMatthew N. Dodd #endif
2129ade362SMatthew N. Dodd 
22c905e45dSPeter Wemm #ifdef COMPAT_32BIT
23c905e45dSPeter Wemm #undef _PATH_LIBMAP_CONF
24c905e45dSPeter Wemm #define	_PATH_LIBMAP_CONF	"/etc/libmap32.conf"
25c905e45dSPeter Wemm #endif
26c905e45dSPeter Wemm 
2729ade362SMatthew N. Dodd TAILQ_HEAD(lm_list, lm);
2829ade362SMatthew N. Dodd struct lm {
2929ade362SMatthew N. Dodd 	char *f;
3029ade362SMatthew N. Dodd 	char *t;
3129ade362SMatthew N. Dodd 	TAILQ_ENTRY(lm)	lm_link;
3229ade362SMatthew N. Dodd };
3329ade362SMatthew N. Dodd 
3429ade362SMatthew N. Dodd TAILQ_HEAD(lmp_list, lmp) lmp_head = TAILQ_HEAD_INITIALIZER(lmp_head);
3529ade362SMatthew N. Dodd struct lmp {
3629ade362SMatthew N. Dodd 	char *p;
37966efcc7SMatthew N. Dodd 	enum { T_EXACT=0, T_BASENAME, T_DIRECTORY } type;
3829ade362SMatthew N. Dodd 	struct lm_list lml;
3929ade362SMatthew N. Dodd 	TAILQ_ENTRY(lmp) lmp_link;
4029ade362SMatthew N. Dodd };
4129ade362SMatthew N. Dodd 
425b08cb04SMatthew N. Dodd static int lm_count;
435b08cb04SMatthew N. Dodd 
44*68f1db20SKonstantin Belousov static void lmc_parse(char *, size_t);
451340fc10SMatthew N. Dodd static void lm_add(const char *, const char *, const char *);
4629ade362SMatthew N. Dodd static void lm_free(struct lm_list *);
4729ade362SMatthew N. Dodd static char *lml_find(struct lm_list *, const char *);
4829ade362SMatthew N. Dodd static struct lm_list *lmp_find(const char *);
4929ade362SMatthew N. Dodd static struct lm_list *lmp_init(char *);
50966efcc7SMatthew N. Dodd static const char *quickbasename(const char *);
5129ade362SMatthew N. Dodd 
52623b6bd2SMatthew N. Dodd #define	iseol(c)	(((c) == '#') || ((c) == '\0') || \
53623b6bd2SMatthew N. Dodd 			 ((c) == '\n') || ((c) == '\r'))
54623b6bd2SMatthew N. Dodd 
55b36070f5SKonstantin Belousov /*
56b36070f5SKonstantin Belousov  * Do not use ctype.h macros, which rely on working TLS.  It is
57b36070f5SKonstantin Belousov  * too early to have thread-local variables functional.
58b36070f5SKonstantin Belousov  */
5944976acaSSergey Kandaurov #define	rtld_isspace(c)	((c) == ' ' || (c) == '\t')
60b36070f5SKonstantin Belousov 
614402996dSMatthew N. Dodd int
625b08cb04SMatthew N. Dodd lm_init(char *libmap_override)
6329ade362SMatthew N. Dodd {
64*68f1db20SKonstantin Belousov 	struct stat st;
65*68f1db20SKonstantin Belousov 	char *lm_map, *p;
66*68f1db20SKonstantin Belousov 	int fd;
6729ade362SMatthew N. Dodd 
68*68f1db20SKonstantin Belousov 	dbg("lm_init(\"%s\")", libmap_override);
6929ade362SMatthew N. Dodd 	TAILQ_INIT(&lmp_head);
7029ade362SMatthew N. Dodd 
71*68f1db20SKonstantin Belousov 	fd = open(_PATH_LIBMAP_CONF, O_RDONLY);
72*68f1db20SKonstantin Belousov 	if (fd == -1) {
73*68f1db20SKonstantin Belousov 		dbg("lm_init: open(\"%s\") failed, %s", _PATH_LIBMAP_CONF,
74*68f1db20SKonstantin Belousov 		    strerror(errno));
75*68f1db20SKonstantin Belousov 		goto override;
765b08cb04SMatthew N. Dodd 	}
77*68f1db20SKonstantin Belousov 	if (fstat(fd, &st) == -1) {
78*68f1db20SKonstantin Belousov 		close(fd);
79*68f1db20SKonstantin Belousov 		dbg("lm_init: fstat(\"%s\") failed, %s", _PATH_LIBMAP_CONF,
80*68f1db20SKonstantin Belousov 		    strerror(errno));
81*68f1db20SKonstantin Belousov 		goto override;
82*68f1db20SKonstantin Belousov 	}
83*68f1db20SKonstantin Belousov 	lm_map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
84*68f1db20SKonstantin Belousov 	if (lm_map == (const char *)MAP_FAILED) {
85*68f1db20SKonstantin Belousov 		close(fd);
86*68f1db20SKonstantin Belousov 		dbg("lm_init: mmap(\"%s\") failed, %s", _PATH_LIBMAP_CONF,
87*68f1db20SKonstantin Belousov 		    strerror(errno));
88*68f1db20SKonstantin Belousov 		goto override;
89*68f1db20SKonstantin Belousov 	}
90*68f1db20SKonstantin Belousov 	close(fd);
91*68f1db20SKonstantin Belousov 	lmc_parse(lm_map, st.st_size);
92*68f1db20SKonstantin Belousov 	munmap(lm_map, st.st_size);
935b08cb04SMatthew N. Dodd 
94*68f1db20SKonstantin Belousov override:
955b08cb04SMatthew N. Dodd 	if (libmap_override) {
96*68f1db20SKonstantin Belousov 		/*
97*68f1db20SKonstantin Belousov 		 * Do some character replacement to make $LIBMAP look
98*68f1db20SKonstantin Belousov 		 * like a text file, then parse it.
99*68f1db20SKonstantin Belousov 		 */
1005b08cb04SMatthew N. Dodd 		libmap_override = xstrdup(libmap_override);
1015b08cb04SMatthew N. Dodd 		for (p = libmap_override; *p; p++) {
1025b08cb04SMatthew N. Dodd 			switch (*p) {
1035b08cb04SMatthew N. Dodd 			case '=':
104*68f1db20SKonstantin Belousov 				*p = ' ';
105*68f1db20SKonstantin Belousov 				break;
1065b08cb04SMatthew N. Dodd 			case ',':
107*68f1db20SKonstantin Belousov 				*p = '\n';
108*68f1db20SKonstantin Belousov 				break;
1095b08cb04SMatthew N. Dodd 			}
1105b08cb04SMatthew N. Dodd 		}
111*68f1db20SKonstantin Belousov 		lmc_parse(p, strlen(p));
112*68f1db20SKonstantin Belousov 		free(p);
1135b08cb04SMatthew N. Dodd 	}
1145b08cb04SMatthew N. Dodd 
1155b08cb04SMatthew N. Dodd 	return (lm_count == 0);
1165b08cb04SMatthew N. Dodd }
1175b08cb04SMatthew N. Dodd 
1185b08cb04SMatthew N. Dodd static void
119*68f1db20SKonstantin Belousov lmc_parse(char *lm_p, size_t lm_len)
1205b08cb04SMatthew N. Dodd {
121*68f1db20SKonstantin Belousov 	char *cp, *f, *t, *c, *p;
1225b08cb04SMatthew N. Dodd 	char prog[MAXPATHLEN];
1235b08cb04SMatthew N. Dodd 	char line[MAXPATHLEN + 2];
124*68f1db20SKonstantin Belousov 	size_t cnt;
125*68f1db20SKonstantin Belousov 	int i;
1265b08cb04SMatthew N. Dodd 
127*68f1db20SKonstantin Belousov 	cnt = 0;
12829ade362SMatthew N. Dodd 	p = NULL;
129*68f1db20SKonstantin Belousov 	while (cnt < lm_len) {
130*68f1db20SKonstantin Belousov 		i = 0;
131*68f1db20SKonstantin Belousov 		while (lm_p[cnt] != '\n' && cnt < lm_len &&
132*68f1db20SKonstantin Belousov 		    i < sizeof(line) - 1) {
133*68f1db20SKonstantin Belousov 			line[i] = lm_p[cnt];
134*68f1db20SKonstantin Belousov 			cnt++;
135*68f1db20SKonstantin Belousov 			i++;
136*68f1db20SKonstantin Belousov 		}
137*68f1db20SKonstantin Belousov 		line[i] = '\0';
138*68f1db20SKonstantin Belousov 		while (lm_p[cnt] != '\n' && cnt < lm_len)
139*68f1db20SKonstantin Belousov 			cnt++;
140*68f1db20SKonstantin Belousov 		/* skip over nl */
141*68f1db20SKonstantin Belousov 		cnt++;
142*68f1db20SKonstantin Belousov 
143*68f1db20SKonstantin Belousov 		cp = &line[0];
1441340fc10SMatthew N. Dodd 		t = f = c = NULL;
145623b6bd2SMatthew N. Dodd 
14629ade362SMatthew N. Dodd 		/* Skip over leading space */
14744976acaSSergey Kandaurov 		while (rtld_isspace(*cp)) cp++;
148623b6bd2SMatthew N. Dodd 
14929ade362SMatthew N. Dodd 		/* Found a comment or EOL */
150486089f0SAlexander Kabaev 		if (iseol(*cp)) continue;
151623b6bd2SMatthew N. Dodd 
152623b6bd2SMatthew N. Dodd 		/* Found a constraint selector */
15329ade362SMatthew N. Dodd 		if (*cp == '[') {
15429ade362SMatthew N. Dodd 			cp++;
155623b6bd2SMatthew N. Dodd 
15629ade362SMatthew N. Dodd 			/* Skip leading space */
15744976acaSSergey Kandaurov 			while (rtld_isspace(*cp)) cp++;
158623b6bd2SMatthew N. Dodd 
15929ade362SMatthew N. Dodd 			/* Found comment, EOL or end of selector */
160486089f0SAlexander Kabaev 			if  (iseol(*cp) || *cp == ']')
161486089f0SAlexander Kabaev 				continue;
162623b6bd2SMatthew N. Dodd 
1631340fc10SMatthew N. Dodd 			c = cp++;
16429ade362SMatthew N. Dodd 			/* Skip to end of word */
16544976acaSSergey Kandaurov 			while (!rtld_isspace(*cp) && !iseol(*cp) && *cp != ']')
166486089f0SAlexander Kabaev 				cp++;
167623b6bd2SMatthew N. Dodd 
168623b6bd2SMatthew N. Dodd 			/* Skip and zero out trailing space */
16944976acaSSergey Kandaurov 			while (rtld_isspace(*cp)) *cp++ = '\0';
170623b6bd2SMatthew N. Dodd 
171623b6bd2SMatthew N. Dodd 			/* Check if there is a closing brace */
172486089f0SAlexander Kabaev 			if (*cp != ']') continue;
173623b6bd2SMatthew N. Dodd 
174623b6bd2SMatthew N. Dodd 			/* Terminate string if there was no trailing space */
17529ade362SMatthew N. Dodd 			*cp++ = '\0';
176623b6bd2SMatthew N. Dodd 
177623b6bd2SMatthew N. Dodd 			/*
178623b6bd2SMatthew N. Dodd 			 * There should be nothing except whitespace or comment
1796d5d786fSAlexander Kabaev 			  from this point to the end of the line.
180623b6bd2SMatthew N. Dodd 			 */
18144976acaSSergey Kandaurov 			while(rtld_isspace(*cp)) cp++;
182486089f0SAlexander Kabaev 			if (!iseol(*cp)) continue;
183623b6bd2SMatthew N. Dodd 
1841340fc10SMatthew N. Dodd 			strcpy(prog, c);
18529ade362SMatthew N. Dodd 			p = prog;
186486089f0SAlexander Kabaev 			continue;
187623b6bd2SMatthew N. Dodd 		}
188623b6bd2SMatthew N. Dodd 
189623b6bd2SMatthew N. Dodd 		/* Parse the 'from' candidate. */
190486089f0SAlexander Kabaev 		f = cp++;
19144976acaSSergey Kandaurov 		while (!rtld_isspace(*cp) && !iseol(*cp)) cp++;
19229ade362SMatthew N. Dodd 
193623b6bd2SMatthew N. Dodd 		/* Skip and zero out the trailing whitespace */
19444976acaSSergey Kandaurov 		while (rtld_isspace(*cp)) *cp++ = '\0';
195623b6bd2SMatthew N. Dodd 
196623b6bd2SMatthew N. Dodd 		/* Found a comment or EOL */
197486089f0SAlexander Kabaev 		if (iseol(*cp)) continue;
198623b6bd2SMatthew N. Dodd 
199623b6bd2SMatthew N. Dodd 		/* Parse 'to' mapping */
200486089f0SAlexander Kabaev 		t = cp++;
20144976acaSSergey Kandaurov 		while (!rtld_isspace(*cp) && !iseol(*cp)) cp++;
202623b6bd2SMatthew N. Dodd 
203486089f0SAlexander Kabaev 		/* Skip and zero out the trailing whitespace */
20444976acaSSergey Kandaurov 		while (rtld_isspace(*cp)) *cp++ = '\0';
205486089f0SAlexander Kabaev 
206486089f0SAlexander Kabaev 		/* Should be no extra tokens at this point */
207486089f0SAlexander Kabaev 		if (!iseol(*cp)) continue;
208486089f0SAlexander Kabaev 
209486089f0SAlexander Kabaev 		*cp = '\0';
2101340fc10SMatthew N. Dodd 		lm_add(p, f, t);
21129ade362SMatthew N. Dodd 	}
21229ade362SMatthew N. Dodd }
21329ade362SMatthew N. Dodd 
21429ade362SMatthew N. Dodd static void
21529ade362SMatthew N. Dodd lm_free (struct lm_list *lml)
21629ade362SMatthew N. Dodd {
21729ade362SMatthew N. Dodd 	struct lm *lm;
21829ade362SMatthew N. Dodd 
2191aac1ed6SMatthew N. Dodd 	dbg("%s(%p)", __func__, lml);
2201aac1ed6SMatthew N. Dodd 
22129ade362SMatthew N. Dodd 	while (!TAILQ_EMPTY(lml)) {
22229ade362SMatthew N. Dodd 		lm = TAILQ_FIRST(lml);
22329ade362SMatthew N. Dodd 		TAILQ_REMOVE(lml, lm, lm_link);
22429ade362SMatthew N. Dodd 		free(lm->f);
22529ade362SMatthew N. Dodd 		free(lm->t);
22629ade362SMatthew N. Dodd 		free(lm);
22729ade362SMatthew N. Dodd 	}
22829ade362SMatthew N. Dodd 	return;
22929ade362SMatthew N. Dodd }
23029ade362SMatthew N. Dodd 
23129ade362SMatthew N. Dodd void
23229ade362SMatthew N. Dodd lm_fini (void)
23329ade362SMatthew N. Dodd {
23429ade362SMatthew N. Dodd 	struct lmp *lmp;
23529ade362SMatthew N. Dodd 
2361aac1ed6SMatthew N. Dodd 	dbg("%s()", __func__);
2371aac1ed6SMatthew N. Dodd 
23829ade362SMatthew N. Dodd 	while (!TAILQ_EMPTY(&lmp_head)) {
23929ade362SMatthew N. Dodd 		lmp = TAILQ_FIRST(&lmp_head);
24029ade362SMatthew N. Dodd 		TAILQ_REMOVE(&lmp_head, lmp, lmp_link);
24129ade362SMatthew N. Dodd 		free(lmp->p);
24229ade362SMatthew N. Dodd 		lm_free(&lmp->lml);
24329ade362SMatthew N. Dodd 		free(lmp);
24429ade362SMatthew N. Dodd 	}
24529ade362SMatthew N. Dodd 	return;
24629ade362SMatthew N. Dodd }
24729ade362SMatthew N. Dodd 
24829ade362SMatthew N. Dodd static void
2491340fc10SMatthew N. Dodd lm_add (const char *p, const char *f, const char *t)
25029ade362SMatthew N. Dodd {
25129ade362SMatthew N. Dodd 	struct lm_list *lml;
25229ade362SMatthew N. Dodd 	struct lm *lm;
25329ade362SMatthew N. Dodd 
25429ade362SMatthew N. Dodd 	if (p == NULL)
25529ade362SMatthew N. Dodd 		p = "$DEFAULT$";
25629ade362SMatthew N. Dodd 
2571aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\", \"%s\", \"%s\")", __func__, p, f, t);
2581aac1ed6SMatthew N. Dodd 
25929ade362SMatthew N. Dodd 	if ((lml = lmp_find(p)) == NULL)
2603467e8b8SMatthew N. Dodd 		lml = lmp_init(xstrdup(p));
26129ade362SMatthew N. Dodd 
2623467e8b8SMatthew N. Dodd 	lm = xmalloc(sizeof(struct lm));
2631340fc10SMatthew N. Dodd 	lm->f = xstrdup(f);
2641340fc10SMatthew N. Dodd 	lm->t = xstrdup(t);
26529ade362SMatthew N. Dodd 	TAILQ_INSERT_HEAD(lml, lm, lm_link);
2665b08cb04SMatthew N. Dodd 	lm_count++;
26729ade362SMatthew N. Dodd }
26829ade362SMatthew N. Dodd 
26929ade362SMatthew N. Dodd char *
27029ade362SMatthew N. Dodd lm_find (const char *p, const char *f)
27129ade362SMatthew N. Dodd {
27229ade362SMatthew N. Dodd 	struct lm_list *lml;
27329ade362SMatthew N. Dodd 	char *t;
27429ade362SMatthew N. Dodd 
2751aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\", \"%s\")", __func__, p, f);
2761aac1ed6SMatthew N. Dodd 
27729ade362SMatthew N. Dodd 	if (p != NULL && (lml = lmp_find(p)) != NULL) {
27829ade362SMatthew N. Dodd 		t = lml_find(lml, f);
2793467e8b8SMatthew N. Dodd 		if (t != NULL) {
2803467e8b8SMatthew N. Dodd 			/*
2813467e8b8SMatthew N. Dodd 			 * Add a global mapping if we have
2823467e8b8SMatthew N. Dodd 			 * a successful constrained match.
2833467e8b8SMatthew N. Dodd 			 */
2841340fc10SMatthew N. Dodd 			lm_add(NULL, f, t);
28529ade362SMatthew N. Dodd 			return (t);
28629ade362SMatthew N. Dodd 		}
2873467e8b8SMatthew N. Dodd 	}
28829ade362SMatthew N. Dodd 	lml = lmp_find("$DEFAULT$");
28929ade362SMatthew N. Dodd 	if (lml != NULL)
29029ade362SMatthew N. Dodd 		return (lml_find(lml, f));
29129ade362SMatthew N. Dodd 	else
29229ade362SMatthew N. Dodd 		return (NULL);
29329ade362SMatthew N. Dodd }
29429ade362SMatthew N. Dodd 
295966efcc7SMatthew N. Dodd /* Given a libmap translation list and a library name, return the
296966efcc7SMatthew N. Dodd    replacement library, or NULL */
297c905e45dSPeter Wemm #ifdef COMPAT_32BIT
298c905e45dSPeter Wemm char *
299c905e45dSPeter Wemm lm_findn (const char *p, const char *f, const int n)
300c905e45dSPeter Wemm {
301c905e45dSPeter Wemm 	char pathbuf[64], *s, *t;
302c905e45dSPeter Wemm 
3038c6a035eSPeter Wemm 	if (n < sizeof(pathbuf) - 1)
304c905e45dSPeter Wemm 		s = pathbuf;
3058c6a035eSPeter Wemm 	else
306c905e45dSPeter Wemm 		s = xmalloc(n + 1);
3078c6a035eSPeter Wemm 	memcpy(s, f, n);
3088c6a035eSPeter Wemm 	s[n] = '\0';
309c905e45dSPeter Wemm 	t = lm_find(p, s);
310c905e45dSPeter Wemm 	if (s != pathbuf)
311c905e45dSPeter Wemm 		free(s);
312c905e45dSPeter Wemm 	return (t);
313c905e45dSPeter Wemm }
314c905e45dSPeter Wemm #endif
315c905e45dSPeter Wemm 
31629ade362SMatthew N. Dodd static char *
31729ade362SMatthew N. Dodd lml_find (struct lm_list *lmh, const char *f)
31829ade362SMatthew N. Dodd {
31929ade362SMatthew N. Dodd 	struct lm *lm;
32029ade362SMatthew N. Dodd 
3211aac1ed6SMatthew N. Dodd 	dbg("%s(%p, \"%s\")", __func__, lmh, f);
3221aac1ed6SMatthew N. Dodd 
32329ade362SMatthew N. Dodd 	TAILQ_FOREACH(lm, lmh, lm_link)
324c905e45dSPeter Wemm 		if (strcmp(f, lm->f) == 0)
32529ade362SMatthew N. Dodd 			return (lm->t);
326d33da23fSMatthew N. Dodd 	return (NULL);
32729ade362SMatthew N. Dodd }
32829ade362SMatthew N. Dodd 
329966efcc7SMatthew N. Dodd /* Given an executable name, return a pointer to the translation list or
330966efcc7SMatthew N. Dodd    NULL if no matches */
33129ade362SMatthew N. Dodd static struct lm_list *
33229ade362SMatthew N. Dodd lmp_find (const char *n)
33329ade362SMatthew N. Dodd {
33429ade362SMatthew N. Dodd 	struct lmp *lmp;
33529ade362SMatthew N. Dodd 
3361aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\")", __func__, n);
3371aac1ed6SMatthew N. Dodd 
33829ade362SMatthew N. Dodd 	TAILQ_FOREACH(lmp, &lmp_head, lmp_link)
339966efcc7SMatthew N. Dodd 		if ((lmp->type == T_EXACT && strcmp(n, lmp->p) == 0) ||
340966efcc7SMatthew N. Dodd 		    (lmp->type == T_DIRECTORY && strncmp(n, lmp->p, strlen(lmp->p)) == 0) ||
341966efcc7SMatthew N. Dodd 		    (lmp->type == T_BASENAME && strcmp(quickbasename(n), lmp->p) == 0))
34229ade362SMatthew N. Dodd 			return (&lmp->lml);
34329ade362SMatthew N. Dodd 	return (NULL);
34429ade362SMatthew N. Dodd }
34529ade362SMatthew N. Dodd 
34629ade362SMatthew N. Dodd static struct lm_list *
34729ade362SMatthew N. Dodd lmp_init (char *n)
34829ade362SMatthew N. Dodd {
34929ade362SMatthew N. Dodd 	struct lmp *lmp;
35029ade362SMatthew N. Dodd 
3511aac1ed6SMatthew N. Dodd 	dbg("%s(\"%s\")", __func__, n);
3521aac1ed6SMatthew N. Dodd 
3533467e8b8SMatthew N. Dodd 	lmp = xmalloc(sizeof(struct lmp));
35429ade362SMatthew N. Dodd 	lmp->p = n;
355966efcc7SMatthew N. Dodd 	if (n[strlen(n)-1] == '/')
356966efcc7SMatthew N. Dodd 		lmp->type = T_DIRECTORY;
357966efcc7SMatthew N. Dodd 	else if (strchr(n,'/') == NULL)
358966efcc7SMatthew N. Dodd 		lmp->type = T_BASENAME;
359966efcc7SMatthew N. Dodd 	else
360966efcc7SMatthew N. Dodd 		lmp->type = T_EXACT;
36129ade362SMatthew N. Dodd 	TAILQ_INIT(&lmp->lml);
36229ade362SMatthew N. Dodd 	TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link);
36329ade362SMatthew N. Dodd 
36429ade362SMatthew N. Dodd 	return (&lmp->lml);
36529ade362SMatthew N. Dodd }
366966efcc7SMatthew N. Dodd 
367966efcc7SMatthew N. Dodd /* libc basename is overkill.  Return a pointer to the character after the
368966efcc7SMatthew N. Dodd    last /, or the original string if there are no slashes. */
369966efcc7SMatthew N. Dodd static const char *
370966efcc7SMatthew N. Dodd quickbasename (const char *path)
371966efcc7SMatthew N. Dodd {
372966efcc7SMatthew N. Dodd 	const char *p = path;
373d33da23fSMatthew N. Dodd 	for (; *path; path++) {
374966efcc7SMatthew N. Dodd 		if (*path == '/')
375966efcc7SMatthew N. Dodd 			p = path+1;
376966efcc7SMatthew N. Dodd 	}
377d33da23fSMatthew N. Dodd 	return (p);
378966efcc7SMatthew N. Dodd }
379