linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Oleg O. Ossovitskii" <oleg@kgpa.ru>
To: xmp <xmp@multipasto.net.co>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: Misc C question
Date: Thu, 20 Jun 2002 09:28:23 +0300	[thread overview]
Message-ID: <1293884575.20020620092823@kgpa.ru> (raw)
In-Reply-To: <20020619214332.A299@nietzsche>

Hello xmp,

Thursday, June 20, 2002, 5:46:02 AM, you wrote:

x> hi, What is the correct unix-style way to check if a file exists ? create it ? and delete it ?.
x> Maybe trying to fopen() it and if call != NULL it exist, else it doesnt?.
x> I want to know the most standard and used in system such as FreeBSD and solaris, linux.

You should use stat() function:

=========================================================================
stat()
get information about a file or directory

Synopsis:
#include <sys/stat.h>
int stat( const char *path, struct stat *buf );
Description:
The stat() function obtains information about the file or directory referenced in path. This information is placed in the structure located at the address indicated by buf. 


--------------------------------------------------------------------------------
 This function follows symbolic links and gives you information about the resulting file or directory. If you want information about the link itself, use lstat() instead. 

--------------------------------------------------------------------------------

The file <sys/stat.h> contains definitions for the structure stat. 

At least the following macros are defined in the <sys/stat.h> header file: 

S_ISBLK(m) 
Test for block special file. 
S_ISCHR(m) 
Test for character special file. 
S_ISDIR(m) 
Test for directory file. 
S_ISFIFO(m) 
Test for FIFO. 
S_ISREG(m) 
Test for regular file. 
The value m supplied to the macros is the value of the st_mode field of a stat structure. The macro evaluates to a nonzero value if the test is true, and zero if the test is false. 

The access permissions for the file or directory are specified as a combination of bits in the st_mode field of a stat structure. These bits are defined in the <sys/stat.h> header file, and are described in the section on this file in the Header Files chapter. The following bits are also encoded in the st_mode field: 

S_ISUID 
Set user ID on execution. The process's effective user ID is set to that of the owner of the file when the file is run as a program. On a regular file, this bit should be cleared on any write. 
S_ISGID 
Set group ID on execution. Set effective group ID on the process to the file's group when the file is run as a program. On a regular file, this bit should be cleared on any write. 
Returns:
0 
the information was successfully obtained. 
-1 
the information wasn't successfully obtained. 
Errors:
EACCES 
Search permission is denied for a component of path. 
EIO 
A physical error occurred on the block device. 
ENAMETOOLONG 
The argument path exceeds PATH_MAX in length, or a pathname component is longer than NAME_MAX. These manifests are defined in the <limits.h> header file. 
ENOENT 
The named file doesn't exist, or path is an empty string. 
ENOTDIR 
A component of path isn't a directory. 
Examples:
#include <stdio.h>
#include <sys/stat.h>

void main()
  {
    struct stat buf;

    if( stat( "file", &buf ) != -1 ) {
      printf( "File size = %d\n", buf.st_size );
    }
  }

===================================================================================


Best regards, Oleg O. Ossovitskii
programming engineer, KGPA Ltd.
tel: +7(0112)46-23-40, fax: +7(0112)43-64-96
mailto:oleg@kgpa.ru, icq# 33366588


  parent reply	other threads:[~2002-06-20  6:28 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-06-20  2:46 Misc C question xmp
2002-06-20  3:10 ` mike
2002-06-20  4:44   ` xmp
2002-06-20  5:08     ` mike
2002-06-20 12:46       ` William N. Zanatta
2002-06-20  6:28 ` Oleg O. Ossovitskii [this message]
2002-06-20  8:05   ` Mohammed Khalid Ansari
2002-06-20  8:51     ` Re[2]: " Oleg O. Ossovitskii
2002-06-20 13:23 ` Glynn Clements
2002-06-20 20:42   ` GOMEZ NOGUERA DAVIDEDUARDO
2002-06-20 21:53     ` James Stevenson
2002-06-20 23:24     ` Glynn Clements
2002-06-21  0:07       ` Christopher Quinn
2002-06-20 23:24     ` Glynn Clements
2002-06-20 18:09 ` James Stevenson

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=1293884575.20020620092823@kgpa.ru \
    --to=oleg@kgpa.ru \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=xmp@multipasto.net.co \
    /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).