{"id":51740,"date":"2024-12-03T09:17:15","date_gmt":"2024-12-03T01:17:15","guid":{"rendered":"https:\/\/fwq.ai\/blog\/51740\/"},"modified":"2024-12-03T09:17:15","modified_gmt":"2024-12-03T01:17:15","slug":"linux%e4%b8%ad%e5%a6%82%e4%bd%95%e6%89%93%e5%bc%80%e4%b8%80%e4%b8%aa%e7%9b%ae%e5%bd%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/51740\/","title":{"rendered":"Linux\u4e2d\u5982\u4f55\u6253\u5f00\u4e00\u4e2a\u76ee\u5f55"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Linux\u4e2d\u5982\u4f55\u6253\u5f00\u4e00\u4e2a\u76ee\u5f55<\/h1>\n<p>Linux\u64cd\u4f5c\u7cfb\u7edf\u7684opendir()\u51fd\u6570\u7528\u4e8e\u6253\u5f00\u6307\u5b9a\u7684\u76ee\u5f55\uff0c\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u76ee\u5f55\u6d41\u7684DIR\u6307\u9488\u3002\u8be5\u51fd\u6570\u6839\u636e\u8be5\u76ee\u5f55\u6d41\u5bf9\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\u8fdb\u884c\u8bfb\u53d6\u548c\u641c\u7d22\u3002opendir()\u51fd\u6570\u7684\u8bed\u6cd5\u4e3a\uff1aDIR * opendir(const char * name)\uff0c\u5176\u4e2dname\u662f\u8981\u6253\u5f00\u7684\u76ee\u5f55\u7684\u540d\u79f0\u3002<\/p>\n<\/p>\n<blockquote>\n<p>linux\u4e2d\u6253\u5f00\u76ee\u5f55\u7528opendir()\u51fd\u6570\u3002opendir()\u51fd\u6570\u53ef\u4ee5\u6253\u5f00\u6307\u5b9a\u7684\u76ee\u5f55\uff0c\u5e76\u8fd4\u56de\u201cDIR*\u201d\u5f62\u6001\u7684\u76ee\u5f55\u6d41\uff0c\u800c\u5bf9\u6307\u5b9a\u76ee\u5f55\u7684\u8bfb\u53d6\u548c\u641c\u7d22\u90fd\u8981\u4f7f\u7528\u6b64\u8fd4\u56de\u503c\uff1b\u8bed\u6cd5\u201cDIR * opendir(const char * name);\u201d\u3002<\/p>\n<\/blockquote>\n<p><strong>linux opendir()\u51fd\u6570\uff1a\u6253\u5f00\u76ee\u5f55\u51fd\u6570<\/strong><\/p>\n<p><strong>1\uff0c\u5934\u6587\u4ef6\uff1a<\/strong><\/p>\n<pre>#include&nbsp;&lt;sys\/types.h&gt;&nbsp;&nbsp;&nbsp;\n#include&nbsp;&lt;dirent.h&gt;<\/pre>\n<p><strong>2\uff0c\u5b9a\u4e49\u51fd\u6570<\/strong><\/p>\n<pre>DIR&nbsp;*&nbsp;opendir(const&nbsp;char&nbsp;*&nbsp;name);<\/pre>\n<p><strong>3\uff0c\u51fd\u6570\u8bf4\u660e\uff1a<\/strong><\/p>\n<p>opendir()\u7528\u6765\u6253\u5f00\u53c2\u6570name \u6307\u5b9a\u7684\u76ee\u5f55, \u5e76\u8fd4\u56deDIR*\u5f62\u6001\u7684\u76ee\u5f55\u6d41, \u548copen()\u7c7b\u4f3c, \u63a5\u4e0b\u6765\u5bf9\u76ee\u5f55\u7684\u8bfb\u53d6\u548c\u641c\u7d22\u90fd\u8981\u4f7f\u7528\u6b64\u8fd4\u56de\u503c.<\/p>\n<p><strong>4\uff0c\u8fd4\u56de\u503c\uff1a<\/strong><\/p>\n<p>\u6210\u529f\u5219\u8fd4\u56deDIR* \u578b\u6001\u7684\u76ee\u5f55\u6d41, \u6253\u5f00\u5931\u8d25\u5219\u8fd4\u56deNULL.<\/p>\n<p><strong>5\uff0c\u9519\u8bef\u4ee3\u7801\uff1a<\/strong><\/p>\n<ul>\n<li>\n<p>EACCESS \u6743\u9650\u4e0d\u8db3\u3002<\/p>\n<\/li>\n<li>\n<p>EMFILE \u5df2\u8fbe\u5230\u8fdb\u7a0b\u53ef\u540c\u65f6\u6253\u5f00\u7684\u6587\u4ef6\u6570\u4e0a\u9650\u3002<\/p>\n<\/li>\n<li>\n<p>ENFILE \u5df2\u8fbe\u5230\u7cfb\u7edf\u53ef\u540c\u65f6\u6253\u5f00\u7684\u6587\u4ef6\u6570\u4e0a\u9650\u3002<\/p>\n<\/li>\n<li>\n<p>ENOTDIR \u53c2\u6570name \u975e\u771f\u6b63\u7684\u76ee\u5f55\u3002<\/p>\n<\/li>\n<li>\n<p>ENOENT \u53c2\u6570name \u6307\u5b9a\u7684\u76ee\u5f55\u4e0d\u5b58\u5728, \u6216\u662f\u53c2\u6570name \u4e3a\u4e00\u7a7a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/li>\n<li>\n<p>ENOMEM \u6838\u5fc3\u5185\u5b58\u4e0d\u8db3\u3002<\/p>\n<\/li>\n<\/ul>\n<p><strong>6\uff0c\u4e8b\u4f8b\uff1a<\/strong><\/p>\n<pre>#include&nbsp;&lt;sys\/types.h&gt;\n#include&nbsp;&lt;dirent.h&gt;\n#include&nbsp;&lt;stdio.h&gt;\nint&nbsp;main()\n{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;a&nbsp;=&nbsp;opendir(\"\/etc\/passwd\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(\"a&nbsp;=&nbsp;%d\\n\",a);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;\n}<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240327\/17115069886603862ca2bfa.png\" class=\"aligncenter\" title=\"Linux\u4e2d\u5982\u4f55\u6253\u5f00\u4e00\u4e2a\u76ee\u5f55\u63d2\u56fe\" alt=\"Linux\u4e2d\u5982\u4f55\u6253\u5f00\u4e00\u4e2a\u76ee\u5f55\u63d2\u56fe\" \/><\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aLinux\u4e2d\u5982\u4f55\u6253\u5f00\u4e00\u4e2a\u76ee\u5f55\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux\u4e2d\u5982\u4f55\u6253\u5f00\u4e00\u4e2a\u76ee\u5f55 Linux\u64cd\u4f5c\u7cfb\u7edf\u7684opendir()\u51fd\u6570\u7528\u4e8e\u6253\u5f00\u6307\u5b9a\u7684\u76ee\u5f55\uff0c\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u76ee\u5f55\u6d41\u7684DIR\u6307\u9488\u3002\u8be5\u51fd\u6570\u6839\u636e\u8be5\u76ee\u5f55\u6d41\u5bf9\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\u8fdb\u884c\u8bfb\u53d6\u548c\u641c\u7d22\u3002opendir()\u51fd\u6570\u7684\u8bed\u6cd5\u4e3a\uff1aDIR * opendir(const char * name)\uff0c\u5176\u4e2dname\u662f\u8981\u6253\u5f00\u7684\u76ee\u5f55\u7684\u540d\u79f0\u3002 linux\u4e2d\u6253\u5f00\u76ee\u5f55\u7528opendir()\u51fd\u6570\u3002opendir()\u51fd\u6570\u53ef\u4ee5\u6253\u5f00\u6307\u5b9a\u7684\u76ee\u5f55\uff0c\u5e76\u8fd4\u56de\u201cDIR*\u201d\u5f62\u6001\u7684\u76ee\u5f55\u6d41\uff0c\u800c\u5bf9\u6307\u5b9a\u76ee\u5f55\u7684\u8bfb\u53d6\u548c\u641c\u7d22\u90fd\u8981\u4f7f\u7528\u6b64\u8fd4\u56de\u503c\uff1b\u8bed\u6cd5\u201cDIR * opendir(const char * name);\u201d\u3002 linux opendir()\u51fd\u6570\uff1a\u6253\u5f00\u76ee\u5f55\u51fd\u6570 1\uff0c\u5934\u6587\u4ef6\uff1a #include&nbsp;&lt;sys\/types.h&gt;&nbsp;&nbsp;&nbsp; #include&nbsp;&lt;dirent.h&gt; 2\uff0c\u5b9a\u4e49\u51fd\u6570 DIR&nbsp;*&nbsp;opendir(const&nbsp;char&nbsp;*&nbsp;name); 3\uff0c\u51fd\u6570\u8bf4\u660e\uff1a opendir()\u7528\u6765\u6253\u5f00\u53c2\u6570name \u6307\u5b9a\u7684\u76ee\u5f55, \u5e76\u8fd4\u56deDIR*\u5f62\u6001\u7684\u76ee\u5f55\u6d41, \u548copen()\u7c7b\u4f3c, \u63a5\u4e0b\u6765\u5bf9\u76ee\u5f55\u7684\u8bfb\u53d6\u548c\u641c\u7d22\u90fd\u8981\u4f7f\u7528\u6b64\u8fd4\u56de\u503c. 4\uff0c\u8fd4\u56de\u503c\uff1a \u6210\u529f\u5219\u8fd4\u56deDIR* \u578b\u6001\u7684\u76ee\u5f55\u6d41, \u6253\u5f00\u5931\u8d25\u5219\u8fd4\u56deNULL. 5\uff0c\u9519\u8bef\u4ee3\u7801\uff1a EACCESS \u6743\u9650\u4e0d\u8db3\u3002 EMFILE \u5df2\u8fbe\u5230\u8fdb\u7a0b\u53ef\u540c\u65f6\u6253\u5f00\u7684\u6587\u4ef6\u6570\u4e0a\u9650\u3002 ENFILE \u5df2\u8fbe\u5230\u7cfb\u7edf\u53ef\u540c\u65f6\u6253\u5f00\u7684\u6587\u4ef6\u6570\u4e0a\u9650\u3002 ENOTDIR \u53c2\u6570name \u975e\u771f\u6b63\u7684\u76ee\u5f55\u3002 ENOENT \u53c2\u6570name \u6307\u5b9a\u7684\u76ee\u5f55\u4e0d\u5b58\u5728, \u6216\u662f\u53c2\u6570name \u4e3a\u4e00\u7a7a\u5b57\u7b26\u4e32\u3002 ENOMEM \u6838\u5fc3\u5185\u5b58\u4e0d\u8db3\u3002 6\uff0c\u4e8b\u4f8b\uff1a #include&nbsp;&lt;sys\/types.h&gt; #include&nbsp;&lt;dirent.h&gt; #include&nbsp;&lt;stdio.h&gt; int&nbsp;main() { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;a&nbsp;=&nbsp;opendir(&#8220;\/etc\/passwd&#8221;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;a&nbsp;=&nbsp;%d\\n&#8221;,a); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-51740","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/51740","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=51740"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/51740\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=51740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=51740"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=51740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}