From: Holger Kiehl <Holger.Kiehl@dwd.de>
To: Glynn Clements <glynn@gclements.plus.com>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: Testing if a file or directory exist
Date: Mon, 10 Sep 2007 15:39:44 +0000 (GMT) [thread overview]
Message-ID: <Pine.LNX.4.64.0709101531480.17374@diagnostix.dwd.de> (raw)
In-Reply-To: <18147.61965.904640.471967@cerise.gclements.plus.com>
On Sun, 9 Sep 2007, Glynn Clements wrote:
>
> Holger Kiehl wrote:
>
>> 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. The code
>> for the test programms is shown below.
>>
>> The question I have is there any other system call that I can use
>> that would be cheaper then access(). Even if they are linux specific
>> system calls I would like to know.
>>
>> Thanks,
>> Holger
>>
>> access.c
>>
>> #include <stdio.h>
>> #include <string.h>
>> #include <unistd.h>
>> #include <errno.h>
>>
>> #define MAX_LOOPS 5000000
>>
>> int main(void)
>> {
>> unsigned int i;
>>
>> for (i = 0; i < MAX_LOOPS; i++)
>> {
>> if (access("abcd", R_OK) != 0)
>
> If you just want to check for existence, use F_OK. If you use R_OK,
> the call will fail if you don't have read permission for the file.
>
Right. I thought F_OK was just for files and not directories, but that is
wrong, a directory is also just a file. Also, testing showed that F_OK is
quicker then R_OK.
Thanks a lot for the advice!
Holger
prev parent reply other threads:[~2007-09-10 15:39 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
2007-09-10 18:01 ` Glynn Clements
2007-09-09 13:15 ` Glynn Clements
2007-09-10 15:39 ` Holger Kiehl [this message]
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.0709101531480.17374@diagnostix.dwd.de \
--to=holger.kiehl@dwd.de \
--cc=glynn@gclements.plus.com \
--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).