home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / progc / snip1091.arj / GETOPTS.C < prev    next >
C/C++ Source or Header  |  1991-07-19  |  5KB  |  114 lines

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