|
3 | 3 | extern char *optarg;
|
4 | 4 | extern int optind;
|
5 | 5 |
|
| 6 | +#ifndef __CPROVER_STRING_H_INCLUDED |
| 7 | +#include <string.h> |
| 8 | +#define __CPROVER_STRING_H_INCLUDED |
| 9 | +#endif |
| 10 | + |
6 | 11 | inline int getopt(int argc, char * const argv[],
|
7 | 12 | const char *optstring)
|
8 | 13 | {
|
9 | 14 | __CPROVER_HIDE:;
|
10 |
| - int result_index; |
11 |
| - __CPROVER_assume(result_index>=0); |
12 |
| - (void)*optstring; |
13 |
| - if(optind>=argc) |
| 15 | + int result=-1; |
| 16 | + |
| 17 | + if(optind==0) |
| 18 | + optind=1; |
| 19 | + |
| 20 | + if(optind>=argc || argv[optind][0]!='-') |
14 | 21 | return -1;
|
15 |
| - __CPROVER_assume(result_index<argc && result_index>=optind); |
| 22 | + |
| 23 | + size_t result_index; |
| 24 | + __CPROVER_assume( |
| 25 | + result_index<strlen(optstring) && optstring[result_index]!=':'); |
16 | 26 | #ifdef __CPROVER_STRING_ABSTRACTION
|
17 | 27 | __CPROVER_assert(__CPROVER_is_zero_string(optstring),
|
18 | 28 | "getopt zero-termination of 3rd argument");
|
19 | 29 | #endif
|
20 |
| - optarg = argv[result_index]; |
21 |
| - optind = result_index+1; |
22 |
| - int retval; |
23 |
| - return retval; |
| 30 | + |
| 31 | + _Bool found; |
| 32 | + if(found) |
| 33 | + { |
| 34 | + result=optstring[result_index]; |
| 35 | + if(skipped) |
| 36 | + ++optind; |
| 37 | + } |
| 38 | + |
| 39 | + if(result!=-1 && optind<argc && optstring[result_index+1]==':') |
| 40 | + { |
| 41 | + _Bool has_no_arg; |
| 42 | + if(has_no_arg) |
| 43 | + { |
| 44 | + optarg=argv[optind]; |
| 45 | + ++optind; |
| 46 | + } |
| 47 | + else |
| 48 | + optarg=NULL; |
| 49 | + } |
| 50 | + |
| 51 | + return result; |
| 52 | +} |
| 53 | + |
| 54 | +/* FUNCTION: getopt_long */ |
| 55 | + |
| 56 | +int getopt(int argc, char * const argv[], const char *optstring); |
| 57 | + |
| 58 | +inline int getopt_long(int argc, char * const argv[], const char *optstring, |
| 59 | + const struct option *longopts, int *longindex) |
| 60 | +{ |
| 61 | + // trigger valid-pointer checks (if enabled), even though we don't |
| 62 | + // use the parameter in this model |
| 63 | + (void)*longopts; |
| 64 | + // avoid unused-parameter warnings when compiling using GCC (for |
| 65 | + // internal library syntax checks) |
| 66 | + (void)longindex; |
| 67 | + |
| 68 | + return getopt(argc, argv, optstring); |
24 | 69 | }
|
0 commit comments