diff options
author | Leo C | 2014-10-16 11:08:47 +0200 |
---|---|---|
committer | Leo C | 2014-10-16 11:08:47 +0200 |
commit | 6dc26e92c20eedcfcba9e0b75a015a5b160748c5 (patch) | |
tree | a2a7c5a22b3353b4d26b8eb1b3a8caef7f6411ac /avr/getopt-min.c | |
parent | 349c01b10d1f6e223f963c6cbdf6a94d0b618895 (diff) | |
parent | 8f23e84c6a08a384d25582f9cf79c4f5549bc852 (diff) | |
download | z180-stamp-6dc26e92c20eedcfcba9e0b75a015a5b160748c5.zip |
Merge branch 'master' into hostcomm_avr
Diffstat (limited to 'avr/getopt-min.c')
-rw-r--r-- | avr/getopt-min.c | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/avr/getopt-min.c b/avr/getopt-min.c new file mode 100644 index 0000000..571c14f --- /dev/null +++ b/avr/getopt-min.c @@ -0,0 +1,76 @@ +#include "common.h" +#include <avr/pgmspace.h> + + +/* + * Minimum getopt, original version was: + */ + +/* + getopt -- public domain version of standard System V routine + + Strictly enforces the System V Command Syntax Standard; + provided by D A Gwyn of BRL for generic ANSI C implementations +*/ +/* $Id: getopt.c,v 1.2 1992/12/07 11:12:52 nickc Exp $ */ + +#include <string.h> + +int optind = 1; /* next argv[] index */ +char *optarg; /* option parameter if any */ + + +int +getopt( /* returns letter, '?', EOF */ + int argc, /* argument count from main */ + char *const argv[], /* argument vector from main */ + const FLASH char *optstring ) /* allowed args, e.g. "ab:c" */ +{ + static int sp = 1; /* position within argument */ + int osp; /* saved `sp' for param test */ + int c; /* option letter */ + const FLASH char *cp; /* -> option in `optstring' */ + + optarg = NULL; + + if ( sp == 1 ) /* fresh argument */ + { + if ( optind >= argc /* no more arguments */ + || argv[optind][0] != '-' /* no more options */ + || argv[optind][1] == '\0' /* not option; stdin */ + ) + return -1; + } + + c = argv[optind][sp]; /* option letter */ + osp = sp++; /* get ready for next letter */ + + if ( argv[optind][sp] == '\0' ) /* end of argument */ + { + ++optind; /* get ready for next try */ + sp = 1; /* beginning of next argument */ + } + + if ( c == ':' /* optstring syntax conflict */ + || (cp = strchr_P( optstring, c )) == NULL /* not found */ + ) + return '?'; + + if ( cp[1] == ':' ) /* option takes parameter */ + { + if ( osp != 1 ) + return '?'; + + if ( sp != 1 ) /* reset by end of argument */ + return '?'; + + if ( optind >= argc ) + return '?'; + + optarg = argv[optind]; /* make parameter available */ + ++optind; /* skip over parameter */ + } + + return c; +} + |