Changing errno - legal?

This is a discussion on Changing errno - legal? within the C Programming forums, part of the General Programming Boards category; Hello all, I'm writing a library in C. I have no idea where and how it's going to be used, ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262

    Changing errno - legal?

    Hello all,

    I'm writing a library in C. I have no idea where and how it's going to be used, so it should be generic.
    Anyways, when an error occurs, I'm planning on changing errno and returning an integer indication an error.

    My first question: is this legal? It's probably going to work on most - if not all - implementations, but I'd like to know if it's actually valid C. And I don't have the C standard anywhere.

    Also, is there a portable way to add more exceptions? Is there an error code that is defined not to be used by libc?

    Thanks in advance

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by EVOEx View Post
    I'm writing a library in C. I have no idea where and how it's going to be used
    How does that work then? Surely, you're writing a library for specific purpose.
    Also it's not entirely clean *why* you would want to change errno. What's wrong with returning enumerated error codes?

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by QuantumPete View Post
    How does that work then? Surely, you're writing a library for specific purpose.
    Also it's not entirely clean *why* you would want to change errno. What's wrong with returning enumerated error codes?

    QuantumPete
    Thanks for your reply.

    The library is a big integer library. I'm not sure if it's even going to be used, but I do want it to be as good as possible. Just as a challenge.

    Because I like the libc style:
    Code:
    if(somefunction() == -1) {
    	switch(errno) {
    	case ENOMEM:
    		/* Something */
    	/* Other cases */
    	}
    }
    And it would keep a consisting coding style. I think it's quite a mess if every library you use uses its own style for indicating what went wrong.

    But, like I said, I'm not sure if it's legal.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The C99 standard specifies three values of errno (that is, it specifies three macros that have distinct values; what those values are are compiler-specific). Macro names starting with E<digit> and E<uppercase letter> are reserved to the implementation.

    As far as I'm aware, you're allowed to use errno for your own devious purposes. No one will expect you to, but hey.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by tabstop View Post
    The C99 standard specifies three values of errno (that is, it specifies three macros that have distinct values; what those values are are compiler-specific). Macro names starting with E<digit> and E<uppercase letter> are reserved to the implementation.

    As far as I'm aware, you're allowed to use errno for your own devious purposes. No one will expect you to, but hey.
    So, you think it's bad practice to do such a thing? You'd rather every library has its own error handling system? Where one returns the error code, the other requires the call to a system-specific function?

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by tabstop View Post
    As far as I'm aware, you're allowed to use errno for your own devious purposes. No one will expect you to, but hey.
    Precisely, virtually every library I've worked with returns an error code, which is enumerated in a header file somewhere. In your example, you could do:
    Code:
    int rcode = 0;
    if((rcode = somefunction()) != APINAME_SUCCESS) {
    	switch(rcode) {
    	case APINAME_NO_MEM:
    		/* Something */
    	/* Other cases */
    	}
    }
    You'd rather every library has its own error handling system? Where one returns the error code, the other requires the call to a system-specific function
    The system-specific function is only called for the standard library. You're talking about an API (which is not standard), which in almost all cases returns an error code.

    QuantumPete
    Last edited by QuantumPete; 02-27-2009 at 11:58 AM.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I would expect it's just going to be tricky to not step on error codes; errno.h on this machine (mac osx) defines errnos from 1 to 103 (it also defines the last errno as 103, but you could change that if necessary I suppose); I don't know how other systems look.

    From a purely personal standpoint, to me, errno only makes sense when the return type has only one/a limited number of possible values that could mean error (like a pointer being NULL, or a float being NaN). In your example above, why not just do something like
    Code:
    errorcode = somefunction();
    switch(errorcode)
    {
    etc.
    }
    without taking the extra step that you don't need?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. errno function, error TLS/non TLS
    By fran.b in forum C Programming
    Replies: 13
    Last Post: 03-25-2009, 08:06 AM
  2. What is legal and what is illegal?
    By dlwlsdn in forum C Programming
    Replies: 3
    Last Post: 11-14-2008, 11:48 AM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. Changing windows without changing?
    By Lionmane in forum Windows Programming
    Replies: 7
    Last Post: 10-19-2005, 11:41 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21