anish
anish

Reputation: 1033

stat systecall in linux returning error

I am using RHEL 4

i am using syscall stat as follows:-

if (stat ("file",&stat_obj)){

     if (errno == ENOENT){
        printf("File not found");
     }else{
        printf("Unexpected error occured %d ",errno);
     }
}

sometimes i get error message as ""Unexpected error occured 0"

That means i get error as "0" . i checked file permissions that are ok

what does that mean? I am not able to understand why sometimes this is happening ?

Any suggestions?

Upvotes: 0

Views: 1182

Answers (2)

mark4o
mark4o

Reputation: 60843

Do you have a signal handler in your program? If so, and it may affect errno, then make sure that it saves errno on entry and restores it to its original value before returning.

Also ensure that you #include <errno.h>, and are not declaring errno yourself, especially if your program is multithreaded. errno is a per-thread variable so if you declare it as a global you can get the wrong one. (On some platforms you sometimes also need a special compilation flag like -D_TS_ERRNO for thread-safe errno, but no such flag is needed on Linux.)

Upvotes: 1

Lars Haugseth
Lars Haugseth

Reputation: 14881

Does it give you any meaningful error message if you call it like this?

   if (stat("file", &stat_obj) == -1) {
       perror("stat");
   }

Upvotes: 1

Related Questions