From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id WAA31954; Wed, 30 Jul 2003 22:53:02 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id WAA01100 for ; Wed, 30 Jul 2003 22:53:01 +0200 (MET DST) Received: from opus.davidb.org (66-75-152-1.san.rr.com [66.75.152.1]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h6UKr0f07840 for ; Wed, 30 Jul 2003 22:53:00 +0200 (MET DST) Received: from davidb by opus.davidb.org with local (Exim 3.31 #1 (Debian)) id 19hxwO-0007T9-00; Wed, 30 Jul 2003 13:52:56 -0700 Date: Wed, 30 Jul 2003 13:52:56 -0700 From: David Brown To: Chris Uzdavinis Cc: caml-list@inria.fr Subject: Re: [Caml-list] Reading directory Problem Message-ID: <20030730205256.GA28673@davidb.org> References: <69FB2CA668C6D841BE0326C13071C69FA8D49B@hq-ex2kpo1.filenet.fn.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.4i X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 0400,:01 struct:01 readdir:01 filenames:01 symlinks:01 symlink:01 recursively:01 opendir:01 closedir:01 nesting:01 chris:01 ocaml:01 null:01 handles:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Wed, Jul 30, 2003 at 03:16:54PM -0400, Chris Uzdavinis wrote: > "Le, To Trinh" writes: > > > Hello, > > > > I have problem with determine directory or file type. I wrote > > DIR * dirp; struct dirent *pDirent; > > while((pDirent=readdir(dirp)) != NULL) > > { > > if(pDirent->d_name[0] == '.' continue; > > This isn't a C newsgroup... but... The code is also incorrect, in that it only compares the first character of the name with '.'. Better to compare the whole string against "." and "..". The code given will discard any filenames that start with '.', which is definitely not what you want. The Unix library contains both stat and lstat. Lstat is useful if you want to see symlinks as symlinks. This is ususally useful when writing archiving or other types of operations. stat will just show you the type of what a symlink targets. BTW, if you are recursively reading directories, you should opendir, readdir, and closedir and entire directory before going on to the next. Ocaml makes it very easy to put the results into a list. There is a fixed number open file handles that are permitted, so nesting the opendirs will fail in the general case. Dave ------------------- 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