errno

Hey, Can I assume that for certain function calls, errno can never be set to a certain value.

More specifically, can I assume that for if the stat function call fails, the errno can never be [ENOSPC] or "No space left on device."

I am assuming that a read function cannot fail because of no space left.

Thanks !!

Interesting !

I would assume 'yes'. I don't see a reason for ENOSPC and read system call failing due to that.

Read the man page for stat/read on your system. It lists the errno values stat (or any listed syscall) can return. When you go from Linux to HPUX - as an example - you can see differences.

Linux:

HPUX

If you find a different errno value then it was set before or after your stat call.

If stat() returns -1, THEN errno will be meaningful. Some implementations of libraries don't reset errno unless there actually was an error, meaning you might get the 'leftover' error of a previous call.

What is the reason that you want such an implementation?
errno is usually set by the called function when it returns to indicate whether it failed or succeeded.
Exceptions are those functions that don't use errno but may set it unknowingly. In any case it's not advisable to assume anything.

This is a very important observation for the original poster. The errno variable may not be accurate if there are more functions being called after the error occurred.

function1(); // let's says this function fails with EPERM errno will be 1
function2(); // this function also fails with ERANGE errno will be 34
/* errno was changed twice, so if you would analyze it here not knowing function2() had failed, you might consider an error which didn't belong to function1() */
function3(); // ...