{"id":4981,"date":"2021-09-25T00:01:24","date_gmt":"2021-09-25T07:01:24","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4981"},"modified":"2021-09-18T10:16:55","modified_gmt":"2021-09-18T17:16:55","slug":"the-strerror-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4981","title":{"rendered":"The <em>strerror()<\/em> Function"},"content":{"rendered":"<p>System errors happen. Your program accesses the operating system and . . . something goes wrong. When it does, the function returns -1 and your code must rely upon our old buddy <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1735\"><em>errno<\/em><\/a> to discover what went wrong and possibly output an informative error message.<br \/>\n<!--more--><br \/>\nFor example, you use the <em>rename()<\/em> function to rename a file. A value other than 0 (success) is returned. The function&#8217;s <em>man<\/em> page says, &#8220;the global variable <em>errno<\/em> indicates the reason for the failure.&#8221; So your code sifts through the value present in <em>errno<\/em> to determine the cause: File Not Found, Permission Denied, and so on.<\/p>\n<p>Working with <em>errno<\/em> is the handy <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1740\"><em>perror()<\/em> function<\/a>, or as the French call it, <em>le Pierre()<\/em> function. It outputs a default message based on the <em>errno<\/em> error code, as well as an optional string of text that may help the user further understand the problem. For example:<\/p>\n<p><code>perror(\"File renaming error\");<\/code><\/p>\n<p>This text is prepended to the system error message text, as in:<\/p>\n<p><code>File renaming error: No such file or directory<\/code><\/p>\n<p>The full message is output to the standard error device, <em>stderr<\/em>.<\/p>\n<p>The <em>perror()<\/em> function has a sibling, <em>strerror()<\/em>. Here&#8217;s its <em>man<\/em> page format:<\/p>\n<p><code>char *strerror(int errnum);<\/code><\/p>\n<p>Given an integer value, <code>errnum<\/code>, the <em>strerror()<\/em> function returns a string describing the system error message. Like <em>perror()<\/em>, this function is prototyped in the <code>stdio.h<\/code> header file.<\/p>\n<p>Valid values for <code>errnum<\/code> range from 1 through <em>sys_nerr<\/em>, a system variable. Values out of range generate the message <code>Undefined error:<\/code> followed by the invalid integer.<\/p>\n<p>When I first learned of the range 1 through <em>sys_nerr<\/em>, I immediately wrote a program that outputs all the system error messages. Here&#8217;s the code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_09_25-Lesson.c\" rel=\"noopener\" target=\"_blank\">2021_09_25-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main()\r\n{\r\n    int x;\r\n\r\n    puts(\"System error messages:\");\r\n    for( x=1; x&lt;sys_nerr; x++ )\r\n        printf(\"%3d: %s\\n\",x,strerror(x));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Line 9 loops through all valid system error messages, from 1 up through <em>sys_nerr<\/em>. On my system, I count 106 error messages. This size is the reason why I use the <code>%3d<\/code> placeholder in the <em>printf()<\/em> statement (Line 10), to ensure that the output width stays the same for each line of text output. Here are the first few lines of output:<\/p>\n<p><code>System error messages:<br \/>\n&nbsp;&nbsp;1: Operation not permitted<br \/>\n&nbsp;&nbsp;2: No such file or directory<br \/>\n&nbsp;&nbsp;3: No such process<br \/>\n&nbsp;&nbsp;4: Interrupted system call<br \/>\n&nbsp;&nbsp;5: Input\/output error<br \/>\n&nbsp;&nbsp;6: Device not configured<br \/>\n. . .<\/code><\/p>\n<p>You can see that system error message 2 is <code>No such file or directory<\/code>, which is output with the <em>perror()<\/em> function when using <em>rename()<\/em> to rename a file that doesn&#8217;t exist. If the <em>errno<\/em> value of this message is 2, then I can connect the dots and see that the <em>strerror()<\/em> function&#8217;s <code>errnum<\/code> argument parallels the value of the global variable <em>errno<\/em>.<\/p>\n<p>A quick peek into the definitions for <em>errno<\/em> in its header file reveals the following:<\/p>\n<p><code>#define ENOENT&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/* No such file or directory *\/<\/code><\/p>\n<p>Ah. The universe makes sense.<\/p>\n<p>While <em>perror()<\/em> allows you to output an <em>errno<\/em> value&#8217;s associated message along with a custom text message, the <em>strerror()<\/em> function swallows the <em>errno<\/em> value itself to output the system&#8217;s error message, nothing further. It&#8217;s not a unique tool, just another solution presented for the same problem.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s yet another, fun C library function that spews forth a system error message. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4981\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-4981","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4981","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4981"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4981\/revisions"}],"predecessor-version":[{"id":4988,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4981\/revisions\/4988"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}