[APUE]第二章UNIX标准及实现
这一章的阅读相对枯燥,20世纪80年代UNIX版本种类剧增以及他们之间的差别的扩大,导致很多用户呼吁对其进行标准化。 #include<unistd.h>
long sysconf(int name);
long pathconf(const char *pathname,int name);
long fpathname(int fd,int name);
后两个函数的差别是:一个用路径名作为参数,一个则取文件描述符作为参数。 #include "apue.h"
#include <errno.h>
#include <limits.h>
static void pr_sysconf(char *,int);
static void pr_pathconf(char *,char *,int);
int
main(int argc,char *argv[])
{
if (argc != 2)
err_quit("usage: a.out <dirname>");
#ifdef ARG_MAX
printf("ARG_MAX defined to be %ldn",(long)ARG_MAX+0);
#else
printf("no symbol for ARG_MAXn");
#endif
#ifdef _SC_ARG_MAX
pr_sysconf("ARG_MAX =",_SC_ARG_MAX);
#else
printf("no symbol for _SC_ARG_MAXn");
#endif
#ifdef ATEXIT_MAX
printf("ATEXIT_MAX defined to be %ldn",(long)ATEXIT_MAX+0);
#else
printf("no symbol for ATEXIT_MAXn");
#endif
#ifdef _SC_ATEXIT_MAX
pr_sysconf("ATEXIT_MAX =",_SC_ATEXIT_MAX);
#else
printf("no symbol for _SC_ATEXIT_MAXn");
#endif
#ifdef CHARCLASS_NAME_MAX
printf("CHARCLASS_NAME_MAX defined to be %ldn",(long)CHARCLASS_NAME_MAX+0);
#else
printf("no symbol for CHARCLASS_NAME_MAXn");
#endif
#ifdef _SC_CHARCLASS_NAME_MAX
pr_sysconf("CHARCLASS_NAME_MAX =",_SC_CHARCLASS_NAME_MAX);
#else
printf("no symbol for _SC_CHARCLASS_NAME_MAXn");
#endif
#ifdef CHILD_MAX
printf("CHILD_MAX defined to be %ldn",(long)CHILD_MAX+0);
#else
printf("no symbol for CHILD_MAXn");
#endif
#ifdef _SC_CHILD_MAX
pr_sysconf("CHILD_MAX =",_SC_CHILD_MAX);
#else
printf("no symbol for _SC_CHILD_MAXn");
#endif
#ifdef CLOCKTICKSPERSECOND /*clock ticks/second*/
printf("CLOCKTICKSPERSECOND /*clock ticks/second*/ defined to be %ldn",(long)CLOCKTICKSPERSECOND /*clock ticks/second*/+0);
#else
printf("no symbol for CLOCKTICKSPERSECOND /*clock ticks/second*/n");
#endif
#ifdef _SC_CLK_TCK
pr_sysconf("CLOCKTICKSPERSECOND /*clock ticks/second*/ =",_SC_CLK_TCK);
#else
printf("no symbol for _SC_CLK_TCKn");
#endif
#ifdef COLL_WEIGHTS_MAX
printf("COLL_WEIGHTS_MAX defined to be %ldn",(long)COLL_WEIGHTS_MAX+0);
#else
printf("no symbol for COLL_WEIGHTS_MAXn");
#endif
#ifdef _SC_COLL_WEIGHTS_MAX
pr_sysconf("COLL_WEIGHTS_MAX =",_SC_COLL_WEIGHTS_MAX);
#else
printf("no symbol for _SC_COLL_WEIGHTS_MAXn");
#endif
#ifdef DELAYTIMER_MAX
printf("DELAYTIMER_MAX defined to be %ldn",(long)DELAYTIMER_MAX+0);
#else
printf("no symbol for DELAYTIMER_MAXn");
#endif
#ifdef _SC_DELAYTIMER_MAX
pr_sysconf("DELAYTIMER_MAX =",_SC_DELAYTIMER_MAX);
#else
printf("no symbol for _SC_DELAYTIMER_MAXn");
#endif
#ifdef HOST_NAME_MAX
printf("HOST_NAME_MAX defined to be %ldn",(long)HOST_NAME_MAX+0);
#else
printf("no symbol for HOST_NAME_MAXn");
#endif
#ifdef _SC_HOST_NAME_MAX
pr_sysconf("HOST_NAME_MAX =",_SC_HOST_NAME_MAX);
#else
printf("no symbol for _SC_HOST_NAME_MAXn");
#endif
#ifdef IOV_MAX
printf("IOV_MAX defined to be %ldn",(long)IOV_MAX+0);
#else
printf("no symbol for IOV_MAXn");
#endif
#ifdef _SC_IOV_MAX
pr_sysconf("IOV_MAX =",_SC_IOV_MAX);
#else
printf("no symbol for _SC_IOV_MAXn");
#endif
#ifdef LINE_MAX
printf("LINE_MAX defined to be %ldn",(long)LINE_MAX+0);
#else
printf("no symbol for LINE_MAXn");
#endif
#ifdef _SC_LINE_MAX
pr_sysconf("LINE_MAX =",_SC_LINE_MAX);
#else
printf("no symbol for _SC_LINE_MAXn");
#endif
#ifdef LOGIN_NAME_MAX
printf("LOGIN_NAME_MAX defined to be %ldn",(long)LOGIN_NAME_MAX+0);
#else
printf("no symbol for LOGIN_NAME_MAXn");
#endif
#ifdef _SC_LOGIN_NAME_MAX
pr_sysconf("LOGIN_NAME_MAX =",_SC_LOGIN_NAME_MAX);
#else
printf("no symbol for _SC_LOGIN_NAME_MAXn");
#endif
#ifdef NGROUPS_MAX
printf("NGROUPS_MAX defined to be %ldn",(long)NGROUPS_MAX+0);
#else
printf("no symbol for NGROUPS_MAXn");
#endif
#ifdef _SC_NGROUPS_MAX
pr_sysconf("NGROUPS_MAX =",_SC_NGROUPS_MAX);
#else
printf("no symbol for _SC_NGROUPS_MAXn");
#endif
#ifdef OPEN_MAX
printf("OPEN_MAX defined to be %ldn",(long)OPEN_MAX+0);
#else
printf("no symbol for OPEN_MAXn");
#endif
#ifdef _SC_OPEN_MAX
pr_sysconf("OPEN_MAX =",_SC_OPEN_MAX);
#else
printf("no symbol for _SC_OPEN_MAXn");
#endif
#ifdef PAGESIZE
printf("PAGESIZE defined to be %ldn",(long)PAGESIZE+0);
#else
printf("no symbol for PAGESIZEn");
#endif
#ifdef _SC_PAGESIZE
pr_sysconf("PAGESIZE =",_SC_PAGESIZE);
#else
printf("no symbol for _SC_PAGESIZEn");
#endif
#ifdef PAGE_SIZE
printf("PAGE_SIZE defined to be %ldn",(long)PAGE_SIZE+0);
#else
printf("no symbol for PAGE_SIZEn");
#endif
#ifdef _SC_PAGE_SIZE
pr_sysconf("PAGE_SIZE =",_SC_PAGE_SIZE);
#else
printf("no symbol for _SC_PAGE_SIZEn");
#endif
#ifdef RE_DUP_MAX
printf("RE_DUP_MAX defined to be %ldn",(long)RE_DUP_MAX+0);
#else
printf("no symbol for RE_DUP_MAXn");
#endif
#ifdef _SC_RE_DUP_MAX
pr_sysconf("RE_DUP_MAX =",_SC_RE_DUP_MAX);
#else
printf("no symbol for _SC_RE_DUP_MAXn");
#endif
#ifdef RTSIG_MAX
printf("RTSIG_MAX defined to be %ldn",(long)RTSIG_MAX+0);
#else
printf("no symbol for RTSIG_MAXn");
#endif
#ifdef _SC_RTSIG_MAX
pr_sysconf("RTSIG_MAX =",_SC_RTSIG_MAX);
#else
printf("no symbol for _SC_RTSIG_MAXn");
#endif
#ifdef SEM_NSEMS_MAX
printf("SEM_NSEMS_MAX defined to be %ldn",(long)SEM_NSEMS_MAX+0);
#else
printf("no symbol for SEM_NSEMS_MAXn");
#endif
#ifdef _SC_SEM_NSEMS_MAX
pr_sysconf("SEM_NSEMS_MAX =",_SC_SEM_NSEMS_MAX);
#else
printf("no symbol for _SC_SEM_NSEMS_MAXn");
#endif
#ifdef SEM_VALUE_MAX
printf("SEM_VALUE_MAX defined to be %ldn",(long)SEM_VALUE_MAX+0);
#else
printf("no symbol for SEM_VALUE_MAXn");
#endif
#ifdef _SC_SEM_VALUE_MAX
pr_sysconf("SEM_VALUE_MAX =",_SC_SEM_VALUE_MAX);
#else
printf("no symbol for _SC_SEM_VALUE_MAXn");
#endif
#ifdef SIGQUEUE_MAX
printf("SIGQUEUE_MAX defined to be %ldn",(long)SIGQUEUE_MAX+0);
#else
printf("no symbol for SIGQUEUE_MAXn");
#endif
#ifdef _SC_SIGQUEUE_MAX
pr_sysconf("SIGQUEUE_MAX =",_SC_SIGQUEUE_MAX);
#else
printf("no symbol for _SC_SIGQUEUE_MAXn");
#endif
#ifdef STREAM_MAX
printf("STREAM_MAX defined to be %ldn",(long)STREAM_MAX+0);
#else
printf("no symbol for STREAM_MAXn");
#endif
#ifdef _SC_STREAM_MAX
pr_sysconf("STREAM_MAX =",_SC_STREAM_MAX);
#else
printf("no symbol for _SC_STREAM_MAXn");
#endif
#ifdef SYMLOOP_MAX
printf("SYMLOOP_MAX defined to be %ldn",(long)SYMLOOP_MAX+0);
#else
printf("no symbol for SYMLOOP_MAXn");
#endif
#ifdef _SC_SYMLOOP_MAX
pr_sysconf("SYMLOOP_MAX =",_SC_SYMLOOP_MAX);
#else
printf("no symbol for _SC_SYMLOOP_MAXn");
#endif
#ifdef TIMER_MAX
printf("TIMER_MAX defined to be %ldn",(long)TIMER_MAX+0);
#else
printf("no symbol for TIMER_MAXn");
#endif
#ifdef _SC_TIMER_MAX
pr_sysconf("TIMER_MAX =",_SC_TIMER_MAX);
#else
printf("no symbol for _SC_TIMER_MAXn");
#endif
#ifdef TTY_NAME_MAX
printf("TTY_NAME_MAX defined to be %ldn",(long)TTY_NAME_MAX+0);
#else
printf("no symbol for TTY_NAME_MAXn");
#endif
#ifdef _SC_TTY_NAME_MAX
pr_sysconf("TTY_NAME_MAX =",_SC_TTY_NAME_MAX);
#else
printf("no symbol for _SC_TTY_NAME_MAXn");
#endif
#ifdef TZNAME_MAX
printf("TZNAME_MAX defined to be %ldn",(long)TZNAME_MAX+0);
#else
printf("no symbol for TZNAME_MAXn");
#endif
#ifdef _SC_TZNAME_MAX
pr_sysconf("TZNAME_MAX =",_SC_TZNAME_MAX);
#else
printf("no symbol for _SC_TZNAME_MAXn");
#endif
#ifdef FILESIZEBITS
printf("FILESIZEBITS defined to be %ldn",(long)FILESIZEBITS+0);
#else
printf("no symbol for FILESIZEBITSn");
#endif
#ifdef _PC_FILESIZEBITS
pr_pathconf("FILESIZEBITS =",argv[1],_PC_FILESIZEBITS);
#else
printf("no symbol for _PC_FILESIZEBITSn");
#endif
#ifdef LINK_MAX
printf("LINK_MAX defined to be %ldn",(long)LINK_MAX+0);
#else
printf("no symbol for LINK_MAXn");
#endif
#ifdef _PC_LINK_MAX
pr_pathconf("LINK_MAX =",_PC_LINK_MAX);
#else
printf("no symbol for _PC_LINK_MAXn");
#endif
#ifdef MAX_CANON
printf("MAX_CANON defined to be %ldn",(long)MAX_CANON+0);
#else
printf("no symbol for MAX_CANONn");
#endif
#ifdef _PC_MAX_CANON
pr_pathconf("MAX_CANON =",_PC_MAX_CANON);
#else
printf("no symbol for _PC_MAX_CANONn");
#endif
#ifdef MAX_INPUT
printf("MAX_INPUT defined to be %ldn",(long)MAX_INPUT+0);
#else
printf("no symbol for MAX_INPUTn");
#endif
#ifdef _PC_MAX_INPUT
pr_pathconf("MAX_INPUT =",_PC_MAX_INPUT);
#else
printf("no symbol for _PC_MAX_INPUTn");
#endif
#ifdef NAME_MAX
printf("NAME_MAX defined to be %ldn",(long)NAME_MAX+0);
#else
printf("no symbol for NAME_MAXn");
#endif
#ifdef _PC_NAME_MAX
pr_pathconf("NAME_MAX =",_PC_NAME_MAX);
#else
printf("no symbol for _PC_NAME_MAXn");
#endif
#ifdef PATH_MAX
printf("PATH_MAX defined to be %ldn",(long)PATH_MAX+0);
#else
printf("no symbol for PATH_MAXn");
#endif
#ifdef _PC_PATH_MAX
pr_pathconf("PATH_MAX =",_PC_PATH_MAX);
#else
printf("no symbol for _PC_PATH_MAXn");
#endif
#ifdef PIPE_BUF
printf("PIPE_BUF defined to be %ldn",(long)PIPE_BUF+0);
#else
printf("no symbol for PIPE_BUFn");
#endif
#ifdef _PC_PIPE_BUF
pr_pathconf("PIPE_BUF =",_PC_PIPE_BUF);
#else
printf("no symbol for _PC_PIPE_BUFn");
#endif
#ifdef SYMLINK_MAX
printf("SYMLINK_MAX defined to be %ldn",(long)SYMLINK_MAX+0);
#else
printf("no symbol for SYMLINK_MAXn");
#endif
#ifdef _PC_SYMLINK_MAX
pr_pathconf("SYMLINK_MAX =",_PC_SYMLINK_MAX);
#else
printf("no symbol for _PC_SYMLINK_MAXn");
#endif
#ifdef _POSIX_TIMESTAMP_RESOLUTION
printf("_POSIX_TIMESTAMP_RESOLUTION defined to be %ldn",(long)_POSIX_TIMESTAMP_RESOLUTION+0);
#else
printf("no symbol for _POSIX_TIMESTAMP_RESOLUTIONn");
#endif
#ifdef _PC_TIMESTAMP_RESOLUTION
pr_pathconf("_POSIX_TIMESTAMP_RESOLUTION =",_PC_TIMESTAMP_RESOLUTION);
#else
printf("no symbol for _PC_TIMESTAMP_RESOLUTIONn");
#endif
exit(0);
}
static void
pr_sysconf(char *mesg,int name)
{
long val;
fputs(mesg,stdout);
errno = 0;
if ((val = sysconf(name)) < 0) {
if (errno != 0) {
if (errno == EINVAL)
fputs(" (not supported)n",stdout);
else
err_sys("sysconf error");
} else {
fputs(" (no limit)n",stdout);
}
} else {
printf(" %ldn",val);
}
}
static void
pr_pathconf(char *mesg,char *path,stdout);
errno = 0;
if ((val = pathconf(path,name)) < 0) {
if (errno != 0) {
if (errno == EINVAL)
fputs(" (not supported)n",stdout);
else
err_sys("pathconf error,path = %s",path);
} else {
fputs(" (no limit)n",val);
}
}
功能测试宏如果在编译一个程序时希望他至于POSIX的定义相关,而不与任何实现定义的常量冲突,那么就需要常量_POSIX_C_SOURCE。一旦定义_POSIX_C_SOURCE,所有POSIX.1头文件都使用此常量来排除任何实现转悠的定义。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |