"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.16.7/lib/dns/gen-win32.h" (4 Sep 2020, 7651 Bytes) of package /linux/misc/dns/bind9/9.16.7/bind-9.16.7.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "gen-win32.h" see the Fossies "Dox" file reference documentation.

    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 http://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
   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 
  105         if (isc_commandline_index >= argc ||
  106             *(place = argv[isc_commandline_index]) != '-')
  107         {
  108             /*
  109              * Index out of range or points to non-option.
  110              */
  111             place = ENDOPT;
  112             return (-1);
  113         }
  114 
  115         if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
  116             /*
  117              * Found '--' to signal end of options.  Advance
  118              * index to next argv, the first non-option.
  119              */
  120             isc_commandline_index++;
  121             place = ENDOPT;
  122             return (-1);
  123         }
  124     }
  125 
  126     isc_commandline_option = *place++;
  127     option = strchr(options, isc_commandline_option);
  128 
  129     /*
  130      * Ensure valid option has been passed as specified by options string.
  131      * '-:' is never a valid command line option because it could not
  132      * distinguish ':' from the argument specifier in the options string.
  133      */
  134     if (isc_commandline_option == ':' || option == NULL) {
  135         if (*place == '\0') {
  136             isc_commandline_index++;
  137         }
  138 
  139         if (isc_commandline_errprint && *options != ':') {
  140             fprintf(stderr, "%s: illegal option -- %c\n",
  141                 isc_commandline_progname,
  142                 isc_commandline_option);
  143         }
  144 
  145         return (BADOPT);
  146     }
  147 
  148     if (*++option != ':') {
  149         /*
  150          * Option does not take an argument.
  151          */
  152         isc_commandline_argument = NULL;
  153 
  154         /*
  155          * Skip to next argv if at the end of the current argv.
  156          */
  157         if (*place == '\0') {
  158             ++isc_commandline_index;
  159         }
  160     } else {
  161         /*
  162          * Option needs an argument.
  163          */
  164         if (*place != '\0') {
  165             /*
  166              * Option is in this argv, -D1 style.
  167              */
  168             isc_commandline_argument = place;
  169         } else if (argc > ++isc_commandline_index) {
  170             /*
  171              * Option is next argv, -D 1 style.
  172              */
  173             isc_commandline_argument = argv[isc_commandline_index];
  174         } else {
  175             /*
  176              * Argument needed, but no more argv.
  177              */
  178             place = ENDOPT;
  179 
  180             /*
  181              * Silent failure with "missing argument" return
  182              * when ':' starts options string, per historical spec.
  183              */
  184             if (*options == ':') {
  185                 return (BADARG);
  186             }
  187 
  188             if (isc_commandline_errprint) {
  189                 fprintf(stderr,
  190                     "%s: option requires an argument -- "
  191                     "%c\n",
  192                     isc_commandline_progname,
  193                     isc_commandline_option);
  194             }
  195 
  196             return (BADOPT);
  197         }
  198 
  199         place = ENDOPT;
  200 
  201         /*
  202          * Point to argv that follows argument.
  203          */
  204         isc_commandline_index++;
  205     }
  206 
  207     return (isc_commandline_option);
  208 }
  209 
  210 typedef struct {
  211     HANDLE handle;
  212     WIN32_FIND_DATA find_data;
  213     bool first_file;
  214     char *filename;
  215 } isc_dir_t;
  216 
  217 bool
  218 start_directory(const char *path, isc_dir_t *dir) {
  219     char pattern[_MAX_PATH], *p;
  220 
  221     /*
  222      * Need space for slash-splat and final NUL.
  223      */
  224     if (strlen(path) + 3 > sizeof(pattern)) {
  225         return (false);
  226     }
  227 
  228     strcpy(pattern, path);
  229 
  230     /*
  231      * Append slash (if needed) and splat.
  232      */
  233     p = pattern + strlen(pattern);
  234     if (p != pattern && p[-1] != '\\' && p[-1] != ':') {
  235         *p++ = '\\';
  236     }
  237     *p++ = '*';
  238     *p++ = '\0';
  239 
  240     dir->first_file = true;
  241 
  242     dir->handle = FindFirstFile(pattern, &dir->find_data);
  243 
  244     if (dir->handle == INVALID_HANDLE_VALUE) {
  245         dir->filename = NULL;
  246         return (false);
  247     } else {
  248         dir->filename = dir->find_data.cFileName;
  249         return (true);
  250     }
  251 }
  252 
  253 bool
  254 next_file(isc_dir_t *dir) {
  255     if (dir->first_file) {
  256         dir->first_file = false;
  257     } else if (dir->handle != INVALID_HANDLE_VALUE) {
  258         if (FindNextFile(dir->handle, &dir->find_data) == TRUE) {
  259             dir->filename = dir->find_data.cFileName;
  260         } else {
  261             dir->filename = NULL;
  262         }
  263     } else {
  264         dir->filename = NULL;
  265     }
  266 
  267     if (dir->filename != NULL) {
  268         return (true);
  269     } else {
  270         return (false);
  271     }
  272 }
  273 
  274 void
  275 end_directory(isc_dir_t *dir) {
  276     if (dir->handle != INVALID_HANDLE_VALUE) {
  277         FindClose(dir->handle);
  278     }
  279 }
  280 
  281 inline struct tm *
  282 gmtime_r(const time_t *clock, struct tm *result) {
  283     errno_t ret = gmtime_s(result, clock);
  284     if (ret != 0) {
  285         errno = ret;
  286         return (NULL);
  287     }
  288     return (result);
  289 }
  290 
  291 ISC_LANG_ENDDECLS
  292 
  293 #endif /* DNS_GEN_WIN32_H */