eccodes/tools/wingetopt.c

86 lines
1.5 KiB
C
Raw Normal View History

2013-03-25 14:23:07 +00:00
/*
* Copyright 2005-2012 ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
*
* In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
* virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
*/
#ifdef _WIN32
/* Microsoft Windows Visual Studio support */
#include "string.h"
#include "wingetopt.h"
char *optarg; /* global argument pointer */
int optind = 0; /* global argv index */
int opterr = 0;
int getopt(int argc, char *argv[], const char *optstring)
{
static char *next = 0;
char c;
char *cp;
if (optind == 0)
next = 0;
optarg = 0;
if (next == 0 || *next == '\0')
{
if (optind == 0)
optind++;
if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
{
optarg = 0;
if (optind < argc)
optarg = argv[optind];
return -1;
}
if (strcmp(argv[optind], "--") == 0)
{
optind++;
optarg = NULL;
if (optind < argc)
optarg = argv[optind];
return -1;
}
next = argv[optind];
next++; /* skip past - */
optind++;
}
c = *next++;
cp = strrchr(optstring, c);
if (cp == 0 || c == ':')
return '?';
cp++;
if (*cp == ':')
{
if (*next != '\0')
{
optarg = next;
next = 0;
}
else if (optind < argc)
{
optarg = argv[optind];
optind++;
}
else
{
return '?';
}
}
return c;
}
#endif