11/*
2- * Shows information obtained from storage media (SM) devices
2+ * Shows information obtained from a storage media (SM) device.
33 *
44 * Copyright (C) 2010-2026, Joachim Metz <joachim.metz@gmail.com>
55 *
5656info_handle_t * smdevinfo_info_handle = NULL ;
5757int smdevinfo_abort = 0 ;
5858
59- /* Prints the executable usage information
60- */
61- void usage_fprint (
62- FILE * stream )
63- {
64- if ( stream == NULL )
65- {
66- return ;
67- }
68- fprintf ( stream , "Use smdevinfo to determine information about storage\n"
69- "media (SM) device(s)\n\n" );
70-
71- fprintf ( stream , "Usage: smdevinfo [ -hivV ] source\n\n" );
72-
73- fprintf ( stream , "\tsource: the source device file\n\n" );
74-
75- fprintf ( stream , "\t-h: shows this help\n" );
76- fprintf ( stream , "\t-v: verbose output to stderr\n" );
77- fprintf ( stream , "\t-V: print version\n" );
78- }
79-
8059/* Signal handler for smdevinfo
8160 */
8261void smdevinfo_signal_handler (
@@ -89,19 +68,21 @@ void smdevinfo_signal_handler(
8968
9069 smdevinfo_abort = 1 ;
9170
92- if ( ( smdevinfo_info_handle != NULL )
93- && ( info_handle_signal_abort (
94- smdevinfo_info_handle ,
95- & error ) != 1 ) )
71+ if ( smdevinfo_info_handle != NULL )
9672 {
97- libcnotify_printf (
98- "%s: unable to signal info handle to abort.\n" ,
99- function );
100-
101- libcnotify_print_error_backtrace (
102- error );
103- libcerror_error_free (
104- & error );
73+ if ( info_handle_signal_abort (
74+ smdevinfo_info_handle ,
75+ & error ) != 1 )
76+ {
77+ libcnotify_printf (
78+ "%s: unable to signal info handle to abort.\n" ,
79+ function );
80+
81+ libcnotify_print_error_backtrace (
82+ error );
83+ libcerror_error_free (
84+ & error );
85+ }
10586 }
10687 /* Force stdin to close otherwise any function reading it will remain blocked
10788 */
@@ -127,10 +108,22 @@ int wmain( int argc, wchar_t * const argv[] )
127108int main ( int argc , char * const argv [] )
128109#endif
129110{
130- libcerror_error_t * error = NULL ;
111+ const char * description = \
112+ "Use smdevinfo to determine information about a storage media (SM) device." ;
113+
114+ smdevtools_option_t options [ ] = {
115+ { 'h' , NULL , "shows this help" },
116+ { 'v' , NULL , "verbose output to stderr" },
117+ { 'V' , NULL , "print version" },
118+ { 0 , "source" , "the source device" },
119+ };
120+ system_character_t options_string [ 32 ];
121+
122+ libsmdev_error_t * error = NULL ;
131123 system_character_t * source = NULL ;
132124 char * program = "smdevinfo" ;
133125 system_integer_t option = 0 ;
126+ int number_of_options = (int ) ( sizeof ( options ) / sizeof ( smdevtools_option_t ) );
134127 int verbose = 0 ;
135128
136129#if defined( __MINGW32__ ) && defined( HAVE_MINGW_BINMODE )
@@ -145,7 +138,7 @@ int main( int argc, char * const argv[] )
145138 1 );
146139
147140 if ( libclocale_initialize (
148- "smdevtools" ,
141+ "smdevtools" ,
149142 & error ) != 1 )
150143 {
151144 fprintf (
@@ -154,24 +147,36 @@ int main( int argc, char * const argv[] )
154147
155148 goto on_error ;
156149 }
157- if ( smdevtools_output_initialize (
158- _IONBF ,
159- & error ) != 1 )
150+ if ( smdevtools_output_initialize (
151+ _IONBF ,
152+ & error ) != 1 )
160153 {
161154 fprintf (
162155 stderr ,
163156 "Unable to initialize output settings.\n" );
164157
165158 goto on_error ;
166159 }
167- smdevoutput_version_fprint (
160+ smdevtools_output_version_fprint (
168161 stdout ,
169162 program );
170163
164+ if ( smdevtools_getopt_get_options_string (
165+ options ,
166+ number_of_options ,
167+ options_string ,
168+ 32 ) != 1 )
169+ {
170+ fprintf (
171+ stderr ,
172+ "Unable to determine options string.\n" );
173+
174+ goto on_error ;
175+ }
171176 while ( ( option = smdevtools_getopt (
172177 argc ,
173178 argv ,
174- _SYSTEM_STRING ( "hvV" ) ) ) != (system_integer_t ) - 1 )
179+ options_string ) ) != (system_integer_t ) - 1 )
175180 {
176181 switch ( option )
177182 {
@@ -182,14 +187,22 @@ int main( int argc, char * const argv[] )
182187 "Invalid argument: %" PRIs_SYSTEM "\n" ,
183188 argv [ optind - 1 ] );
184189
185- usage_fprint (
186- stdout );
190+ smdevtools_getopt_usage_fprint (
191+ stdout ,
192+ program ,
193+ description ,
194+ options ,
195+ number_of_options );
187196
188197 return ( EXIT_FAILURE );
189198
190199 case (system_integer_t ) 'h' :
191- usage_fprint (
192- stdout );
200+ smdevtools_getopt_usage_fprint (
201+ stdout ,
202+ program ,
203+ description ,
204+ options ,
205+ number_of_options );
193206
194207 return ( EXIT_SUCCESS );
195208
@@ -199,7 +212,7 @@ int main( int argc, char * const argv[] )
199212 break ;
200213
201214 case (system_integer_t ) 'V' :
202- smdevoutput_copyright_fprint (
215+ smdevtools_output_copyright_fprint (
203216 stdout );
204217
205218 return ( EXIT_SUCCESS );
@@ -209,10 +222,14 @@ int main( int argc, char * const argv[] )
209222 {
210223 fprintf (
211224 stderr ,
212- "Missing source file .\n" );
225+ "Missing source device .\n" );
213226
214- usage_fprint (
215- stdout );
227+ smdevtools_getopt_usage_fprint (
228+ stdout ,
229+ program ,
230+ description ,
231+ options ,
232+ number_of_options );
216233
217234 return ( EXIT_FAILURE );
218235 }
@@ -230,13 +247,9 @@ int main( int argc, char * const argv[] )
230247 & smdevinfo_info_handle ,
231248 & error ) != 1 )
232249 {
233- smdevoutput_version_fprint (
234- stderr ,
235- program );
236-
237250 fprintf (
238251 stderr ,
239- "Unable to create info handle.\n" );
252+ "Unable to initialize info handle.\n" );
240253
241254 goto on_error ;
242255 }
@@ -260,18 +273,17 @@ int main( int argc, char * const argv[] )
260273 {
261274 fprintf (
262275 stderr ,
263- "Unable to open file: %" PRIs_SYSTEM ".\n" ,
264- source );
276+ "Unable to open source device.\n" );
265277
266278 goto on_error ;
267279 }
268- if ( info_handle_handle_fprint (
280+ if ( info_handle_device_fprint (
269281 smdevinfo_info_handle ,
270282 & error ) != 1 )
271283 {
272284 fprintf (
273285 stderr ,
274- "Unable to print information.\n" );
286+ "Unable to print device information.\n" );
275287
276288 goto on_error ;
277289 }
@@ -333,5 +345,5 @@ int main( int argc, char * const argv[] )
333345 NULL );
334346 }
335347 return ( EXIT_FAILURE );
336-
337348}
349+
0 commit comments