关于unp.h

来源:互联网 发布:angular.min.js.map 编辑:程序博客网 时间:2024/06/08 14:49

最近在学UNIX下的网络编程,所以就找来一本UNIX网络编程看了看。结果刚看到第一章的第一个例子的时候傻眼了。unp.h,貌似没见过,百度百科后知道是要自己下载的,

所以就试试了。我分为3步。

1,先从网上下载一个叫unpv.13e.tar.gz的东东。

2,解压后make。(解压后在终端下进入解压后的目录下。执行下面的命令行)

     (1)  ./configure

      (2)cd  lib

      (3)make

完成后即在在根目录中生成libunp.a文件。然后打开子目录lib下的unp.h文件。将里面的unp.h中将#include "../config.h"改成#include "config.h" ,如果已经是。则不用改。

3,拷贝文件。(需要有拷贝的权限,所以我是切换到root用户下去做的)

(1)将刚生成的libunp.a文件复制这个静态库到/usr/lib/和/usr/lib64/中,

(2)将刚改过的unp.h文件和主目录的config.h文件拷贝到/usr/include中。

这三个做完后。就已经接近尾声了。当你把例子完完整整的敲上后,编译发现还是有问题。没有err_为前缀的函数,那是因为少了一个库文件(apueerror.h),在网上找到后。将文件拷贝到/usr/include 中后。编译ok。

下面将apueerror.h的代码复制出来。

 

  1. #include <errno.h>      /* for definition of errno */   
  2. #include <stdarg.h>     /* ISO C variable aruments */   
  3.   
  4. static void err_doit(intintconst char *, va_list);  
  5.   
  6. /* 
  7.  * Nonfatal error related to a system call. 
  8.  * Print a message and return. 
  9.  */  
  10. void  
  11. err_ret(const char *fmt, ...)  
  12. {  
  13.     va_list     ap;  
  14.   
  15.     va_start(ap, fmt);  
  16.     err_doit(1, errno, fmt, ap);  
  17.     va_end(ap);  
  18. }  
  19.   
  20.   
  21. /* 
  22.  * Fatal error related to a system call. 
  23.  * Print a message and terminate. 
  24.  */  
  25. void  
  26. err_sys(const char *fmt, ...)  
  27. {  
  28.     va_list     ap;  
  29.   
  30.     va_start(ap, fmt);  
  31.     err_doit(1, errno, fmt, ap);  
  32.     va_end(ap);  
  33.     exit(1);  
  34. }  
  35.   
  36.   
  37. /* 
  38.  * Fatal error unrelated to a system call. 
  39.  * Error code passed as explict parameter. 
  40.  * Print a message and terminate. 
  41.  */  
  42. void  
  43. err_exit(int error, const char *fmt, ...)  
  44. {  
  45.     va_list     ap;  
  46.   
  47.     va_start(ap, fmt);  
  48.     err_doit(1, error, fmt, ap);  
  49.     va_end(ap);  
  50.     exit(1);  
  51. }  
  52.   
  53.   
  54. /* 
  55.  * Fatal error related to a system call. 
  56.  * Print a message, dump core, and terminate. 
  57.  */  
  58. void  
  59. err_dump(const char *fmt, ...)  
  60. {  
  61.     va_list     ap;  
  62.   
  63.     va_start(ap, fmt);  
  64.     err_doit(1, errno, fmt, ap);  
  65.     va_end(ap);  
  66.     abort();        /* dump core and terminate */  
  67.     exit(1);        /* shouldn't get here */  
  68. }  
  69.   
  70.   
  71. /* 
  72.  * Nonfatal error unrelated to a system call. 
  73.  * Print a message and return. 
  74.  */  
  75. void  
  76. err_msg(const char *fmt, ...)  
  77. {  
  78.     va_list     ap;  
  79.   
  80.     va_start(ap, fmt);  
  81.     err_doit(0, 0, fmt, ap);  
  82.     va_end(ap);  
  83. }  
  84.   
  85.   
  86. /* 
  87.  * Fatal error unrelated to a system call. 
  88.  * Print a message and terminate. 
  89.  */  
  90. void  
  91. err_quit(const char *fmt, ...)  
  92. {  
  93.     va_list     ap;  
  94.   
  95.     va_start(ap, fmt);  
  96.     err_doit(0, 0, fmt, ap);  
  97.     va_end(ap);  
  98.     exit(1);  
  99. }  
  100.   
  101.   
  102. /* 
  103.  * Print a message and return to caller. 
  104.  * Caller specifies "errnoflag". 
  105.  */  
  106. static void  
  107. err_doit(int errnoflag, int error, const char *fmt, va_list ap)  
  108. {  
  109.     char    buf[MAXLINE];  
  110.    vsnprintf(buf, MAXLINE, fmt, ap);  
  111.    if (errnoflag)  
  112.        snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s",  
  113.          strerror(error));  
  114.    strcat(buf, "/n");  
  115.    fflush(stdout);     /* in case stdout and stderr are the same */  
  116.    fputs(buf, stderr);  
  117.    fflush(NULL);       /* flushes all stdio output streams */  
  118. }  
原创粉丝点击