《unix环境高级编程》--- Unix标准化及实现

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/u012319493/article/details/80323692
#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 %d\n", ARG_MAX+0);  /* exec函数的参数最大长度(字节数)*/
 #else
    printf("no symbol for ARG_MAX\n");
 #endif
 #ifdef _SC_ARG_MAX
    pr_sysconf("ARG_MAX = ", _SC_ARG_MAX);
 #else
    printf("no symbol for _SC_ARG_MAX\n");
 #endif
 /* similar processing for all the rest of the sysconf symbols... */
 #ifdef MAX_CANON
    printf("MAX_CANON defined to be %d\n", MAX_CANON+0);
 #else
    printf("no symbol for MAX_CANON\n");
 #endif
 #ifdef _PC_MAX_CANON
    pr_pathconf("MAX_CANON = ", argv[1], _PC_MAX_CANON);
 #else
    printf("no symbol for _PC_MAX_CANON\n");
 #endif
 /*similar processing for all the rest of the pathconf symbols. */
    exit(0);
}

static void pr_sysconf(char *mesg, int name)
{
    long val;
    fputs(mesg, stdout);
    errno = 0;

    /*
        long sysconf(int  name) 
        name: 标识系统限制, _SC_开头
    */
    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(" %ld\n", val);
    }
}

static void pr_pathconf(char *mesg, char *path, int name)
{
    long val;
    fputs(mesg, stdout);
    errno = 0;

    /*
        long pathconf(const char *pathname, int name);
        pathname: 路径名
        name: 标识系统限制, _PC_开头
    */
    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", stdout);
        }
    }   
    else    
    {
        printf(" %ld\n", val);
    }
}

这里写图片描述

为路径名动态地分配空间

#include "apue.h"
#include <errno.h>
#include <limits.h>

#ifdef PATH_MAX
static int pathmax = PATH_MAX;
#else
static int pathmax = 0;
#endif

#define SUSV3 200112L

static long posix_version = 0;

/* If PATH_MAX is indeterminate, no guarantee this is adequate */
#define PATH_MAX_GUESS 1024

char *path_alloc(size_t *sizep) /* also return allocated size, if nonull */
{
    char *ptr;
    size_t size;

    if(posix_version == 0)
        posix_version = sysconf(_SC_VERSION);

    if(pathmax == 0) /* first time through */
    {
        errno = 0;

        /* 根目录 “/” 开辟的空间 */
        if((pathmax = pathconf("/", _PC_PATH_MAX)) < 0)
        {
            if(errno == 0) /* pathconf 没有出错 */
                pathmax = PATH_MAX_GUESS; /* it's indeterminate */
            else
                err_sys("pathconf error for _PC_PATH_MAX");
        }
        else
        {
            pathmax++;  /* add one since it's relative to root */
        }
    }

    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);
}

确定文件描述符数

#include "apue.h"
#include <errno.h>
#include <limits.h>


#ifdef OPEN_MAX
static long openmax = OPEN_MAX;
#else
static long openmax = 0;
#endif

/*
   If OPEN_MAX is indeterminate, we're not 
   guaranteed that this is adequate
*/
#define OPEN_MAX_GUESS 256

long open_max(void)
{
    if(openmax == 0) /* first time through */
    {
        errno = 0;
        if(openmax = sysconf(_SC_OPEN_MAX)) < 0)
        {
            if(errno == 0)
                openmax = OPEN_MAX_GUESS;  /* it's indeterminate */
            else
                err_sys("sysconf error fro _SC_OPEN_MAX");
        }
    }

    return openmax;
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页