GNU glibc库中函数执行成功与失败的返回值
2017-08-08 22:38
260 查看
最初学习C语言编程的时候,主函数是可以没有返回值的。工作之后,重新接触的编译基本上都要求主函数有一个int类型的返回值。起初我有些不明白,为什么很多历程中的返回值是0,后来在一个教程中看到有人返回值是一个宏,追本溯源之后才知道0原来是执行成功的返回值。
具体的宏定义如下:
#defineEXIT_SUCCESS 0
#defineEXIT_FAILURE 1
上面这是对于主函数的定义,那么对于一般函数呢?比如说,刚刚接触到的access函数。我查找了编译器中的头文件,没有找出一个比较合理的信息。借助搜索引擎,找到了gnu的网页上的解释如下:
The return valueis 0 if the access is permitted, and -1 otherwise. (Inother words, treated as a predicate function, access returns true ifthe requested access is denied.)
从上面的解释可以知道:成功的时候返回0,但是失败的时候返回-1。现在稍微明确一点了,只是还是不清楚是不是这是一个比较共性的方式。或许查看一下这个功能实现的源代码会更能够弄清楚这些,也许执行的成功与失败都是定义好的宏,然后作为函数执行的返回值。从GNU的FTP下载了源代码,找到了相应的函数如下:
/* Copyright (C)1991-2017 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you canredistribute it and/or
modify it under the terms of the GNU LesserGeneral Public
License as published by the Free SoftwareFoundation; either
version 2.1 of the License, or (at youroption) any later version.
The GNU C Library is distributed in the hopethat it will be useful,
but WITHOUT ANY WARRANTY; without even theimplied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULARPURPOSE. See the GNU
Lesser General Public License for moredetails.
You should have received a copy of the GNULesser General Public
License along with the GNU C Library; ifnot, see
<http://www.gnu.org/licenses/>. */
#include<errno.h>
#include<stddef.h>
#include<unistd.h>
/* Test for accessto FILE without setting errno. */
int
__access_noerrno(const char *file, int type)
{
return -1;
}
/* Test for accessto FILE. */
int
__access (constchar *file, int type)
{
if (file == NULL || (type &~(R_OK|W_OK|X_OK|F_OK)) != 0)
{
__set_errno (EINVAL);
return -1;
}
__set_errno (ENOSYS);
return -1;
}
stub_warning(access)
weak_alias(__access, access)
这下子算是终于死心了,其实这个函数的成功和失败的返回值没有什么宏。这当然还是不能够排除这是大家通用的一个数值对儿,但是至少从规范性上来说,似乎是少了一种强制性的约定。
具体的宏定义如下:
#defineEXIT_SUCCESS 0
#defineEXIT_FAILURE 1
上面这是对于主函数的定义,那么对于一般函数呢?比如说,刚刚接触到的access函数。我查找了编译器中的头文件,没有找出一个比较合理的信息。借助搜索引擎,找到了gnu的网页上的解释如下:
The return valueis 0 if the access is permitted, and -1 otherwise. (Inother words, treated as a predicate function, access returns true ifthe requested access is denied.)
从上面的解释可以知道:成功的时候返回0,但是失败的时候返回-1。现在稍微明确一点了,只是还是不清楚是不是这是一个比较共性的方式。或许查看一下这个功能实现的源代码会更能够弄清楚这些,也许执行的成功与失败都是定义好的宏,然后作为函数执行的返回值。从GNU的FTP下载了源代码,找到了相应的函数如下:
/* Copyright (C)1991-2017 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you canredistribute it and/or
modify it under the terms of the GNU LesserGeneral Public
License as published by the Free SoftwareFoundation; either
version 2.1 of the License, or (at youroption) any later version.
The GNU C Library is distributed in the hopethat it will be useful,
but WITHOUT ANY WARRANTY; without even theimplied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULARPURPOSE. See the GNU
Lesser General Public License for moredetails.
You should have received a copy of the GNULesser General Public
License along with the GNU C Library; ifnot, see
<http://www.gnu.org/licenses/>. */
#include<errno.h>
#include<stddef.h>
#include<unistd.h>
/* Test for accessto FILE without setting errno. */
int
__access_noerrno(const char *file, int type)
{
return -1;
}
/* Test for accessto FILE. */
int
__access (constchar *file, int type)
{
if (file == NULL || (type &~(R_OK|W_OK|X_OK|F_OK)) != 0)
{
__set_errno (EINVAL);
return -1;
}
__set_errno (ENOSYS);
return -1;
}
stub_warning(access)
weak_alias(__access, access)
这下子算是终于死心了,其实这个函数的成功和失败的返回值没有什么宏。这当然还是不能够排除这是大家通用的一个数值对儿,但是至少从规范性上来说,似乎是少了一种强制性的约定。
相关文章推荐
- ###(很难找的页面错误)查询操作不需要 返回 操作成功后失败。【方法不能同时执行两个response返回两次数据。导致页面显示数据失败】
- TransactNamedPipe函数在切换线程时失败,GetLastError 返回 231。 再次 Read 成功。
- 自定义 打印 成功跳转 失败返回 函数
- php调用Linux命令成功执行但函数不返回[已解决]
- system函数返回(如何判断调用的shell命令是否执行成功)
- PHP学习笔记--字符串操作 作者:yezi33 字符串的整理: trim():除去字符串开头和末尾的空格或其他字符。函数执行成功时返回删除了string字符串首部和尾部空格的字符串,发生错误时返回空
- PHP调用python,shell命令执行成功,web运行失败
- validate中remote返回函数中+号导致submitHandler无法执行
- fork函数的两次返回和父子进程的执行顺序简介
- 如何把Action后台是否执行成功返回到页面中
- 可以返回执行结果的system函数加强版本
- 2.6 编写一个函数setbits(x, p ,n, y),该函数返回对x执行下列操作后的结果值: 将x中从第p位开始的n个(二进制)位设置为y中最右边n位的值,x的其余各位保持不变。
- GDI函数执行成功与否的检验方式
- Facebook使用应用登陆成功后回调返回认证失败解决方法
- GetNextDataObject函数执行失败
- 可以返回执行结果的system函数加强版本
- C++获取Lua全局变量和执行Lua多参数多返回值函数
- SQL技巧:如何获取存储过程用return返回的值与判断SQL Server事务是否执行成功?
- Hbuilder新建移动app选择登录模板,登录不成功,可能执行太快,main页面loaded事件尚未触发就执行自定义事件,此时必然会失败
- 判断PHP的exec函数是否执行成功