--- pgp/src/getopt.c 2018/04/24 16:37:53 1.1.1.1 +++ pgp/src/getopt.c 2018/04/24 16:40:52 1.1.1.4 @@ -19,42 +19,42 @@ */ #include +#include +#include "getopt.h" /*LINTLIBRARY*/ #ifndef NULL #define NULL 0 #endif #define EOF (-1) -#define ERR(s, c) if(opterr){\ - extern int write();\ - char errbuf[2];\ - errbuf[0] = c; errbuf[1] = '\n';\ - (void) write(2, argv[0], (unsigned)strlen(argv[0]));\ - (void) write(2, s, (unsigned)strlen(s));\ - (void) write(2, errbuf, 2);} +#define ERR(str, chr) (opterr ? fprintf(stderr, "%s%s%c\n", argv[0], str, chr) : 0) int opterr = 1; int optind = 1; -int optopt; -char *optarg; +int optopt = 0; +char *optarg = 0; int -getopt(argc, argv, opts) -int argc; -char **argv, *opts; +pgp_getopt(int argc, char **argv, char *opts) { static int sp = 1; register int c; register char *cp; - if(sp == 1) - if(optind >= argc || - argv[optind][0] != '-' || argv[optind][1] == '\0') - return(EOF); + if(sp == 1) { + if(optind >= argc || (argv[optind][0] != '+' && + argv[optind][0] != '-') || argv[optind][1] == '\0') + return EOF; else if(strcmp(argv[optind], "--") == 0) { optind++; - return(EOF); + return EOF; } + /* '+' for config options, '+' should not be in the opts list */ + if (argv[optind][0] == '+') { + optarg = argv[optind++] + 1; + return '+'; + } + } optopt = c = argv[optind][sp]; if(c == ':' || (cp=strchr(opts, c)) == NULL) { ERR(": illegal option -- ", c); @@ -62,7 +62,7 @@ char **argv, *opts; optind++; sp = 1; } - return('\0'); + return '\0'; } if(*++cp == ':') { if(argv[optind][sp+1] != '\0') @@ -70,7 +70,7 @@ char **argv, *opts; else if(++optind >= argc) { ERR(": option requires an argument -- ", c); sp = 1; - return('\0'); + return '\0'; } else optarg = argv[optind++]; sp = 1; @@ -81,6 +81,6 @@ char **argv, *opts; } optarg = NULL; } - return(c); + return c; }