Rizin
unix-like reverse engineering framework and cli tools
getopt.h File Reference
#include <string.h>
#include <stdio.h>

Go to the source code of this file.

Macros

#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
 
const char * optarg
 

Macro Definition Documentation

◆ BADARG

#define BADARG   (int)':'

Definition at line 12 of file getopt.h.

◆ BADCH

#define BADCH   (int)'?'

Definition at line 11 of file getopt.h.

◆ EMSG

#define EMSG   ""

Definition at line 13 of file getopt.h.

Function Documentation

◆ getopt()

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

Definition at line 20 of file getopt.h.

21 {
22  static const char *place = EMSG; /* option letter processing */
23  const char *oli; /* option letter list index */
24 
25  if (optreset || !*place) { /* update scanning pointer */
26  optreset = 0;
27  if (optind >= nargc || *(place = nargv[optind]) != '-') {
28  place = EMSG;
29  return (-1);
30  }
31  if (place[1] && *++place == '-') { /* found "--" */
32  ++optind;
33  place = EMSG;
34  return (-1);
35  }
36  } /* option letter okay? */
37  if ((optopt = (int)*place++) == (int)':' ||
38  !(oli = strchr (ostr, optopt))) {
39  /*
40  * if the user didn't specify '-' as an option,
41  * assume it means -1.
42  */
43  if (optopt == (int)'-')
44  return (-1);
45  if (!*place)
46  ++optind;
47  if (opterr && *ostr != ':')
48  (void)printf ("illegal option -- %c\n", optopt);
49  return (BADCH);
50  }
51  if (*++oli != ':') { /* don't need argument */
52  optarg = NULL;
53  if (!*place)
54  ++optind;
55  }
56  else { /* need an argument */
57  if (*place) /* no white space */
58  optarg = place;
59  else if (nargc <= ++optind) { /* no arg */
60  place = EMSG;
61  if (*ostr == ':')
62  return (BADARG);
63  if (opterr)
64  (void)printf ("option requires an argument -- %c\n", optopt);
65  return (BADCH);
66  }
67  else /* white space */
68  optarg = nargv[optind];
69  place = EMSG;
70  ++optind;
71  }
72  return optopt; /* dump back option letter */
73 }
#define BADCH
Definition: getopt.h:11
int optopt
Definition: getopt.h:7
int optreset
Definition: getopt.h:8
#define BADARG
Definition: getopt.h:12
#define EMSG
Definition: getopt.h:13
const char * optarg
Definition: getopt.h:9
int optind
Definition: getopt.h:6
int opterr
Definition: getopt.h:5
#define NULL
Definition: cris-opc.c:27
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93

References BADARG, BADCH, EMSG, NULL, optarg, opterr, optind, optopt, optreset, and printf().

Referenced by main().

Variable Documentation

◆ optarg

const char* optarg

Definition at line 9 of file getopt.h.

Referenced by getopt(), main(), and parse_real().

◆ opterr

int opterr = 1

Definition at line 5 of file getopt.h.

Referenced by getopt().

◆ optind

int optind = 1

Definition at line 6 of file getopt.h.

Referenced by args_parse(), getopt(), main(), and parse_environment().

◆ optopt

int optopt

Definition at line 7 of file getopt.h.

Referenced by getopt().

◆ optreset

int optreset

Definition at line 8 of file getopt.h.

Referenced by getopt(), and parse_environment().