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