chacham15
chacham15

Reputation: 14251

stat not working

I am writing a file watcher and stat for some reason cant get a hold of file information, why?


struct stat info;
int fd = open(path, O_EVTONLY);
if (fd <= 0){
    exit(-1);
}
int result = fstat(fd, &info);
if (!result){
    exit(-1); //This happens! Errno says "No such file or directory" but that cant be because open would've failed
}

Upvotes: 2

Views: 1150

Answers (3)

ouah
ouah

Reputation: 145829

int result = fstat(fd, &info);
if (!result){
    exit(-1);
}

Check fstat man page, on success 0 is returned.

Upvotes: 5

wildplasser
wildplasser

Reputation: 44220

From your usage, I assume you want fstat(). fstat() takes a fd as argument, stat() a string.

Upvotes: 1

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137398

stat returns zero on success, as do most standard libc functions.

This is designed as such, so you can easily check for errors in a chain of library calls:

if (stat(fd, &info)) {
    perror("stat");
    exit(1);
}
//stat succeeded.

if (...) {

}

Upvotes: 3

Related Questions