{"id":687,"date":"2010-06-22T22:53:41","date_gmt":"2010-06-23T04:53:41","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=687"},"modified":"2010-06-23T04:18:54","modified_gmt":"2010-06-23T10:18:54","slug":"%e6%8a%8a%e9%94%99%e8%af%af%e6%8d%95%e8%8e%b7%e5%87%bd%e6%95%b0%e6%94%be%e5%85%a5error-h","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/687","title":{"rendered":"\u628a\u9519\u8bef\u6355\u83b7\u51fd\u6570\u653e\u5165error.h"},"content":{"rendered":"<p>\u4e4b\u524d\u6211\u5199\u8fc7\u4e00\u4e2a\u5173\u4e8elinux\u4e0bC\u7f16\u7a0b\u4e2d\u9519\u8bef\u6355\u83b7\u51fd\u6570\u7684<a href=\"http:\/\/edsionte.com\/techblog\/?p=638\" target=\"_blank\">\u6587\u7ae0<\/a>\uff0c\u5373\u6211\u4eec\u53ef\u4ee5\u81ea\u5df1\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u6355\u83b7\u4e00\u4e9b\u9519\u8bef\uff0c\u8ba9\u7a0b\u5e8f\u5458\u5904\u7406\u9519\u8bef\u7684\u65f6\u5019\u66f4\u7b80\u5355\u3002\u5982\u679c\u5c06\u8fd9\u4e2a\u51fd\u6570\u653e\u5165error.h\u5934\u6587\u4ef6\u4e2d\uff0c\u90a3\u4e48\u4ee5\u540e\u5c31\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u4e86\u3002\u672c\u6587\u4fbf\u662f\u548c\u5927\u5bb6\u4e00\u8d77\u8ba8\u8bba\u5982\u4f55\u5c06\u81ea\u5df1\u7f16\u5199\u7684\u9519\u8bef\u6355\u83b7\u51fd\u6570\u653e\u5165\u5934\u6587\u4ef6error.h\u4e2d\u3002<\/p>\n<p>\u5934\u6587\u4ef6\u653e\u5728\/usr\/include\u76ee\u5f55\u4e0b\uff0c\u8fdb\u5165\u8fd9\u4e2a\u76ee\u5f55\uff0c\u7136\u540e\u7528vi\u7f16\u8f91\u5668\u6253\u5f00error.h\u6587\u4ef6\u3002\u8981\u6dfb\u52a0\u7684\u4e24\u4e2a\u51fd\u6570\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:\">void my_err1(int error)\r\n{\r\n\tprintf(\"error:  %s with errno: %d\\n\",strerror(error),error);\r\n\texit(1);\r\n}\r\nvoid my_err2(const char* err_string,int line,int error)\r\n{\r\n\tprintf(\"error:  line:%d %s():%s with errno:%d\\n\",line,err_string,strerror(error),error);\r\n        exit(1);\r\n}<\/pre>\n<p>\u5c06\u4e0a\u8ff0\u4e24\u51fd\u6570\u6dfb\u52a0\u81f3\u6587\u4ef6\u672b\u5c3e\u3002\u7531\u4e8estrerror()\u51fd\u6570\u6d89\u53ca\u5230\u5934\u6587\u4ef6string.h\uff0c\u6240\u4ee5\u5728\u5df2\u6253\u5f00\u7684error.h\u6587\u4ef6\u4e2d\u8fd8\u5f97\u6dfb\u52a0string.h\u5934\u6587\u4ef6\u3002\u5177\u4f53\u6dfb\u52a0\u4f4d\u7f6e\u53ef\u53c2\u8003\u4e0b\u9762\uff1a<\/p>\n<pre class=\"brush:\"> 20 #ifndef _ERROR_H\r\n 21 #define _ERROR_H 1\r\n 22\r\n 23 #include \"features.h\"\r\n 24\r\n 25 \/\/personal addition\r\n 26 #include \"string.h\"\r\n 27 \/\/personal addition end\r\n 28\r\n 29 __BEGIN_DECLS\r\n 30\r\n\r\n 59 \/\/personal addition\r\n 60 \/\/brief function\r\n 61 void my_err1(int error)\r\n 62 {\r\n 63                 printf(\"error:  %s with errno: %d\\n\",strerror(error),error);\r\n 64                 exit(1);\r\n 65 }\r\n 66\r\n 67 \/\/detailed function\r\n 68 void my_err2(const char* err_string,int line,int error)\r\n 69 {\r\n 70                 printf(\"error:  line:%d %s():%s with errno:%d\\n\",line,err_string,strerror(error),error);\r\n 71                 exit(1);\r\n 72 }\r\n 73 \/\/persoanl addition end\r\n 74 __END_DECLS\r\n 75\r\n 76 #endif \/* error.h *\/<\/pre>\n<p>\u5176\u4e2dmy_err1\u548cmy_err2\u663e\u793a\u7684\u9519\u8bef\u4fe1\u606f\u7a0d\u6709\u4e0d\u540c\u3002\u6dfb\u52a0\u597d\u540e\u5c31\u53ef\u4ee5\u4fdd\u5b58\u4e86\u3002\u4f46\u662f\u73b0\u5728\u95ee\u9898\u6765\u4e86:\u6309\u7167\u5e73\u65f6\u6211\u4eec\u7684\u4fdd\u5b58\u65b9\u6cd5\uff1awq\u4fdd\u5b58\u65f6\uff0c\u4f1a\u63d0\u793a\uff1a<strong>E45: \u5df2\u8bbe\u5b9a\u9009\u9879 &#8216;readonly&#8217; (\u8bf7\u52a0 ! \u5f3a\u5236\u6267\u884c)<\/strong>\u3002\u8fd9\u662f\u56e0\u4e3a\u5934\u6587\u4ef6\u5c5e\u4e8e\u7cfb\u7edf\u6587\u4ef6\uff0clinux\u4e3a\u4e86\u5b89\u5168\u8d77\u89c1\uff0c\u4e0d\u5141\u8bb8\u666e\u901a\u7528\u6237\u5bf9\u5176\u8fdb\u884c\u4fee\u6539\u3002\u6211\u4eec\u53ef\u4ee5\u7528ls -l\u67e5\u770b\u5176\u5c5e\u6027\uff1a<\/p>\n<pre class=\"brush:\">edsionte@edsionte-laptop:\/usr\/include$ ls -l error.h\r\n-rw-r--r-- 1 root root 2557 2010-06-23 12:11 error.h<\/pre>\n<p>\u4ece\u4e0a\u9762\u7684\u6587\u4ef6\u5b58\u53d6\u6743\u9650\u53ef\u4ee5\u770b\u51fa\uff0croot\u7528\u6237\u53ef\u4ee5\u5bf9\u5176\u8bfb\u5199\uff0croot\u6240\u5728\u7684root\u7ec4\u53ef\u5bf9\u5176\u8bfb\uff0c\u5176\u4ed6\u7ec4\u7528\u6237\u53ea\u53ef\u5bf9\u5176\u8bfb\u3002\u73b0\u5728\u6211\u4eec\u66f4\u6539\u8fd9\u4e2a\u6587\u4ef6\u7684\u6743\u9650\uff0c\u8ba9\u975eroot\u7528\u6237\u53ef\u5bf9\u5176\u4fee\u6539\u3002<\/p>\n<pre class=\"brush:\">edsionte@edsionte-laptop:\/usr\/include$ sudo chmod 777 error.h\r\n[sudo] password for edsionte:\r\nedsionte@edsionte-laptop:\/usr\/include$ ls -l error.h\r\n-rwxrwxrwx 1 root root 2557 2010-06-23 12:11 error.h<\/pre>\n<p>\u4fee\u6539\u597d\u6743\u9650\u540e\uff0c\u5c31\u53ef\u4ee5\u7528\u4e0a\u9762\u7684\u65b9\u6cd5\u5bf9error.h\u6587\u4ef6\u8fdb\u884c\u4fee\u6539\u5e76\u4fdd\u5b58\u3002<br \/>\n\u4e0b\u9762\u8fdb\u884c\u6d4b\u8bd5\uff1a<br \/>\n\u6d4b\u8bd5\u6e90\u7801\u5982\u4e0b\u3002<\/p>\n<pre class=\"brush:\">#include \u201cerrno.h\u201d\r\n#include \u201cfcntl.h\u201c\r\n#include \"unistd.h\u201d\r\n#include \"stdlib.h\u201c\r\n#include \"stdio.h\u201d\r\n#include \u201dsys\/types.h\u201c\r\n#include \u201csys\/stat.h\u201d\r\n#include \u201dstring.h\u201c\r\n#include \u201cerror.h\u201d\r\n\r\nint main()\r\n{\r\n\tint fd;\r\n\tif((fd=open(\"example_test.c\",O_CREAT|O_EXCL,S_IRUSR|S_IWUSR))==-1)\r\n\t{\r\n\t\tmy_err1(errno);\r\n\/\/\t\tmy_err2(\"open\",__LINE__,errno);\r\n\t}\r\n\tclose(fd);\r\n\treturn 0;\r\n}\r\n}<\/pre>\n<p>\u9996\u5148\u6d4b\u8bd5my_err1,\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:\">edsionte@edsionte-laptop:~\/code$ .\/my_error\r\nerror:  File exists with errno: 17<\/pre>\n<p>\u7136\u540e\u5c06my_err1\u6ce8\u91ca\u6389\uff0c\u6d4b\u8bd5my_err2\uff0c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:\">edsionte@edsionte-laptop:~\/code$ .\/my_error\r\nerror:  line:17 open():File exists with errno:17<\/pre>\n<p>\u7ed3\u679c\u548c\u524d\u9762\u4ecb\u7ecd\u6355\u83b7\u51fd\u6570\u65f6\u7684\u6d4b\u8bd5\u7ed3\u679c\u4e00\u6837\u3002\u4f60\u4ee5\u4e3a\u73b0\u5728\u5c31\u7ed3\u675f\u4e86\u5417\uff1f\u5f53\u7136\u6ca1\u6709\uff0c\u6211\u4eec\u8981\u628aerror.h\u5934\u6587\u4ef6\u7684\u6743\u9650\u4fee\u6539\u6210\u539f\u6837\u3002<\/p>\n<pre class=\"brush:\">\r\nedsionte@edsionte-laptop:\/usr\/include$ sudo chmod 644 error.h\r\n[sudo] password for edsionte:\r\nedsionte@edsionte-laptop:\/usr\/include$ ls -l error.h\r\n-rw-r--r-- 1 root root 2557 2010-06-23 12:50 error.h\r\n<\/pre>\n<p>\u5927\u529f\u544a\u6210\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e4b\u524d\u6211\u5199\u8fc7\u4e00\u4e2a\u5173\u4e8elinux\u4e0bC\u7f16\u7a0b\u4e2d\u9519\u8bef\u6355\u83b7\u51fd\u6570\u7684\u6587\u7ae0\uff0c\u5373\u6211\u4eec\u53ef\u4ee5\u81ea\u5df1\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u6355\u83b7\u4e00\u4e9b\u9519\u8bef\uff0c\u8ba9\u7a0b\u5e8f\u5458\u5904 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[109,108,107,115],"class_list":["post-687","post","type-post","status-publish","format-standard","hentry","category-linuxc","tag-c","tag-linux","tag-107","tag-115"],"views":6443,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/687","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=687"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/687\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=687"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}