Home | History | Annotate | Download | only in test
      1 /*
      2  * getopt.c
      3  *
      4  * a minimal implementation of the getopt() function, written so that
      5  * test applications that use that function can run on non-POSIX
      6  * platforms
      7  *
      8  */
      9 /*
     10  *
     11  * Copyright (c) 2001-2006 Cisco Systems, Inc.
     12  * All rights reserved.
     13  *
     14  * Redistribution and use in source and binary forms, with or without
     15  * modification, are permitted provided that the following conditions
     16  * are met:
     17  *
     18  *   Redistributions of source code must retain the above copyright
     19  *   notice, this list of conditions and the following disclaimer.
     20  *
     21  *   Redistributions in binary form must reproduce the above
     22  *   copyright notice, this list of conditions and the following
     23  *   disclaimer in the documentation and/or other materials provided
     24  *   with the distribution.
     25  *
     26  *   Neither the name of the Cisco Systems, Inc. nor the names of its
     27  *   contributors may be used to endorse or promote products derived
     28  *   from this software without specific prior written permission.
     29  *
     30  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     31  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     32  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     33  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     34  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     35  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     36  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     37  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     40  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
     41  * OF THE POSSIBILITY OF SUCH DAMAGE.
     42  *
     43  */
     44 
     45 #include <stdlib.h>  /* for NULL */
     46 
     47 int optind_s = 0;
     48 
     49 char *optarg_s;
     50 
     51 #define GETOPT_FOUND_WITHOUT_ARGUMENT    2
     52 #define GETOPT_FOUND_WITH_ARGUMENT       1
     53 #define GETOPT_NOT_FOUND                 0
     54 
     55 static int
     56 getopt_check_character(char c, const char *string) {
     57   unsigned int max_string_len = 128;
     58 
     59   while (*string != 0) {
     60     if (max_string_len == 0) {
     61       return '?';
     62     }
     63     if (*string++ == c) {
     64       if (*string == ':') {
     65 	return GETOPT_FOUND_WITH_ARGUMENT;
     66       } else {
     67 	return GETOPT_FOUND_WITHOUT_ARGUMENT;
     68       }
     69     }
     70   }
     71   return GETOPT_NOT_FOUND;
     72 }
     73 
     74 int
     75 getopt_s(int argc,
     76        char * const argv[],
     77        const char *optstring) {
     78 
     79 
     80   while (optind_s + 1 < argc) {
     81     char *string;
     82 
     83     /* move 'string' on to next argument */
     84     optind_s++;
     85     string = argv[optind_s];
     86 
     87     if (string == NULL)
     88       return '?'; /* NULL argument string */
     89 
     90     if (string[0] != '-')
     91       return -1; /* found an unexpected character */
     92 
     93     switch(getopt_check_character(string[1], optstring)) {
     94     case GETOPT_FOUND_WITH_ARGUMENT:
     95       if (optind_s + 1 < argc) {
     96 	optind_s++;
     97 	optarg_s = argv[optind_s];
     98 	return string[1];
     99       } else {
    100 	return '?';  /* argument missing */
    101       }
    102     case GETOPT_FOUND_WITHOUT_ARGUMENT:
    103       return string[1];
    104     case GETOPT_NOT_FOUND:
    105     default:
    106       return '?'; /* didn't find expected character */
    107       break;
    108     }
    109   }
    110 
    111   return -1;
    112 }
    113