/* * [Which.java] * * Summary: Finds exe, dll, class, jar on path and identifies type. * * Copyright: (c) 2012-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2012-01-09 initial version. * 1.1 2012-05-07 update Jet version signature. add debugging code. * 1.2 2012-11-15 update Jet version signature for MP5 * 1.3 2013-04-19 update Jet version signature for 8.0 * 1.4 2014-07-25 update Jet version signature for 9.0 * 1.5 2014-07-27 update Jet version signature for 10.0 and 11.0 * 1.6 2016-06-20 update to Jet 11.0 mp3 */ package com.mindprod.which; import com.mindprod.common18.EIO; import com.mindprod.common18.ST; import java.io.File; import java.io.FilenameFilter; import java.util.regex.Pattern; import static java.lang.System.*; /** * Finds exe, dll, class, jar on path and identifies type. *

* Recognises 16-bit DOS, 16-bit Windows 32-bit Windows, 32-bit Jet, 64-bit Jet. *

* Determines if a file is a Jet executable. Searches the Java library path for file mentioned on the command line. * I composed this program in a dream, waking up a dozen times * to discover I had not hit a single keystroke. I composed this * not so much because I think anyone will need this, but because * I want to shut up the damn dream. * Searches for exe, dll and identifies type *

* Put file you want to examine on command line. * * @author Roedy Green, Canadian Mind Products * @version 1.6 2016-06-20 update to Jet 11.0 mp3 * @see com.mindprod.which.FileType * @see FileType#classifyJet * @since 2012-01-09 */ public class Which { /** * true if want extra debug output. also change FileType.DEBUGGING */ private static final boolean DEBUGGING = false; private static final int FIRST_COPYRIGHT_YEAR = 2012; /** * undisplayed copyright notice */ @SuppressWarnings( { "UnusedDeclaration" } ) private static final String EMBEDDED_COPYRIGHT = "Copyright: (c) 2012-2017 Roedy Green, Canadian Mind Products, http://mindprod.com"; @SuppressWarnings( { "UnusedDeclaration" } ) private static final String RELEASE_DATE = "2016-06-20"; /** * how to use this utility. */ private static final String USAGE = "\nUsage: Which.exe someprog.exe (Where someprog.exe is somewhere on the path.)."; /** * embedded version string. */ private static final String VERSION_STRING = "1.6"; /** * used to split out various legs of the path */ private static final Pattern PATH_SPLITTER = Pattern.compile( System.getProperty( "path.separator" ) ); /** * Search path for DLL or exe, and identify types of files found. * * @param fileToExamine DLL or exe to search for. */ private static void searchForDLLorExe( final String fileToExamine ) { final String path = System.getProperty( "java.library.path" ); boolean first = true; int firstRetCode = 91; if ( path != null ) { String[] legs = PATH_SPLITTER.split( path ); if ( DEBUGGING ) { int count = 1; out.println( "Raw java.library.path:" ); for ( String leg : legs ) { if ( !ST.isEmpty( leg ) ) { out.println( count++ + " [" + leg + "]" ); } } } // get rid of dups. We cannot sort or would find wrong instance first. for ( int i = 1; i < legs.length; i++ ) { for ( int j = 0; j < i; j++ ) { assert legs[ i ] != null : "null leg"; if ( ST.isEmpty( legs[ i ] ) ) { legs[ i ] = null; } else if ( legs[ i ].equalsIgnoreCase( legs[ j ] ) ) { if ( DEBUGGING ) { out.println( "Duplicate on path " + legs[ i ] ); } // get rid of dup so we won't report exe twice legs[ i ] = null; break; } } } final FilenameFilter onlyMatch = new OnlyMatch( fileToExamine ); // leg is one element of the path, text between semicolons. int legNo = 0; for ( String leg : legs ) { // ignore nulled dups if ( leg == null ) { continue; } legNo++; // no jars on path, just dirs. Only Classpath has jars. final File dir = new File( leg ); if ( !dir.isDirectory() ) { // C:\WINDOWS\Sun\Java\bin does not exist, but that is expected. if ( !EIO.getCanOrAbsPath( dir ).equalsIgnoreCase( "C:\\Windows\\Sun\\Java\\bin" ) ) { err.println( "Warning: non existent " + legNo + "th dir [" + leg + "] found on path" ); } continue; } String[] candidates = dir.list( onlyMatch ); for ( String candidate : candidates ) { final File hit = new File( dir, candidate ); FileType type = FileType.classify( hit ); out.println( "found: " + EIO.getCanOrAbsPath( hit ) + " : " + type ); if ( first ) { first = false; firstRetCode = type.getRetCode(); } } } } if ( first ) { out.println( "not found: " + fileToExamine ); } System.exit( firstRetCode ); } /** * Determine if a file in an ordinary exe or a Jet exe. * * @param args name of an exe file to be found somewhere on the path */ public static void main( String[] args ) { if ( args.length != 1 ) { err.println( "Must have exactly one unqualified (no dir) .exe or .dll file name on the command line.\n" + USAGE ); System.exit( 99 ); } final String fileToExamine = args[ 0 ]; if ( fileToExamine.contains( "/" ) || fileToExamine.contains( "\\" ) || fileToExamine.contains( ":" ) ) { err.println( "Must have exactly one unqualified (no dir) .exe or .dll file name on the command line.\n" + USAGE ); System.exit( 99 ); } if ( ( fileToExamine.endsWith( ".exe" ) || fileToExamine.endsWith( ".dll" ) ) ) { searchForDLLorExe( fileToExamine ); } err.println( "Filename must end in .exe or .dl\n" + USAGE ); System.exit( 99 ); } }