您的位置:首页 > 编程语言

UNIX环境高级编程第二版第一个程序运行成功

2014-07-07 00:06 447 查看
开始看《UNIX环境高级编程 第二版》,谁知在运行第一个程序的时候出现了一些问题,最后看了http://blog.csdn.net/abc5382334/article/details/18518423这个博客的方法解决了。先记录一下,方便日后查看。

首先到这个网站http://www.apuebook.com/code2e.html下载源代码,名字为:src.2e.tar.gz,然后解压到当前目录下,然后进入到/home/ahuang1900/apue.2e,修改Make.defines.linux中的WKDIR=/home/sar/apue.2e,为WKDIR=/home/ahuang1900/apue.2e,这就是我们将要make的工作目录,然后再进入std目录,用vi打开linux.mk,将里面的nawk全部改为awk。之后把include下的apue.h

拷贝到目录/usr/include下,另外代码中有两个函数err_quit和err_sys,这是作者自己定义,因此我们这里需要单独定义一个头文件,如my_err.h

#include "apue.h"
#include <errno.h>  /* for definition of errno */
#include <stdarg.h> /* ISO C variable arguments */

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 specifes "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, " ");
	fflush(stdout);		/* in case stdout and stderr are the same */
	fputs(buf, stderr);
	fflush(NULL); 		/* flushes all stdio output streams */
}


最后我们把书中第一个程序也附上,主要这里多添加了一个#include <my_err.h>

#include "apue.h" //err_quit, err_sys
#include <dirent.h>
#include <my_err.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);
	return 0;
}


测试结果:

ahuang1900@hellen1900:~/apue$ gcc -Wall 1-1.c
ahuang1900@hellen1900:~/apue$ ls
1-1.c  1-1.c~  a.out  file.list
ahuang1900@hellen1900:~/apue$ ./a.out .
a.out
..
1-1.c~
.
1-1.c
file.list
ahuang1900@hellen1900:~/apue$
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: