<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2003/01/b0cc1fe837514fbfc9cf209458fc068a"
  from="Issac Trotts &lt;ijtrotts@u...&gt;"
  author="Issac Trotts"
  date="2003-01-21T22:38:13"
  subject="Re: [Caml-list] uname for Ocaml"
  prev="2003/01/6ccb28289ee64d32905dfd95daa0f506"
  next="2003/01/7a41ba7170d9a763c7f88f79fb4881a5"
  prev-in-thread="2003/01/6ccb28289ee64d32905dfd95daa0f506"
  next-in-thread="2003/01/3ec900eef42b08b0fd4f98e4b1164ef2"
  prev-thread="2003/01/ff00f860be700a943d78413e07a43f74"
  next-thread="2003/01/4a9754e53ff07723caf21b4496d1d267"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] uname for Ocaml">
<msg 
  url="2003/01/0e9c2bc35f3c3fbbe7e967dccafbddfc"
  from="Basile STARYNKEVITCH &lt;basile@s...&gt;"
  author="Basile STARYNKEVITCH"
  date="2003-01-21T15:23:15"
  subject="[Caml-list] uname for Ocaml">
<msg 
  url="2003/01/8d002668cc150c0c58d3c343dbd39a6e"
  from="Issac Trotts &lt;ijtrotts@u...&gt;"
  author="Issac Trotts"
  date="2003-01-21T20:26:01"
  subject="Re: [Caml-list] uname for Ocaml">
<msg 
  url="2003/01/6ccb28289ee64d32905dfd95daa0f506"
  from="Basile STARYNKEVITCH &lt;basile@s...&gt;"
  author="Basile STARYNKEVITCH"
  date="2003-01-21T20:53:17"
  subject="Re: [Caml-list] uname for Ocaml">
<msg 
  url="2003/01/b0cc1fe837514fbfc9cf209458fc068a"
  from="Issac Trotts &lt;ijtrotts@u...&gt;"
  author="Issac Trotts"
  date="2003-01-21T22:38:13"
  subject="Re: [Caml-list] uname for Ocaml">
</msg>
</msg>
</msg>
<msg 
  url="2003/01/3ec900eef42b08b0fd4f98e4b1164ef2"
  from="michael-grunewald@w..."
  author="michael-grunewald@w..."
  date="2003-01-23T10:27:55"
  subject="[Caml-list] Re: uname for Ocaml">
<msg 
  url="2003/01/eb3e199755827e6ec36840e2b70fec49"
  from="Michal Moskal &lt;malekith@p...&gt;"
  author="Michal Moskal"
  date="2003-01-24T12:02:46"
  subject="Re: [Caml-list] Re: uname for Ocaml">
</msg>
</msg>
</msg>
</thread>

<contents>
On Tue, Jan 21, 2003 at 09:53:12PM +0100, Basile STARYNKEVITCH wrote:
&gt; 
&gt; Issac cited me after his reply - I put it in the right order.
&gt; 
&gt; &gt;&gt;&gt;&gt;&gt; "Issac" == Issac Trotts &lt;ijtrotts@ucdavis.edu&gt; writes:
&gt; 
&gt; 
&gt; 
&gt;     Issac&gt; On Tue, Jan 21, 2003 at 04:23:03PM +0100, Basile
&gt;     Issac&gt; STARYNKEVITCH wrote:
&gt;     &gt;&gt; Dear All,
&gt;     &gt;&gt; 
&gt;     &gt;&gt; If you need to call uname(2) from some Ocaml program you can
&gt;     &gt;&gt; steal my tiny code in util.ml util.mli util_ml.c on
&gt;     &gt;&gt; 
&gt;     &gt;&gt; http://www2.poesia-filter.org:8000/cgi-bin/cvsweb.cgi/PoesiaSoft/PoesiaMonIcap/
&gt;     &gt;&gt; 
&gt;     &gt;&gt; I actually wish that uname(2) will be incorporated in the Unix
&gt;     &gt;&gt; module.
&gt;     &gt;&gt; 
&gt; 
&gt; 
&gt;     Issac&gt; An alternative would be to use #load "unix.cma"
&gt; 
&gt; I agree with Issac, but I still wish that most (or perhaps all) of the
&gt; standard Posix system calls should be callable from Unix module!
&gt; 
&gt;     Issac&gt;   let uname() = let (inc, outc) = Unix.open_process "uname"
&gt;     Issac&gt; in let name = input_line inc in close_in inc; close_out
&gt;     Issac&gt; outc; name;;
&gt; 
&gt; Won't work if the (malicious) user have an unrelated uname in his
&gt; path. I suggest at least using the "/bin/uname" which should be a
&gt; POSIX standard IIRC. Also, forking a shell process for each system
&gt; call might be not optimal (and not robust).
&gt; 
&gt; Also, my code return the equivalent of /bin/uname -a
&gt; 
&gt;     Issac&gt; Here's a more general way to capture the stdout of a Unix
&gt;     Issac&gt; call:
&gt; 
&gt;     Issac&gt;   let syscall cmd = let (inc, outc) = Unix.open_process cmd
&gt;     Issac&gt; in let buf = Buffer.create 16 in (try while true do
&gt;     Issac&gt; Buffer.add_channel buf inc 1 done with _ -&gt; ()); close_in
&gt;     Issac&gt; inc; close_out outc; Buffer.contents buf;;
&gt; 
&gt; Thanks for this suggestion, but why not use Unix.open_process_in in
&gt; this and similar cases?

You're right: it's shorter to say

  let sys_call cmd =                                                     
    let inc = Unix.open_process_in cmd in
    let buf = Buffer.create 16 in
    (try while true do Buffer.add_channel buf inc 1 done with _ -&gt; ());
    close_in inc;
    Buffer.contents buf;;

Or we can break the call results into lines:

  let rec input_lines file =                                                  
    try  
      let ln = input_line file in
      ln :: input_lines file
    with
      End_of_file -&gt; [];;
  
  let sys_lines cmd =
    let pipe = Unix.open_process_in cmd in
    let lines = input_lines pipe in
    close_in f;
    lines;;

Sometimes it would be better to iterate a function over the lines:

  let sys_iter f cmd =
    let pipe = Unix.open_process_in cmd in
    (try while true do f (input_line pipe) done with _ -&gt; ());
    close_in pipe;;
    
This can be used for things like
  
  sys_iter print_endline "ls";;

Issac

-- 
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

</contents>

</message>

