CBMC
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
getopt.c
Go to the documentation of this file.
1/* FUNCTION: _getopt */
2
3#ifndef __CPROVER_STRING_H_INCLUDED
4#include <string.h>
5#define __CPROVER_STRING_H_INCLUDED
6#endif
7
8char *optarg = NULL;
9int optind = 1;
10
13
14int _getopt(int argc, char *const argv[], const char *optstring)
15{
17 // re-init requested by environment, we just reset optind
18 if(optind == 0)
19 optind = 1;
20
21 // test whether all arguments have been processed
22 if(optind >= argc)
23 return -1;
24 char *const arg = argv[optind];
25 if(arg[0] != '-' || arg[1] == '\0' || (arg[1] == '-' && arg[2] == '\0'))
26 return -1;
27
28 // test whether the option is in optstring at all
29 size_t optstring_len = strlen(optstring);
30 // gcc doesn't know __CPROVER_forall
31#ifndef LIBRARY_CHECK
33 {
34 size_t i;
35 i<optstring_len ==> optstring[i] != arg[1]
36 };
37 if(not_found)
38 return '?';
39#endif
40
41 // option is in optstring, find the exact index
44 result_index < optstring_len && optstring[result_index] == arg[1]);
45#ifdef __CPROVER_STRING_ABSTRACTION
47 __CPROVER_is_zero_string(optstring),
48 "getopt zero-termination of 3rd argument");
49#endif
50
51 // is an argument required?
52 if(result_index + 1 == optstring_len || optstring[result_index + 1] != ':')
53 {
54 optarg = NULL;
55 return arg[1];
56 }
57
58 // test whether a required argument can be found
59 if(arg[2] != '\0')
60 {
61 optarg = &arg[2];
62 return arg[1];
63 }
64 else if(optind + 1 < argc)
65 {
66 optarg = argv[optind + 1];
67 ++optind;
68 return arg[1];
69 }
70 else
71 {
72 optarg = NULL;
73 return optstring[0] == ':' ? ':' : '?';
74 }
75}
76
77/* FUNCTION: getopt */
78
79int _getopt(int argc, char *const argv[], const char *optstring);
80
81int getopt(int argc, char *const argv[], const char *optstring)
82{
84 return _getopt(argc, argv, optstring);
85}
86
87/* FUNCTION: getopt_long */
88
89#ifndef _WIN32
90// MSVC doesn't provide getopt.h, which we need for struct option
91
92#ifndef __CPROVER_GETOPT_H_INCLUDED
93#include <getopt.h>
94#define __CPROVER_GETOPT_H_INCLUDED
95#endif
96
98 int argc,
99 char *const argv[],
100 const char *optstring,
101 const struct option *longopts,
102 int *longindex)
103{
104 // trigger valid-pointer checks (if enabled), even though we don't
105 // use the parameter in this model
106 (void)*longopts;
107 // avoid unused-parameter warnings when compiling using GCC (for
108 // internal library syntax checks)
110
111 return getopt(argc, argv, optstring);
112}
113
114#endif
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
void __CPROVER_assert(__CPROVER_bool assertion, const char *description)
_Bool __CPROVER_is_zero_string(const void *)
void __CPROVER_assume(__CPROVER_bool assumption)
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition getopt.c:97
int getopt(int argc, char *const argv[], const char *optstring)
Definition getopt.c:81
__CPROVER_bool __VERIFIER_nondet___CPROVER_bool(void)
size_t __VERIFIER_nondet_size_t(void)
int optind
Definition getopt.c:9
char * optarg
Definition getopt.c:8
int _getopt(int argc, char *const argv[], const char *optstring)
Definition getopt.c:14
size_t strlen(const char *s)
Definition string.c:561