1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.File;
  25 import java.util.HashMap;
  26 import java.util.Map;
  27 
  28 /**
  29  * Utility class for finding the command on the current system
  30  */
  31 public class UnixCommands {
  32 
  33     private static final String[] paths = {"/bin", "/usr/bin",
  34             "/usr/local/bin", "/system/bin", "/sbin", "/usr/bin/sbin",
  35             "/usr/local/sbin", "/system/sbin"};
  36 
  37     private static Map<String,String> nameToCommand = new HashMap<>(16);
  38 
  39     /**
  40      * If can find the path to the command, returns the command with the full path.
  41      * Otherwise, returns the name of the command.
  42      */
  43     public static String cat() { return findCommand("cat"); }
  44     public static String sh() { return findCommand("sh"); }
  45     public static String kill() { return findCommand("kill"); }
  46     public static String sleep() { return findCommand("sleep"); }
  47     public static String tee() { return findCommand("tee"); }
  48     public static String echo() { return findCommand("echo"); }
  49     public static String true_() { return findCommand("true"); }
  50     public static String false_() { return findCommand("false"); }
  51 
  52     public static String findCommand(String name) {
  53         if (nameToCommand.containsKey(name)) {
  54             return nameToCommand.get(name);
  55         }
  56         String command = findCommand0(name);
  57         nameToCommand.put(name, command);
  58         return command;
  59     }
  60 
  61     private static String findCommand0(String name) {
  62         for (String path : paths) {
  63             String command = path + '/' + name;
  64             if (new File(command).canExecute()) {
  65                 return command;
  66             }
  67         }
  68         return name;
  69     }
  70 }