1999-10-08 22:57:20 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
1999-10-08 22:57:20 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1987, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2014-01-24 09:46:00 -08:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1999-10-08 22:57:20 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2005-04-27 04:57:32 +00:00
|
|
|
* \note This file was adapted from the NetBSD project's source tree, RCS ID:
|
1999-10-08 22:57:20 +00:00
|
|
|
* NetBSD: getopt.c,v 1.15 1999/09/20 04:39:37 lukem Exp
|
|
|
|
*
|
|
|
|
* The primary change has been to rename items to the ISC namespace
|
|
|
|
* and format in the ISC coding style.
|
|
|
|
*
|
|
|
|
* This file is responsible for defining two operations that are not
|
|
|
|
* directly portable between Unix-like systems and Windows NT, option
|
|
|
|
* parsing and directory scanning. It is here because it was decided
|
|
|
|
* that the "gen" build utility was not to depend on libisc.a, so
|
2009-01-17 14:45:17 +00:00
|
|
|
* the functions declared in isc/commandline.h and isc/dir.h could not
|
1999-10-08 22:57:20 +00:00
|
|
|
* be used.
|
|
|
|
*
|
|
|
|
* The commandline stuff is pretty much a straight copy from the initial
|
|
|
|
* isc/commandline.c. The dir stuff was shrunk to fit the needs of gen.c.
|
|
|
|
*/
|
|
|
|
|
2000-04-29 02:02:38 +00:00
|
|
|
#ifndef DNS_GEN_WIN32_H
|
|
|
|
#define DNS_GEN_WIN32_H 1
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
1999-10-08 22:57:20 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2020-03-13 08:38:37 +01:00
|
|
|
#include <time.h>
|
1999-10-08 22:57:20 +00:00
|
|
|
#include <windows.h>
|
|
|
|
|
2018-08-07 16:46:53 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
#define ISC_LANG_BEGINDECLS extern "C" {
|
|
|
|
#define ISC_LANG_ENDDECLS }
|
|
|
|
#else /* ifdef __cplusplus */
|
|
|
|
#define ISC_LANG_BEGINDECLS
|
|
|
|
#define ISC_LANG_ENDDECLS
|
|
|
|
#endif /* ifdef __cplusplus */
|
1999-10-08 22:57:20 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
int isc_commandline_index = 1; /* Index into parent argv vector. */
|
|
|
|
int isc_commandline_option; /* Character checked for validity. */
|
2001-07-26 03:15:16 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
char *isc_commandline_argument; /* Argument associated with option. */
|
|
|
|
char *isc_commandline_progname; /* For printing error messages. */
|
2001-07-26 03:15:16 +00:00
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
bool isc_commandline_errprint = true; /* Print error messages. */
|
2020-02-12 13:59:18 +01:00
|
|
|
bool isc_commandline_reset = true; /* Reset processing. */
|
1999-10-08 22:57:20 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#define BADOPT '?'
|
|
|
|
#define BADARG ':'
|
|
|
|
#define ENDOPT ""
|
1999-10-08 22:57:20 +00:00
|
|
|
|
2000-05-01 17:59:10 +00:00
|
|
|
ISC_LANG_BEGINDECLS
|
|
|
|
|
1999-10-08 22:57:20 +00:00
|
|
|
/*
|
|
|
|
* getopt --
|
|
|
|
* Parse argc/argv argument vector.
|
|
|
|
*/
|
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_commandline_parse(int argc, char *const *argv, const char *options) {
|
1999-10-08 22:57:20 +00:00
|
|
|
static char *place = ENDOPT;
|
2020-02-14 08:14:03 +01:00
|
|
|
char *option; /* Index into *options of option. */
|
1999-10-08 22:57:20 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Update scanning pointer, either because a reset was requested or
|
|
|
|
* the previous argv was finished.
|
|
|
|
*/
|
|
|
|
if (isc_commandline_reset || *place == '\0') {
|
2018-04-17 08:29:14 -07:00
|
|
|
isc_commandline_reset = false;
|
1999-10-08 22:57:20 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (isc_commandline_progname == NULL) {
|
1999-10-08 22:57:20 +00:00
|
|
|
isc_commandline_progname = argv[0];
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-10-08 22:57:20 +00:00
|
|
|
|
|
|
|
if (isc_commandline_index >= argc ||
|
2020-02-13 14:44:37 -08:00
|
|
|
*(place = argv[isc_commandline_index]) != '-')
|
|
|
|
{
|
1999-10-08 22:57:20 +00:00
|
|
|
/*
|
|
|
|
* Index out of range or points to non-option.
|
|
|
|
*/
|
|
|
|
place = ENDOPT;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
|
|
|
|
/*
|
2001-07-26 20:31:48 +00:00
|
|
|
* Found '--' to signal end of options. Advance
|
1999-10-08 22:57:20 +00:00
|
|
|
* index to next argv, the first non-option.
|
|
|
|
*/
|
|
|
|
isc_commandline_index++;
|
|
|
|
place = ENDOPT;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_commandline_option = *place++;
|
|
|
|
option = strchr(options, isc_commandline_option);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure valid option has been passed as specified by options string.
|
|
|
|
* '-:' is never a valid command line option because it could not
|
|
|
|
* distinguish ':' from the argument specifier in the options string.
|
|
|
|
*/
|
|
|
|
if (isc_commandline_option == ':' || option == NULL) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (*place == '\0') {
|
1999-10-08 22:57:20 +00:00
|
|
|
isc_commandline_index++;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-10-08 22:57:20 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (isc_commandline_errprint && *options != ':') {
|
1999-10-08 22:57:20 +00:00
|
|
|
fprintf(stderr, "%s: illegal option -- %c\n",
|
|
|
|
isc_commandline_progname,
|
|
|
|
isc_commandline_option);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-10-08 22:57:20 +00:00
|
|
|
|
|
|
|
return (BADOPT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*++option != ':') {
|
|
|
|
/*
|
|
|
|
* Option does not take an argument.
|
|
|
|
*/
|
|
|
|
isc_commandline_argument = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Skip to next argv if at the end of the current argv.
|
|
|
|
*/
|
2020-02-13 21:48:23 +01:00
|
|
|
if (*place == '\0') {
|
1999-10-08 22:57:20 +00:00
|
|
|
++isc_commandline_index;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-10-08 22:57:20 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Option needs an argument.
|
|
|
|
*/
|
2020-02-13 21:48:23 +01:00
|
|
|
if (*place != '\0') {
|
1999-10-08 22:57:20 +00:00
|
|
|
/*
|
|
|
|
* Option is in this argv, -D1 style.
|
|
|
|
*/
|
|
|
|
isc_commandline_argument = place;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else if (argc > ++isc_commandline_index) {
|
1999-10-08 22:57:20 +00:00
|
|
|
/*
|
|
|
|
* Option is next argv, -D 1 style.
|
|
|
|
*/
|
|
|
|
isc_commandline_argument = argv[isc_commandline_index];
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
1999-10-08 22:57:20 +00:00
|
|
|
/*
|
|
|
|
* Argument needed, but no more argv.
|
|
|
|
*/
|
|
|
|
place = ENDOPT;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Silent failure with "missing argument" return
|
|
|
|
* when ':' starts options string, per historical spec.
|
|
|
|
*/
|
2020-02-13 21:48:23 +01:00
|
|
|
if (*options == ':') {
|
1999-10-08 22:57:20 +00:00
|
|
|
return (BADARG);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-10-08 22:57:20 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (isc_commandline_errprint) {
|
1999-10-08 22:57:20 +00:00
|
|
|
fprintf(stderr,
|
2020-02-12 13:59:18 +01:00
|
|
|
"%s: option requires an argument -- "
|
|
|
|
"%c\n",
|
|
|
|
isc_commandline_progname,
|
|
|
|
isc_commandline_option);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-10-08 22:57:20 +00:00
|
|
|
|
|
|
|
return (BADOPT);
|
|
|
|
}
|
|
|
|
|
|
|
|
place = ENDOPT;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Point to argv that follows argument.
|
|
|
|
*/
|
|
|
|
isc_commandline_index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (isc_commandline_option);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
2020-02-14 08:14:03 +01:00
|
|
|
HANDLE handle;
|
2020-02-12 13:59:18 +01:00
|
|
|
WIN32_FIND_DATA find_data;
|
2020-02-14 08:14:03 +01:00
|
|
|
bool first_file;
|
|
|
|
char *filename;
|
1999-10-08 22:57:20 +00:00
|
|
|
} isc_dir_t;
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
bool
|
2020-02-13 14:44:37 -08:00
|
|
|
start_directory(const char *path, isc_dir_t *dir) {
|
1999-10-08 22:57:20 +00:00
|
|
|
char pattern[_MAX_PATH], *p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Need space for slash-splat and final NUL.
|
|
|
|
*/
|
2020-02-13 21:48:23 +01:00
|
|
|
if (strlen(path) + 3 > sizeof(pattern)) {
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-10-08 22:57:20 +00:00
|
|
|
|
|
|
|
strcpy(pattern, path);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Append slash (if needed) and splat.
|
|
|
|
*/
|
|
|
|
p = pattern + strlen(pattern);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (p != pattern && p[-1] != '\\' && p[-1] != ':') {
|
1999-10-08 22:57:20 +00:00
|
|
|
*p++ = '\\';
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-10-08 22:57:20 +00:00
|
|
|
*p++ = '*';
|
|
|
|
*p++ = '\0';
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
dir->first_file = true;
|
1999-10-08 22:57:20 +00:00
|
|
|
|
|
|
|
dir->handle = FindFirstFile(pattern, &dir->find_data);
|
|
|
|
|
|
|
|
if (dir->handle == INVALID_HANDLE_VALUE) {
|
|
|
|
dir->filename = NULL;
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
1999-10-08 22:57:20 +00:00
|
|
|
} else {
|
|
|
|
dir->filename = dir->find_data.cFileName;
|
2018-04-17 08:29:14 -07:00
|
|
|
return (true);
|
1999-10-08 22:57:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
bool
|
2020-02-13 14:44:37 -08:00
|
|
|
next_file(isc_dir_t *dir) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (dir->first_file) {
|
2018-04-17 08:29:14 -07:00
|
|
|
dir->first_file = false;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else if (dir->handle != INVALID_HANDLE_VALUE) {
|
|
|
|
if (FindNextFile(dir->handle, &dir->find_data) == TRUE) {
|
1999-10-08 22:57:20 +00:00
|
|
|
dir->filename = dir->find_data.cFileName;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
1999-10-08 22:57:20 +00:00
|
|
|
dir->filename = NULL;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
} else {
|
1999-10-08 22:57:20 +00:00
|
|
|
dir->filename = NULL;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-10-08 22:57:20 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (dir->filename != NULL) {
|
2018-04-17 08:29:14 -07:00
|
|
|
return (true);
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-10-08 22:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
end_directory(isc_dir_t *dir) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (dir->handle != INVALID_HANDLE_VALUE) {
|
1999-10-08 22:57:20 +00:00
|
|
|
FindClose(dir->handle);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-10-08 22:57:20 +00:00
|
|
|
}
|
2000-04-29 02:02:38 +00:00
|
|
|
|
2020-03-13 08:38:37 +01:00
|
|
|
inline struct tm *
|
|
|
|
gmtime_r(const time_t *clock, struct tm *result) {
|
|
|
|
errno_t ret = gmtime_s(result, clock);
|
|
|
|
if (ret != 0) {
|
|
|
|
errno = ret;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2000-05-01 17:59:10 +00:00
|
|
|
ISC_LANG_ENDDECLS
|
|
|
|
|
2000-04-29 02:02:38 +00:00
|
|
|
#endif /* DNS_GEN_WIN32_H */
|