summaryrefslogtreecommitdiff
path: root/devdocs/c/string%2Fbyte%2Fstrerror.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/string%2Fbyte%2Fstrerror.html
new repository
Diffstat (limited to 'devdocs/c/string%2Fbyte%2Fstrerror.html')
-rw-r--r--devdocs/c/string%2Fbyte%2Fstrerror.html67
1 files changed, 67 insertions, 0 deletions
diff --git a/devdocs/c/string%2Fbyte%2Fstrerror.html b/devdocs/c/string%2Fbyte%2Fstrerror.html
new file mode 100644
index 00000000..ffa63b45
--- /dev/null
+++ b/devdocs/c/string%2Fbyte%2Fstrerror.html
@@ -0,0 +1,67 @@
+ <h1 id="firstHeading" class="firstHeading">strerror, strerror_s, strerrorlen_s</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;string.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td> <pre data-language="c">char* strerror( int errnum );</pre>
+</td> <td> (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">errno_t strerror_s( char *buf, rsize_t bufsz, errno_t errnum );</pre>
+</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">size_t strerrorlen_s( errno_t errnum );</pre>
+</td> <td> (3) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> </table> <div class="t-li1">
+<span class="t-li">1)</span> Returns a pointer to the textual description of the system error code <code>errnum</code>, identical to the description that would be printed by <code><a href="../../io/perror" title="c/io/perror">perror()</a></code>.</div> <div class="t-li1">
+ <code>errnum</code> is usually acquired from the <code>errno</code> variable, however the function accepts any value of type <code>int</code>. The contents of the string are locale-specific.</div> <div class="t-li1">
+ The returned string must not be modified by the program, but may be overwritten by a subsequent call to the <code>strerror</code> function. <code>strerror</code> is not required to be thread-safe. Implementations may be returning different pointers to static read-only string literals or may be returning the same pointer over and over, pointing at a static buffer in which strerror places the string.</div> <div class="t-li1">
+<span class="t-li">2)</span> Same as <span class="t-v">(1)</span>, except that the message is copied into user-provided storage <code>buf</code>. No more than <code>bufsz-1</code> bytes are written, the buffer is always null-terminated. If the message had to be truncated to fit the buffer and <code>bufsz</code> is greater than 3, then only <code>bufsz-4</code> bytes are written, and the characters <code>"..."</code> are appended before the null terminator. In addition, the following errors are detected at runtime and call the currently installed <a href="../../error/set_constraint_handler_s" title="c/error/set constraint handler s">constraint handler</a> function: <ul>
+<li> <code>buf</code> is a null pointer </li>
+<li> <code>bufsz</code> is zero or greater than <code>RSIZE_MAX</code>
+</li>
+</ul>
+</div> <div class="t-li1">
+ The behavior is undefined if writing to <code>buf</code> occurs past the end of the array, which can happen when the size of the buffer pointed to by <code>buf</code> is less than the number of characters in the error message which in turn is less than <code>bufsz</code>.</div> <div class="t-li1">
+<span class="t-li">3)</span> Computes the length of the untruncated locale-specific error message that <code>strerror_s</code> would write if it were called with <code>errnum</code>. The length does not include the null terminator. As with all bounds-checked functions, <code>strerror_s</code> and <code>strerrorlen_s</code> are only guaranteed to be available if <code>__STDC_LIB_EXT1__</code> is defined by the implementation and if the user defines <code>__STDC_WANT_LIB_EXT1__</code> to the integer constant <code>1</code> before including <a href="../byte" title="c/string/byte"><code>&lt;string.h&gt;</code></a>.</div> <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> errnum </td> <td> - </td> <td> integral value referring to an error code </td>
+</tr> <tr class="t-par"> <td> buf </td> <td> - </td> <td> pointer to a user-provided buffer </td>
+</tr> <tr class="t-par"> <td> bufsz </td> <td> - </td> <td> size of the user-provided buffer </td>
+</tr>
+</table> <h3 id="Return_value"> Return value</h3> <div class="t-li1">
+<span class="t-li">1)</span> Pointer to a null-terminated byte string corresponding to the <code><a href="../../error/errno" title="c/error/errno">errno</a></code> error code <code>errnum</code>.</div> <div class="t-li1">
+<span class="t-li">2)</span> Zero if the entire message was successfully stored in <code>buf</code>, non-zero otherwise.</div> <div class="t-li1">
+<span class="t-li">3)</span> Length (not including the null terminator) of the message that <code>strerror_s</code> would return</div> <h3 id="Notes"> Notes</h3> <p><a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/strerror.html">POSIX</a> allows subsequent calls to <code>strerror</code> to invalidate the pointer value returned by an earlier call. It also specifies that it is the <a href="../../locale/lc_categories" title="c/locale/LC categories"><code>LC_MESSAGES</code></a> locale facet that controls the contents of these messages.</p>
+<p><code>strerror_s</code> is the only bounds-checked function that allows truncation, because providing as much information as possible about a failure was deemed to be more desirable. POSIX also defines <a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/strerror.html"><code>strerror_r</code></a> for similar purposes.</p>
+<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#define __STDC_WANT_LIB_EXT1__ 1
+#include &lt;stdio.h&gt;
+#include &lt;errno.h&gt;
+#include &lt;string.h&gt;
+#include &lt;locale.h&gt;
+
+int main(void)
+{
+ FILE *fp = fopen(tmpnam((char[L_tmpnam]){0}), "r");
+ if(fp==NULL) {
+ printf("File opening error: %s\n", strerror(errno));
+ setlocale(LC_MESSAGES, "de_DE.utf8");
+ printf("Now in German: %s\n", strerror(errno));
+#ifdef __STDC_LIB_EXT1__
+ setlocale(LC_ALL, "ja_JP.utf8"); // printf needs CTYPE for multibyte output
+ size_t errmsglen = strerrorlen_s(errno) + 1;
+ char errmsg[errmsglen];
+ strerror_s(errmsg, errmsglen, errno);
+ printf("Now in Japanese: %s\n", errmsg);
+#endif
+ }
+}</pre></div> <p>Possible output:</p>
+<div class="text source-text"><pre data-language="c">File opening error: No such file or directory
+Now in German: Datei oder Verzeichnis nicht gefunden
+Now in Japanese: そのようなファイル、又はディレクトリはありません</pre></div> </div> <h3 id="References"> References</h3> <ul>
+<li> C11 standard (ISO/IEC 9899:2011): </li>
+<ul>
+<li> 7.24.6.2 The strerror function (p: 371) </li>
+<li> K.3.7.4.2 The strerror_s function (p: 622) </li>
+<li> K.3.7.4.3 The strerrorlen_s function (p: 623) </li>
+</ul>
+<li> C99 standard (ISO/IEC 9899:1999): </li>
+<ul><li> 7.21.6.2 The strerror function (p: 334) </li></ul>
+<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
+<ul><li> 4.11.6.2 The strerror function </li></ul>
+</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="../../io/perror" title="c/io/perror"> <span class="t-lines"><span>perror</span></span></a></div> </td> <td> displays a character string corresponding of the current error to <code><a href="../../io/std_streams" title="c/io/std streams">stderr</a></code> <br> <span class="t-mark">(function)</span> </td>
+</tr> <tr class="t-dsc"> <td> <div><a href="../../error/errno" title="c/error/errno"> <span class="t-lines"><span>errno</span></span></a></div> </td> <td> macro which expands to POSIX-compatible thread-local error number variable<br><span class="t-mark">(macro variable)</span> </td>
+</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/string/byte/strerror" title="cpp/string/byte/strerror">C++ documentation</a></span> for <code>strerror</code> </td>
+</tr> </table> <div class="_attribution">
+ <p class="_attribution-p">
+ &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
+ <a href="https://en.cppreference.com/w/c/string/byte/strerror" class="_attribution-link">https://en.cppreference.com/w/c/string/byte/strerror</a>
+ </p>
+</div>