473,734 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ 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 9015
In <4r************ *************** *****@4ax.com> Paul Emmons <pe*****@voicen et.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*********@su n.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
1555
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 multi-threaded programs, but others may who want to use my library. Most likely they'll be using a pthreads-compatible threads system. How can I best accommodate them? Presumably they will want the error object to be in per-thread storage, so to set and...
3
2435
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
3847
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 >interactive programmers work is never done :-) in how I see it errno (or errno like) would have 32 states 0 /* No error */ 1u, 2u, 4u, 8u, 16u, 32u, 64u, 128u, 256u, 512u, 1024u, 2048u 4096u, 8192u, 16384u, 32768u, 65536u, 131072u, 262144u,
11
2528
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
20199
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
2364
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 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...
3
3390
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 access different file system
13
1937
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 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 ?
22
2499
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
9310
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9236
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9182
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.