2 Copyright (C) Slava Astashonok <sla@0n.ru>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License.
7 $Id: my_getopt.c,v 1.2.2.3 2004/02/02 08:06:24 sla Exp $
18 #include <my_getopt.h>
21 extern int optind, opterr, optopt;
23 int my_getopt(int argc, char * const argv[],
24 struct getopt_parms parms[])
27 static int flag = 0, my_opterr = 1;
28 static char optstring[MY_GETOPT_MAX_OPTSTR];
32 while ((parms[i].name != 0) && (p < (MY_GETOPT_MAX_OPTSTR - 1))) {
33 optstring[p++] = parms[i].name;
34 if (parms[i].flag & MY_GETOPT_ARG_REQUIRED) optstring[p++] = ':';
41 c = getopt(argc, argv, optstring);
44 if (my_opterr) fprintf(stderr, "Wrong parameters\n");
50 while (parms[i].name != 0) {
51 if ((parms[i].flag & MY_GETOPT_REQUIRED) && (parms[i].count == 0)) {
52 if (my_opterr) fprintf(stderr, "Missing required option\n");
61 while ((parms[i].name != 0) && (parms[i].name != c)) {
64 if (parms[i].flag & MY_GETOPT_ARG_REQUIRED) {
66 if (my_opterr) fprintf(stderr, "Option `-%c': %s\n",
67 c, "require parameter");
69 } else parms[i].arg = optarg;
71 if ((++parms[i].count > 1) && !(parms[i].flag & MY_GETOPT_ALLOW_REPEAT)) {
72 if (my_opterr) fprintf(stderr, "Option `-%c': %s\n",
73 c, "repeat not allowed");