1/*	$NetBSD: gen-unix.h,v 1.1 2024/02/18 20:57:31 christos Exp $	*/
2
3/*
4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 *
6 * SPDX-License-Identifier: MPL-2.0
7 *
8 * This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this
10 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11 *
12 * See the COPYRIGHT file distributed with this work for additional
13 * information regarding copyright ownership.
14 */
15
16/*! \file
17 * \brief
18 * This file is responsible for defining two operations that are not
19 * directly portable between Unix-like systems and Windows NT, option
20 * parsing and directory scanning.  It is here because it was decided
21 * that the "gen" build utility was not to depend on libisc.a, so
22 * the functions declared in isc/commandline.h and isc/dir.h could not
23 * be used.
24 *
25 * The commandline stuff is really just a wrapper around getopt().
26 * The dir stuff was shrunk to fit the needs of gen.c.
27 */
28
29#ifndef DNS_GEN_UNIX_H
30#define DNS_GEN_UNIX_H 1
31
32#include <dirent.h>
33#include <errno.h>
34#include <stdbool.h>
35#include <stdlib.h>
36#include <sys/types.h> /* Required on some systems for dirent.h. */
37#include <unistd.h>    /* XXXDCL Required for ?. */
38
39#include <isc/lang.h>
40
41#ifdef NEED_OPTARG
42extern char *optarg;
43#endif /* ifdef NEED_OPTARG */
44
45#define isc_commandline_parse	 getopt
46#define isc_commandline_argument optarg
47
48typedef struct {
49	DIR *handle;
50	char *filename;
51} isc_dir_t;
52
53ISC_LANG_BEGINDECLS
54
55static bool
56start_directory(const char *path, isc_dir_t *dir) {
57	dir->handle = opendir(path);
58
59	if (dir->handle != NULL) {
60		return (true);
61	} else {
62		return (false);
63	}
64}
65
66static bool
67next_file(isc_dir_t *dir) {
68	struct dirent *dirent;
69
70	dir->filename = NULL;
71
72	if (dir->handle != NULL) {
73		errno = 0;
74		dirent = readdir(dir->handle);
75		if (dirent != NULL) {
76			dir->filename = dirent->d_name;
77		} else {
78			if (errno != 0) {
79				exit(1);
80			}
81		}
82	}
83
84	if (dir->filename != NULL) {
85		return (true);
86	} else {
87		return (false);
88	}
89}
90
91static void
92end_directory(isc_dir_t *dir) {
93	if (dir->handle != NULL) {
94		(void)closedir(dir->handle);
95	}
96
97	dir->handle = NULL;
98}
99
100ISC_LANG_ENDDECLS
101
102#endif /* DNS_GEN_UNIX_H */
103