LoginSignup
0
0

More than 3 years have passed since last update.

C言語 ファイルやパス関係の最大値をプログラムから取得する方法 Mac編

Last updated at Posted at 2020-12-03

取得方法

unistd.hで定義されているpathconfという関数を使います。
直接に環境変数の中には定義されておらず、pathconf関数を実行して取得する必要があります。

SYNOPSIS

     #include <unistd.h>

     long
     fpathconf(int fildes, int name);

     long
     pathconf(const char *path, int name);

一覧

変数 説明
_PC_LINK_MAX The maximum file link count.
_PC_MAX_CANON The maximum number of bytes in terminal canonical input line.
_PC_MAX_INPUT The minimum maximum number of bytes for which space is available in a terminal input queue.
_PC_NAME_MAX The maximum number of bytes in a file name.
_PC_PATH_MAX The maximum number of bytes in a pathname.
_PC_PIPE_BUF The maximum number of bytes which will be written atomically to a pipe.
_PC_CHOWN_RESTRICTED Return 1 if appropriate privileges are required for the chown(2) system call, otherwise 0.
_PC_NO_TRUNC Return 1 if file names longer than KERN_NAME_MAX are truncated.
_PC_VDISABLE Returns the terminal character disabling value.
_PC_XATTR_SIZE_BITS Returns the number of bits used to store maximum extended attribute size in bytes.
For example, if the maximum attribute size supported by a file system is 128K, the value returned will be 18.
However a value 18 can mean that the maximum attribute size can be anywhere from (256KB - 1) to 128KB.
As a special case, the resource fork can have much larger size, and some file system specific extended attributes can have smaller and preset size; for example, Finder Info is always 32 bytes.
_PC_MIN_HOLE_SIZE If a file system supports the reporting of holes (see lseek(2)), pathconf() and fpathconf() return a positive number that represents the minimum hole size returned in bytes.
The offsets of holes returned will be aligned to this same value.
A special value of 1 is returned if the file system does not specify the minimum hole size but still reports holes.

使用例

#include <unistd.h>
#include <stdio.h>

int main()
{
    printf("_PC_LINK_MAX         : %lu\n", pathconf(".", _PC_LINK_MAX));
    printf("_PC_MAX_CANON        : %lu\n", pathconf(".", _PC_MAX_CANON));
    printf("_PC_MAX_INPUT        : %lu\n", pathconf(".", _PC_MAX_INPUT));
    printf("_PC_NAME_MAX         : %lu\n", pathconf(".", _PC_NAME_MAX));
    printf("_PC_PATH_MAX         : %lu\n", pathconf(".", _PC_PATH_MAX));
    printf("_PC_PIPE_BUF         : %lu\n", pathconf(".", _PC_PIPE_BUF));
    printf("_PC_CHOWN_RESTRICTED : %lu\n", pathconf(".", _PC_CHOWN_RESTRICTED));
    printf("_PC_NO_TRUNC         : %lu\n", pathconf(".", _PC_NO_TRUNC));
    printf("_PC_VDISABLE         : %lu\n", pathconf(".", _PC_VDISABLE));
    printf("_PC_XATTR_SIZE_BITS  : %lu\n", pathconf(".", _PC_XATTR_SIZE_BITS));
    printf("_PC_MIN_HOLE_SIZE    : %lu\n", pathconf(".", _PC_MIN_HOLE_SIZE));
    return (0);
}

実行結果

_PC_LINK_MAX         : 32767
_PC_MAX_CANON        : 18446744073709551615
_PC_MAX_INPUT        : 18446744073709551615
_PC_NAME_MAX         : 255
_PC_PATH_MAX         : 1024
_PC_PIPE_BUF         : 512
_PC_CHOWN_RESTRICTED : 200112
_PC_NO_TRUNC         : 200112
_PC_VDISABLE         : 18446744073709551615
_PC_XATTR_SIZE_BITS  : 64
_PC_MIN_HOLE_SIZE    : 4096

エラー処理について

ちゃんとエラー処理をする書き方だと、以下のようになります。

pathconf() - 構成可能パス名変数の判別から借りてきました。

/*

CELEBP01
This example determines the maximum number of characters in a file name.

*/

#define _POSIX_SOURCE
#include <errno.h>
#include <unistd.h>
#undef _POSIX_SOURCE
#include <stdio.h>

int main()
{
    long result;

    errno = 0;
    puts("examining NAME_MAX limit for root filesystem");
    if ((result = pathconf(".", _PC_NAME_MAX)) == -1)
        if (errno == 0)
            puts("There is no limit to NAME_MAX.");
        else perror("pathconf() error");
    else
        printf("NAME_MAX is %ld¥n", result);
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0