errno.hbits/errno.hlinux/errno.hasm/errno.hasm-generic/errno.hasm-generic/errno-base.h
1、 文件包含关系 (/usr/include/)
errno.h--->bits/errno.h--->linux/errno.h--->asm/errno.h--->asm-generic/errno.h--->asm-generic/errno-base.h
其中,asm-generic/errno-base.h定义了错误号1-34对应的错误类型,asm-generic/errno.h定义35-132对应的错误类型。
2、 原理
每次系统调用或库函数调用失败后,都会把全局变量的错误号errno设置为相应的值,strerror函数根据errno的值打印出对应的错误类型字符串。而系统也定义了一个函数perror来处理这一过程:
函数原型:
void perror(const char *s);
先打印用户提供的字串s,紧跟着一个冒号和空格,再后面是errno对应的错误类型字串。
3、 验证程序
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main(void)
{
int i;
for (i = 0; i <=132; i++)
{
printf("[%d]:\t%s\n", i, strerror(i));
}
return 0;
}
4、 运行结果
[0]: Success
[1]: Operation not permitted
[2]: No such file or directory
[3]: No such process
[4]: Interrupted system call
[5]: Input/output error
[6]: No such device or address
[7]: Argument list too long
[8]: Exec format error
......
[124]: Wrong medium type
[125]: Operation canceled
[126]: Required key not available
[127]: Key has expired
[128]: Key has been revoked
[129]: Key was rejected by service
[130]: Owner died
[131]: State not recoverable
[132]: Operation not possible due to RF-kill