在哪里可以看到与 errno 交互的函数列表?

发布于 2024-07-26 23:23:45 字数 246 浏览 8 评论 0原文

《C语言编程》一书中这样写道:

“当发生错误或文件结束时,库中的许多函数都会设置状态指示器。这些 可以明确地设置和测试指标。 另外,整数表达式errno(声明为 在 中)可能包含一个错误号,该错误号提供有关最 最近的错误。”

在哪里可以看到这些函数的列表?

In the book "The C Programming Language" it says:

"Many of the functions in the library set status indicators when error or end of file occur. These
indicators may be set and tested explicitly. In addition, the integer expression errno (declared
in <errno.h>) may contain an error number that gives further information about the most
recent error."

Where can I see a list of these functions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

暗藏城府 2024-08-02 23:23:45

该标准对 errno 是这样说的:

程序启动时 errno 的值为零,但任何库都不会将其设置为零
功能。 errno 的值可以通过库函数调用设置为非零,无论是否存在错误,前提是本国际标准中的函数描述中未记录 errno 的使用。

这对我来说,任何库函数都可以以任何它喜欢的方式使用 errno ,除了:

  • 它不能将 errno 设置为 0
  • 它如果标准明确另有说明,则不能做它喜欢的事情

请注意,该标准在脚注中建议以下内容:

因此,使用 errno 进行错误检查的程序应在库函数调用之前将其设置为零,然后在后续库函数调用之前检查它。 当然,库函数可以在输入时保存errno的值,然后将其设置为零,只要如果errno的值仍然是,则恢复原始值返回前为零。

正如其他答案中所述,标准之外的函数也通常会设置 errno 。

The standard says this about errno:

The value of errno is zero at program startup, but is never set to zero by any library
function. The value of errno may be set to nonzero by a library function call whether or not there is an error, provided the use of errno is not documented in the description of the function in this International Standard.

Which says to me that any library function can screw around with errno in any way it likes except:

  • it can't set errno to 0
  • it can't do what it likes if the standard explicitly says otherwise

Note that the standard suggests the following in a footnote:

Thus, a program that uses errno for error checking should set it to zero before a library function call, then inspect it before a subsequent library function call. Of course, a library function can save the value of errno on entry and then set it to zero, as long as the original value is restored if errno's value is still zero just before the return.

As noted in other answers, it's common for functions that are not in the standard to set errno as well.

-小熊_ 2024-08-02 23:23:45

您应该假设任何函数都可以设置 errno,而不仅仅是标准库中的函数。 因此,列出一份清单有点毫无意义。

You should assume that any function can set errno, not just those in the standard library. A list would therefore be a little pointless.

给妤﹃绝世温柔 2024-08-02 23:23:45

如果发生错误,即函数返回 -1 时,几乎所有 posix 库函数都可以设置 errno。
线程函数是一个例外,因为从多个线程设置一个全局错误变量将是非常危险的。 成功时返回 0,否则返回错误代码(此代码与 errno 兼容,因此您可以在其上使用 strerror 和 perror 函数)。

Nearly all posix library functions can set errno if an error occurs, that is when the function returns -1.
An exception are threading functions because setting one global error variable from multiple threads would be very dangerous. They return 0 on success, the errorcode otherwise (This code is compatible with errno so you can use the strerror and perror functions on it).

踏雪无痕 2024-08-02 23:23:45

一个正确的问题可能是价值观是什么
errno可以得到以及它们各自的含义。 您可以在 intro(2) 中看到它们的列表。

A proper question might be what are the values
errno can get and what each of them means. You can see them listed in intro(2).

剩余の解释 2024-08-02 23:23:45

@Adam,正如尼尔所说,任何函数都可以针对 errno.h 进行编译并设置 errno。 根据定义,不可能列出以这种方式编译以使用核心 errno 功能的所有实用程序。

也就是说,可以通过多种方式将错误报告给用户。 使用 errno 只是其中之一。

@Adam, as Neil said, any function can be compiled against errno.h and set errno. It is by definition, impossible to list all utilities that have been compiled in this way to use the core errno functionality.

That said, there are several ways that errors may be reported back to a user. Using errno is just one.

怪异←思 2024-08-02 23:23:45

您可以使用您喜欢的编辑器和“在文件中查找...”来搜索包含 errno 关键字的文件。

You can use your favorite editor and the "Find in files..." to search for files that contain the errno keyword.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文