Unix Programming - open file or directory

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > May 2007 > open file or directory





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author open file or directory
Mr. X.

2007-05-29, 7:21 pm

Hello,
What is the command that is both good for file & directory
(for openning) ?
I have tried open( ...), but when I open a directory - it doesn't return an
error,
but if I do later :
fstat(fd, &buf) and check buf by : S_ISDIR(buf.st_mode),
I get always a false value.

Thanks


Darko

2007-05-29, 7:21 pm

On May 29, 11:43 pm, "Mr. X." <no_spam_please@nospam_please.com>
wrote:
> Hello,
> What is the command that is both good for file & directory
> (for openning) ?
> I have tried open( ...), but when I open a directory - it doesn't return an
> error,
> but if I do later :
> fstat(fd, &buf) and check buf by : S_ISDIR(buf.st_mode),
> I get always a false value.
>
> Thanks


Why wouldn't you first use stat() to determine the type of file, and
then use open and opendir accordingly?

Pascal Bourguignon

2007-05-30, 1:21 am

Darko <darko.maksimovic@gmail.com> writes:

> On May 29, 11:43 pm, "Mr. X." <no_spam_please@nospam_please.com>
> wrote:
>
> Why wouldn't you first use stat() to determine the type of file, and
> then use open and opendir accordingly?


Because in between the two syscalls, any other process may unlink the
file and mkdir the directory, or vice-versa, so you need to just try
to open or opendir and check the error anyways!


--
__Pascal Bourguignon__ http://www.informatimago.com/

NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.
guru

2007-05-30, 7:17 am

On May 30, 2:43 am, "Mr. X." <no_spam_please@nospam_please.com> wrote:
> Hello,
> What is the command that is both good for file & directory
> (for openning) ?
> I have tried open( ...), but when I open a directory - it doesn't return an
> error,
> but if I do later :
> fstat(fd, &buf) and check buf by : S_ISDIR(buf.st_mode),
> I get always a false value.
>
> Thanks


open(...) is for all type of file except directory (special
file)....Try opendir(), its returns DIR* for the directory opened.
This DIR* is passed to readdir() to read the content of directory.

for more info read man 3 readdir() or man 2 readdir()

Bye
Guru Jois

Joachim Schmitz

2007-05-30, 7:17 am

"guru" <guru.jois@gmail.com> schrieb im Newsbeitrag
news:1180525357.293193.197940@i13g2000prf.googlegroups.com...
> On May 30, 2:43 am, "Mr. X." <no_spam_please@nospam_please.com> wrote:
>
> open(...) is for all type of file except directory (special
> file)....Try opendir(), its returns DIR* for the directory opened.
> This DIR* is passed to readdir() to read the content of directory.
>
> for more info read man 3 readdir() or man 2 readdir()

but opendir() doesn't open files. The OP wanted to know what works for both,
files _and_ directories. Actually he asked for a command, but apparently
meant API...
However: there's no such thing.

Bye, Jojo


phil-news-nospam@ipal.net

2007-05-30, 7:17 am

On 30 May 2007 04:42:37 -0700 guru <guru.jois@gmail.com> wrote:
| On May 30, 2:43 am, "Mr. X." <no_spam_please@nospam_please.com> wrote:
|> Hello,
|> What is the command that is both good for file & directory
|> (for openning) ?
|> I have tried open( ...), but when I open a directory - it doesn't return an
|> error,
|> but if I do later :
|> fstat(fd, &buf) and check buf by : S_ISDIR(buf.st_mode),
|> I get always a false value.
|>
|> Thanks
|
| open(...) is for all type of file except directory (special
| file)....Try opendir(), its returns DIR* for the directory opened.
| This DIR* is passed to readdir() to read the content of directory.

Why do you say open() is not for directory? Do you mean it cannot be
used to open a directory? If it cannot open a directory, then how do
I get a descriptor that can be used with the fchdir() syscall?

--
|---------------------------------------/----------------------------------|
| Phil Howard KA9WGN (ka9wgn.ham.org) / Do not send to the address below |
| first name lower case at ipal.net / spamtrap-2007-05-30-0711@ipal.net |
|------------------------------------/-------------------------------------|
Geoff Clare

2007-05-30, 1:25 pm

Mr. X. wrote:

> Hello,
> What is the command that is both good for file & directory
> (for openning) ?
> I have tried open( ...), but when I open a directory - it doesn't return an
> error,
> but if I do later :
> fstat(fd, &buf) and check buf by : S_ISDIR(buf.st_mode),
> I get always a false value.


There is a bug in your code. A file descriptor returned by open() on
a directory would definitely have S_ISDIR(st_mode) true.

If you post the code (preferably as part of a complete, compilable,
example program), we can tell you where the bug is.

--
Geoff Clare <netnews@gclare.org.uk>
Mr. X.

2007-05-30, 7:20 pm

Nothing special on code,
but what may be wrong,
(because even on directory, the follows runs :
printf("file\n"); )

#include <sys/stat.h>
....

int fd;
struct stat buf;
....
if ((fd = open("my_dir", O_RDONLY) == -1))
{ ...
}
else if (fstat(fd, &buf) == -1)
{
...
}
else
{
if (S_ISDIR(buf.st_mod))
printf("directory\n");
else
printf("file\n");
}


David Schwartz

2007-05-30, 7:20 pm

On May 30, 12:53 pm, "Mr. X." <no_spam_please@nospam_please.com>
wrote:
> Nothing special on code,
> but what may be wrong,
> (because even on directory, the follows runs :


Works for me. Post complete, compilable code.

#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
if(argc==2)
{
int fd;
struct stat buf;
fd=open(argv[1], O_RDONLY);
if(fd<0) fprintf(stderr, "ouch\n");
if(fstat(fd, &buf)==-1) fprintf(stderr, "bad\n");
if(S_ISDIR(buf.st_mode)) printf("directory\n");
else printf("not directory\n");
}
}

$ ./t some_file
not directory

$ ./t some_dir
directory

DS

David Schwartz

2007-05-30, 7:20 pm

On May 30, 12:53 pm, "Mr. X." <no_spam_please@nospam_please.com>
wrote:
> Nothing special on code,
> but what may be wrong,
> (because even on directory, the follows runs :


Works for me. Post complete, compilable code.

#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
if(argc==2)
{
int fd;
struct stat buf;
fd=open(argv[1], O_RDONLY);
if(fd<0) fprintf(stderr, "ouch\n");
if(fstat(fd, &buf)==-1) fprintf(stderr, "bad\n");
if(S_ISDIR(buf.st_mode)) printf("directory\n");
else printf("not directory\n");
}
}

$ ./t some_file
not directory

$ ./t some_dir
directory

DS

Geoff Clare

2007-05-31, 1:21 pm

Mr. X. wrote:

> Nothing special on code,
> but what may be wrong,
> (because even on directory, the follows runs :
> printf("file\n"); )
>
> #include <sys/stat.h>
> ...
>
> int fd;
> struct stat buf;
> ...
> if ((fd = open("my_dir", O_RDONLY) == -1))


Misplaced parenthesis.

if ((fd = open("my_dir", O_RDONLY)) == -1)

Your code set fd to 0, not to the file descriptor returned by open().

--
Geoff Clare <netnews@gclare.org.uk>
Mr. X.

2007-05-31, 7:24 pm

Can I do open(...) to a symbol link ?

Thanks


moi

2007-05-31, 7:24 pm

On Fri, 01 Jun 2007 00:13:46 +0300, Mr. X. wrote:

> Can I do open(...) to a symbol link ?
>
> Thanks


just try.
YMMV

HTH,
AvK
David Schwartz

2007-05-31, 7:24 pm

On May 31, 2:13 pm, "Mr. X." <no_spam_please@nospam_please.com> wrote:
> Can I do open(...) to a symbol link ?
>
> Thanks


The 'open' function will follow symbolic links unless you ask it not
to. A symbolic link itself cannot be opened, as there would be nothing
useful you could do to it (except perhaps 'fstat' it).

DS

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com