你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux下把错误捕获函数放入error.h

[日期:2010-08-18] 来源:edsionte.com  作者:edsionte [字体:大 中 小]

从上面的文件存取权限可以看出,root用户可以对其读写,root所在的root组可对其读,其他组用户只可对其读。现在我们更改这个文件的权限,让非root用户可对其修改。

edsionte@edsionte-laptop:/usr/include$ sudo chmod 777 error.h
[sudo] password for edsionte:
edsionte@edsionte-laptop:/usr/include$ ls -l error.h
-rwxrwxrwx 1 root root 2557 2010-06-23 12:11 error.h

修改好权限后,就可以用上面的方法对error.h文件进行修改并保存。
下面进行测试:
测试源码如下。

#include “errno.h”
#include “fcntl.h“
#include "unistd.h”
#include "stdlib.h“
#include "stdio.h”
#include ”sys/types.h“
#include “sys/stat.h”
#include ”string.h“
#include “error.h”

int main()
{
 int fd;
 if((fd=open("example_test.c",O_CREAT|O_EXCL,S_IRUSR|S_IWUSR))==-1)
 {
  my_err1(errno);
//  my_err2("open",__LINE__,errno);
 }
 close(fd);
 return 0;
}
}

首先测试my_err1,结果如下:

edsionte@edsionte-laptop:~/code$ ./my_error
error:  File exists with errno: 17

然后将my_err1注释掉,测试my_err2,结果如下:

edsionte@edsionte-laptop:~/code$ ./my_error
error:  line:17 open():File exists with errno:17

结果和前面介绍捕获函数时的测试结果一样。你以为现在就结束了吗?当然没有,我们要把error.h头文件的权限修改成原样。

edsionte@edsionte-laptop:/usr/include$ sudo chmod 644 error.h
[sudo] password for edsionte:
edsionte@edsionte-laptop:/usr/include$ ls -l error.h
-rw-r--r-- 1 root root 2557 2010-06-23 12:50 error.h

大功告成。

linux
相关资讯       c编程  Linux编程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款