关于UNIX/Linux下安装《UNIX环境高级编程》源代码的问题
《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环境高级编程》源代码的问题的更多相关文章
- Java java jdk在Linux下安装与环境变量的配置
java jdk在Linux下安装与环境变量的配置 by:授客 QQ:1033553122 linux环境:CentOS-6.0-x86_64-bin-DVD1.iso [root@localhost ...
- Linux下安装php环境并且配置Nginx支持php-fpm模块[www]
Linux下安装php环境并且配置Nginx支持php-fpm模块 http://www.cnblogs.com/freeweb/p/5425554.html 5分钟搭建 nginx +php --- ...
- Linux下安装Java环境配置
1.下载安装文件 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 2. ...
- linux下安装python环境
1.linux下安装python3 a. 准备编译环境(环境如果不对的话,可能遇到各种问题,比如wget无法下载https链接的文件) yum groupinstall 'Development To ...
- go系列(1)- linux下安装go环境
安装GO 打开安装包下载地址,查看linux下go的最新版本 https://golang.google.cn/dl/ 经查看go的最新版本为go1.11.4.linux-amd64.tar.gz 右 ...
- Linux下安装Go环境
登录Linux Mac或Linux的用户可以用命令ssh root@xxx.xxx.xxx.xxx登录主机Window的用户可以使用SecureCRT登录主机虚拟机用户直接打开你的虚拟机 安装Go环境 ...
- Linux下安装python3环境搭建
Linux下python3环境搭建 Linux安装软件有哪些方式? rpm软件包 手动安装 拒绝此方式 需要手动解决依赖关系 yum自动化安装 自动处理依赖关系 非常好用 源代码编译安装,可自定义的功 ...
- linux下安装apache环境
Centos6.5 64位下安装apache php mysql(安装包版本一定要正确,网上很多都不准,不要用32位的安装包装再64位上,后期有很多问题,下载安装后一定要检出版本,有的网友操蛋,把3 ...
- Linux下安装Java环境配置步骤详述
0.下载jdk8 登录网址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html选择对 ...
随机推荐
- 利用cca进行fmri分析
在肖柯的硕士毕业论文中<基于CCA的fMRI时空模型数据处理方法的研究>,他的总体思路是利用cca提取出fmri图像在时间和空间上两个相关系数,也就是两个特征,然后利用pca,对这两个特征 ...
- 配置Node Manager启动同一机器的两个域
进入/u01/FMW_Home/weblogic/wlserver_10.3/server/bin目录,在需要配置的节点上启动node manager,以weblogic用户登录 c d /u01/F ...
- OpenCV图像金字塔
图像金字塔 目标 本文档尝试解答如下问题: 如何使用OpenCV函数 pyrUp 和 pyrDown 对图像进行向上和向下采样. 原理 Note 以下内容来自于Bradski和Kaehler的大作: ...
- iOS: 复选框使用---第三方框架SSCheckBoxView-master
在iOS开发中对应用程序进行设置时一般都用UISwitch,偶尔显得单调,这时候你可以选择使用第三方开源类库SSCheckBoxView . SSCheckBoxView是一个可用在iOS上一个复选框 ...
- Java中文语言处理HanLP
官网:http://hanlp.linrunsoft.com/ 1.中文分词:http://hanlp.linrunsoft.com/doc/_build/html/segment.html 2.摘要 ...
- python中子类调用父类的方法
1子类调用父类构造方法 class Animal(object): def __init__(self): print("init Animal class~") def run( ...
- C# 继承实现父类方法、重写、重载
继承是派生类(子类)去实现(重写<override>.重构<new>)基类(父类)的方法或属性.从而获取在派生类中要实现的功能. 子类调用父类构造方法,在父类中有个实现姓名和年 ...
- HDU-2665-Kth number(划分树)
Problem Description Give you a sequence and ask you the kth big number of a inteval. Input The fir ...
- 如何在 MyEclipse 下用 Gradle 构建 Web Application
一.安装 Gradle 插件,参考我的另一篇文章:MyEclipse 集成 Gradle开发环境 二.新建一个 Web Project, 在项目根目录下新建 build.gradle 文件, 并向其中 ...
- PHP面向对象之接口 (interface)
1.使用接口,接口中指定了某个类必须实现的某些方法,这些方法都是空的(不需要定义这些方法的具体内容) 2. 要实现一个接口用关键字implements,类中必须包含接口中所有的方法,否则会出现一个致命 ...