473,401 Members | 2,139 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,401 software developers and data experts.

Preserving the value of errno

Assume I'm writing a function which is going to set
the value of errno if something went wrong but I also
want to guarantee that errno will remain unchanged
if the function completed its task succesfully. So at
the beginning of my code I have something like
int errsto = errno ;
and just before every successful return I have
errno = errsto ;

Is this ok ?

Jul 30 '07 #1
13 1903
Spiros Bousbouras wrote:
Assume I'm writing a function which is going to set
the value of errno if something went wrong but I also
want to guarantee that errno will remain unchanged
if the function completed its task succesfully. So at
the beginning of my code I have something like
int errsto = errno ;
and just before every successful return I have
errno = errsto ;

Is this ok ?
Why not just set errno to your error before returning when something
goes wrong?
--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Jul 30 '07 #2
Pietro Cerutti wrote:
Spiros Bousbouras wrote:
>Assume I'm writing a function which is going to set
the value of errno if something went wrong but I also
want to guarantee that errno will remain unchanged
if the function completed its task succesfully. So at
the beginning of my code I have something like
int errsto = errno ;
and just before every successful return I have
errno = errsto ;

Is this ok ?

Why not just set errno to your error before returning when something
goes wrong?

!!!!!

Very good answer.
Jul 30 '07 #3
Pietro Cerutti wrote:
Spiros Bousbouras wrote:
>>Assume I'm writing a function which is going to set
the value of errno if something went wrong but I also
want to guarantee that errno will remain unchanged
if the function completed its task succesfully. So at
the beginning of my code I have something like
int errsto = errno ;
and just before every successful return I have
errno = errsto ;

Is this ok ?


Why not just set errno to your error before returning when something
goes wrong?

I assumed he wanted to guard against it being changed by any functions
he called in turn...
Jul 30 '07 #4
Mark Bluemel wrote:
Pietro Cerutti wrote:
>Spiros Bousbouras wrote:
>>Assume I'm writing a function which is going to set
the value of errno if something went wrong but I also
want to guarantee that errno will remain unchanged
if the function completed its task succesfully. So at
the beginning of my code I have something like
int errsto = errno ;
and just before every successful return I have
errno = errsto ;

Is this ok ?


Why not just set errno to your error before returning when something
goes wrong?

I assumed he wanted to guard against it being changed by any functions
he called in turn...
Sorry, I misread the OP's post.

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Jul 30 '07 #5
Pietro Cerutti said:
Mark Bluemel wrote:
>Pietro Cerutti wrote:
<snip>
>>Why not just set errno to your error before returning when something
goes wrong?

I assumed he wanted to guard against it being changed by any
functions he called in turn...

Sorry, I misread the OP's post.
Easily done, isn't it? <sighAh well, tomorrow is another day.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 30 '07 #6
On Mon, 30 Jul 2007 16:34:47 -0000, Spiros Bousbouras
<sp****@gmail.comwrote:
>Assume I'm writing a function which is going to set
the value of errno if something went wrong but I also
want to guarantee that errno will remain unchanged
if the function completed its task succesfully. So at
the beginning of my code I have something like
int errsto = errno ;
and just before every successful return I have
errno = errsto ;

Is this ok ?
Sure. It's not something that's often useful, but I have written
library routines that did this. You might, for example, want to
capture an error return from another function, then return either that
or some other error depending on further processing.

--
Al Balmer
Sun City, AZ
Jul 30 '07 #7
Richard Heathfield wrote:
Pietro Cerutti said:
>Mark Bluemel wrote:
>>Pietro Cerutti wrote:

<snip>
>>>Why not just set errno to your error before returning when something
goes wrong?
I assumed he wanted to guard against it being changed by any
functions he called in turn...
Sorry, I misread the OP's post.

Easily done, isn't it? <sighAh well, tomorrow is another day.
Yes it is. What's the problem?

Sorry, I don't want to embark in off-topical debates, but what's wrong
with you?

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Jul 30 '07 #8
Pietro Cerutti said:
Sorry, I don't want to embark in off-topical debates, but what's wrong
with you?
I don't know. We all have our off-days, n'est-ce-pas? Well, I suppose
that today (actually, yesterday, as of four minutes ago) just happened
to be mine.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 30 '07 #9
Richard Heathfield wrote:
Pietro Cerutti said:
>Sorry, I don't want to embark in off-topical debates, but what's wrong
with you?

I don't know. We all have our off-days, n'est-ce-pas? Well, I suppose
that today (actually, yesterday, as of four minutes ago) just happened
to be mine.
No problem with that, best wishes for tomorrow (actually, today) ;-)

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Jul 30 '07 #10
On Jul 30, 6:21 pm, Eric Sosman <Eric.Sos...@sun.comwrote:
Spiros Bousbouras wrote:
>Assume I'm writing a function which is going to set
the value of errno if something went wrong but I also
want to guarantee that errno will remain unchanged
if the function completed its task succesfully. So at
the beginning of my code I have something like
int errsto = errno ;
and just before every successful return I have
errno = errsto ;
>Is this ok ?
errno is a less-than-wonderful mechanism, agreed.
But occasionally it's useful to preserve an errno value
"just in case" it holds something that might be useful.
The O.P.'s use case doesn't make much sense to me (if
the caller cares about a pre-existing errno value, the
caller should grab it before calling other functions)...
Indeed the caller could but I consider it The Right Thing
that a function which is going to use errno should only
modify it if something went wrong. I believe it makes
things overall more tidy. I actually believe that the
standard should specify that library functions should
only modify errno if something went wrong.

Consider the Unix specific readdir() For those not familiar
with it it returns successive directory entries on a Unix
system. You first open the directory with opendir() and
then you read the files by calling readdir() again and again.
readdir() will return a NULL pointer if it encounters an error
of if there are no more files to be read. So once readdir()
returns NULL the only way to tell if it was an error or the
end of the directory is to test the value of errno. This in
turns means that if you have a loop which is meant to read
all the directory entries then you have to set errno to 0
*every* time the loop gets repeated. On the other hand if it
was guaranteed that readdir() would only modify errno if
something went wrong and your loop only called readdir() then
you would only have to set errno to 0 once before entering
the loop.

This is as an example of why I feel that a function should
only modify errno if something went wrong. It's perhaps
a stylistic preference more than anything else but I do
prefer it.

Aug 8 '07 #11
Spiros Bousbouras wrote:
On Jul 30, 6:21 pm, Eric Sosman <Eric.Sos...@sun.comwrote:
>>Spiros Bousbouras wrote:
Assume I'm writing a function which is going to set
the value of errno if something went wrong but I also
want to guarantee that errno will remain unchanged
if the function completed its task succesfully. So at
the beginning of my code I have something like
int errsto = errno ;
and just before every successful return I have
errno = errsto ;
Is this ok ?
> errno is a less-than-wonderful mechanism, agreed.
But occasionally it's useful to preserve an errno value
"just in case" it holds something that might be useful.
The O.P.'s use case doesn't make much sense to me (if
the caller cares about a pre-existing errno value, the
caller should grab it before calling other functions)...

Indeed the caller could but I consider it The Right Thing
that a function which is going to use errno should only
modify it if something went wrong. I believe it makes
things overall more tidy. I actually believe that the
standard should specify that library functions should
only modify errno if something went wrong.

Consider the Unix specific readdir() For those not familiar
with it it returns successive directory entries on a Unix
system. You first open the directory with opendir() and
then you read the files by calling readdir() again and again.
readdir() will return a NULL pointer if it encounters an error
of if there are no more files to be read. So once readdir()
returns NULL the only way to tell if it was an error or the
end of the directory is to test the value of errno. This in
turns means that if you have a loop which is meant to read
all the directory entries then you have to set errno to 0
*every* time the loop gets repeated. On the other hand if it
was guaranteed that readdir() would only modify errno if
something went wrong and your loop only called readdir() then
you would only have to set errno to 0 once before entering
the loop.
Same amount of source code in either case, just
differently positioned. Nor is there likely to be any
gain in efficiency, because the fact that your `errno = 0;'
moves out of the loop is offset by the fact that readdir()
must now preserve and restore `errno' on every call.
This is as an example of why I feel that a function should
only modify errno if something went wrong. It's perhaps
a stylistic preference more than anything else but I do
prefer it.
Your preferences are your business, of course, but they
don't happen to be enforced by the Standard. Question 12.24
in the FAQ provides a pretty good hint about why this is so.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Aug 8 '07 #12
In article <6o******************************@comcast.com>,
Eric Sosman <es*****@ieee-dot-org.invalidwrote:
>Nor is there likely to be any
gain in efficiency, because the fact that your `errno = 0;'
moves out of the loop is offset by the fact that readdir()
must now preserve and restore `errno' on every call.
Only if it does something that may change it when no error has
occurred.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Aug 8 '07 #13
On 8 Aug, 14:05, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
Spiros Bousbouras wrote:
The O.P.'s use case doesn't make much sense to me (if
the caller cares about a pre-existing errno value, the
caller should grab it before calling other functions)...
Indeed the caller could but I consider it The Right Thing
that a function which is going to use errno should only
modify it if something went wrong. I believe it makes
things overall more tidy. I actually believe that the
standard should specify that library functions should
only modify errno if something went wrong.
Consider the Unix specific readdir() For those not familiar
with it it returns successive directory entries on a Unix
system. You first open the directory with opendir() and
then you read the files by calling readdir() again and again.
readdir() will return a NULL pointer if it encounters an error
of if there are no more files to be read. So once readdir()
returns NULL the only way to tell if it was an error or the
end of the directory is to test the value of errno. This in
turns means that if you have a loop which is meant to read
all the directory entries then you have to set errno to 0
*every* time the loop gets repeated. On the other hand if it
was guaranteed that readdir() would only modify errno if
something went wrong and your loop only called readdir() then
you would only have to set errno to 0 once before entering
the loop.

Same amount of source code in either case, just
differently positioned. Nor is there likely to be any
gain in efficiency, because the fact that your `errno = 0;'
moves out of the loop is offset by the fact that readdir()
must now preserve and restore `errno' on every call.
The positioning of code is important. I believe that annoying
little details should be dealt with at the lowest level possible
unless there are other (good) reasons to do otherwise and in
this case I don't feel there are. Nor is it necessary only a
matter of code positioning. If all the library functions
guaranteed that errno would only be modified in the case of an
error then many would not have to take any explicit measures
to preserve it. The exceptions would be functions in implementations
like those mentioned in FAQ 12.24 or functions which could do
their job in more than one ways and I feel these are a minority.

Aug 10 '07 #14

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

Similar topics

0
by: Steve Juranich | last post by:
I know that there's os.strerror to get the error string, and the errno module gives me all of the mappings like 34 -> ERANGE. But I have yet to find out how to check the value of errno in a Python...
11
by: Marcus Jacobs | last post by:
Dear Group I have written a file conversion program that uses strtof to convert text strings to floats. It works as I intended except for my error messages. It is my understanding that strtof...
3
by: Mac | last post by:
Is it legal to declare errno after you've included errno.h? For example: #include<errno.h> .... int main (void) {
3
by: RoSsIaCrIiLoIA | last post by:
On Wed, 05 May 2004, CBFalconer <cbfalconer@yahoo.com> wrote: >Even if sscanf succeeds in inputting various fields, there are >probably range and other validity checks to be applied. The...
11
by: Vijay Kumar R Zanvar | last post by:
> In <pan.2004.04.22.04.06.05.969827@bar.net> "Mac" <foo@bar.net> writes: > > >Is it legal to declare errno after you've included errno.h? > > > >For example: > > > >#include<errno.h> > > >...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
5
by: Urs Beeli | last post by:
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...
3
by: sophia.agnes | last post by:
Dear all, I was going through the book "C a software engineering approach by darnell & Margolis" there was a section named sign preserving vs value preserving it is as follows sign...
22
by: viza | last post by:
Hi all, A quick one - since errno is a lvalue, can I do: fread( & errno, sizeof errno, 1, fp ) ? TIA viza
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.