eccodes/tools/wingetopt.c

86 lines
1.8 KiB
C
Raw Normal View History

2013-03-25 14:23:07 +00:00
/*
2015-01-05 15:45:46 +00:00
* Copyright 2005-2015 ECMWF.
2013-03-25 14:23:07 +00:00
*
* 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.
*/
2013-04-09 10:46:01 +00:00
#include "wingetopt.h"
#ifdef GRIB_ON_WINDOWS
2013-03-25 14:23:07 +00:00
#include "string.h"
2014-01-10 14:07:02 +00:00
char *optarg; /* global argument pointer */
int optind = 0; /* global argv index */
2013-03-25 14:23:07 +00:00
int opterr = 0;
int getopt(int argc, char *argv[], const char *optstring)
{
2014-01-10 14:07:02 +00:00
static char *next = 0;
char c;
char *cp;
if (optind == 0)
next = 0;
2013-03-25 14:23:07 +00:00
2014-01-10 14:07:02 +00:00
optarg = 0;
2013-03-25 14:23:07 +00:00
2014-01-10 14:07:02 +00:00
if (next == 0 || *next == '\0')
{
if (optind == 0)
optind++;
2013-03-25 14:23:07 +00:00
2014-01-10 14:07:02 +00:00
if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
{
optarg = 0;
if (optind < argc)
optarg = argv[optind];
return -1;
}
2013-03-25 14:23:07 +00:00
2014-01-10 14:07:02 +00:00
if (strcmp(argv[optind], "--") == 0)
{
optind++;
optarg = NULL;
if (optind < argc)
optarg = argv[optind];
return -1;
}
2013-03-25 14:23:07 +00:00
2014-01-10 14:07:02 +00:00
next = argv[optind];
next++; /* skip past - */
optind++;
}
2013-03-25 14:23:07 +00:00
2014-01-10 14:07:02 +00:00
c = *next++;
cp = strrchr(optstring, c);
2013-03-25 14:23:07 +00:00
2014-01-10 14:07:02 +00:00
if (cp == 0 || c == ':')
return '?';
2013-03-25 14:23:07 +00:00
2014-01-10 14:07:02 +00:00
cp++;
if (*cp == ':')
{
if (*next != '\0')
{
optarg = next;
next = 0;
}
else if (optind < argc)
{
optarg = argv[optind];
optind++;
}
else
{
return '?';
}
}
2013-03-25 14:23:07 +00:00
2014-01-10 14:07:02 +00:00
return c;
2013-03-25 14:23:07 +00:00
}
#endif