473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

errno questions (leading to a slightly OT follow-up question)

I have a question regarding errno. If I understand it correctly,
including <errno.h> allows me to check "errno" for error values
that some standard library functions may set.

This brings up some questions:

- As I understand it, most functions only set errno in an error
case. I take this to mean, that on success, errno is not
necessarily set to EOK and I would either need to set errno
to EOK before calling the function in question to determine
its outcome or I would need to check the return value first,
and then in an error case inspect errno to get more details.
Is this understanding correct?

- Am I allowed to modify errno in my own functions as well? I
would say yes, but am not sure.

- Assuming I may modify errno in my own functions, I guess it
would also be ok for me to set it to EOK on success to allow
someone to determine the success/failure of my function by
purely inspecting the return value (I would probably tend
to just return errno at the end of the function, so both
approaches would work). Is this true?

- I am assuming that errno is a global integer value. Is this
correct?

<OT>
And the last question leads me to the announced off-topic follow-up
question:

- if errno is implemented with a global variable, how can any
multi-threaded application (and I know that this is beyond
the C standard) such as a POSIX implementation support standard
functions that make use of errno?
- is there an obvious trick on how to implement errno differently
that I am missing?
- or can those functions still be used but errno may not be
evaluated safely?
- or does POSIX redefine the C standard library to not make
use of errno
- any other solution/situation I did not think of?
</OT>

Looking forward to enlightment
/urs
--
"Change is inevitable, except from a vending machine."
-- Urs Beeli, <usenet@CONCATENATE MY FIRST AND LAST NAME.ch>
Mar 9 '06 #1
5 2333

Urs Beeli wrote:
I have a question regarding errno. If I understand it correctly,
including <errno.h> allows me to check "errno" for error values
that some standard library functions may set.

This brings up some questions:

- As I understand it, most functions only set errno in an error
case. I take this to mean, that on success, errno is not
necessarily set to EOK and I would either need to set errno
to EOK before calling the function in question to determine
its outcome or I would need to check the return value first,
and then in an error case inspect errno to get more details.
Is this understanding correct?

At the start of program errno is set to zero then it can be modified by
any function
All the library functions that modify errno - set "errno" to non zero
int value
- Am I allowed to modify errno in my own functions as well? I
would say yes, but am not sure.
you can definitely modify errno

- Assuming I may modify errno in my own functions, I guess it
would also be ok for me to set it to EOK on success to allow
someone to determine the success/failure of my function by
purely inspecting the return value (I would probably tend
to just return errno at the end of the function, so both
approaches would work). Is this true?

actually errno has predefined meanings defined in errno.h and usually
we determine the kind of error by using functions :

void perror(const char *s);

So if EOK is defined then surely u can use it
- I am assuming that errno is a global integer value. Is this
correct?
yes its a global identifier

<OT>
And the last question leads me to the announced off-topic follow-up
question:

- if errno is implemented with a global variable, how can any
multi-threaded application (and I know that this is beyond
the C standard) such as a POSIX implementation support standard
functions that make use of errno?
- is there an obvious trick on how to implement errno differently
that I am missing?
- or can those functions still be used but errno may not be
evaluated safely?
- or does POSIX redefine the C standard library to not make
use of errno
- any other solution/situation I did not think of?
</OT>
may be we need to lock the portion of code where we are querying errno.

Looking forward to enlightment
/urs
--
"Change is inevitable, except from a vending machine."
-- Urs Beeli, <usenet@CONCATENATE MY FIRST AND LAST NAME.ch>


Mar 9 '06 #2
Urs Beeli <in*****@invalid.invalid> writes:
- As I understand it, most functions only set errno in an error
case. I take this to mean, that on success, errno is not
necessarily set to EOK and I would either need to set errno
to EOK before calling the function in question to determine
its outcome or I would need to check the return value first,
and then in an error case inspect errno to get more details.
Is this understanding correct?
First, there's no (portable) constant EOK. An errno of 0
indicates a lack of error status.

Your questions are answered directly by the text of the standard:

The value of errno is zero at program startup, but is never
set to zero by any library function.170) The value of errno
may be set to nonzero by a library function call whether or
not there is an error, provided the use of errno is not
documented in the description of the function in this
International Standard.
- Am I allowed to modify errno in my own functions as well? I
would say yes, but am not sure.
Yes, you may modify errno.
- Assuming I may modify errno in my own functions, I guess it
would also be ok for me to set it to EOK on success to allow
someone to determine the success/failure of my function by
purely inspecting the return value (I would probably tend
to just return errno at the end of the function, so both
approaches would work). Is this true?
It would be more consistent with the standard library for your
function to only set errno if it has some kind of status to
report, typically an error. It would be quite unlike the
standard library for your function to set errno to 0; it would be
more normal for its caller to do so.

By the way, I'd only use errno for reporting errors if you're
passing along errors similar to those already reported by
standard library functions. Otherwise, it's probably better not
to use global data to report your error.
- I am assuming that errno is a global integer value. Is this
correct?
From a standard C perspective, yes. It's not necessarily a
variable, though; it could also be a macro.
<OT>
- if errno is implemented with a global variable, how can any
multi-threaded application (and I know that this is beyond
the C standard) such as a POSIX implementation support standard
functions that make use of errno?
- is there an obvious trick on how to implement errno differently
that I am missing?


It's only obvious once you've seen it:
#define errno (*__errno_function ())
where __errno_function() returns a thread-specific pointer.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Mar 9 '06 #3
On 2006-03-09, Urs Beeli <in*****@invalid.invalid> wrote:
<OT>
And the last question leads me to the announced off-topic follow-up
question:

- if errno is implemented with a global variable, how can any
multi-threaded application (and I know that this is beyond
the C standard) such as a POSIX implementation support standard
functions that make use of errno?
It could be a global variable in thread-local storage, or a macro
expanding to a call to a function that returns a location in such
storage.

My system's <errno.h>
int * __error(void);
#define errno (* __error())
- is there an obvious trick on how to implement errno differently
that I am missing?
- or can those functions still be used but errno may not be
evaluated safely?
- or does POSIX redefine the C standard library to not make use of
errno
- any other solution/situation I did not think of?
</OT>

Mar 9 '06 #4
> > <OT>
And the last question leads me to the announced off-topic follow-up
question:

- if errno is implemented with a global variable, how can any
multi-threaded application (and I know that this is beyond
the C standard) such as a POSIX implementation support standard
functions that make use of errno?


It could be a global variable in thread-local storage, or a macro
expanding to a call to a function that returns a location in such
storage.

My system's <errno.h>
int * __error(void);
#define errno (* __error())


Yup. Exactly.

Mar 9 '06 #5
On Thu, 09 Mar 2006 07:26:35 -0800 Ben Pfaff wrote:
Urs Beeli <in*****@invalid.invalid> writes:

First, there's no (portable) constant EOK. An errno of 0
indicates a lack of error status.
Oops, I had not realised it wasn't standardised. Thanks for the
hint.
Your questions are answered directly by the text of the standard:

The value of errno is zero at program startup, but is never
set to zero by any library function.170) The value of errno
may be set to nonzero by a library function call whether or
not there is an error, provided the use of errno is not
documented in the description of the function in this
International Standard.
Thanks.
Yes, you may modify errno.
- Assuming I may modify errno in my own functions, I guess it
would also be ok for me to set it to EOK on success to allow
someone to determine the success/failure of my function by
purely inspecting the return value (I would probably tend
to just return errno at the end of the function, so both
approaches would work). Is this true?
It would be more consistent with the standard library for your
function to only set errno if it has some kind of status to
report, typically an error. It would be quite unlike the
standard library for your function to set errno to 0; it would be
more normal for its caller to do so.


Thanks, that makes sense and I will map the behaviour of my functions
to those of the standard library.
By the way, I'd only use errno for reporting errors if you're
passing along errors similar to those already reported by
standard library functions. Otherwise, it's probably better not
to use global data to report your error.


Obviously :-)
<OT>
- if errno is implemented with a global variable, how can any
multi-threaded application (and I know that this is beyond
the C standard) such as a POSIX implementation support standard
functions that make use of errno?
- is there an obvious trick on how to implement errno differently
that I am missing?


It's only obvious once you've seen it:
#define errno (*__errno_function ())
where __errno_function() returns a thread-specific pointer.


Ah, that makes sense. Thanks. I had been sure that there must be a trick
to make it work :-)

Thanks, Ben and all the other posters, for your help.

Cheers
/urs

--
"Change is inevitable, except from a vending machine."
-- Urs Beeli, <usenet@CONCATENATE MY FIRST AND LAST NAME.ch>
Mar 10 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Faheem Mitha | last post by:
Hi, I'm not sure what would be more appropriate, so I'm ccing it to both alt.comp.lang.learn.c-c++ and comp.lang.python, with followup to alt.comp.lang.learn.c-c++. While working with a...
3
by: K Finegan | last post by:
Hello All, I'm trying to find out exactly what JOIN does eg. SELECT A.Name FROM Author A JOIN Publisher P ON A.SomeID = P.SomeID WHERE P.Country = 'X'
4
by: TR | last post by:
I have an asp:Panel that contains a variety of controls, including checkboxes and HtmlGenericControls. I can find the panel in codebehind using FindControl(id), but then I want to either: a)...
4
by: Earl | last post by:
Is it poor practice to have one custom library depend on another custom library? For example, my data manipulation library depends on my replication library, which in turn pulls serial data out of...
0
by: Gregory Piñero | last post by:
Hey Folks, Some import questions that a search didn't turn up for me. 1. Will "from somemodule import onething" take as long to start up as import somemodule? 2. Is there anyway I can get at...
2
by: rbg | last post by:
You are right, I did not include the exact query since it has a whole of joins and many where clauses in it. I did not want to make the post very hard to read, hence I simplified it. In the...
6
by: clmp75 | last post by:
so im editing this file that looks sort of like this: #include "myH.h" void methodA(){ ..... } #ifdef PartA{ void Method1(){
3
by: pauldepstein | last post by:
I am using the latest version of Visual c++ on windows XP. I am trying to build a project and get a lnk1104 error regarding a file of the form .../../ ProjectName.lib No other errors, just this...
3
by: AlfredE | last post by:
The perl::ftp's mdtm method returns the last mod date of a specified remote file. We've found that on our sftp server, the last mod date is often earlier than the date and time the file is actually...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.