home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / man / cat3 / getopt.0 < prev    next >
Text File  |  1993-12-07  |  5KB  |  133 lines

  1.  
  2. GETOPT(3)                  UNIX Programmer's Manual                  GETOPT(3)
  3.  
  4. NNAAMMEE
  5.      ggeettoopptt - get option letter from argv
  6.  
  7. SSYYNNOOPPSSIISS
  8.      ##iinncclluuddee <<ssttddlliibb..hh>>
  9.  
  10.      _e_x_t_e_r_n _c_h_a_r _*_o_p_t_a_r_g
  11.      _e_x_t_e_r_n _i_n_t _o_p_t_i_n_d
  12.      _e_x_t_e_r_n _i_n_t _o_p_t_e_r_r
  13.  
  14.      _i_n_t
  15.      ggeettoopptt(_i_n_t _a_r_g_c, _c_h_a_r _* _c_o_n_s_t _*_a_r_g_v, _c_o_n_s_t _c_h_a_r _*_o_p_t_s_t_r_i_n_g)
  16.  
  17. DDEESSCCRRIIPPTTIIOONN
  18.      The ggeettoopptt() function gets the next _k_n_o_w_n option character from _a_r_g_v. An
  19.      option character is _k_n_o_w_n if it has been specified in the string of ac­
  20.      cepted option characters, _o_p_t_s_t_r_i_n_g.
  21.  
  22.      The option string _o_p_t_s_t_r_i_n_g may contain the following characters; letters
  23.      and letters followed by a colon to indicate an option argument is to fol­
  24.      low. It does not matter to ggeettoopptt() if a following argument has leading
  25.      white space.
  26.  
  27.      On return from ggeettoopptt(), _o_p_t_a_r_g points to an option argument, if it is
  28.      anticipated, and the variable _o_p_t_i_n_d contains the index to the next _a_r_g_v
  29.      argument for a subsequent call to ggeettoopptt().
  30.  
  31.      The variable _o_p_t_e_r_r and _o_p_t_i_n_d are both initialized to 1.  In order to
  32.      use ggeettoopptt() to evaluate multiple sets of arguments, or to evaluate a
  33.      single set of arguments multiple times, _o_p_t_i_n_d must be initialized to the
  34.      number of argv entries to be skipped in each evaluation.
  35.  
  36.      The ggeettoopptt() function returns an EOF when the argument list is exhausted,
  37.      or a non­recognized option is encountered.  The interpretation of options
  38.      in the argument list may be cancelled by the option `­­' (double dash)
  39.      which causes ggeettoopptt() to signal the end of argument processing and return
  40.      an EOF. When all options have been processed (i.e., up to the first non­
  41.      option argument), ggeettoopptt() returns EOF.
  42.  
  43. DDIIAAGGNNOOSSTTIICCSS
  44.      If the ggeettoopptt() function encounters a character not found in the string
  45.      _o_p_t_a_r_g or detects a missing option argument it writes error message `?'
  46.      to the _s_t_d_e_r_r. Setting _o_p_t_e_r_r to a zero will disable these error mes­
  47.      sages.
  48.  
  49. EEXXAAMMPPLLEE
  50.      extern char *optarg;
  51.      extern int optind;
  52.      int bflag, ch, fd;
  53.  
  54.      bflag = 0;
  55.      while ((ch = getopt(argc, argv, "bf:")) != EOF)
  56.              switch(ch) {
  57.              case 'b':
  58.                      bflag = 1;
  59.                      break;
  60.              case 'f':
  61.                      if ((fd = open(optarg, O_RDONLY, 0)) < 0) {
  62.                              (void)fprintf(stderr,
  63.                                      "myname: unable to read file %s.\n", optarg);
  64.                              exit(1) ;
  65.                      }
  66.                      break;
  67.              case '?':
  68.              default:
  69.                      usage();
  70.      }
  71.      argc ­= optind;
  72.      argv += optind;
  73.  
  74. HHIISSTTOORRYY
  75.      The ggeettoopptt() function appeared 4.3BSD.
  76.  
  77. BBUUGGSS
  78.      Option arguments are allowed to begin with ``-''; this is reasonable but
  79.      reduces the amount of error checking possible.
  80.  
  81.      A single dash ``­'' may be specified as an character in _o_p_t_s_t_r_i_n_g, howev­
  82.      er it should _n_e_v_e_r have an argument associated with it.  This allows
  83.      ggeettoopptt() to be used with programs that expect ``­'' as an option flag.
  84.      This practice is wrong, and should not be used in any current develop­
  85.      ment.  It is provided for backward compatibility _o_n_l_y. By default, a sin­
  86.      gle dash causes ggeettoopptt() to return EOF. This is, we believe, compatible
  87.      with System V.
  88.  
  89.      It is also possible to handle digits as option letters.  This allows
  90.      ggeettoopptt() to be used with programs that expect a number (``­3'') as an op­
  91.      tion.  This practice is wrong, and should not be used in any current de­
  92.      velopment.  It is provided for backward compatibility _o_n_l_y. The following
  93.      code fragment works fairly well.
  94.  
  95.            int length;
  96.            char *p;
  97.  
  98.            while ((c = getopt(argc, argv, "0123456789")) != EOF)
  99.                    switch (c) {
  100.                    case '0': case '1': case '2': case '3': case '4':
  101.                    case '5': case '6': case '7': case '8': case '9':
  102.                            p = argv[optind ­ 1];
  103.                            if (p[0] == '­' && p[1] == ch && !p[2])
  104.                                    length = atoi(++p);
  105.                            else
  106.                                    length = atoi(argv[optind] + 1);
  107.                            break;
  108.                    }
  109.            }
  110.  
  111. 4.3 Berkeley Distribution       April 19, 1991                               2
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.