OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [uC-libc/] [misc/] [getopt.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
 
2
/*
3
 * From: gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) Newsgroups: net.sources
4
 * Subject: getopt library routine Date: 30 Mar 85 04:45:33 GMT
5
 */
6
/*
7
 * getopt -- public domain version of standard System V routine
8
 *
9
 * Strictly enforces the System V Command Syntax Standard; provided by D A
10
 * Gwyn of BRL for generic ANSI C implementations
11
 *
12
 * #define STRICT to prevent acceptance of clustered options with arguments
13
 * and ommision of whitespace between option and arg.
14
 */
15
 
16
#include <stdio.h>
17
#include <string.h>
18
 
19
int   opterr = 1;               /* error => print message */
20
int   optind = 1;               /* next argv[] index */
21
char *optarg = NULL;            /* option parameter if any */
22
 
23
static int
24
Err(name, mess, c)              /* returns '?' */
25
char *name;                     /* program name argv[0] */
26
char *mess;                     /* specific message */
27
int   c;                        /* defective option letter */
28
{
29
   if (opterr)
30
   {
31
      (void) fprintf(stderr,
32
                     "%s: %s -- %c\n",
33
                     name, mess, c
34
          );
35
   }
36
 
37
   return '?';                  /* erroneous-option marker */
38
}
39
 
40
int
41
getopt(argc, argv, optstring)   /* returns letter, '?', EOF */
42
int   argc;                     /* argument count from main */
43
char *argv[];                   /* argument vector from main */
44
char *optstring;                /* allowed args, e.g. "ab:c" */
45
{
46
   static int sp = 1;           /* position within argument */
47
   register int osp;            /* saved `sp' for param test */
48
#ifndef STRICT
49
   register int oind;           /* saved `optind' for param test */
50
#endif
51
   register int c;              /* option letter */
52
   register char *cp;           /* -> option in `optstring' */
53
 
54
   optarg = NULL;
55
 
56
   if (sp == 1)                 /* fresh argument */
57
      if (optind >= argc        /* no more arguments */
58
          || argv[optind][0] != '-'      /* no more options */
59
          || argv[optind][1] == '\0'    /* not option; stdin */
60
          )
61
         return EOF;
62
      else if (strcmp(argv[optind], "--") == 0)
63
      {
64
         ++optind;              /* skip over "--" */
65
         return EOF;            /* "--" marks end of options */
66
      }
67
 
68
   c = argv[optind][sp];        /* option letter */
69
   osp = sp++;                  /* get ready for next letter */
70
 
71
#ifndef STRICT
72
   oind = optind;               /* save optind for param test */
73
#endif
74
   if (argv[optind][sp] == '\0')/* end of argument */
75
   {
76
      ++optind;                 /* get ready for next try */
77
      sp = 1;                   /* beginning of next argument */
78
   }
79
 
80
   if (c == ':' || c == '?'     /* optstring syntax conflict */
81
       || (cp = strchr(optstring, c)) == NULL   /* not found */
82
       )
83
      return Err(argv[0], "illegal option", c);
84
 
85
   if (cp[1] == ':')            /* option takes parameter */
86
   {
87
#ifdef STRICT
88
      if (osp != 1)
89
         return Err(argv[0],
90
                    "option must not be clustered",
91
                    c
92
             );
93
 
94
      if (sp != 1)              /* reset by end of argument */
95
         return Err(argv[0],
96
                    "option must be followed by white space",
97
                    c
98
             );
99
 
100
#else
101
      if (oind == optind)       /* argument w/o whitespace */
102
      {
103
         optarg = &argv[optind][sp];
104
         sp = 1;                /* beginning of next argument */
105
      }
106
 
107
      else
108
#endif
109
      if (optind >= argc)
110
         return Err(argv[0],
111
                    "option requires an argument",
112
                    c
113
             );
114
 
115
      else                      /* argument w/ whitespace */
116
         optarg = argv[optind];
117
 
118
      ++optind;                 /* skip over parameter */
119
   }
120
 
121
   return c;
122
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.