Rizin
unix-like reverse engineering framework and cli tools
getopt.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "getopt.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
 
char * optarg
 

Macro Definition Documentation

◆ BADARG

#define BADARG   (int)':'

Definition at line 51 of file getopt.c.

◆ BADCH

#define BADCH   (int)'?'

Definition at line 50 of file getopt.c.

◆ EMSG

#define EMSG   ""

Definition at line 52 of file getopt.c.

Function Documentation

◆ getopt()

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

Definition at line 59 of file getopt.c.

59  {
60  static char *place = EMSG; /* option letter processing */
61  char *oli; /* option letter list index */
62 
63  if (optreset || !*place) { /* update scanning pointer */
64  optreset = 0;
65  if (optind >= nargc || *(place = nargv[optind]) != '-') {
66  place = EMSG;
67  return (EOF);
68  }
69  if (place[1] && *++place == '-') { /* found "--" */
70  ++optind;
71  place = EMSG;
72  return (EOF);
73  }
74  } /* option letter okay? */
75  if ((optopt = (int)*place++) == (int)':' || !(oli = (char *)strchr(ostr, optopt))) {
76  /*
77  * if the user didn't specify '-' as an option,
78  * assume it means EOF.
79  */
80  if (optopt == (int)'-')
81  return (EOF);
82  if (!*place)
83  ++optind;
84  if (opterr && *ostr != ':')
85  (void)fprintf(stderr, "illegal option -- %c\n", optopt);
86  return (BADCH);
87  }
88  if (*++oli != ':') { /* don't need argument */
89  optarg = NULL;
90  if (!*place)
91  ++optind;
92  }
93  else { /* need an argument */
94  if (*place) /* no white space */
95  optarg = place;
96  else if (nargc <= ++optind) { /* no arg */
97  place = EMSG;
98  if (*ostr == ':')
99  return (BADARG);
100  if (opterr)
101  (void)fprintf(stderr, "option requires an argument -- %c\n", optopt);
102  return (BADCH);
103  }
104  else /* white space */
105  optarg = nargv[optind];
106  place = EMSG;
107  ++optind;
108  }
109  return (optopt); /* dump back option letter */
110 }
#define NULL
Definition: cris-opc.c:27
int optopt
Definition: getopt.c:153
int optind
Definition: getopt.c:127
char * optarg
Definition: getopt.c:112
int opterr
Definition: getopt.c:147
#define BADCH
Definition: getopt.c:50
int optreset
Definition: getopt.c:47
#define BADARG
Definition: getopt.c:51
#define EMSG
Definition: getopt.c:52

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

Variable Documentation

◆ optarg

char* optarg

Definition at line 48 of file getopt.c.

◆ opterr

int opterr = 1

Definition at line 44 of file getopt.c.

◆ optind

int optind = 1

Definition at line 45 of file getopt.c.

◆ optopt

int optopt

Definition at line 46 of file getopt.c.

◆ optreset

int optreset

Definition at line 47 of file getopt.c.

Referenced by getopt().