Blame src/FileUtilWHICH.ml

Packit 9ff65e
(******************************************************************************)
Packit 9ff65e
(*  ocaml-fileutils: files and filenames common operations                    *)
Packit 9ff65e
(*                                                                            *)
Packit 9ff65e
(*  Copyright (C) 2003-2014, Sylvain Le Gall                                  *)
Packit 9ff65e
(*                                                                            *)
Packit 9ff65e
(*  This library is free software; you can redistribute it and/or modify it   *)
Packit 9ff65e
(*  under the terms of the GNU Lesser General Public License as published by  *)
Packit 9ff65e
(*  the Free Software Foundation; either version 2.1 of the License, or (at   *)
Packit 9ff65e
(*  your option) any later version, with the OCaml static compilation         *)
Packit 9ff65e
(*  exception.                                                                *)
Packit 9ff65e
(*                                                                            *)
Packit 9ff65e
(*  This library is distributed in the hope that it will be useful, but       *)
Packit 9ff65e
(*  WITHOUT ANY WARRANTY; without even the implied warranty of                *)
Packit 9ff65e
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file         *)
Packit 9ff65e
(*  COPYING for more details.                                                 *)
Packit 9ff65e
(*                                                                            *)
Packit 9ff65e
(*  You should have received a copy of the GNU Lesser General Public License  *)
Packit 9ff65e
(*  along with this library; if not, write to the Free Software Foundation,   *)
Packit 9ff65e
(*  Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA             *)
Packit 9ff65e
(******************************************************************************)
Packit 9ff65e
Packit 9ff65e
open FileUtilTypes
Packit 9ff65e
open FilePath
Packit 9ff65e
open FileUtilTEST
Packit 9ff65e
Packit 9ff65e
Packit 9ff65e
let which ?(path) fln =
Packit 9ff65e
  let real_path =
Packit 9ff65e
    match path with
Packit 9ff65e
      | None ->
Packit 9ff65e
          path_of_string
Packit 9ff65e
            (try
Packit 9ff65e
               Sys.getenv "PATH"
Packit 9ff65e
             with Not_found ->
Packit 9ff65e
               "")
Packit 9ff65e
      | Some x ->
Packit 9ff65e
        x
Packit 9ff65e
  in
Packit 9ff65e
  let exec_test = test (And(Is_exec, Is_file)) in
Packit 9ff65e
  let which_path =
Packit 9ff65e
    match Sys.os_type with
Packit 9ff65e
    | "Win32" ->
Packit 9ff65e
      begin
Packit 9ff65e
        let real_ext =
Packit 9ff65e
          List.map
Packit 9ff65e
            (fun dot_ext ->
Packit 9ff65e
               (* Remove leading "." if it exists *)
Packit 9ff65e
               if (String.length dot_ext) >= 1 && dot_ext.[0] = '.' then
Packit 9ff65e
                 String.sub dot_ext 1 ((String.length dot_ext) - 1)
Packit 9ff65e
               else
Packit 9ff65e
                 dot_ext)
Packit 9ff65e
            (* Extract possible extension from PATHEXT *)
Packit 9ff65e
            (path_of_string
Packit 9ff65e
               (try
Packit 9ff65e
                  Sys.getenv "PATHEXT"
Packit 9ff65e
                with Not_found ->
Packit 9ff65e
                  ""))
Packit 9ff65e
        in
Packit 9ff65e
        let to_filename dirname ext = add_extension (concat dirname fln) ext in
Packit 9ff65e
        let ctst dirname ext = exec_test (to_filename dirname ext) in
Packit 9ff65e
        List.fold_left
Packit 9ff65e
          (fun found dirname ->
Packit 9ff65e
             if found = None then begin
Packit 9ff65e
               try
Packit 9ff65e
                 let ext = List.find (ctst dirname) real_ext in
Packit 9ff65e
                 Some (to_filename dirname ext)
Packit 9ff65e
               with Not_found ->
Packit 9ff65e
                 None
Packit 9ff65e
             end else
Packit 9ff65e
               found)
Packit 9ff65e
          None
Packit 9ff65e
          real_path
Packit 9ff65e
      end
Packit 9ff65e
    | _ ->
Packit 9ff65e
      begin
Packit 9ff65e
        let to_filename dirname = concat dirname fln in
Packit 9ff65e
        try
Packit 9ff65e
          Some
Packit 9ff65e
            (to_filename
Packit 9ff65e
               (List.find
Packit 9ff65e
                  (fun dirname ->
Packit 9ff65e
                     exec_test (to_filename dirname)) real_path))
Packit 9ff65e
        with Not_found ->
Packit 9ff65e
          None
Packit 9ff65e
      end
Packit 9ff65e
  in
Packit 9ff65e
  match which_path with
Packit 9ff65e
  | Some fn -> fn
Packit 9ff65e
  | None -> raise Not_found