If stat(2) does not return an error,
this means that the file does exist, but you still
have to check if the file is a directory and if
you will be able to write on it. On a successful
return stat(2) will fill a structure of
type stat that corresponds to the
file name given to stat(2) as the first
argument. The st_mode field of this
structure can be used to test if the file in
question
is a directory by calling a macro
S_ISDIR,
which is defined on /usr/include/sys/stat.h.
For example,
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
...
char *file_name;
struct stat buf;
...
if (stat(file_name, &buf) == 0) {
if (S_ISDIR(buf.st_mode)) {
/* The file in question is a directory. */
...
} else {
/* The file in question is not a directory. */
...
}
} else {
/* The file in question probably does not exist or it cannot
be queried. Check errno. */
...
}