1. UNIX系统的限制:sysconf 和 pathconf 和 fpathconf
UNIX系统实现定义了很多幻数和常量。如何确定这些幻数和常量的限制呢?限制分为两种类型:
1> 编译时限制(如:短整型的最大值)
2> 运行时限制(如:文件名有多少个字符)
编译时限制可在头文件中定义,然后编译时包含这些头文件即可。运行时限制要求进程调用一个函数获得。这些函数是:
#include <unistd.h>
long sysconf( int name ); //用于标识系统限制中以__SC__开始的常量
//下面两个用于标识系统限制中以__PC__开始的常量
long pathconf( const char *pathname, int name );
long fpathconf( int filedes, int name );
注:返回值问题
1> 如果name参数不是一个合适的常量,这三个函数都返回-1,并把errno置为EINVAL。
①对sysconf的限制及name参数
②对pathconf和fpathconf的限制及name参数
2> 有些name会返回一个变量值(>=0)或者提示该值是不确定的。不确定的值通过返回-1来体现,而不改变errno的值。
使用实例: ①.为路径名动态地分配空间
#include<errno.h>
#include<limits.h>
#include<unistd.h>
//先判断是否在编译时提供了PATH_MAX值
#ifdef PATH_MAX
static int pathmax=PATH_MAX;
#else
static int pathmax=0;
#endif
#define PATH_MAX_GUESS 256
//size!=null则用于保存开辟空间大小
char * path_alloc(int *size){
char *ptr;
if(pathmax==0){
errno=0;
if( (pathmax=pathconf("/",_PC_PATH_MAX)) <0){
//pathconf返回了负数,但是并没有出错,只是说明该值不确定性
if(errno==0){
pathmax=PATH_MAX_GUESS;
}
else{
perror("Error in pathconf .");
}
}
else{
//解决 根目录 “/” 开辟的空间
pathmax++;
}
}
//因为limits中没有为字符串的null预留空间
if( (ptr=malloc(pathmax+1)) ==null){
perror("Error in malloc .");
}
if(size!=null){
*size=pathmax+1;
}
return ptr;
}
②.最大打开文件数
//关闭所有文件描述符
#include <limits.h>
#include <unistd.h>
#include <errno.h>
#ifdef OPEN_MAX
static openmax=OPEN_MAX;
#else
static openmax=0;
#endif
#define OPEN_MAX_GUESS=256;
int open_max(void){
if(openmax==0){
errno=0;
if( (openmax=sysconf(_SC_OPEN_MAX)) <0){
if(errno==0){
openmax=OPEN_MAX_GUESS;
}
else{
perror("Error in sysconf .");
}
}
}
return openmax;
}
2.基本数据类型:一些常用的基本系统数据类型![](https://img-blog.csdn.net/20170920091747555?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGMyNTAxMjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)