add win32 methods to help find exe associated w/ a script extension

This commit is contained in:
Doug MacEachern 2006-01-10 22:37:27 +00:00
parent a22dcc8af2
commit 83d4958bb0
2 changed files with 89 additions and 1 deletions

View File

@ -1286,3 +1286,31 @@ JNIEXPORT jlong SIGAR_JNI(ResourceLimit_INFINITY)
return RLIM_INFINITY;
#endif
}
JNIEXPORT jstring SIGAR_JNI(win32_Win32_findExecutable)
(JNIEnv *env, jclass sigar_class, jstring jname)
{
#ifdef WIN32
#include "shellapi.h"
const char *name;
jboolean is_copy;
char exe[MAX_PATH];
LONG result;
jstring jexe = NULL;
name = JENV->GetStringUTFChars(env, jname, &is_copy);
if ((result = (LONG)FindExecutable(name, ".", exe)) > 32) {
jexe = JENV->NewStringUTF(env, exe);
}
if (is_copy) {
JENV->ReleaseStringUTFChars(env, jname, name);
}
return jexe;
#else
sigar_throw_notimpl(env, "win32 only");
return NULL;
#endif
}

View File

@ -1,9 +1,11 @@
package net.hyperic.sigar.win32;
import java.io.File;
import net.hyperic.sigar.Sigar;
import net.hyperic.sigar.SigarException;
abstract class Win32 {
public abstract class Win32 {
static {
try {
@ -12,4 +14,62 @@ abstract class Win32 {
}
}
public static native String findExecutable(String name)
throws SigarException;
public static String findScriptExecutable(String name) {
int ix = name.lastIndexOf(".");
if (ix == -1) {
return null;
}
String ext = name.substring(ix+1);
if (ext.equals("exe") ||
ext.equals("bat") ||
ext.equals("com"))
{
return null;
}
String exe;
try {
exe = findExecutable(name);
} catch (SigarException e) {
return null;
}
if (exe == null) {
return null; //no association
}
exe = exe.toLowerCase();
name = name.toLowerCase();
if (exe.equals(name) || exe.endsWith(name)) {
return null; //same thing
}
File file = new File(exe);
//rewrite to use cscript for command line stuff
if (file.getName().equals("wscript.exe")) {
exe =
file.getParent() +
File.separator +
"cscript.exe";
}
return exe;
}
public static void main(String[] args) throws Exception {
for (int i=0; i<args.length; i++) {
String file =
new File(args[i]).getAbsoluteFile().toString();
String exe =
findScriptExecutable(file);
if (exe == null) {
continue;
}
System.out.println(args[i] + "=" + exe);
}
}
}