freeswitch/libs/srtp/test/getopt_s.c

110 lines
3.3 KiB
C
Raw Permalink Normal View History

2013-01-17 18:59:53 -05:00
/*
* getopt.c
*
* a minimal implementation of the getopt() function, written so that
* test applications that use that function can run on non-POSIX
* platforms
2013-01-17 18:59:53 -05:00
*
*/
/*
*
2017-06-26 13:11:02 -04:00
* Copyright (c) 2001-2017 Cisco Systems, Inc.
2013-01-17 18:59:53 -05:00
* All rights reserved.
*
2013-01-17 18:59:53 -05:00
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
2013-01-17 18:59:53 -05:00
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
2013-01-17 18:59:53 -05:00
* 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.
*
2013-01-17 18:59:53 -05:00
* Neither the name of the Cisco Systems, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
2013-01-17 18:59:53 -05:00
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
* COPYRIGHT HOLDERS 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.
*
*/
#include <stdlib.h> /* for NULL */
2013-01-17 18:59:53 -05:00
int optind_s = 0;
char *optarg_s;
#define GETOPT_FOUND_WITHOUT_ARGUMENT 2
#define GETOPT_FOUND_WITH_ARGUMENT 1
#define GETOPT_NOT_FOUND 0
2013-01-17 18:59:53 -05:00
static int getopt_check_character(char c, const char *string)
{
unsigned int max_string_len = 128;
2013-01-17 18:59:53 -05:00
while (*string != 0) {
if (max_string_len == 0) {
2021-08-28 07:19:05 -04:00
return GETOPT_NOT_FOUND;
}
2021-08-28 07:19:05 -04:00
max_string_len--;
if (*string++ == c) {
if (*string == ':') {
return GETOPT_FOUND_WITH_ARGUMENT;
} else {
return GETOPT_FOUND_WITHOUT_ARGUMENT;
}
}
2013-01-17 18:59:53 -05:00
}
return GETOPT_NOT_FOUND;
2013-01-17 18:59:53 -05:00
}
int getopt_s(int argc, char *const argv[], const char *optstring)
{
while (optind_s + 1 < argc) {
char *string;
2013-01-17 18:59:53 -05:00
/* move 'string' on to next argument */
optind_s++;
string = argv[optind_s];
2013-01-17 18:59:53 -05:00
if (string == NULL)
return '?'; /* NULL argument string */
2013-01-17 18:59:53 -05:00
if (string[0] != '-')
return -1; /* found an unexpected character */
2013-01-17 18:59:53 -05:00
switch (getopt_check_character(string[1], optstring)) {
case GETOPT_FOUND_WITH_ARGUMENT:
if (optind_s + 1 < argc) {
optind_s++;
optarg_s = argv[optind_s];
return string[1];
} else {
return '?'; /* argument missing */
}
case GETOPT_FOUND_WITHOUT_ARGUMENT:
return string[1];
case GETOPT_NOT_FOUND:
default:
return '?'; /* didn't find expected character */
break;
}
2013-01-17 18:59:53 -05:00
}
return -1;
2013-01-17 18:59:53 -05:00
}