鱼C论坛

 找回密码
 立即注册
查看: 1632|回复: 0

[技术交流] 学习ngx之ngx_ngx_strerror_init()

[复制链接]
发表于 2015-5-17 01:55:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>

  4. #define NGX_SYS_NERR    132
  5. #define NGX_OK  0                       //src/core/ngx_core.h:36
  6. #define NGX_ERROR       -1                      //src/core/ngx_core.h:37
  7. #define ngx_strlen(s)   strlen((const char *) s)        //src/core/ngx_string:61



  8. typedef unsigned char u_char;

  9. typedef int     ngx_err_t;                              //src/os/unix/ngx_errno.h:16


  10. typedef struct {                                        //src/core/ngx_string.h:16
  11.         size_t  len;
  12.         u_char  *data;
  13. } ngx_str_t;


  14. static ngx_str_t        *ngx_sys_errlist;       //src/os/unix/ngx_errno.h:28


  15. void *                                  //src/core/ngx_string.h:1965
  16. ngx_memcpy(void *dst, const void *src, size_t n) {
  17. //      if (n > NGX_MEMCPY_LIMIT) {
  18. //              ngx_log_error(NGX_LOG_ALETER, ngx_cycle->log, 0, "memcpy %uz bytes", n);
  19. //              ngx_debug_point();
  20. //      }

  21.         return memcpy(dst, src, n);
  22. }



  23. int
  24. ngx_strerror_init(void) {
  25.         char    *msg;
  26.         u_char  *p;
  27.         size_t  len;
  28.         ngx_err_t       err;

  29.         len = NGX_SYS_NERR * sizeof(ngx_str_t);

  30.         ngx_sys_errlist = malloc(len);

  31.         if (ngx_sys_errlist == NULL) {
  32.                 goto failed;
  33.         }

  34.         for ( err = 0; err < NGX_SYS_NERR; err++) {
  35.                 msg = strerror(err);    // get error infor from number by using system function strerror()
  36.                 len = ngx_strlen(msg);

  37.                 p = malloc(len);                //get a memory for current error string by use malloc(), which return a char pointer
  38.                 if( p == NULL ) {
  39.                         goto failed;
  40.                 }

  41.                 ngx_memcpy(p, msg, len);
  42.                 ngx_sys_errlist[err].len = len;
  43.                 ngx_sys_errlist[err].data = p;
  44.         }

  45.         return NGX_OK;

  46. failed:
  47. //      err = errno;
  48. //      ngx_log_stderr(0, "malloc(%uz) failed (%d: %s)", len, err, strerror(err));

  49.         return NGX_ERROR;
  50. }




  51. int
  52. main(int argc, char ** argv) {

  53.         //call the ngx_strerror_init() function to get the table of ngx_sys_errlist
  54.         if (ngx_strerror_init() == NGX_OK) {
  55.                 int i;
  56.                 for (i = 0; i < NGX_SYS_NERR; i++) {
  57.                         printf("%d\t%d\t%s\n", i, ngx_sys_errlist[i].len, ngx_sys_errlist[i].data);
  58.                 }
  59.         }

  60.         return 0;
  61. }
复制代码

因为只先考虑了函数实现,但是这之中的日志处理,暂时将其注释了,,,还有待研究研究。。。唉,,,必须努力啊




。。。


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-19 22:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表