1 /*********************************************************************/
2 /*                                                                   */
3 /*  This Program Written by Paul Edwards.                            */
4 /*  Released to the public domain                                    */
5 /*                                                                   */
6 /*********************************************************************/
7 /*********************************************************************/
8 /*                                                                   */
9 /*  getopts - scan the command line for switches.                    */
10 /*                                                                   */
11 /*  This program takes the following parameters:                     */
12 /*                                                                   */
13 /*  1) argc (which was given to main)                                */
14 /*  2) argv (which was given to main)                                */
15 /*  3) Array of options                                              */
16 /*                                                                   */
17 /*  Returns the number of the argument that is next to be processed  */
18 /*  that wasn't recognised as an option.                             */
19 /*  Example of use:                                                  */
20 /*                                                                   */
21 /*  #include <getopts.h>                                             */
22 /*  int baud = 2400;                                                 */
23 /*  char fon[13] = "telix.fon";                                      */
24 /*  opt_t opttable[] =                                               */
25 /*  {                                                                */
26 /*    { "b", OPTINT, &baud },                                        */
27 /*    { "f", OPTSTR, fon },                                          */
28 /*    { NULL, 0, NULL }                                              */
29 /*  }                                                                */
30 /*  optup = getopts(argc,argv,opttable);                             */
31 /*                                                                   */
32 /*  The OPTINT means that an integer is being supplied.  OPTSTR      */
33 /*  means a string (with no check for overflow).  Also there is      */
34 /*  OPTBOOL which means it is a switch that is being passed, and an  */
35 /*  OPTLONG to specify a long.  Also OPTFLOAT for float.             */
36 /*                                                                   */
37 /*  This program was inspired by a description of a getargs function */
38 /*  written by Dr Dobbs Small-C Handbook.  Naturally I didn't get    */
39 /*  to see the code, otherwise I wouldn't be writing this!           */
40 /*                                                                   */
41 /*  This program is dedicated to the public domain.  It would be     */
42 /*  nice but not necessary if you gave me credit for it.  I would    */
43 /*  like to thank the members of the International C Conference      */
44 /*  (in Fidonet) for the help they gave me in writing this.          */
45 /*                                                                   */
46 /*  Written 16-Feb-1990.                                             */
47 /*                                                                   */
48 /*********************************************************************/
49 
50 /*#define DOFLOAT*/
51 
52 #include <stdlib.h>
53 #include <string.h>
54 #include <errno.h>
55 #include <stdio.h>
56 
57 #include "getopts.h"
58 
getopts(int argc,char ** argv,opt_t opttable[])59 int getopts(int argc, char **argv, opt_t opttable[])
60 {
61   int i,j;
62   argv++;
63   argc--;
64   for (i=1;i<=argc;i++)
65   {
66     if ((*(*argv) != '-') && (*(*argv) != '/')) return (i);
67     for (j=0;opttable[j].sw != NULL;j++)
68     {
69       if (strncmp(*argv+1,opttable[j].sw,
70           strlen(opttable[j].sw)) == 0)
71       {
72         switch ((int)opttable[j].opttyp)
73         {
74           case OPTINT :
75             *((int *)opttable[j].var) =
76                 (int)strtol(*argv+1+strlen(opttable[j].sw),NULL,10);
77             if (errno == ERANGE) return (i);
78             break;
79           case OPTSTR :
80             strcpy((char *)opttable[j].var,
81                 *argv+1+strlen((char *)opttable[j].sw));
82             break;
83           case OPTBOOL :
84             *((int *)opttable[j].var) = 1;
85             break;
86           case OPTLONG :
87             *((long *)opttable[j].var) =
88                 strtol(*argv+1+strlen(opttable[j].sw),NULL,10);
89             if (errno == ERANGE) return (i);
90             break;
91 #ifdef DOFLOAT
92           case OPTFLOAT :
93             *((float *)opttable[j].var) =
94                 (float)strtod(*argv+1+strlen(opttable[j].sw),NULL);
95             break;
96 #endif
97         }
98         break;
99       }
100     }
101     argv++;
102   }
103   return (i);
104 }
105