caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] uname for Ocaml
@ 2003-01-21 15:23 Basile STARYNKEVITCH
  2003-01-21 20:29 ` Issac Trotts
  2003-01-22 16:47 ` [Caml-list] " Michaël Grünewald
  0 siblings, 2 replies; 6+ messages in thread
From: Basile STARYNKEVITCH @ 2003-01-21 15:23 UTC (permalink / raw)
  To: caml-list

Dear All,

If you need to call uname(2) from some Ocaml program you can steal my
tiny code in util.ml util.mli util_ml.c on

http://www2.poesia-filter.org:8000/cgi-bin/cvsweb.cgi/PoesiaSoft/PoesiaMonIcap/

I actually wish that uname(2) will be incorporated in the Unix module.

regards
-- 

Basile STARYNKEVITCH         http://starynkevitch.net/Basile/ 
email: basile<at>starynkevitch<dot>net 
alias: basile<at>tunes<dot>org 
8, rue de la Faïencerie, 92340 Bourg La Reine, France
-------------------
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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] uname for Ocaml
  2003-01-21 15:23 [Caml-list] uname for Ocaml Basile STARYNKEVITCH
@ 2003-01-21 20:29 ` Issac Trotts
  2003-01-21 20:53   ` Basile STARYNKEVITCH
  2003-01-22 16:47 ` [Caml-list] " Michaël Grünewald
  1 sibling, 1 reply; 6+ messages in thread
From: Issac Trotts @ 2003-01-21 20:29 UTC (permalink / raw)
  To: OCaml Mailing List

An alternative would be to use

  #load "unix.cma"

  let uname() =                                                      
    let (inc, outc) = Unix.open_process "uname" in  
    let name = input_line inc in                  
    close_in inc;
    close_out outc;
    name;;

Here's a more general way to capture the stdout of a Unix call:

  let syscall cmd =                                    
    let (inc, outc) = Unix.open_process cmd in  
    let buf = Buffer.create 16 in                       
    (try while true do Buffer.add_channel buf inc 1 done with _ -> ());
    close_in inc;
    close_out outc;
    Buffer.contents buf;;

Issac


On Tue, Jan 21, 2003 at 04:23:03PM +0100, Basile STARYNKEVITCH wrote:
> Dear All,
> 
> If you need to call uname(2) from some Ocaml program you can steal my
> tiny code in util.ml util.mli util_ml.c on
> 
> http://www2.poesia-filter.org:8000/cgi-bin/cvsweb.cgi/PoesiaSoft/PoesiaMonIcap/
> 
> I actually wish that uname(2) will be incorporated in the Unix module.
> 
> regards
> -- 
> 
> Basile STARYNKEVITCH         http://starynkevitch.net/Basile/ 
> email: basile<at>starynkevitch<dot>net 
> alias: basile<at>tunes<dot>org 
> 8, rue de la Fa?encerie, 92340 Bourg La Reine, France
> -------------------
> 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

-- 
-------------------
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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] uname for Ocaml
  2003-01-21 20:29 ` Issac Trotts
@ 2003-01-21 20:53   ` Basile STARYNKEVITCH
  2003-01-21 22:42     ` Issac Trotts
  0 siblings, 1 reply; 6+ messages in thread
From: Basile STARYNKEVITCH @ 2003-01-21 20:53 UTC (permalink / raw)
  To: Issac Trotts; +Cc: caml-list


Issac cited me after his reply - I put it in the right order.

>>>>> "Issac" == Issac Trotts <ijtrotts@ucdavis.edu> writes:



    Issac> On Tue, Jan 21, 2003 at 04:23:03PM +0100, Basile
    Issac> STARYNKEVITCH wrote:
    >> Dear All,
    >> 
    >> If you need to call uname(2) from some Ocaml program you can
    >> steal my tiny code in util.ml util.mli util_ml.c on
    >> 
    >> http://www2.poesia-filter.org:8000/cgi-bin/cvsweb.cgi/PoesiaSoft/PoesiaMonIcap/
    >> 
    >> I actually wish that uname(2) will be incorporated in the Unix
    >> module.
    >> 


    Issac> An alternative would be to use #load "unix.cma"

I agree with Issac, but I still wish that most (or perhaps all) of the
standard Posix system calls should be callable from Unix module!

    Issac>   let uname() = let (inc, outc) = Unix.open_process "uname"
    Issac> in let name = input_line inc in close_in inc; close_out
    Issac> outc; name;;

Won't work if the (malicious) user have an unrelated uname in his
path. I suggest at least using the "/bin/uname" which should be a
POSIX standard IIRC. Also, forking a shell process for each system
call might be not optimal (and not robust).

Also, my code return the equivalent of /bin/uname -a

    Issac> Here's a more general way to capture the stdout of a Unix
    Issac> call:

    Issac>   let syscall cmd = let (inc, outc) = Unix.open_process cmd
    Issac> in let buf = Buffer.create 16 in (try while true do
    Issac> Buffer.add_channel buf inc 1 done with _ -> ()); close_in
    Issac> inc; close_out outc; Buffer.contents buf;;

Thanks for this suggestion, but why not use Unix.open_process_in in
this and similar cases?

On a related, but independent, issue, Linux hackers might be
interested in the procfs.ml[i] files from

http://www2.poesia-filter.org:8000/cgi-bin/cvsweb.cgi/PoesiaSoft/PoesiaMonIcap/

The idea is to get the status of a process by parsing /proc/#/stat
into an intelligible structure. This is Linux specific (works on linux
2.4 & 2.5 kernels). The code is mostly a huge (and boring) scanf,
using a format stolen from the linux kernel (see comments in
procfs.ml).

Regards.


-- 

Basile STARYNKEVITCH         http://starynkevitch.net/Basile/ 
email: basile<at>starynkevitch<dot>net 
alias: basile<at>tunes<dot>org 
8, rue de la Faïencerie, 92340 Bourg La Reine, France
-------------------
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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] uname for Ocaml
  2003-01-21 20:53   ` Basile STARYNKEVITCH
@ 2003-01-21 22:42     ` Issac Trotts
  0 siblings, 0 replies; 6+ messages in thread
From: Issac Trotts @ 2003-01-21 22:42 UTC (permalink / raw)
  To: OCaml Mailing List

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Caml-list] Re: uname for Ocaml
  2003-01-21 15:23 [Caml-list] uname for Ocaml Basile STARYNKEVITCH
  2003-01-21 20:29 ` Issac Trotts
@ 2003-01-22 16:47 ` Michaël Grünewald
  2003-01-24 10:36   ` Michal Moskal
  1 sibling, 1 reply; 6+ messages in thread
From: Michaël Grünewald @ 2003-01-22 16:47 UTC (permalink / raw)
  To: caml-list

Basile STARYNKEVITCH <basile@starynkevitch.net> writes:

> Dear All,
> 
> If you need to call uname(2) from some Ocaml program you can steal my
> tiny code in util.ml util.mli util_ml.c on
> 
> I actually wish that uname(2) will be incorporated in the Unix module.

Would not be more accurate to complete, and much more simple, the
'Sys.os_type' information string by a 'Sys.os_name' information string
(or os_uname ...).
-- 
Michaël Grünewald <michael-grunewald@wanadoo.fr>
-------------------
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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Re: uname for Ocaml
  2003-01-22 16:47 ` [Caml-list] " Michaël Grünewald
@ 2003-01-24 10:36   ` Michal Moskal
  0 siblings, 0 replies; 6+ messages in thread
From: Michal Moskal @ 2003-01-24 10:36 UTC (permalink / raw)
  To: Michaël Grünewald; +Cc: caml-list

On Wed, Jan 22, 2003 at 05:47:29PM +0100, Michaël Grünewald wrote:
> Basile STARYNKEVITCH <basile@starynkevitch.net> writes:
> 
> > Dear All,
> > 
> > If you need to call uname(2) from some Ocaml program you can steal my
> > tiny code in util.ml util.mli util_ml.c on
> > 
> > I actually wish that uname(2) will be incorporated in the Unix module.
> 
> Would not be more accurate to complete, and much more simple, the
> 'Sys.os_type' information string by a 'Sys.os_name' information string
> (or os_uname ...).

There are also other fields in structure returned by uname:

              struct utsname {
                      char sysname[];
                      char nodename[];
                      char release[];
                      char version[];
                      char machine[];
              #ifdef _GNU_SOURCE
                      char domainname[];
              #endif
              };

I don't see much point messing with Sys modules with all this stuff.

-- 
: Michal Moskal ::::: malekith/at/pld-linux.org :  GCS {C,UL}++++$ a? !tv
: PLD Linux ::::::: Wroclaw University, CS Dept :  {E-,w}-- {b++,e}>+++ h
-------------------
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


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2003-01-24 12:02 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-01-21 15:23 [Caml-list] uname for Ocaml Basile STARYNKEVITCH
2003-01-21 20:29 ` Issac Trotts
2003-01-21 20:53   ` Basile STARYNKEVITCH
2003-01-21 22:42     ` Issac Trotts
2003-01-22 16:47 ` [Caml-list] " Michaël Grünewald
2003-01-24 10:36   ` Michal Moskal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).