在UNIX中是否有任何函数将errno转换为其对应的字符串,例如EIDRM到"EIDRM".调试以检查这些整数errnos的错误非常烦人.
我可以使用perror()或strerror()打印属于a的"人类可读"错误消息errno,但是如果我还要打印符号名称(例如" EAGAIN")的话errno呢?
任何方便的功能或宏来做到这一点?
更新:附上我最终编写的代码,基于下面接受的答案及其评论的想法:
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
int get_errno_name(char *buf, int buf_size) {
// Using the linux-only gawk instead of awk, because of the convenient
// match() functionality. For Posix portability, use a different recipe...
char cmd[] = "e= && " // errno to be inserted here (max digits = 6)
"echo '#include <errno.h>' | "
"gcc -dM -E - | " // optionally, use …Run Code Online (Sandbox Code Playgroud) 我正在寻找一个将errno整数转换为其名称的API。
例如:
int fd;
if((fd = open(path, O_RDONLY)) == -1)
printf("error: %d %s %s\n", errno, strerror(errno), ERRNONAME(errno));
Run Code Online (Sandbox Code Playgroud)
所以,ERRNONAME将产生一个名称,如"EINVAL","EPERM"等这可能吗?