perror实现分析(2.0: 具体实现)

需要用到的结构(错误号,及对应的详细信息)

struct Pair {
  int code;
  const char* msg;
};

生成错误信息数组:

struct Pair _sys_error_strings[] = {
#define  __BIONIC_ERRDEF(x,y,z)  { x, z },
#include "./_errdefs.h"
  { 0, NULL }
};

_errdefs.h 的大概内容如下:

__BIONIC_ERRDEF( 0              ,   0, "Success" )
__BIONIC_ERRDEF( EPERM          ,   1, "Operation not permitted" )
__BIONIC_ERRDEF( ENOENT         ,   2, "No such file or directory" )
__BIONIC_ERRDEF( ESRCH          ,   3, "No such process" )

const char* __strerror_lookup(int error_number) {
  return __code_string_lookup(_sys_error_strings, error_number);
}

根据错误号查找对应的详细信息:

int strerror_r(int error_number, char* buf, size_t buf_len) {
  int saved_errno = errno;
  size_t length;

  const char* error_name = __strerror_lookup(error_number);
  if (error_name != NULL) {
    length = snprintf(buf, buf_len, "%s", error_name);
  } else {
    length = snprintf(buf, buf_len, "Unknown error %u", error_number);
  }
  if (length >= buf_len) {
    errno = ERANGE;
    return -1;
  }

  errno = saved_errno;
  return 0;
}


函数原型,为了编译的时候不出错函数名修改为 myperror ,

void myperror(const char *prefix)
{
    char   buff[256]={0x00};

    mystrerror_r( errno, buff, sizeof(buff) );
    printf("buff is %s",buff);
    if (prefix) {
        write( 2, prefix, strlen(prefix) );
        write( 2, ": ", 2 );
    }
    write( 2, buff, strlen(buff) );
    write( 2, "\n", 1 );
}

在看看具体的应用实例:

#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
int main()
{
   int fd;
   fd = open("/dev/really_no_exists", O_RDWR);
   if(fd<0)
   {
   	 perror("perror");
   	 myperror("myperror "); 
   }
}

输出:

myperror : No such file or directory
perror : No such file or directory

与ubuntu系统实现的效果一致:

参考:

bionic/libc/bionic/strerror_r.cpp

bionic/libc/include/sys/_errdefs.h

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值