11cb0ef41Sopenharmony_ci/*
21cb0ef41Sopenharmony_ci * Original file name getopt.c  Initial import into the c-ares source tree
31cb0ef41Sopenharmony_ci * on 2007-04-11.  Lifted from version 5.2 of the 'Open Mash' project with
41cb0ef41Sopenharmony_ci * the modified BSD license, BSD license without the advertising clause.
51cb0ef41Sopenharmony_ci *
61cb0ef41Sopenharmony_ci */
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci/*
91cb0ef41Sopenharmony_ci * getopt.c --
101cb0ef41Sopenharmony_ci *
111cb0ef41Sopenharmony_ci *      Standard UNIX getopt function.  Code is from BSD.
121cb0ef41Sopenharmony_ci *
131cb0ef41Sopenharmony_ci * Copyright (c) 1987-2001 The Regents of the University of California.
141cb0ef41Sopenharmony_ci * All rights reserved.
151cb0ef41Sopenharmony_ci *
161cb0ef41Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
171cb0ef41Sopenharmony_ci * modification, are permitted provided that the following conditions are met:
181cb0ef41Sopenharmony_ci *
191cb0ef41Sopenharmony_ci * A. Redistributions of source code must retain the above copyright notice,
201cb0ef41Sopenharmony_ci *    this list of conditions and the following disclaimer.
211cb0ef41Sopenharmony_ci * B. Redistributions in binary form must reproduce the above copyright notice,
221cb0ef41Sopenharmony_ci *    this list of conditions and the following disclaimer in the documentation
231cb0ef41Sopenharmony_ci *    and/or other materials provided with the distribution.
241cb0ef41Sopenharmony_ci * C. Neither the names of the copyright holders nor the names of its
251cb0ef41Sopenharmony_ci *    contributors may be used to endorse or promote products derived from this
261cb0ef41Sopenharmony_ci *    software without specific prior written permission.
271cb0ef41Sopenharmony_ci *
281cb0ef41Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
291cb0ef41Sopenharmony_ci * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
301cb0ef41Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
311cb0ef41Sopenharmony_ci * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
321cb0ef41Sopenharmony_ci * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
331cb0ef41Sopenharmony_ci * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
341cb0ef41Sopenharmony_ci * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
351cb0ef41Sopenharmony_ci * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
361cb0ef41Sopenharmony_ci * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
371cb0ef41Sopenharmony_ci * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
381cb0ef41Sopenharmony_ci * POSSIBILITY OF SUCH DAMAGE.
391cb0ef41Sopenharmony_ci *
401cb0ef41Sopenharmony_ci * SPDX-License-Identifier: BSD-3-Clause
411cb0ef41Sopenharmony_ci */
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci#include <stdio.h>
441cb0ef41Sopenharmony_ci#include <stdlib.h>
451cb0ef41Sopenharmony_ci#include <string.h>
461cb0ef41Sopenharmony_ci#include "ares_getopt.h"
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci#define BADCH  (int)'?'
491cb0ef41Sopenharmony_ci#define BADARG (int)':'
501cb0ef41Sopenharmony_ci#define EMSG   (char *)""
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_civoid ares_getopt_init(ares_getopt_state_t *state, int nargc, const char **nargv)
531cb0ef41Sopenharmony_ci{
541cb0ef41Sopenharmony_ci  memset(state, 0, sizeof(*state));
551cb0ef41Sopenharmony_ci  state->opterr = 1;
561cb0ef41Sopenharmony_ci  state->optind = 1;
571cb0ef41Sopenharmony_ci  state->place  = EMSG;
581cb0ef41Sopenharmony_ci  state->argc   = nargc;
591cb0ef41Sopenharmony_ci  state->argv   = nargv;
601cb0ef41Sopenharmony_ci}
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci/*
631cb0ef41Sopenharmony_ci * ares_getopt --
641cb0ef41Sopenharmony_ci *    Parse argc/argv argument vector.
651cb0ef41Sopenharmony_ci */
661cb0ef41Sopenharmony_ciint ares_getopt(ares_getopt_state_t *state, const char *ostr)
671cb0ef41Sopenharmony_ci{
681cb0ef41Sopenharmony_ci  const char *oli; /* option letter list index */
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  /* update scanning pointer */
711cb0ef41Sopenharmony_ci  if (!*state->place) {
721cb0ef41Sopenharmony_ci    if (state->optind >= state->argc) {
731cb0ef41Sopenharmony_ci      return -1;
741cb0ef41Sopenharmony_ci    }
751cb0ef41Sopenharmony_ci    state->place = state->argv[state->optind];
761cb0ef41Sopenharmony_ci    if (*(state->place) != '-') {
771cb0ef41Sopenharmony_ci      return -1;
781cb0ef41Sopenharmony_ci    }
791cb0ef41Sopenharmony_ci    state->place++;
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci    /* found "--" */
821cb0ef41Sopenharmony_ci    if (*(state->place) == '-') {
831cb0ef41Sopenharmony_ci      state->optind++;
841cb0ef41Sopenharmony_ci      return -1;
851cb0ef41Sopenharmony_ci    }
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci    /* Found just - */
881cb0ef41Sopenharmony_ci    if (!*(state->place)) {
891cb0ef41Sopenharmony_ci      state->optopt = 0;
901cb0ef41Sopenharmony_ci      return BADCH;
911cb0ef41Sopenharmony_ci    }
921cb0ef41Sopenharmony_ci  }
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci  /* option letter okay? */
951cb0ef41Sopenharmony_ci  state->optopt = *(state->place);
961cb0ef41Sopenharmony_ci  state->place++;
971cb0ef41Sopenharmony_ci  oli = strchr(ostr, state->optopt);
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci  if (oli == NULL) {
1001cb0ef41Sopenharmony_ci    if (!(*state->place)) {
1011cb0ef41Sopenharmony_ci      ++state->optind;
1021cb0ef41Sopenharmony_ci    }
1031cb0ef41Sopenharmony_ci    if (state->opterr) {
1041cb0ef41Sopenharmony_ci      (void)fprintf(stderr, "%s: illegal option -- %c\n", __FILE__,
1051cb0ef41Sopenharmony_ci                    state->optopt);
1061cb0ef41Sopenharmony_ci    }
1071cb0ef41Sopenharmony_ci    return BADCH;
1081cb0ef41Sopenharmony_ci  }
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci  /* don't need argument */
1111cb0ef41Sopenharmony_ci  if (*++oli != ':') {
1121cb0ef41Sopenharmony_ci    state->optarg = NULL;
1131cb0ef41Sopenharmony_ci    if (!*state->place) {
1141cb0ef41Sopenharmony_ci      ++state->optind;
1151cb0ef41Sopenharmony_ci    }
1161cb0ef41Sopenharmony_ci  } else {
1171cb0ef41Sopenharmony_ci    /* need an argument */
1181cb0ef41Sopenharmony_ci    if (*state->place) {                         /* no white space */
1191cb0ef41Sopenharmony_ci      state->optarg = state->place;
1201cb0ef41Sopenharmony_ci    } else if (state->argc <= ++state->optind) { /* no arg */
1211cb0ef41Sopenharmony_ci      state->place = EMSG;
1221cb0ef41Sopenharmony_ci      if (*ostr == ':') {
1231cb0ef41Sopenharmony_ci        return BADARG;
1241cb0ef41Sopenharmony_ci      }
1251cb0ef41Sopenharmony_ci      if (state->opterr) {
1261cb0ef41Sopenharmony_ci        (void)fprintf(stderr, "%s: option requires an argument -- %c\n",
1271cb0ef41Sopenharmony_ci                      __FILE__, state->optopt);
1281cb0ef41Sopenharmony_ci      }
1291cb0ef41Sopenharmony_ci      return BADARG;
1301cb0ef41Sopenharmony_ci    } else { /* white space */
1311cb0ef41Sopenharmony_ci      state->optarg = state->argv[state->optind];
1321cb0ef41Sopenharmony_ci    }
1331cb0ef41Sopenharmony_ci    state->place = EMSG;
1341cb0ef41Sopenharmony_ci    ++state->optind;
1351cb0ef41Sopenharmony_ci  }
1361cb0ef41Sopenharmony_ci  return state->optopt; /* dump back option letter */
1371cb0ef41Sopenharmony_ci}
138