1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7 *
8 * See the COPYRIGHT file distributed with this work for additional
9 * information regarding copyright ownership.
10 */
11
12 /*
13 * Copyright (c) 1987, 1993, 1994
14 * The Regents of the University of California. All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 */
40
41 /*
42 * \note This file was adapted from the NetBSD project's source tree, RCS ID:
43 * NetBSD: getopt.c,v 1.15 1999/09/20 04:39:37 lukem Exp
44 *
45 * The primary change has been to rename items to the ISC namespace
46 * and format in the ISC coding style.
47 *
48 * This file is responsible for defining two operations that are not
49 * directly portable between Unix-like systems and Windows NT, option
50 * parsing and directory scanning. It is here because it was decided
51 * that the "gen" build utility was not to depend on libisc.a, so
52 * the functions declared in isc/commandline.h and isc/dir.h could not
53 * be used.
54 *
55 * The commandline stuff is pretty much a straight copy from the initial
56 * isc/commandline.c. The dir stuff was shrunk to fit the needs of gen.c.
57 */
58
59 #ifndef DNS_GEN_WIN32_H
60 #define DNS_GEN_WIN32_H 1
61
62 #include <stdbool.h>
63 #include <stdio.h>
64 #include <string.h>
65 #include <time.h>
66 #include <windows.h>
67
68 #include <isc/lang.h>
69
70 int isc_commandline_index = 1; /* Index into parent argv vector. */
71 int isc_commandline_option; /* Character checked for validity. */
72
73 char *isc_commandline_argument; /* Argument associated with option. */
74 char *isc_commandline_progname; /* For printing error messages. */
75
76 bool isc_commandline_errprint = true; /* Print error messages. */
77 bool isc_commandline_reset = true; /* Reset processing. */
78
79 #define BADOPT '?'
80 #define BADARG ':'
81 #define ENDOPT ""
82
83 ISC_LANG_BEGINDECLS
84
85 /*
86 * getopt --
87 * Parse argc/argv argument vector.
88 */
89 int
isc_commandline_parse(int argc,char * const * argv,const char * options)90 isc_commandline_parse(int argc, char * const *argv, const char *options) {
91 static char *place = ENDOPT;
92 char *option; /* Index into *options of option. */
93
94 /*
95 * Update scanning pointer, either because a reset was requested or
96 * the previous argv was finished.
97 */
98 if (isc_commandline_reset || *place == '\0') {
99 isc_commandline_reset = false;
100
101 if (isc_commandline_progname == NULL)
102 isc_commandline_progname = argv[0];
103
104 if (isc_commandline_index >= argc ||
105 *(place = argv[isc_commandline_index]) != '-') {
106 /*
107 * Index out of range or points to non-option.
108 */
109 place = ENDOPT;
110 return (-1);
111 }
112
113 if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
114 /*
115 * Found '--' to signal end of options. Advance
116 * index to next argv, the first non-option.
117 */
118 isc_commandline_index++;
119 place = ENDOPT;
120 return (-1);
121 }
122 }
123
124 isc_commandline_option = *place++;
125 option = strchr(options, isc_commandline_option);
126
127 /*
128 * Ensure valid option has been passed as specified by options string.
129 * '-:' is never a valid command line option because it could not
130 * distinguish ':' from the argument specifier in the options string.
131 */
132 if (isc_commandline_option == ':' || option == NULL) {
133 if (*place == '\0')
134 isc_commandline_index++;
135
136 if (isc_commandline_errprint && *options != ':')
137 fprintf(stderr, "%s: illegal option -- %c\n",
138 isc_commandline_progname,
139 isc_commandline_option);
140
141 return (BADOPT);
142 }
143
144 if (*++option != ':') {
145 /*
146 * Option does not take an argument.
147 */
148 isc_commandline_argument = NULL;
149
150 /*
151 * Skip to next argv if at the end of the current argv.
152 */
153 if (*place == '\0')
154 ++isc_commandline_index;
155
156 } else {
157 /*
158 * Option needs an argument.
159 */
160 if (*place != '\0')
161 /*
162 * Option is in this argv, -D1 style.
163 */
164 isc_commandline_argument = place;
165
166 else if (argc > ++isc_commandline_index)
167 /*
168 * Option is next argv, -D 1 style.
169 */
170 isc_commandline_argument = argv[isc_commandline_index];
171
172 else {
173 /*
174 * Argument needed, but no more argv.
175 */
176 place = ENDOPT;
177
178 /*
179 * Silent failure with "missing argument" return
180 * when ':' starts options string, per historical spec.
181 */
182 if (*options == ':')
183 return (BADARG);
184
185 if (isc_commandline_errprint)
186 fprintf(stderr,
187 "%s: option requires an argument -- %c\n",
188 isc_commandline_progname,
189 isc_commandline_option);
190
191 return (BADOPT);
192 }
193
194 place = ENDOPT;
195
196 /*
197 * Point to argv that follows argument.
198 */
199 isc_commandline_index++;
200 }
201
202 return (isc_commandline_option);
203 }
204
205 typedef struct {
206 HANDLE handle;
207 WIN32_FIND_DATA find_data;
208 bool first_file;
209 char *filename;
210 } isc_dir_t;
211
212 bool
start_directory(const char * path,isc_dir_t * dir)213 start_directory(const char *path, isc_dir_t *dir) {
214 char pattern[_MAX_PATH], *p;
215
216 /*
217 * Need space for slash-splat and final NUL.
218 */
219 if (strlen(path) + 3 > sizeof(pattern))
220 return (false);
221
222 strcpy(pattern, path);
223
224 /*
225 * Append slash (if needed) and splat.
226 */
227 p = pattern + strlen(pattern);
228 if (p != pattern && p[-1] != '\\' && p[-1] != ':')
229 *p++ = '\\';
230 *p++ = '*';
231 *p++ = '\0';
232
233 dir->first_file = true;
234
235 dir->handle = FindFirstFile(pattern, &dir->find_data);
236
237 if (dir->handle == INVALID_HANDLE_VALUE) {
238 dir->filename = NULL;
239 return (false);
240 } else {
241 dir->filename = dir->find_data.cFileName;
242 return (true);
243 }
244 }
245
246 bool
next_file(isc_dir_t * dir)247 next_file(isc_dir_t *dir) {
248 if (dir->first_file)
249 dir->first_file = false;
250
251 else if (dir->handle != INVALID_HANDLE_VALUE) {
252 if (FindNextFile(dir->handle, &dir->find_data) == TRUE)
253 dir->filename = dir->find_data.cFileName;
254 else
255 dir->filename = NULL;
256
257 } else
258 dir->filename = NULL;
259
260 if (dir->filename != NULL)
261 return (true);
262 else
263 return (false);
264 }
265
266 void
end_directory(isc_dir_t * dir)267 end_directory(isc_dir_t *dir) {
268 if (dir->handle != INVALID_HANDLE_VALUE)
269 FindClose(dir->handle);
270 }
271
272 inline struct tm *
gmtime_r(const time_t * clock,struct tm * result)273 gmtime_r(const time_t *clock, struct tm *result) {
274 errno_t ret = gmtime_s(result, clock);
275 if (ret != 0) {
276 errno = ret;
277 return (NULL);
278 }
279 return (result);
280 }
281
282 ISC_LANG_ENDDECLS
283
284 #endif /* DNS_GEN_WIN32_H */
285