From: Holger Kiehl <Holger.Kiehl@dwd.de>
To: linux-c-programming@vger.kernel.org
Subject: Re: Testing if a file or directory exist
Date: Mon, 10 Sep 2007 15:53:15 +0000 (GMT) [thread overview]
Message-ID: <Pine.LNX.4.64.0709101540300.17374@diagnostix.dwd.de> (raw)
In-Reply-To: <Pine.LNX.4.64.0709090605220.31293@localhost.localdomain>
On Sun, 9 Sep 2007, Robert P. J. Day wrote:
> On Sun, 9 Sep 2007, Holger Kiehl wrote:
>
>> Hello
>>
>> What is the quickest way to test if a file or directory exist. I can
>> think of three different system calls that can be used: access(),
>> stat() and open(). Writting a little test program I found that this
>> is also the order of which is the quickest, that is access() is the
>> quickest and open() the slowest.
>
> if all you want to do is check for existence, then, execution time
> notwithstanding, you should use the method which accomplishes that and
> nothing more, so the obvious solution would be stat().
>
That is what I first thought as well. But I think the problem is that
stat() needs to fill up the structure with all the data, is what takes
most the time. So I thought calling stat() as follows would solve it:
stat("abcd", NULL)
But that would fail with EFAULT (Bad address). I wonder why this is
the case, because then I assume it should be as quick as access("abcd", F_OK)
or maybe even quicker. By providing NULL as argument I tell the function
not to fillup the structure and just test if the file exist. Most proberly
POSIX defines it that way.
Holger
next prev parent reply other threads:[~2007-09-10 15:53 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-09-09 9:26 Testing if a file or directory exist Holger Kiehl
2007-09-09 10:07 ` Robert P. J. Day
2007-09-09 10:16 ` Benoit Rouits
2007-09-09 15:04 ` LDB
2007-09-10 15:53 ` Holger Kiehl [this message]
2007-09-10 18:01 ` Glynn Clements
2007-09-09 13:15 ` Glynn Clements
2007-09-10 15:39 ` Holger Kiehl
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Pine.LNX.4.64.0709101540300.17374@diagnostix.dwd.de \
--to=holger.kiehl@dwd.de \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).