home *** CD-ROM | disk | FTP | other *** search
/ ftp.ncftp.com / ftp.ncftp.com.zip / ftp.ncftp.com / ncftp / older_versions / ncftp-3.2.2-src.tar.bz2 / ncftp-3.2.2-src.tar / ncftp-3.2.2 / libncftp / u_getopt.c < prev    next >
C/C++ Source or Header  |  2005-01-01  |  2KB  |  81 lines

  1. /* u_getopt.c
  2.  *
  3.  * Copyright (c) 1996-2005 Mike Gleason, NcFTP Software.
  4.  * All rights reserved.
  5.  * 
  6.  */
  7.  
  8. #include "syshdrs.h"
  9. #ifdef PRAGMA_HDRSTOP
  10. #    pragma hdrstop
  11. #endif
  12.  
  13. /* This must be called before each Getopt. */
  14. void
  15. GetoptReset(GetoptInfo *const opt)
  16. {
  17.     memset(opt, 0, sizeof(GetoptInfo));
  18.     opt->ind = 1;
  19.     opt->init = 0xF123456F;
  20. }    /* GetoptReset */
  21.  
  22.  
  23.  
  24. int
  25. Getopt(GetoptInfo *const opt, int nargc, char **const nargv, const char *const ostr)
  26. {
  27.     const char *oli;                   /* Option letter list index */
  28.     if ((opt == NULL) || (nargc == 0) || (nargv == (char **) 0) || (ostr == NULL))
  29.         return (EOF);
  30.  
  31.     if (opt->init != 0xF123456F) {
  32.         /* We can do it for them. */
  33.         GetoptReset(opt);
  34.     }
  35.  
  36.     if ((opt->place == NULL) || (opt->place[0] == '\0')) {
  37.         /* update scanning pointer */
  38.         if (opt->ind >= nargc || *(opt->place = nargv[opt->ind]) != '-')
  39.             return (EOF);
  40.         if (opt->place[1] && *++opt->place == '-') {    /* found "--" */
  41.             ++opt->ind;
  42.             return (EOF);
  43.         }
  44.     }                                   /* Option letter okay? */
  45.  
  46.     if (opt->place == NULL)
  47.         oli = NULL;
  48.     else if ((opt->opt = (int) *opt->place++) == (int) ':')
  49.         oli = NULL;
  50.     else
  51.         oli = strchr(ostr, opt->opt);
  52.  
  53.     if (oli == NULL) {
  54.         if ((opt->place == NULL) || (opt->place[0] == '\0'))
  55.             ++opt->ind;
  56.         if (opt->err)
  57.             (void) fprintf(stderr, "%s%s%c\n", *nargv, ": illegal option -- ", opt->opt);
  58.         return((int) '?');
  59.     }
  60.     if (*++oli != ':') {               /* don't need argument */
  61.         opt->arg = NULL;
  62.         if ((opt->place == NULL) || (opt->place[0] == '\0'))
  63.             ++opt->ind;
  64.     } else {                           /* need an argument */
  65.         if ((opt->place != NULL) && (opt->place[0] != '\0'))
  66.             opt->arg = (char *) opt->place;
  67.         else if (nargc <= ++opt->ind) {  /* no arg */
  68.             opt->place = NULL;
  69.             if (opt->err) 
  70.                 (void) fprintf(stderr, "%s%s%c\n", *nargv, ": option requires an argument -- ", opt->opt);
  71.             return((int) '?');
  72.         } else                           /* white space */
  73.             opt->arg = (char *) nargv[opt->ind];
  74.         opt->place = NULL;
  75.         ++opt->ind;
  76.     }
  77.     return (opt->opt);                   /* dump back Option letter */
  78. }                                       /* Getopt */
  79.  
  80. /* eof */
  81.