image-threads/getopt.c
2023-01-23 11:35:33 +02:00

32 lines
604 B
C

#include "getopt.h"
#include "string.h"
char* optarg = NULL;
int optind = 1;
int getopt(int argc, char *const argv[], const char *optstring)
{
if ((optind >= argc) || (argv[optind][0] != '-') || (argv[optind][0] == 0))
{
return -1;
}
int opt = argv[optind][1];
const char *p = strchr(optstring, opt);
if (p == NULL)
{
return '?';
}
if (p[1] == ':')
{
optind++;
if (optind >= argc)
{
return '?';
}
optarg = argv[optind];
optind++;
}
return opt;
}