This is Unofficial EPICS BASE Doxygen Site
epicsGetopt.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <epicsGetopt.h>
+ Include dependency graph for epicsGetopt.c:

Go to the source code of this file.

Macros

#define _getprogname()   nargv[0]
 
#define BADCH   (int)'?'
 
#define BADARG   (int)':'
 
#define EMSG   ""
 

Functions

int getopt (int nargc, char *const *nargv, const char *ostr)
 

Variables

int opterr = 1
 
int optind = 1
 
int optopt
 
int optreset
 
char * optarg
 

Macro Definition Documentation

#define _getprogname ( )    nargv[0]

Definition at line 46 of file epicsGetopt.c.

#define BADARG   (int)':'

Definition at line 58 of file epicsGetopt.c.

#define BADCH   (int)'?'

Definition at line 57 of file epicsGetopt.c.

#define EMSG   ""

Definition at line 59 of file epicsGetopt.c.

Function Documentation

int getopt ( int  nargc,
char * const *  nargv,
const char *  ostr 
)

Definition at line 65 of file epicsGetopt.c.

69 {
70  static char *place = EMSG; /* option letter processing */
71  char *oli; /* option letter list index */
72 
73  if (optreset || !*place) { /* update scanning pointer */
74  optreset = 0;
75  if (optind >= nargc || *(place = nargv[optind]) != '-') {
76  place = EMSG;
77  return (-1);
78  }
79  if (place[1] && *++place == '-') { /* found "--" */
80  ++optind;
81  place = EMSG;
82  return (-1);
83  }
84  } /* option letter okay? */
85  if ((optopt = (int)*place++) == (int)':' ||
86  !(oli = strchr(ostr, optopt))) {
87  /*
88  * if the user didn't specify '-' as an option,
89  * assume it means -1.
90  */
91  if (optopt == (int)'-')
92  return (-1);
93  if (!*place)
94  ++optind;
95  if (opterr && *ostr != ':' && optopt != BADCH)
96  (void)fprintf(stderr, "%s: illegal option -- %c\n",
97  _getprogname(), optopt);
98  return (BADCH);
99  }
100  if (*++oli != ':') { /* don't need argument */
101  optarg = NULL;
102  if (!*place)
103  ++optind;
104  }
105  else { /* need an argument */
106  if (*place) /* no white space */
107  optarg = place;
108  else if (nargc <= ++optind) { /* no arg */
109  place = EMSG;
110  if (*ostr == ':')
111  return (BADARG);
112  if (opterr)
113  (void)fprintf(stderr,
114  "%s: option requires an argument -- %c\n",
115  _getprogname(), optopt);
116  return (BADCH);
117  }
118  else /* white space */
119  optarg = nargv[optind];
120  place = EMSG;
121  ++optind;
122  }
123  return (optopt); /* dump back option letter */
124 }
int optind
Definition: epicsGetopt.c:50
#define EMSG
Definition: epicsGetopt.c:59
#define NULL
Definition: catime.c:38
int optopt
Definition: epicsGetopt.c:50
int opterr
Definition: epicsGetopt.c:49
#define _getprogname()
Definition: epicsGetopt.c:46
#define BADCH
Definition: epicsGetopt.c:57
int optreset
Definition: epicsGetopt.c:52
#define stderr
Definition: epicsStdio.h:32
#define BADARG
Definition: epicsGetopt.c:58
char * optarg
Definition: epicsGetopt.c:55

Variable Documentation

char* optarg

Definition at line 55 of file epicsGetopt.c.

int opterr = 1

Definition at line 49 of file epicsGetopt.c.

int optind = 1

Definition at line 50 of file epicsGetopt.c.

int optopt

Definition at line 50 of file epicsGetopt.c.

int optreset

Definition at line 52 of file epicsGetopt.c.