关于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选择对 ...
随机推荐
- [Linux Memory] 用/proc/stat计算cpu的占用率
转载自:http://blog.csdn.net/pppjob/article/details/4060336 在Linux下,CPU利用率分为用户态,系统态和空闲态,分别表示CPU处于用户态执行的时 ...
- [Android Memory] Android系统中查看某个应用当前流量的方法
转载自: http://blog.sina.com.cn/s/blog_628cc2b70101dbyy.html 一.查看原理:某个应用的网络流量数据保存在系统的/proc/uid_stat/$UI ...
- 《Go语言实战》笔记之第三章 ----包
原文地址: http://www.niu12.com/article/10 ####包 所有的.go 文件,除了空行和注释,都应该在第一行声明自己所属的包. 每个包都在一个单独的目录里. 不能把多个包 ...
- SQLAlchemy会话与事务控制:互斥锁和共享锁
关于sqlalchemy,可以细度这个网址:http://www.codexiu.cn/python/SQLAlchemy%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B/73 ...
- logback中打印sql语句
To log SQL statements for particular mybatis mapper set DEBUG (TRACE to see query parameters and res ...
- Android获取前台进程的方法
概述 项目中很多场景交互非常依赖于客户端的前后景状态以及其他一些辅助信息上传,譬如当前新闻在前台(看到的是新闻界面)播放时,语音开启音乐应用,此时我们希望能看到音乐界面,并且音乐在播;而在导航应用在前 ...
- ant font 本地化
要解决的问题1.antd默认iconfont指向的是阿里在公网CDN上部署的url 2.项目需要在本地进行部署,使用的是本地文件的访问方式,希望能内网/离线使用 在ant-design-pro中的配置 ...
- <译>流计算容错
这篇文档描述了Flink的流式计算的容错机制 Introduction Checkpointing Barriers State Exactly Once vs. At Least Once Reco ...
- hp-ux 集群,内存 小记
-----查看hp 集群状态信息 # cmviewcl -v CLUSTER STATUS dbsvr up NODE ...
- 【VB编程】05.MsgBox与InputBox函数
在VBA程序中,数据的输入输出是通过函数实现的,其实现的方式是通过对话框的形式表示出来的.例如MsgBox,Inputbox等,不要误认为是输入输出语句的关键字,其实仅仅是一个普通函数而已. [Msg ...