PageRenderTime 69ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/Sources/selp_glibc-2.5.90-19.0.46/html/Error-Messages.html

https://bitbucket.org/Mali_Laurent/ps50c550
HTML | 418 lines | 317 code | 56 blank | 45 comment | 0 complexity | ad456bf72a5a3b94dd7c497b095f34c8 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1, AGPL-1.0, CC-BY-SA-3.0, BSD-3-Clause
  1. <html lang="en">
  2. <head>
  3. <title>Error Messages - The GNU C Library</title>
  4. <meta http-equiv="Content-Type" content="text/html">
  5. <meta name="description" content="The GNU C Library">
  6. <meta name="generator" content="makeinfo 4.8">
  7. <link title="Top" rel="start" href="index.html#Top">
  8. <link rel="up" href="Error-Reporting.html#Error-Reporting" title="Error Reporting">
  9. <link rel="prev" href="Error-Codes.html#Error-Codes" title="Error Codes">
  10. <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
  11. <meta http-equiv="Content-Style-Type" content="text/css">
  12. <style type="text/css"><!--
  13. pre.display { font-family:inherit }
  14. pre.format { font-family:inherit }
  15. pre.smalldisplay { font-family:inherit; font-size:smaller }
  16. pre.smallformat { font-family:inherit; font-size:smaller }
  17. pre.smallexample { font-size:smaller }
  18. pre.smalllisp { font-size:smaller }
  19. span.sc { font-variant:small-caps }
  20. span.roman { font-family:serif; font-weight:normal; }
  21. span.sansserif { font-family:sans-serif; font-weight:normal; }
  22. --></style>
  23. </head>
  24. <body>
  25. <div class="node">
  26. <p>
  27. <a name="Error-Messages"></a>
  28. Previous:&nbsp;<a rel="previous" accesskey="p" href="Error-Codes.html#Error-Codes">Error Codes</a>,
  29. Up:&nbsp;<a rel="up" accesskey="u" href="Error-Reporting.html#Error-Reporting">Error Reporting</a>
  30. <hr>
  31. </div>
  32. <h3 class="section">2.3 Error Messages</h3>
  33. <p>The library has functions and variables designed to make it easy for
  34. your program to report informative error messages in the customary
  35. format about the failure of a library call. The functions
  36. <code>strerror</code> and <code>perror</code> give you the standard error message
  37. for a given error code; the variable
  38. <code>program_invocation_short_name</code><!-- /@w --> gives you convenient access to the
  39. name of the program that encountered the error.
  40. <!-- string.h -->
  41. <!-- ISO -->
  42. <div class="defun">
  43. &mdash; Function: char * <b>strerror</b> (<var>int errnum</var>)<var><a name="index-strerror-210"></a></var><br>
  44. <blockquote><p>The <code>strerror</code> function maps the error code (see <a href="Checking-for-Errors.html#Checking-for-Errors">Checking for Errors</a>) specified by the <var>errnum</var> argument to a descriptive error
  45. message string. The return value is a pointer to this string.
  46. <p>The value <var>errnum</var> normally comes from the variable <code>errno</code>.
  47. <p>You should not modify the string returned by <code>strerror</code>. Also, if
  48. you make subsequent calls to <code>strerror</code>, the string might be
  49. overwritten. (But it's guaranteed that no library function ever calls
  50. <code>strerror</code> behind your back.)
  51. <p>The function <code>strerror</code> is declared in <samp><span class="file">string.h</span></samp>.
  52. </p></blockquote></div>
  53. <!-- string.h -->
  54. <!-- GNU -->
  55. <div class="defun">
  56. &mdash; Function: char * <b>strerror_r</b> (<var>int errnum, char *buf, size_t n</var>)<var><a name="index-strerror_005fr-211"></a></var><br>
  57. <blockquote><p>The <code>strerror_r</code> function works like <code>strerror</code> but instead of
  58. returning the error message in a statically allocated buffer shared by
  59. all threads in the process, it returns a private copy for the
  60. thread. This might be either some permanent global data or a message
  61. string in the user supplied buffer starting at <var>buf</var> with the
  62. length of <var>n</var> bytes.
  63. <p>At most <var>n</var> characters are written (including the NUL byte) so it is
  64. up to the user to select the buffer large enough.
  65. <p>This function should always be used in multi-threaded programs since
  66. there is no way to guarantee the string returned by <code>strerror</code>
  67. really belongs to the last call of the current thread.
  68. <p>This function <code>strerror_r</code> is a GNU extension and it is declared in
  69. <samp><span class="file">string.h</span></samp>.
  70. </p></blockquote></div>
  71. <!-- stdio.h -->
  72. <!-- ISO -->
  73. <div class="defun">
  74. &mdash; Function: void <b>perror</b> (<var>const char *message</var>)<var><a name="index-perror-212"></a></var><br>
  75. <blockquote><p>This function prints an error message to the stream <code>stderr</code>;
  76. see <a href="Standard-Streams.html#Standard-Streams">Standard Streams</a>. The orientation of <code>stderr</code> is not
  77. changed.
  78. <p>If you call <code>perror</code> with a <var>message</var> that is either a null
  79. pointer or an empty string, <code>perror</code> just prints the error message
  80. corresponding to <code>errno</code>, adding a trailing newline.
  81. <p>If you supply a non-null <var>message</var> argument, then <code>perror</code>
  82. prefixes its output with this string. It adds a colon and a space
  83. character to separate the <var>message</var> from the error string corresponding
  84. to <code>errno</code>.
  85. <p>The function <code>perror</code> is declared in <samp><span class="file">stdio.h</span></samp>.
  86. </p></blockquote></div>
  87. <p><code>strerror</code> and <code>perror</code> produce the exact same message for any
  88. given error code; the precise text varies from system to system. On the
  89. GNU system, the messages are fairly short; there are no multi-line
  90. messages or embedded newlines. Each error message begins with a capital
  91. letter and does not include any terminating punctuation.
  92. <p><strong>Compatibility Note:</strong> The <code>strerror</code> function was introduced
  93. in ISO&nbsp;C89<!-- /@w -->. Many older C systems do not support this function yet.
  94. <p><a name="index-program-name-213"></a><a name="index-name-of-running-program-214"></a>Many programs that don't read input from the terminal are designed to
  95. exit if any system call fails. By convention, the error message from
  96. such a program should start with the program's name, sans directories.
  97. You can find that name in the variable
  98. <code>program_invocation_short_name</code>; the full file name is stored the
  99. variable <code>program_invocation_name</code>.
  100. <!-- errno.h -->
  101. <!-- GNU -->
  102. <div class="defun">
  103. &mdash; Variable: char * <b>program_invocation_name</b><var><a name="index-program_005finvocation_005fname-215"></a></var><br>
  104. <blockquote><p>This variable's value is the name that was used to invoke the program
  105. running in the current process. It is the same as <code>argv[0]</code>. Note
  106. that this is not necessarily a useful file name; often it contains no
  107. directory names. See <a href="Program-Arguments.html#Program-Arguments">Program Arguments</a>.
  108. </p></blockquote></div>
  109. <!-- errno.h -->
  110. <!-- GNU -->
  111. <div class="defun">
  112. &mdash; Variable: char * <b>program_invocation_short_name</b><var><a name="index-program_005finvocation_005fshort_005fname-216"></a></var><br>
  113. <blockquote><p>This variable's value is the name that was used to invoke the program
  114. running in the current process, with directory names removed. (That is
  115. to say, it is the same as <code>program_invocation_name</code> minus
  116. everything up to the last slash, if any.)
  117. </p></blockquote></div>
  118. <p>The library initialization code sets up both of these variables before
  119. calling <code>main</code>.
  120. <p><strong>Portability Note:</strong> These two variables are GNU extensions. If
  121. you want your program to work with non-GNU libraries, you must save the
  122. value of <code>argv[0]</code> in <code>main</code>, and then strip off the directory
  123. names yourself. We added these extensions to make it possible to write
  124. self-contained error-reporting subroutines that require no explicit
  125. cooperation from <code>main</code>.
  126. <p>Here is an example showing how to handle failure to open a file
  127. correctly. The function <code>open_sesame</code> tries to open the named file
  128. for reading and returns a stream if successful. The <code>fopen</code>
  129. library function returns a null pointer if it couldn't open the file for
  130. some reason. In that situation, <code>open_sesame</code> constructs an
  131. appropriate error message using the <code>strerror</code> function, and
  132. terminates the program. If we were going to make some other library
  133. calls before passing the error code to <code>strerror</code>, we'd have to
  134. save it in a local variable instead, because those other library
  135. functions might overwrite <code>errno</code> in the meantime.
  136. <pre class="smallexample"> #include &lt;errno.h&gt;
  137. #include &lt;stdio.h&gt;
  138. #include &lt;stdlib.h&gt;
  139. #include &lt;string.h&gt;
  140. FILE *
  141. open_sesame (char *name)
  142. {
  143. FILE *stream;
  144. errno = 0;
  145. stream = fopen (name, "r");
  146. if (stream == NULL)
  147. {
  148. fprintf (stderr, "%s: Couldn't open file %s; %s\n",
  149. program_invocation_short_name, name, strerror (errno));
  150. exit (EXIT_FAILURE);
  151. }
  152. else
  153. return stream;
  154. }
  155. </pre>
  156. <p>Using <code>perror</code> has the advantage that the function is portable and
  157. available on all systems implementing ISO&nbsp;C<!-- /@w -->. But often the text
  158. <code>perror</code> generates is not what is wanted and there is no way to
  159. extend or change what <code>perror</code> does. The GNU coding standard, for
  160. instance, requires error messages to be preceded by the program name and
  161. programs which read some input files should should provide information
  162. about the input file name and the line number in case an error is
  163. encountered while reading the file. For these occasions there are two
  164. functions available which are widely used throughout the GNU project.
  165. These functions are declared in <samp><span class="file">error.h</span></samp>.
  166. <!-- error.h -->
  167. <!-- GNU -->
  168. <div class="defun">
  169. &mdash; Function: void <b>error</b> (<var>int status, int errnum, const char *format, <small class="dots">...</small></var>)<var><a name="index-error-217"></a></var><br>
  170. <blockquote><p>The <code>error</code> function can be used to report general problems during
  171. program execution. The <var>format</var> argument is a format string just
  172. like those given to the <code>printf</code> family of functions. The
  173. arguments required for the format can follow the <var>format</var> parameter.
  174. Just like <code>perror</code>, <code>error</code> also can report an error code in
  175. textual form. But unlike <code>perror</code> the error value is explicitly
  176. passed to the function in the <var>errnum</var> parameter. This eliminates
  177. the problem mentioned above that the error reporting function must be
  178. called immediately after the function causing the error since otherwise
  179. <code>errno</code> might have a different value.
  180. <p>The <code>error</code> prints first the program name. If the application
  181. defined a global variable <code>error_print_progname</code> and points it to a
  182. function this function will be called to print the program name.
  183. Otherwise the string from the global variable <code>program_name</code> is
  184. used. The program name is followed by a colon and a space which in turn
  185. is followed by the output produced by the format string. If the
  186. <var>errnum</var> parameter is non-zero the format string output is followed
  187. by a colon and a space, followed by the error message for the error code
  188. <var>errnum</var>. In any case is the output terminated with a newline.
  189. <p>The output is directed to the <code>stderr</code> stream. If the
  190. <code>stderr</code> wasn't oriented before the call it will be narrow-oriented
  191. afterwards.
  192. <p>The function will return unless the <var>status</var> parameter has a
  193. non-zero value. In this case the function will call <code>exit</code> with
  194. the <var>status</var> value for its parameter and therefore never return. If
  195. <code>error</code> returns the global variable <code>error_message_count</code> is
  196. incremented by one to keep track of the number of errors reported.
  197. </p></blockquote></div>
  198. <!-- error.h -->
  199. <!-- GNU -->
  200. <div class="defun">
  201. &mdash; Function: void <b>error_at_line</b> (<var>int status, int errnum, const char *fname, unsigned int lineno, const char *format, <small class="dots">...</small></var>)<var><a name="index-error_005fat_005fline-218"></a></var><br>
  202. <blockquote>
  203. <p>The <code>error_at_line</code> function is very similar to the <code>error</code>
  204. function. The only difference are the additional parameters <var>fname</var>
  205. and <var>lineno</var>. The handling of the other parameters is identical to
  206. that of <code>error</code> except that between the program name and the string
  207. generated by the format string additional text is inserted.
  208. <p>Directly following the program name a colon, followed by the file name
  209. pointer to by <var>fname</var>, another colon, and a value of <var>lineno</var> is
  210. printed.
  211. <p>This additional output of course is meant to be used to locate an error
  212. in an input file (like a programming language source code file etc).
  213. <p>If the global variable <code>error_one_per_line</code> is set to a non-zero
  214. value <code>error_at_line</code> will avoid printing consecutive messages for
  215. the same file and line. Repetition which are not directly following
  216. each other are not caught.
  217. <p>Just like <code>error</code> this function only returned if <var>status</var> is
  218. zero. Otherwise <code>exit</code> is called with the non-zero value. If
  219. <code>error</code> returns the global variable <code>error_message_count</code> is
  220. incremented by one to keep track of the number of errors reported.
  221. </p></blockquote></div>
  222. <p>As mentioned above the <code>error</code> and <code>error_at_line</code> functions
  223. can be customized by defining a variable named
  224. <code>error_print_progname</code>.
  225. <!-- error.h -->
  226. <!-- GNU -->
  227. <div class="defun">
  228. &mdash; Variable: void (* <b>error_print_progname</b>) (<var>void</var>)<var><a name="index-error_005fprint_005fprogname-219"></a></var><br>
  229. <blockquote><p>If the <code>error_print_progname</code> variable is defined to a non-zero
  230. value the function pointed to is called by <code>error</code> or
  231. <code>error_at_line</code>. It is expected to print the program name or do
  232. something similarly useful.
  233. <p>The function is expected to be print to the <code>stderr</code> stream and
  234. must be able to handle whatever orientation the stream has.
  235. <p>The variable is global and shared by all threads.
  236. </p></blockquote></div>
  237. <!-- error.h -->
  238. <!-- GNU -->
  239. <div class="defun">
  240. &mdash; Variable: unsigned int <b>error_message_count</b><var><a name="index-error_005fmessage_005fcount-220"></a></var><br>
  241. <blockquote><p>The <code>error_message_count</code> variable is incremented whenever one of
  242. the functions <code>error</code> or <code>error_at_line</code> returns. The
  243. variable is global and shared by all threads.
  244. </p></blockquote></div>
  245. <!-- error.h -->
  246. <!-- GNU -->
  247. <div class="defun">
  248. &mdash; Variable: int <b>error_one_per_line</b><var><a name="index-error_005fone_005fper_005fline-221"></a></var><br>
  249. <blockquote><p>The <code>error_one_per_line</code> variable influences only
  250. <code>error_at_line</code>. Normally the <code>error_at_line</code> function
  251. creates output for every invocation. If <code>error_one_per_line</code> is
  252. set to a non-zero value <code>error_at_line</code> keeps track of the last
  253. file name and line number for which an error was reported and avoid
  254. directly following messages for the same file and line. This variable
  255. is global and shared by all threads.
  256. </p></blockquote></div>
  257. <p class="noindent">A program which read some input file and reports errors in it could look
  258. like this:
  259. <pre class="smallexample"> {
  260. char *line = NULL;
  261. size_t len = 0;
  262. unsigned int lineno = 0;
  263. error_message_count = 0;
  264. while (! feof_unlocked (fp))
  265. {
  266. ssize_t n = getline (&amp;line, &amp;len, fp);
  267. if (n &lt;= 0)
  268. /* <span class="roman">End of file or error.</span> */
  269. break;
  270. ++lineno;
  271. /* <span class="roman">Process the line.</span> */
  272. ...
  273. if (<span class="roman">Detect error in line</span>)
  274. error_at_line (0, errval, filename, lineno,
  275. "some error text %s", some_variable);
  276. }
  277. if (error_message_count != 0)
  278. error (EXIT_FAILURE, 0, "%u errors found", error_message_count);
  279. }
  280. </pre>
  281. <p><code>error</code> and <code>error_at_line</code> are clearly the functions of
  282. choice and enable the programmer to write applications which follow the
  283. GNU coding standard. The GNU libc additionally contains functions which
  284. are used in BSD for the same purpose. These functions are declared in
  285. <samp><span class="file">err.h</span></samp>. It is generally advised to not use these functions. They
  286. are included only for compatibility.
  287. <!-- err.h -->
  288. <!-- BSD -->
  289. <div class="defun">
  290. &mdash; Function: void <b>warn</b> (<var>const char *format, <small class="dots">...</small></var>)<var><a name="index-warn-222"></a></var><br>
  291. <blockquote><p>The <code>warn</code> function is roughly equivalent to a call like
  292. <pre class="smallexample"> error (0, errno, format, <span class="roman">the parameters</span>)
  293. </pre>
  294. <p class="noindent">except that the global variables <code>error</code> respects and modifies
  295. are not used.
  296. </p></blockquote></div>
  297. <!-- err.h -->
  298. <!-- BSD -->
  299. <div class="defun">
  300. &mdash; Function: void <b>vwarn</b> (<var>const char *format, va_list</var>)<var><a name="index-vwarn-223"></a></var><br>
  301. <blockquote><p>The <code>vwarn</code> function is just like <code>warn</code> except that the
  302. parameters for the handling of the format string <var>format</var> are passed
  303. in as an value of type <code>va_list</code>.
  304. </p></blockquote></div>
  305. <!-- err.h -->
  306. <!-- BSD -->
  307. <div class="defun">
  308. &mdash; Function: void <b>warnx</b> (<var>const char *format, <small class="dots">...</small></var>)<var><a name="index-warnx-224"></a></var><br>
  309. <blockquote><p>The <code>warnx</code> function is roughly equivalent to a call like
  310. <pre class="smallexample"> error (0, 0, format, <span class="roman">the parameters</span>)
  311. </pre>
  312. <p class="noindent">except that the global variables <code>error</code> respects and modifies
  313. are not used. The difference to <code>warn</code> is that no error number
  314. string is printed.
  315. </p></blockquote></div>
  316. <!-- err.h -->
  317. <!-- BSD -->
  318. <div class="defun">
  319. &mdash; Function: void <b>vwarnx</b> (<var>const char *format, va_list</var>)<var><a name="index-vwarnx-225"></a></var><br>
  320. <blockquote><p>The <code>vwarnx</code> function is just like <code>warnx</code> except that the
  321. parameters for the handling of the format string <var>format</var> are passed
  322. in as an value of type <code>va_list</code>.
  323. </p></blockquote></div>
  324. <!-- err.h -->
  325. <!-- BSD -->
  326. <div class="defun">
  327. &mdash; Function: void <b>err</b> (<var>int status, const char *format, <small class="dots">...</small></var>)<var><a name="index-err-226"></a></var><br>
  328. <blockquote><p>The <code>err</code> function is roughly equivalent to a call like
  329. <pre class="smallexample"> error (status, errno, format, <span class="roman">the parameters</span>)
  330. </pre>
  331. <p class="noindent">except that the global variables <code>error</code> respects and modifies
  332. are not used and that the program is exited even if <var>status</var> is zero.
  333. </p></blockquote></div>
  334. <!-- err.h -->
  335. <!-- BSD -->
  336. <div class="defun">
  337. &mdash; Function: void <b>verr</b> (<var>int status, const char *format, va_list</var>)<var><a name="index-verr-227"></a></var><br>
  338. <blockquote><p>The <code>verr</code> function is just like <code>err</code> except that the
  339. parameters for the handling of the format string <var>format</var> are passed
  340. in as an value of type <code>va_list</code>.
  341. </p></blockquote></div>
  342. <!-- err.h -->
  343. <!-- BSD -->
  344. <div class="defun">
  345. &mdash; Function: void <b>errx</b> (<var>int status, const char *format, <small class="dots">...</small></var>)<var><a name="index-errx-228"></a></var><br>
  346. <blockquote><p>The <code>errx</code> function is roughly equivalent to a call like
  347. <pre class="smallexample"> error (status, 0, format, <span class="roman">the parameters</span>)
  348. </pre>
  349. <p class="noindent">except that the global variables <code>error</code> respects and modifies
  350. are not used and that the program is exited even if <var>status</var>
  351. is zero. The difference to <code>err</code> is that no error number
  352. string is printed.
  353. </p></blockquote></div>
  354. <!-- err.h -->
  355. <!-- BSD -->
  356. <div class="defun">
  357. &mdash; Function: void <b>verrx</b> (<var>int status, const char *format, va_list</var>)<var><a name="index-verrx-229"></a></var><br>
  358. <blockquote><p>The <code>verrx</code> function is just like <code>errx</code> except that the
  359. parameters for the handling of the format string <var>format</var> are passed
  360. in as an value of type <code>va_list</code>.
  361. </p></blockquote></div>
  362. </body></html>