xref: /freebsd/contrib/libder/tests/test_common.h (revision 35c0a8c449fd2b7f75029ebed5e10852240f0865)
1*35c0a8c4SKyle Evans /*-
2*35c0a8c4SKyle Evans  * Copyright (c) 2024 Kyle Evans <kevans@FreeBSD.org>
3*35c0a8c4SKyle Evans  *
4*35c0a8c4SKyle Evans  * SPDX-License-Identifier: BSD-2-Clause
5*35c0a8c4SKyle Evans  */
6*35c0a8c4SKyle Evans 
7*35c0a8c4SKyle Evans #include <assert.h>
8*35c0a8c4SKyle Evans #include <fcntl.h>
9*35c0a8c4SKyle Evans #include <libgen.h>
10*35c0a8c4SKyle Evans #include <limits.h>
11*35c0a8c4SKyle Evans #include <stdlib.h>
12*35c0a8c4SKyle Evans 
13*35c0a8c4SKyle Evans static inline int
open_progdir(const char * prog)14*35c0a8c4SKyle Evans open_progdir(const char *prog)
15*35c0a8c4SKyle Evans {
16*35c0a8c4SKyle Evans 	char pdir[PATH_MAX], *resolved;
17*35c0a8c4SKyle Evans 	int dfd;
18*35c0a8c4SKyle Evans 
19*35c0a8c4SKyle Evans 	resolved = realpath(prog, &pdir[0]);
20*35c0a8c4SKyle Evans 	assert(resolved != NULL);
21*35c0a8c4SKyle Evans 
22*35c0a8c4SKyle Evans 	resolved = dirname(&pdir[0]);
23*35c0a8c4SKyle Evans 	assert(resolved != NULL);
24*35c0a8c4SKyle Evans 
25*35c0a8c4SKyle Evans 	dfd = open(resolved, O_DIRECTORY);
26*35c0a8c4SKyle Evans 	assert(dfd != -1);
27*35c0a8c4SKyle Evans 
28*35c0a8c4SKyle Evans 	return (dfd);
29*35c0a8c4SKyle Evans }
30