Help with strerror(errno)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dissectcode
    New Member
    • Jul 2008
    • 66

    Help with strerror(errno)

    Is errno something that I have to modify, or is it automatically modified when certain commands are executed?

    So if I just did something, and then printed strerror(errno) would it know what error to print out?

    Also - what happens if I have an empty strerror() ? Will it cause an error or what is its behavoir?

    thanks!!
  • edwardrsmith
    New Member
    • Feb 2008
    • 62

    #2
    Google is your friend. I typed "strerror" into google and the first page provides a nice overview of the function and answers your question nively. Then I entered "errno" and after looking at the third link (the IBM one) I once again had my answer.

    Try Google.

    Additionally, since I believe that errno.h is a linux header file I am assuming you are using linux. If that is the case then try going to the terminal and entering "man strerror". That should tell you what you want to know about strerror. As for errno, try the same thing (that would be the "man errno" command in this case).

    Edward

    Comment

    • edwardrsmith
      New Member
      • Feb 2008
      • 62

      #3
      As a side note, I have a feeling that one of the reasons that you didn't get a response sooner is because of the incredible ease in which your questions could be answered by using google. People are more willing to help those who have done their research.

      Edward

      Comment

      • dissectcode
        New Member
        • Jul 2008
        • 66

        #4
        Originally posted by edwardrsmith
        As a side note, I have a feeling that one of the reasons that you didn't get a response sooner is because of the incredible ease in which your questions could be answered by using google. People are more willing to help those who have done their research.

        Edward

        I DID do my research in fact I spent the last two hours "googling" and reading all about it. I just had some questions in my head that are not clear to me...and I obvoiusly did not find answers to them OR I WOUDN'T HAVE POSTED THIS MESSAGE.

        Comment

        • Savage
          Recognized Expert Top Contributor
          • Feb 2007
          • 1759

          #5
          Originally posted by dissectcode
          I DID do my research in fact I spent the last two hours "googling" and reading all about it. I just had some questions in my head that are not clear to me...and I obvoiusly did not find answers to them OR I WOUDN'T HAVE POSTED THIS MESSAGE.
          Calm down.Edward didn't wanted to offend you in any way.He just told you what 'usually happens'.If you have done some research you should have explicitly said that in your post.

          Originally posted by dissectcode

          So if I just did something, and then printed strerror(errno) would it know what error to print out?

          thanks!!
          Yes,it would.


          Originally posted by dissectcode

          Also - what happens if I have an empty strerror() ? Will it cause an error or what is its behavoir?
          If NULL is passed it would show last error.

          Comment

          • edwardrsmith
            New Member
            • Feb 2008
            • 62

            #6
            Well, if you see the first link under strerror on google you will see that the method has parameters. You must pass the parameters, you may be able to pass Null but you must pass something.

            As for errno. The page on IBM's website that I referred to shows examples using strerror and errno. In fact, it talks all about error handling and how to do it. The example code it shows does not change errno, so I would say that is a good indicator you don't have to.

            If you read all my post (I don't know if you did or didn't) AND you are running linux then I would really take a look at the man pages. They do an amazing job of explaining functions and commands. Additionally, they tend to tell you what behavior to expect from different functions if NULL is passed. Even if you aren't running linux looking at the man pages on line could be a very good way to get a feel for the functions though the behavior could change drastically under windows.

            Edward

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              dissectcode:

              errno and strerror() are both part of Standard C (they are not limited to only Linux). Why don't you tell us a little of what you've learned about them from your research. For instance,
              > What header file(s) need to be included for them to work properly?
              > How do you use errno to determine whether or not there has been an error?
              > If there was an error, how do you use errno to determine what the error was?
              > In what manner does strerror() provide the error message text?

              My guess is that you're trying this in a single-threaded application. FYI, in a multi-threaded application using errno gets a lot trickier. If you have lots of threads running simultaneously, and they're all calling library functions, then it is very difficult for you to know which thread (and hence which library function) put the error code in errno. Be glad you don't have to worry about that!

              Good luck,
              Don

              Comment

              • dissectcode
                New Member
                • Jul 2008
                • 66

                #8
                [QUOTE=donbock]dissectcode:
                Why don't you tell us a little of what you've learned about them from your research. For instance,
                > What header file(s) need to be included for them to work properly?
                > How do you use errno to determine whether or not there has been an error?
                > If there was an error, how do you use errno to determine what the error was?
                > In what manner does strerror() provide the error message text?

                ok just because you need proof that I DID my research I am answering this without even going back to google for this information:
                1. the errno.h header file is needed to use errno and string.h is needed for strerror()
                2. to use errno, you can print it in a printf statement usign %s format specifier. i am sure you can also use that pointer that was returned to print out custom errors, or do something as a result.
                3. you can evaluate the error code by matching the pointer that was returned, to the list of error codes that C supplies (specific for strerror i guess?)
                4. sterror() provides the pointer to the error from the list.

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  Thanks, I didn't want to get in trouble with the moderator.

                  Originally posted by dissectcode
                  2. to use errno, you can print it in a printf statement usign %s format specifier. i am sure you can also use that pointer that was returned to print out custom errors, or do something as a result.
                  Actually, errno has type int, so you should use "%d" format in printf.

                  Originally posted by dissectcode
                  3. you can evaluate the error code by matching the pointer that was returned, to the list of error codes that C supplies (specific for strerror i guess?)
                  As mentioned above, errno has type int so don't compare it to any pointers. The C Standard says "The value of errno is zero at program startup, but is never set to zero by any library function."

                  Originally posted by dissectcode
                  4. sterror() provides the pointer to the error from the list.
                  The function prototype for strerror() is
                  Code:
                  char *strerror(int errnum);
                  so you should be able to do something like this...
                  Code:
                  if (errno != 0) {
                      printf("%s\n",strerror(errno));
                      errno = 0;
                      }
                  Your compiler might implement errno as a global variable, but it also might do something extremely tricky. However your compiler implements errno, the C Standard requires that errno "expands to a modifiable lvalue". A 'modifiable lvalue' is something that can go on the left side of an equals sign, that's why you're able to reset errno to zero.

                  The type of errno, the fact that it starts at zero and gets set to various nonzero values by errors, and that it is a modifiable lvalue should have been explained in the man page for errno and/or for errno.h.

                  Cheers,
                  Don

                  Comment

                  • donbock
                    Recognized Expert Top Contributor
                    • Mar 2008
                    • 2427

                    #10
                    Originally posted by dissectcode
                    Also - what happens if I have an empty strerror() ? Will it cause an error or what is its behavoir?
                    If by "empty strerror()" you mean that you don't pass any argument, then you should get a compiler error if the strerror() prototype is in scope.

                    If by "empty strerror()" you mean the argument you pass is errno but no error has occurred yet (ie, errno is still zero), then I'm not sure what happens since the C Standard is silent on this detail. Since zero is a well-known value for errno it seems reasonable that strerror() would return some sort of "No error" message. You can avoid this issue by only callling strerror() when errno is nonzero.

                    If by "empty strerror()" you mean the argument you pass is errno but it has some garbage nonzero value that is unknown to strerror, then I'm not sure what happens since the C Standard is silent on this detail. I would be disappointed in the compiler-writer if strerror() did something stupid in this case, it seems reasonable that it would return some sort of "Unknown error" message. You can avoid this issue by being careful to never store any value into errno other than zero.

                    If by "empty strerror()" you mean the argument passed is NULL, then there would be a compiler error if the prototype was in scope because NULL isn't a suitable int.

                    Cheers,
                    Don

                    Comment

                    • dissectcode
                      New Member
                      • Jul 2008
                      • 66

                      #11
                      Originally posted by donbock
                      If by "empty strerror()" you mean that you don't pass any argument, then you should get a compiler error if the strerror() prototype is in scope.

                      If by "empty strerror()" you mean the argument you pass is errno but no error has occurred yet (ie, errno is still zero), then I'm not sure what happens since the C Standard is silent on this detail. Since zero is a well-known value for errno it seems reasonable that strerror() would return some sort of "No error" message. You can avoid this issue by only callling strerror() when errno is nonzero.

                      If by "empty strerror()" you mean the argument you pass is errno but it has some garbage nonzero value that is unknown to strerror, then I'm not sure what happens since the C Standard is silent on this detail. I would be disappointed in the compiler-writer if strerror() did something stupid in this case, it seems reasonable that it would return some sort of "Unknown error" message. You can avoid this issue by being careful to never store any value into errno other than zero.

                      If by "empty strerror()" you mean the argument passed is NULL, then there would be a compiler error if the prototype was in scope because NULL isn't a suitable int.

                      Cheers,
                      Don

                      Quick question: If I don't pass an argument ie strerror(); isn't what is passed technically NULL?
                      thanks!

                      Comment

                      • arnaudk
                        Contributor
                        • Sep 2007
                        • 425

                        #12
                        Originally posted by dissectcode
                        Quick question: If I don't pass an argument ie strerror(); isn't what is passed technically NULL?
                        thanks!
                        No. If a function expects an argument then you have to give it one (unless there is an overloaded version which takes no arguments which is possible in C++ but not C).

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by donbock
                          If by "empty strerror()" you mean the argument you pass is errno but it has some garbage nonzero value that is unknown to strerror, then I'm not sure what happens since the C Standard is silent on this detail. I would be disappointed in the compiler-writer if strerror() did something stupid in this case, it seems reasonable that it would return some sort of "Unknown error" message. You can avoid this issue by being careful to never store any value into errno other than zero.
                          The C99 Standard is not completely silent about any arbitrary value:

                          Originally posted by C99
                          [#2] The strerror function maps the number in errnum to a
                          message string. Typically, the values for errnum come from
                          errno, but strerror shall map any value of type int to a
                          message.
                          Read the last phrase of the last sentence.

                          kind regards,

                          Jos

                          Comment

                          Working...