Debian Codegraph @

vhditools/vhdiinfo.c

raw symbols SCIP
1 /*
2 * Shows information obtained from a Virtual Hard Disk (VHD) image file.
3 *
4 * Copyright (C) 2012-2025, Joachim Metz <[email protected]>
5 *
6 * Refer to AUTHORS for acknowledgements.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <common.h>
23 #include <file_stream.h>
24 #include <memory.h>
25 #include <system_string.h>
26 #include <types.h>
27
28 #include <stdio.h>
29
30 #if defined( HAVE_IO_H ) || defined( WINAPI )
31 #include <io.h>
32 #endif
33
34 #if defined( HAVE_STDLIB_H ) || defined( WINAPI )
35 #include <stdlib.h>
36 #endif
37
38 #if defined( HAVE_UNISTD_H )
39 #include <unistd.h>
40 #endif
41
42 #include "info_handle.h"
43 #include "vhditools_getopt.h"
44 #include "vhditools_libcerror.h"
45 #include "vhditools_libclocale.h"
46 #include "vhditools_libcnotify.h"
47 #include "vhditools_libvhdi.h"
48 #include "vhditools_output.h"
49 #include "vhditools_signal.h"
50 #include "vhditools_unused.h"
51
52 info_handle_t *vhdiinfo_info_handle = NULL;
53 int vhdiinfo_abort = 0;
54
55 /* Prints the executable usage information
56 */
57 void usage_fprint(
58 FILE *stream )
59 {
60 if( stream == NULL )
61 {
62 return;
63 }
64 fprintf( stream, "Use vhdiinfo to determine information about a Virtual Hard Disk (VHD)\n"
65 "image file.\n\n" );
66
67 fprintf( stream, "Usage: vhdiinfo [ -hvV ] source\n\n" );
68
69 fprintf( stream, "\tsource: the source file\n\n" );
70
71 fprintf( stream, "\t-h: shows this help\n" );
72 fprintf( stream, "\t-v: verbose output to stderr\n" );
73 fprintf( stream, "\t-V: print version\n" );
74 }
75
76 /* Signal handler for vhdiinfo
77 */
78 void vhdiinfo_signal_handler(
79 vhditools_signal_t signal VHDITOOLS_ATTRIBUTE_UNUSED )
80 {
81 libcerror_error_t *error = NULL;
82 static char *function = "vhdiinfo_signal_handler";
83
84 VHDITOOLS_UNREFERENCED_PARAMETER( signal )
85
86 vhdiinfo_abort = 1;
87
88 if( vhdiinfo_info_handle != NULL )
89 {
90 if( info_handle_signal_abort(
91 vhdiinfo_info_handle,
92 &error ) != 1 )
93 {
94 libcnotify_printf(
95 "%s: unable to signal info handle to abort.\n",
96 function );
97
98 libcnotify_print_error_backtrace(
99 error );
100 libcerror_error_free(
101 &error );
102 }
103 }
104 /* Force stdin to close otherwise any function reading it will remain blocked
105 */
106 #if defined( WINAPI ) && !defined( __CYGWIN__ )
107 if( _close(
108 0 ) != 0 )
109 #else
110 if( close(
111 0 ) != 0 )
112 #endif
113 {
114 libcnotify_printf(
115 "%s: unable to close stdin.\n",
116 function );
117 }
118 }
119
120 /* The main program
121 */
122 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
123 int wmain( int argc, wchar_t * const argv[] )
124 #else
125 int main( int argc, char * const argv[] )
126 #endif
127 {
128 libvhdi_error_t *error = NULL;
129 system_character_t *source = NULL;
130 char *program = "vhdiinfo";
131 system_integer_t option = 0;
132 int verbose = 0;
133
134 libcnotify_stream_set(
135 stderr,
136 NULL );
137 libcnotify_verbose_set(
138 1 );
139
140 if( libclocale_initialize(
141 "vhditools",
142 &error ) != 1 )
143 {
144 fprintf(
145 stderr,
146 "Unable to initialize locale values.\n" );
147
148 goto on_error;
149 }
150 if( vhditools_output_initialize(
151 _IONBF,
152 &error ) != 1 )
153 {
154 fprintf(
155 stderr,
156 "Unable to initialize output settings.\n" );
157
158 goto on_error;
159 }
160 vhditools_output_version_fprint(
161 stdout,
162 program );
163
164 while( ( option = vhditools_getopt(
165 argc,
166 argv,
167 _SYSTEM_STRING( "hvV" ) ) ) != (system_integer_t) -1 )
168 {
169 switch( option )
170 {
171 case (system_integer_t) '?':
172 default:
173 fprintf(
174 stderr,
175 "Invalid argument: %" PRIs_SYSTEM "\n",
176 argv[ optind - 1 ] );
177
178 usage_fprint(
179 stdout );
180
181 return( EXIT_FAILURE );
182
183 case (system_integer_t) 'h':
184 usage_fprint(
185 stdout );
186
187 return( EXIT_SUCCESS );
188
189 case (system_integer_t) 'v':
190 verbose = 1;
191
192 break;
193
194 case (system_integer_t) 'V':
195 vhditools_output_copyright_fprint(
196 stdout );
197
198 return( EXIT_SUCCESS );
199 }
200 }
201 if( optind == argc )
202 {
203 fprintf(
204 stderr,
205 "Missing source file.\n" );
206
207 usage_fprint(
208 stdout );
209
210 return( EXIT_FAILURE );
211 }
212 source = argv[ optind ];
213
214 libcnotify_verbose_set(
215 verbose );
216 libvhdi_notify_set_stream(
217 stderr,
218 NULL );
219 libvhdi_notify_set_verbose(
220 verbose );
221
222 if( info_handle_initialize(
223 &vhdiinfo_info_handle,
224 &error ) != 1 )
225 {
226 fprintf(
227 stderr,
228 "Unable to initialize info handle.\n" );
229
230 goto on_error;
231 }
232 if( info_handle_open_input(
233 vhdiinfo_info_handle,
234 source,
235 &error ) != 1 )
236 {
237 fprintf(
238 stderr,
239 "Unable to open source file.\n" );
240
241 goto on_error;
242 }
243 if( info_handle_input_fprint(
244 vhdiinfo_info_handle,
245 &error ) != 1 )
246 {
247 fprintf(
248 stderr,
249 "Unable to print input information.\n" );
250
251 goto on_error;
252 }
253 if( info_handle_close(
254 vhdiinfo_info_handle,
255 &error ) != 0 )
256 {
257 fprintf(
258 stderr,
259 "Unable to close info handle.\n" );
260
261 goto on_error;
262 }
263 if( info_handle_free(
264 &vhdiinfo_info_handle,
265 &error ) != 1 )
266 {
267 fprintf(
268 stderr,
269 "Unable to free info handle.\n" );
270
271 goto on_error;
272 }
273 return( EXIT_SUCCESS );
274
275 on_error:
276 if( error != NULL )
277 {
278 libcnotify_print_error_backtrace(
279 error );
280 libcerror_error_free(
281 &error );
282 }
283 if( vhdiinfo_info_handle != NULL )
284 {
285 info_handle_free(
286 &vhdiinfo_info_handle,
287 NULL );
288 }
289 return( EXIT_FAILURE );
290 }
291