@@ -47,7 +47,7 @@ print_usage(
4747 " -V display version information and exit\n"
4848 "VERSION can be:\n"
4949 " 1, 2, 3, 4, 5, 6, 7, 8, 9, 95, 10, 103 (for Uwabami Breakers), 105, 11, 12, 123, 125, 128, 13, 14, 143, 15, or 16\n"
50- "For extraction mode, VERSION can also be set to 'd' to enable automatic file detection \n\n"
50+ "Specify 'd' as VERSION to automatically detect archive format. (-l and -x only) \n\n"
5151 "Report bugs to <" PACKAGE_BUGREPORT ">.\n" , argv0 );
5252}
5353
@@ -345,7 +345,7 @@ main(
345345 exit (1 );
346346 }
347347 mode = opt ;
348- if (opt == 'x' && * util_optarg == 'd' ) {
348+ if (( opt == 'x' || mode == 'l' ) && * util_optarg == 'd' ) {
349349 version = ~0 ;
350350 }
351351 else if (opt != 'd' ) version = parse_version (util_optarg );
@@ -357,6 +357,40 @@ main(
357357 argc = ind ;
358358 argv [argc ] = NULL ;
359359
360+ /* detect version */
361+ if (argc && (mode == 'x' || mode == 'l' ) && version == ~0 ) {
362+ thtk_io_t * file ;
363+ if (!(file = thtk_io_open_file (argv [0 ], "rb" , & error ))) {
364+ print_error (error );
365+ thtk_error_free (& error );
366+ exit (1 );
367+ }
368+ uint32_t out [4 ];
369+ unsigned int heur ;
370+ printf ("Detecting '%s'...\n" ,argv [0 ]);
371+ if (-1 == thdat_detect (argv [0 ], file , out , & heur , & error )) {
372+ thtk_io_close (file );
373+ print_error (error );
374+ thtk_error_free (& error );
375+ exit (1 );
376+ }
377+ if (heur == -1 ) {
378+ const thdat_detect_entry_t * ent ;
379+ printf ("Couldn't detect version!\nPossible versions: " );
380+ while ((ent = thdat_detect_iter (out ))) {
381+ printf ("%d," ,ent -> alias );
382+ }
383+ printf ("\n" );
384+ thtk_io_close (file );
385+ exit (1 );
386+ }
387+ else {
388+ printf ("Detected version %d\n" ,heur );
389+ version = heur ;
390+ }
391+ thtk_io_close (file );
392+ }
393+
360394 switch (mode ) {
361395 case 'd' : {
362396 if (argc < 1 ) {
@@ -425,39 +459,6 @@ main(
425459 exit (1 );
426460 }
427461
428- if (version == ~0 ) {
429- thtk_io_t * file ;
430- if (!(file = thtk_io_open_file (argv [0 ], "rb" , & error ))) {
431- print_error (error );
432- thtk_error_free (& error );
433- exit (1 );
434- }
435- uint32_t out [4 ];
436- unsigned int heur ;
437- printf ("Detecting '%s'...\n" ,argv [0 ]);
438- if (-1 == thdat_detect (argv [0 ], file , out , & heur , & error )) {
439- thtk_io_close (file );
440- print_error (error );
441- thtk_error_free (& error );
442- exit (1 );
443- }
444- if (heur == -1 ) {
445- const thdat_detect_entry_t * ent ;
446- printf ("Couldn't detect version!\nPossible versions: " );
447- while ((ent = thdat_detect_iter (out ))) {
448- printf ("%d," ,ent -> alias );
449- }
450- printf ("\n" );
451- thtk_io_close (file );
452- exit (1 );
453- }
454- else {
455- printf ("Detected version %d\n" ,heur );
456- version = heur ;
457- }
458- thtk_io_close (file );
459- }
460-
461462 thdat_state_t * state = thdat_open_file (version , argv [0 ], & error );
462463 if (!state ) {
463464 print_error (error );
0 commit comments