《UNIX环境高级编程(第三版)》是一本广为人知的unix系统编程书籍。

但是,书中的代码示例,要想正确的编译运行,要先做好准备工作:

1.下载源代码

传送门:http://apuebook.com/code3e.html

2.解压源代码

tar xf  src.3e.tar.gz

3.安装libbsd-devel,不然编译的时候回报错

yum install libbsd-devel -y

4.编译

cd apue.3e/
make
cp ./include/apue.h /usr/include/
cp ./lib/libapue.a /usr/local/lib/
cp ./lib/libapue.a /usr/lib/

5.自定义错误头文件

vim /usr/include/myerr.h

#include <errno.h> /* for definition of errno */
#include <stdarg.h> /* ISO C variable aruments */ static void err_doit(int, int, const char *, va_list); /*
* Nonfatal error related to a system call.
* Print a message and return.
*/
void
err_ret(const char *fmt, ...)
{
va_list ap; va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
} /*
* Fatal error related to a system call.
* Print a message and terminate.
*/
void
err_sys(const char *fmt, ...)
{
va_list ap; va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
exit(1);
} /*
* Fatal error unrelated to a system call.
* Error code passed as explict parameter.
* Print a message and terminate.
*/
void
err_exit(int error, const char *fmt, ...)
{
va_list ap; va_start(ap, fmt);
err_doit(1, error, fmt, ap);
va_end(ap);
exit(1);
} /*
* Fatal error related to a system call.
* Print a message, dump core, and terminate.
*/
void
err_dump(const char *fmt, ...)
{
va_list ap; va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
abort(); /* dump core and terminate */
exit(1); /* shouldn't get here */
} /*
* Nonfatal error unrelated to a system call.
* Print a message and return.
*/
void
err_msg(const char *fmt, ...)
{
va_list ap; va_start(ap, fmt);
err_doit(0, 0, fmt, ap);
va_end(ap);
} /*
* Fatal error unrelated to a system call.
* Print a message and terminate.
*/
void
err_quit(const char *fmt, ...)
{
va_list ap; va_start(ap, fmt);
err_doit(0, 0, fmt, ap);
va_end(ap);
exit(1);
} /*
* Print a message and return to caller.
* Caller specifies "errnoflag".
*/
static void
err_doit(int errnoflag, int error, const char *fmt, va_list ap)
{
char buf[MAXLINE];
vsnprintf(buf, MAXLINE, fmt, ap);
if (errnoflag)
snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s",
strerror(error));
strcat(buf, "\n");
fflush(stdout); /* in case stdout and stderr are the same */
fputs(buf, stderr);
fflush(NULL); /* flushes all stdio output streams */
}

测试

写个程序

vim myls.c

#include "apue.h"
#include "myerr.h"
#include <dirent.h>
int
main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;
if (argc != 2)
err_quit("usage: ls directory_name");
if ((dp = opendir(argv[1])) == NULL)
err_sys("can’t open %s", argv[1]);
while ((dirp = readdir(dp)) != NULL)
printf("%s\n", dirp->d_name);
closedir(dp);
exit(0);
}

编译

gcc myls.c 

执行

./a.out /root

关于UNIX/Linux下安装《UNIX环境高级编程》源代码的问题的更多相关文章

  1. Java java jdk在Linux下安装与环境变量的配置

    java jdk在Linux下安装与环境变量的配置 by:授客 QQ:1033553122 linux环境:CentOS-6.0-x86_64-bin-DVD1.iso [root@localhost ...

  2. Linux下安装php环境并且配置Nginx支持php-fpm模块[www]

    Linux下安装php环境并且配置Nginx支持php-fpm模块 http://www.cnblogs.com/freeweb/p/5425554.html 5分钟搭建 nginx +php --- ...

  3. Linux下安装Java环境配置

    1.下载安装文件 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 2. ...

  4. linux下安装python环境

    1.linux下安装python3 a. 准备编译环境(环境如果不对的话,可能遇到各种问题,比如wget无法下载https链接的文件) yum groupinstall 'Development To ...

  5. go系列(1)- linux下安装go环境

    安装GO 打开安装包下载地址,查看linux下go的最新版本 https://golang.google.cn/dl/ 经查看go的最新版本为go1.11.4.linux-amd64.tar.gz 右 ...

  6. Linux下安装Go环境

    登录Linux Mac或Linux的用户可以用命令ssh root@xxx.xxx.xxx.xxx登录主机Window的用户可以使用SecureCRT登录主机虚拟机用户直接打开你的虚拟机 安装Go环境 ...

  7. Linux下安装python3环境搭建

    Linux下python3环境搭建 Linux安装软件有哪些方式? rpm软件包 手动安装 拒绝此方式 需要手动解决依赖关系 yum自动化安装 自动处理依赖关系 非常好用 源代码编译安装,可自定义的功 ...

  8. linux下安装apache环境

    Centos6.5  64位下安装apache php mysql(安装包版本一定要正确,网上很多都不准,不要用32位的安装包装再64位上,后期有很多问题,下载安装后一定要检出版本,有的网友操蛋,把3 ...

  9. Linux下安装Java环境配置步骤详述

    0.下载jdk8 登录网址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html选择对 ...

随机推荐

  1. java POI实现Excel单元格数据换行

    当我们通过POI设置了表格的列宽的时候,如果文字过长,希望文字能够自己折行显示. 截取代码如下: Workbook wb = new XSSFWorkbook(); //or new HSSFWork ...

  2. Postgres间隔大量写IO的解决办法

    概述 为了保证数据可靠性,同时还要保证好的读写性能,以及读写的一致性,经过多年的积累,REDO日志,shared buffer等基本成为关系型数据库的标配.postgres也不例外. 为了保证数据的可 ...

  3. void fun() const{}; const void fun(){}; 和void const fun(){}; 的区别?

    void fun() const{}; const void fun(){}; 和void const fun(){}; 的区别? const void fun(){};和void const fun ...

  4. python编码规范、js编码规范及IDE的检查插件pylint/eslint等

    一.python规范 参考:https://zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/的风格规范和语 ...

  5. linux xfs文件系统无法用readdir获取dirent文件类型d_type则用stat获取暨stat函数讲解

    stat函数讲解 表头文件:    #include <sys/stat.h>             #include <unistd.h>定义函数:    int stat ...

  6. linux解压分卷压缩的zip文件

    zip -s 0 records.zip --out 1.zip unzip 1.zip

  7. intelliJ idea提示api注释

  8. Sense编辑器(Sense Editor)

    Sense编辑器(Sense Editor) 学习了:https://blog.csdn.net/AbnerGong/article/details/50776605 Sense是一个方便的控制台,用 ...

  9. 请远离include_once和require_once[转]

    来自:http://www.poluoluo.com/jzxy/201306/216921.html 尽量使用include, 而不是include_once, 理由是 include_once需要查 ...

  10. python求pi的方法

    来自 #_*_ coding=utf-8 *_* ## {{{ http://code.activestate.com/recipes/578130/ (r5) def pi(places=10): ...