source: git/src/getopt.c @ f5629a61

RELEASE/1.0RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernloglog-selectstereostereo-2025walls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey
Last change on this file since f5629a61 was d1b1380, checked in by Olly Betts <olly@…>, 28 years ago

Initial revision

git-svn-id: file:///home/survex-svn/survex/trunk@2 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 999 bytes
Line 
1#include <stdio.h>
2#include <string.h>
3
4#define ERR(_s, _c) { if (opterr) fprintf (stderr, "%s%c\n", argv[0], _s, _c);}
5
6int     opterr = 1;
7int     optind = 1;
8int     optopt;
9char    *optarg;
10
11int getopt(int argc, char **argv, char *opts)
12{
13        static int sp = 1;
14        int c;
15        char *cp;
16
17        if(sp == 1)
18                if(optind >= argc ||
19                   argv[optind][0] != '-' || argv[optind][1] == '\0')
20                        return(EOF);
21                else if(strcmp(argv[optind], "--") == NULL) {
22                        optind++;
23                        return(EOF);
24                }
25        optopt = c = argv[optind][sp];
26        if(c == ':' || (cp=strchr(opts, c)) == NULL) {
27                ERR(": illegal option -- ", c);
28                if(argv[optind][++sp] == '\0') {
29                        optind++;
30                        sp = 1;
31                }
32                return('?');
33        }
34        if(*++cp == ':') {
35                if(argv[optind][sp+1] != '\0')
36                        optarg = &argv[optind++][sp+1];
37                else if(++optind >= argc) {
38                        ERR(": option requires an argument -- ", c);
39                        sp = 1;
40                        return('?');
41                } else
42                        optarg = argv[optind++];
43                sp = 1;
44        } else {
45                if(argv[optind][++sp] == '\0') {
46                        sp = 1;
47                        optind++;
48                }
49                optarg = NULL;
50        }
51        return(c);
52}
Note: See TracBrowser for help on using the repository browser.