]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ocaml/contrib/otherlibs/labltk/browser/useunix.ml
update
[l4.git] / l4 / pkg / ocaml / contrib / otherlibs / labltk / browser / useunix.ml
1 (*************************************************************************)
2 (*                                                                       *)
3 (*                Objective Caml LablTk library                          *)
4 (*                                                                       *)
5 (*            Jacques Garrigue, Kyoto University RIMS                    *)
6 (*                                                                       *)
7 (*   Copyright 1999 Institut National de Recherche en Informatique et    *)
8 (*   en Automatique and Kyoto University.  All rights reserved.          *)
9 (*   This file is distributed under the terms of the GNU Library         *)
10 (*   General Public License, with the special exception on linking       *)
11 (*   described in file ../../../LICENSE.                                 *)
12 (*                                                                       *)
13 (*************************************************************************)
14
15 (* $Id: useunix.ml 5094 2002-08-09 10:34:44Z garrigue $ *)
16
17 open StdLabels
18 open UnixLabels
19
20 let get_files_in_directory dir =
21   let len = String.length dir in
22   let dir =
23     if len > 0 && Sys.os_type = "Win32" &&
24      (dir.[len-1] = '/' || dir.[len-1] = '\\')
25     then String.sub dir ~pos:0 ~len:(len-1)
26     else dir
27   in match
28     try Some(opendir dir) with Unix_error _ -> None
29   with
30     None -> []
31   | Some dirh ->
32       let rec get_them l =
33         match
34           try Some(readdir dirh) with _ -> None
35         with
36         | Some x ->
37             get_them (x::l)
38         | None ->
39             closedir dirh; l 
40       in
41       List.sort ~cmp:compare (get_them [])
42
43 let is_directory name =
44   try
45     (stat name).st_kind = S_DIR
46   with _ -> false
47
48 let concat dir name =
49   let len = String.length dir in
50   if len = 0 then name else
51   if dir.[len-1] = '/' then dir ^ name
52   else dir ^ "/" ^ name
53
54 let get_directories_in_files ~path =
55   List.filter ~f:(fun x -> is_directory  (concat path x))
56
57 (************************************************** Subshell call *)
58 let subshell ~cmd =
59   let rc = open_process_in cmd in
60   let rec it l =
61     match
62       try Some(input_line rc) with _ -> None
63     with
64       Some x -> it (x::l)
65     | None -> List.rev l
66   in 
67   let answer = it [] in
68   ignore (close_process_in rc);
69   answer