Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 52 lines (46 sloc) 1.341 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/* Public domain, from ezmlm-0.53. */
/* sgetopt.c, sgetopt.h: (yet another) improved getopt clone, outer layer
D. J. Bernstein, djb@pobox.com.
Depends on subgetopt.h, substdio.h, subfd.h.
No system requirements.
19970208: Cleanups.
931201: Baseline.
No known patent problems.

Documentation in sgetopt.3.
*/

#include "substdio.h"
#include "subfd.h"
#define SGETOPTNOSHORT
#include "sgetopt.h"
#define SUBGETOPTNOSHORT
#include "subgetopt.h"

#define getopt sgetoptmine
#define optind subgetoptind
#define opterr sgetopterr
#define optproblem subgetoptproblem
#define optprogname sgetoptprogname

int opterr = 1;
const char *optprogname = 0;

int getopt(int argc,char *const *argv,const char *opts)
{
  int c;
  const char *s;

  if (!optprogname) {
    optprogname = *argv;
    if (!optprogname) optprogname = "";
    for (s = optprogname;*s;++s) if (*s == '/') optprogname = s + 1;
  }
  c = subgetopt(argc,(const char *const*)argv,opts);
  if (opterr)
    if (c == '?') {
      char chp[2]; chp[0] = optproblem; chp[1] = '\n';
      substdio_puts(subfderr,optprogname);
      if (argv[optind] && (optind < argc))
        substdio_puts(subfderr,": illegal option -- ");
      else
        substdio_puts(subfderr,": option requires an argument -- ");
      substdio_put(subfderr,chp,2);
      substdio_flush(subfderr);
    }
  return c;
}
Something went wrong with that request. Please try again.