关于path_alloc()函数
2016-07-08 09:36
288 查看
在《UNIX环境高级编程》学习中,在第四章的程序清单4-7中,发现了一个“错误”。发现即使apue.h中声明了path_alloc()这个函数,但是在编译程序清单时还是会提示path_alloc()未定义。这其实并不是错误,因为我本来就没有这个函数的实现。
如果我在第二章看得仔细的话,就能发现原来path_alloc()的实现在程序清单2-3中。
现在自己把函数实现贴出来,以示警告:
#include "apue.h"
#include
#include
#ifdef PATH_MAX
static int
pathmax = PATH_MAX;
#else
static int
pathmax = 0;
#endif
#define SUSV3
200112L
static long
posix_version = 0;
#define PATH_MAX_GUESS1024
char *path_alloc(int *sizep)
{
char *ptr;
int size;
if(posix_version == 0)
posix_version = sysconf(_SC_VERSION);
if(pathmax == 0){
errno = 0;
if((pathmax = pathconf("/", _PC_PATH_MAX)) < 0){
if(errno == 0)
pathmax = PATH_MAX_GUESS;
else err_sys("pathconf error for _PC_PATH_MAX");
} else {
pathmax++;
}
}
if(posix_version < SUSV3)
size = pathmax + 1;
else size = pathmax;
if((ptr = malloc(size)) == NULL)
err_sys("malloc error for pathname");
if(sizep != NULL)
*sizep = size;
return(ptr);
}
如果我在第二章看得仔细的话,就能发现原来path_alloc()的实现在程序清单2-3中。
现在自己把函数实现贴出来,以示警告:
#include "apue.h"
#include
#include
#ifdef PATH_MAX
static int
pathmax = PATH_MAX;
#else
static int
pathmax = 0;
#endif
#define SUSV3
200112L
static long
posix_version = 0;
#define PATH_MAX_GUESS1024
char *path_alloc(int *sizep)
{
char *ptr;
int size;
if(posix_version == 0)
posix_version = sysconf(_SC_VERSION);
if(pathmax == 0){
errno = 0;
if((pathmax = pathconf("/", _PC_PATH_MAX)) < 0){
if(errno == 0)
pathmax = PATH_MAX_GUESS;
else err_sys("pathconf error for _PC_PATH_MAX");
} else {
pathmax++;
}
}
if(posix_version < SUSV3)
size = pathmax + 1;
else size = pathmax;
if((ptr = malloc(size)) == NULL)
err_sys("malloc error for pathname");
if(sizep != NULL)
*sizep = size;
return(ptr);
}
相关文章推荐
- Merge k Sorted Lists
- PlSql链接不上Oracle问题跟踪
- 内存管理 & 内存优化技巧 浅析
- [Python]更加Pythonic的多个List合并和Python的安利
- C# 实现是否设置指定程序为开机启动程序
- hbase hive
- 快速掌握Java语言
- nfs,ftp,telnet区别
- IE9 IE8 ajax跨域问题的快速解决方法
- stl string常用函数
- 关于Kendo ui 的简单介绍
- Java NIO原理图文分析及代码实现
- eclipse下执行mapreduce的配置和join
- Activity
- Android逆向之旅--运行时修改内存中的Dalvik指令来改变代码逻辑
- 数据库唯一性约束
- t
- 网络编程小知识
- 斗战神怎么强化装备
- POJ 2492 A Bug's Life