From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.0 required=5.0 tests=AWL,SPF_NEUTRAL autolearn=disabled version=3.1.3 Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 93A75BC69 for ; Thu, 23 Aug 2007 00:35:16 +0200 (CEST) Received: from rv-out-0910.google.com (rv-out-0910.google.com [209.85.198.190]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l7MMZFnS011910 for ; Thu, 23 Aug 2007 00:35:16 +0200 Received: by rv-out-0910.google.com with SMTP id g11so208093rvb for ; Wed, 22 Aug 2007 15:35:14 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=hJshR0LrxFHtzLoOXMCSfDtSvlBLWEqB/uf1qphxdj1Q7MBBclmJNfP31Fzmdsc1jwZ/8mF/6LbEpVaHJFOvNNNvccidBv5oVfRW562xrtDcOeGUePRJIlnOl+eft5EIpDqZ7nDiDpUgCqvkTDRRf59mCDlMjGbaryIHn7/7hrw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=gF1xiz4hIgs+FfEicWAhBWNCl/x2Rcm2QKNzziGmCVCfs8JQKtun9FTpGj8ZKB3m0Yl6Eojcu9iX54FnEz7CMTFnV6wKKhpC+VPGJ832QW8Gzw75+LI1hqZEDgakBQkMNGfcTVnUVi7un7/PIeCGRcobnqLoBpPwCyjtijwwsjE= Received: by 10.115.94.1 with SMTP id w1mr1360088wal.1187822114412; Wed, 22 Aug 2007 15:35:14 -0700 (PDT) Received: by 10.114.57.9 with HTTP; Wed, 22 Aug 2007 15:35:14 -0700 (PDT) Message-ID: <95513600708221535t7508e900h192554da73d37fcf@mail.gmail.com> Date: Thu, 23 Aug 2007 00:35:14 +0200 From: "Olivier Andrieu" To: "Dave Benjamin" Subject: Re: [Caml-list] commands.getoutput () in ocaml? Cc: "Luca de Alfaro" , caml-list@yquem.inria.fr In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <28fa90930708221256t497e8356k84861abd6b2e91b8@mail.gmail.com> X-j-chkmail-Score: MSGID : 46CCBA23.001 on discorde : j-chkmail score : X : 0/20 1 0.000 -> 1 X-Miltered: at discorde with ID 46CCBA23.001 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; andrieu:01 oandrieu:01 ocaml:01 ocaml:01 translated:01 buffer:01 2048:01 buffer:01 subprocess:01 2007,:98 sourceforge:01 wrote:01 wrote:01 unix:01 unix:01 On 8/22/07, Dave Benjamin wrote: > On Wed, 22 Aug 2007, Luca de Alfaro wrote: > > > I am looking for a quick way to do the equivalent of > > s = commands.getoutput ("ls " + name + "*") > > in Ocaml. > > I have translated many of the examples from the Perl Cookbook for the > Process Management and Communication chapter of the OCaml PLEAC. This is > one of the topics covered. > > http://pleac.sourceforge.net/pleac_ocaml/processmanagementetc.html about this function: (* Run a command and return its results as a string. *) let read_process command = let buffer_size = 2048 in let buffer = Buffer.create buffer_size in let string = String.create buffer_size in let in_channel = Unix.open_process_in command in let chars_read = ref 1 in while !chars_read <> 0 do chars_read := input in_channel string 0 buffer_size; Buffer.add_string buffer (String.sub string 0 !chars_read) done; close_in in_channel; Buffer.contents buffer you could use Buffer.add_substring instead of add_string, that will avoid an intermediate copy of the string ... and you should be using Unix.close_process_in, not close_in otherwise the subprocess will stay as a zombie. -- Olivier