473,461 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Setting errno

If I am writing a function that sets errno according to a possible
error condition, should it also set errno to 0 if it executes
normally, or should it leave errno alone on success?

Nov 14 '05 #1
4 8989
In <4r********************************@4ax.com> Paul Emmons <pe*****@voicenet.com> writes:
If I am writing a function that sets errno according to a possible
error condition, should it also set errno to 0 if it executes
normally, or should it leave errno alone on success?


Leave it alone on success. If the function reports successful completion,
the programmer has no business at all to look at errno.

Strictly speaking, you can do anything you want with errno in case of
success, but it's common practice to preserve its value in case of
success.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #2
Paul Emmons wrote:
If I am writing a function that sets errno according to a possible
error condition, should it also set errno to 0 if it executes
normally, or should it leave errno alone on success?


Your choice, really. No Standard library function ever sets
`errno' to zero (7.5/3), so if you want to follow their precedent
you won't do so either.

Note, also, that most library functions do not use `errno' as
a failure indicator, but to convey additional information about a
failure reported by other means. A library function may change
the value of `errno' even if it succeeds (same reference).

Finally, I would discourage you from using `errno' this way.
There are only three Standard-required Exxxx macros, so if you
try to use others you run into portability problems. And there's
no mechanism for extending the implementation's `errno' codes with
your own: Quite aside from the problem of finding a value that hasn't
already been given some other meaning, there's the issue of making
your new codes known to strerror() and perror(). Even for the codes
defined by the Standard and the implementation, `errno' is a pretty
puny interface: limited bandwidth on fragile underpinnings. You
can almost certainly do better.

--
Er*********@sun.com

Nov 14 '05 #3
>> If I am writing a function that sets errno according to a possible
error condition, should it also set errno to 0 if it executes
normally, or should it leave errno alone on success?


Your choice, really. No Standard library function ever sets
`errno' to zero (7.5/3), so if you want to follow their precedent
you won't do so either.

Note, also, that most library functions do not use `errno' as
a failure indicator, but to convey additional information about a
failure reported by other means. A library function may change
the value of `errno' even if it succeeds (same reference).

Finally, I would discourage you from using `errno' this way.
There are only three Standard-required Exxxx macros, so if you
try to use others you run into portability problems. And there's
no mechanism for extending the implementation's `errno' codes with
your own: Quite aside from the problem of finding a value that hasn't
already been given some other meaning, there's the issue of making
your new codes known to strerror() and perror(). Even for the codes
defined by the Standard and the implementation, `errno' is a pretty
puny interface: limited bandwidth on fragile underpinnings. You
can almost certainly do better.


Consider using the com_err library. It is present in a number of
BSD Unix distributions, and although it seems to make a few unportable
assumptions (like the C-implementation-defined values of errno are
"small", as in having values limited to between 1 and 255, inclusive),
it's reasonably portable.

com_err allows independent parts of a program to register their own
set of error codes (one "set" of error codes is 256 of them; but
nothing prevents one module from registering and using more than
one "set" if it needs them. There are something like 2^24 "sets"),
and for the caller of a function to turn that error code into text
even if it does not know about the error code or even what sub-part
of the program it came from. C-implementation-defined error codes
used with errno, perror(), and strerror() are also pre-registered.
If the C-implementation-defined error codes are not within the
assumed range, and you know what that range is, it isn't difficult
to arrange to pre-register them with a code change to com_err
specific to your implementation. Or, maybe you don't even care
about pre-registering the C-implementation-defined error codes.

The com_err library does *NOT* standardize the use of errno. Most
of the functions I've seen using it return the error code or 0 for
success, (which is thread-safe, reentrant, avoids issues of leftover
previous errors that make no sense in the current context, and is
generally better style), rather than using a global variable. But
if you want to use it with errno or return it in some other global,
it works. It's up to you to figure out where the error code is
(errno, h_errno, return from a function, etc.) and feed it to
com_err(), the analog of perror(), or error_message(), the analog
of strerror().

com_err comes with a compiler that turns a file full of error message
symbols and text into C code which you then compile and put in your
program.

Gordon L. Burditt
Nov 14 '05 #4
On Thu, 13 May 2004 12:51:57 -0400, Eric Sosman <Er*********@sun.com>
wrote:
Finally, I would discourage you from using `errno' this way.
There are only three Standard-required Exxxx macros, so if you
try to use others you run into portability problems. And there's
no mechanism for extending the implementation's `errno' codes with
your own: Quite aside from the problem of finding a value that hasn't
already been given some other meaning, there's the issue of making
your new codes known to strerror() and perror(). Even for the codes
defined by the Standard and the implementation, `errno' is a pretty
puny interface: limited bandwidth on fragile underpinnings. You
can almost certainly do better.


Thank you! I'm aware that it is not one of the more admired features
of the language, and I very often do have my functions return their
own error codes according to a set of my own (0 for success, or a
negative value indicating a particular kind of error). I wouldn't
try to invent my own codes for errno, and use them only for a few
functions that are similar to library functions setting ERRNO, and
when one of the standard codes is appropriate, such as
ERANGE or EINVAL.

Nov 14 '05 #5

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

Similar topics

4
by: Richard Tobin | last post by:
In a library I am writing, I want to use an errno-like mechanism for error returns. The error would probably be represented as a struct rather than just an integer. I don't have any...
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> > > >...
6
by: Michael McGarry | last post by:
Hi, How do I interpret errno from fopen()? errno = 24 in my case. Thanks for any help, Michael
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: eyalc1978 | last post by:
Hi Does someone knows what does errno 72 means I got this error when fwriting a structure and still the file is being created. I saw something on the net saying that this is caused when trying to...
13
by: Spiros Bousbouras | last post by:
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...
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
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
1
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.