Re: C++ and S_ISDIR() problem
Bernhard Rosenkraenzer <[email protected]> Sun, 13 Nov 2005 14:02:22 +0100
| Newsgroups | gmane.linux.arklinux.devel |
|---|---|
| Message-ID | <[email protected]> |
On Saturday, 12. November 2005 21:37, N. Thompson wrote:
> I'm trying to write a program that goes through folders recursively and
> lists what it finds in a specific format; however, I am having very strange
> problems with it in Ark Linux and I'm wondering if the problem is with AL,
> or if the posix headers have a major bug.
Probably neither -- if this didn't work correctly, you'd almost certainly see
very odd stuff happening in KDE (KDE/Qt use S_ISDIR internally).
S_ISDIR and friends are not exactly an intuitive API though... The POSIX guys
unfortunately don't have Qt's or KDE's API designers ;)
> The problem so far is that S_ISDIR(...) is returning true in many cases on
> plain text files. This is my absolute first time using POSIX libraries in a
> program so I'm not even sure if I'm doing it right.
Chances are you aren't.
> I'll send the source code to anyone who thinks they can help on the
> understanding
Please do -- or maybe this will help: here's an example of using it correctly:
extern "C" {
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
}
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
if(argc<2) {
cerr << "Usage: " << argv[0] << " filename [filename] [filename] ....." <<
endl;
return 1;
}
for(int i=1; i<argc; i++) {
struct stat s;
if(stat(argv[i], &s))
cerr << argv[i] << ": " << strerror(errno) << endl;
else {
cerr << argv[i] << " is ";
if(!S_ISDIR(s.st_mode))
cerr << "not ";
cerr << "a directory" << endl;
}
}
}