眾所周知,sysconf 用來回傳某種常量的定義或者資源的上限,前者用于應用動態的判斷系統是否支持某種標準或能力、后者用于決定資源分配的尺寸,
但是你可能不知道的是,sysconf 可以回傳四種狀態:
- 常量定義本身或資源上限 (>=0, 整型值)
- 無限制 (no limit)
- 不支持
- 出錯
那一個小小的 int 回傳型別,如何能容納這許多含義? 各位看過下面這段代碼,就一目了然了:
static void
pr_sysconf (char *msg, int name)
{
long val;
fputs (msg, 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 ("%ld\n", val);
}
conf.c
這段代碼用來列印 sysconf 的回傳值,可以看到基本是通過 '回傳值 + errno' 的方式實作的:
- 回傳值 >= 0: 常量定義或資源本身
- 回傳值 < 0:
- errno == 0: 無限制
- errno != 0:
- errno == EINVAL: 不支持
- 其它:出錯
其實看下 sysconf 的手冊頁的話,確實是這么說的:
RETURN VALUE
If name is invalid, -1 is returned, and errno is set to EINVAL. Otherwise, the value returned is the value of the system resource
and errno is not changed. In the case of options, a positive value is returned if a queried option is available, and -1 if it is
not. In the case of limits, -1 means that there is no definite limit.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/56760.html
標籤:Linux
上一篇:求助KALI沒有圖形界面
下一篇:多選題
