File: libcsystem_getopt.c

package info (click to toggle)
libewf 20140804-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 19,284 kB
  • sloc: ansic: 313,293; sh: 6,855; cpp: 3,819; makefile: 1,926; yacc: 1,094; python: 467; lex: 391; sed: 16
file content (170 lines) | stat: -rw-r--r-- 4,498 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * GetOpt functions
 *
 * Copyright (C) 2008-2015, Joachim Metz <joachim.metz@gmail.com>
 *
 * Refer to AUTHORS for acknowledgements.
 *
 * This software is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <common.h>
#include <types.h>

#if defined( HAVE_STDLIB_H ) || defined( WINAPI )
#include <stdlib.h>
#endif

#include "libcsystem_getopt.h"
#include "libcsystem_libcnotify.h"
#include "libcsystem_libcstring.h"

#if !defined( HAVE_GETOPT )

/* The current option argument
 */
libcstring_system_character_t *optarg = NULL;

/* The option index
 * Start with argument 1 (argument 0 is the program name)
 */
int optind = 1;

/* Value to indicate the current option
 */
libcstring_system_integer_t optopt = 0;

/* Get the program options
 * Function for platforms that do not have the getopt function
 * Returns the option character processed, or -1 on error,
 * ? if the option was not in the options string, : if the option argument was missing
 */
libcstring_system_integer_t libcsystem_getopt(
                             int argument_count,
                             libcstring_system_character_t * const argument_values[],
                             const libcstring_system_character_t *options_string )
{
	libcstring_system_character_t *option_value   = NULL;
	libcstring_system_character_t *argument_value = NULL;
	static char *function                         = "libcsystem_getopt";
	size_t options_string_length                  = 0;

	if( optind >= argument_count )
	{
		return( (libcstring_system_integer_t) -1 );
	}
	argument_value = argument_values[ optind ];

	/* Check if the argument value is not an empty string
	 */
	if( *argument_value == 0 )
	{
		return( (libcstring_system_integer_t) -1 );
	}
	/* Check if the first character is a option marker '-'
	 */
	if( *argument_value != (libcstring_system_character_t) '-' )
	{
		return( (libcstring_system_integer_t) -1 );
	}
	argument_value++;

	/* Check if long options are provided '--'
	 */
	if( *argument_value == (libcstring_system_character_t) '-' )
	{
		optind++;

		return( (libcstring_system_integer_t) -1 );
	}
	options_string_length = libcstring_system_string_length(
	                         options_string );

	optopt       = *argument_value;
	option_value = libcstring_system_string_search_character(
	                options_string,
	                optopt,
	                options_string_length );

	argument_value++;

	/* Check if an argument was specified or that the option was not found
	 * in the option string
	 */
	if( ( optopt == (libcstring_system_integer_t) ':' )
	 || ( option_value == NULL ) )
	{
		if( *argument_value == (libcstring_system_character_t) '\0' )
		{
			optind++;
		}
		if( ( *options_string != (libcstring_system_character_t) ':' )
		 && ( optopt != (libcstring_system_integer_t) '?' ) )
		{
			libcnotify_printf(
			 "%s: no such option: %" PRIc_LIBCSTRING_SYSTEM ".\n",
			 function,
			 optopt );
		}
		return( (libcstring_system_integer_t) '?' );
	}
	option_value++;

	/* Check if no option argument is required
	 */
	if( *option_value != (libcstring_system_character_t) ':' )
	{
		optarg = NULL;

		if( *argument_value == (libcstring_system_character_t) '\0' )
		{
			optind++;
		}
	}
	/* Check if the argument is right after the option flag with no space in between
	 */
	else if( *argument_value != (libcstring_system_character_t) '\0' )
	{
		optarg = argument_value;

		optind++;
	}
	else
	{
		optind++;

		/* Check if the argument was provided as the next argument value
		 */
		if( argument_count <= optind )
		{
			if( *option_value == ':' )
			{
				return( (libcstring_system_integer_t) ':' );
			}
			libcnotify_printf(
			 "%s: option: %" PRIc_LIBCSTRING_SYSTEM " requires an argument.\n",
			 function,
			 optopt );

			return( (libcstring_system_integer_t) '?' );
		}
		optarg = argument_values[ optind ];

		optind++;
	}
	return( optopt );
}

#endif