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

《Unix环境高级编程》中的一些问题之 pathalloc()函数问题

2012-04-18 10:54 190 查看
注:在《Unix环境高级编程》这本书中的程序都是自己敲入的,因此会遇到的一些问题,在此与大家分享。

问题:

在《Unix环境高级编程》英文原版Figure4.22和Figure4.24(中文版例子分别是4-7和4-9),直接在VIM中敲入程序Figure4.22后,运行gcc 4-22.c error.c -o a.4-22 发现编译并不能通过,会出现如下错误:



这是由于path_alloc()函数只声明未定义的原因,在《Unix环境高级编程》第一版中该函数包含在“ourhdr.h”中,在《Unix环境高级编程》第二版包含在“apue.h”中,书中附录只有函数声明没有函数定义。

解决办法:
我们只需在编译时加上pathalloc.c,就可以解决问题:(在vim中输入
gcc 4-22.c error.c pathalloc.c -o a.4-22编译即可通过)



代码:
1.
pathalloc.c




2.
4-22.c





3. error.c

#include <errno.h> /* for definition of errno */

#include <stdarg.h> /* ANSI C header file */

#include "ourhdr.h"

static void err_doit(int, const char *, va_list);

char *pname = NULL; /* caller can set this from argv[0] */

/* 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, fmt, ap);

va_end(ap);

return;

}

/* 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, 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, 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, fmt, ap);

va_end(ap);

return;

}

/* 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, fmt, ap);

va_end(ap);

exit(1);

}

/* Print a message and return to caller.

* Caller specifies "errnoflag". */

static void

err_doit(int errnoflag, const char *fmt, va_list ap)

{

int errno_save;

char buf[MAXLINE];

errno_save = errno; /* value caller might want printed */

vsprintf(buf, fmt, ap);

if (errnoflag)

sprintf(buf+strlen(buf), ": %s", strerror(errno_save));

strcat(buf, "\n");

fflush(stdout); /* in case stdout and stderr are the same */

fputs(buf, stderr);

fflush(stderr); /* SunOS 4.1.* doesn't grok NULL argument */

return;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: