{"id":118138,"date":"2026-10-26T12:08:25","date_gmt":"2026-10-26T04:08:25","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/118138.html"},"modified":"2026-10-26T12:08:25","modified_gmt":"2026-10-26T04:08:25","slug":"linux-%e7%9b%ae%e5%bd%95%e6%93%8d%e4%bd%9c%e5%85%a8%e5%a5%97%e7%ac%94%e8%ae%b0%e6%80%bb%e7%bb%93%ef%bc%9a%e4%bb%8e-opendir-%e5%88%b0-stat-%e4%b8%8e%e6%97%b6%e9%97%b4%e6%88%b3","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/118138.html","title":{"rendered":"Linux \u76ee\u5f55\u64cd\u4f5c\u5168\u5957\u7b14\u8bb0\u603b\u7ed3\uff1a\u4ece opendir \u5230 stat \u4e0e\u65f6\u95f4\u6233"},"content":{"rendered":"<p>\u6458\u8981&#xff1a;\u672c\u6587\u7cfb\u7edf\u68b3\u7406 Linux \u76ee\u5f55\u64cd\u4f5c\u7684\u6838\u5fc3 API&#xff0c;\u56f4\u7ed5\u300c\u76ee\u5f55\u672c\u8d28\u4e5f\u662f\u6587\u4ef6\u300d\u8fd9\u4e00\u5927\u524d\u63d0&#xff0c;\u4f9d\u6b21\u8bb2\u89e3 opendir\u3001readdir\u3001closedir \u4e09\u5927\u76ee\u5f55\u51fd\u6570&#xff0c;\u518d\u5ef6\u4f38\u5230 chdir\u3001getcwd\u3001mkdir\u3001rmdir\u3001stat \u7b49\u8f85\u52a9\u51fd\u6570&#xff0c;\u5e76\u7ed3\u5408 Unix \u65f6\u95f4\u6233\u4e0e errno \u9519\u8bef\u5904\u7406\u505a\u4e32\u8054\u3002\u6587\u4e2d\u8865\u5145\u4e86 dirent \u7f13\u51b2\u533a\u9677\u9631\u3001d_type \u515c\u5e95\u65b9\u6848\u3001\u9012\u5f52\u8def\u5f84\u62fc\u63a5\u3001umask \u5bf9 mkdir \u7684\u5f71\u54cd\u3001localtime \u7ebf\u7a0b\u5b89\u5168\u7b49\u9ad8\u9891\u6613\u9519\u70b9&#xff0c;\u6700\u540e\u7ed9\u51fa\u53ef\u76f4\u63a5\u8fd0\u884c\u7684\u6781\u7b80 ls \u6a21\u677f\u4e0e\u9012\u5f52\u904d\u5386\u6269\u5c55\u793a\u4f8b\u3002<\/p>\n<h3>\u4e00\u3001\u6838\u5fc3\u5927\u524d\u63d0&#xff1a;\u76ee\u5f55\u672c\u8d28\u4e5f\u662f\u6587\u4ef6<\/h3>\n<p>\u5728 Linux \u4e2d&#xff0c;\u76ee\u5f55\u662f\u4e00\u79cd\u7279\u6b8a\u6587\u4ef6&#xff0c;\u901a\u5e38\u79f0\u4e3a\u300c\u76ee\u5f55\u6587\u4ef6\u300d\u3002\u666e\u901a\u6587\u4ef6\u91cc\u5b58\u653e\u7684\u662f\u5b57\u8282\u6d41&#xff0c;\u800c\u76ee\u5f55\u6587\u4ef6\u91cc\u5b58\u653e\u7684\u662f\u8be5\u76ee\u5f55\u4e0b\u6240\u6709\u6587\u4ef6\u6761\u76ee\u7684\u4fe1\u606f&#xff0c;\u6bcf\u6761\u4fe1\u606f\u81f3\u5c11\u5305\u542b\u6587\u4ef6\u540d\u548c\u5bf9\u5e94\u7684 inode \u7f16\u53f7&#xff0c;\u8fd9\u5c31\u662f ls \u80fd\u5217\u51fa\u76ee\u5f55\u5185\u5bb9\u7684\u57fa\u7840\u3002<\/p>\n<p>\u6b63\u56e0\u4e3a\u76ee\u5f55\u4e5f\u662f\u6587\u4ef6&#xff0c;\u6240\u4ee5\u64cd\u4f5c\u76ee\u5f55\u540c\u6837\u9075\u5faa\u4e00\u5957\u56fa\u5b9a\u6d41\u7a0b&#xff0c;\u548c\u666e\u901a\u6587\u4ef6\u7684 fopen \u2192 fread \u5faa\u73af\u8bfb\u53d6 \u2192 fclose \u975e\u5e38\u76f8\u4f3c&#xff1a;<\/p>\n<p>\u76ee\u5f55\u64cd\u4f5c\u56fa\u5b9a\u6d41\u7a0b&#xff1a;opendir \u2192 readdir \u5faa\u73af\u8bfb\u53d6 \u2192 closedir \u5173\u95ed\u3002<\/p>\n<p>\u8fd9\u4e2a\u5bf9\u5e94\u5173\u7cfb\u503c\u5f97\u8bb0\u7262&#xff1a;<\/p>\n<table>\n<tr>\u666e\u901a\u6587\u4ef6\u76ee\u5f55\u6587\u4ef6\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>fopen<\/td>\n<td>opendir<\/td>\n<td>\u6253\u5f00\u5e76\u62ff\u5230\u4e00\u4e2a\u6d41\u53e5\u67c4<\/td>\n<\/tr>\n<tr>\n<td>fread \/ fgets<\/td>\n<td>readdir<\/td>\n<td>\u5faa\u73af\u8bfb\u53d6\u5185\u5bb9<\/td>\n<\/tr>\n<tr>\n<td>fclose<\/td>\n<td>closedir<\/td>\n<td>\u5173\u95ed\u5e76\u91ca\u653e\u8d44\u6e90<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u4e8c\u3001\u76ee\u5f55\u4e09\u5927\u6838\u5fc3\u51fd\u6570<\/h3>\n<p>\u8fd9\u7ec4\u51fd\u6570\u662f\u904d\u5386\u76ee\u5f55\u3001\u5b9e\u73b0 ls \u7684\u57fa\u7840&#xff0c;\u58f0\u660e\u5728 &lt;dirent.h&gt; \u5934\u6587\u4ef6\u4e2d\u3002<\/p>\n<h4>2.1 opendir&#xff1a;\u6253\u5f00\u76ee\u5f55\u6d41<\/h4>\n<p>DIR *opendir(const char *name);<\/p>\n<ul>\n<li>\u529f\u80fd&#xff1a;\u6253\u5f00\u4e00\u4e2a\u76ee\u5f55&#xff0c;\u62ff\u5230\u76ee\u5f55\u6d41\u6307\u9488 DIR *\u3002<\/li>\n<li>\u53c2\u6570&#xff1a;name \u662f\u76ee\u5f55\u8def\u5f84&#xff0c;\u76f8\u5bf9\u8def\u5f84\u548c\u7edd\u5bf9\u8def\u5f84\u90fd\u53ef\u4ee5&#xff0c;\u4f8b\u5982 &#034;.&#034; \u8868\u793a\u5f53\u524d\u76ee\u5f55&#xff0c;&#034;..\/&#034; \u8868\u793a\u4e0a\u7ea7\u76ee\u5f55\u3002<\/li>\n<li>\u8fd4\u56de&#xff1a;\u6210\u529f\u8fd4\u56de DIR * \u76ee\u5f55\u6d41\u6307\u9488&#xff1b;\u5931\u8d25\u8fd4\u56de NULL\u3002<\/li>\n<\/ul>\n<p>\u5ef6\u4f38\u7406\u89e3&#xff1a;DIR \u53ef\u4ee5\u770b\u4f5c\u76ee\u5f55\u7684\u300c\u53e5\u67c4\u300d&#xff0c;\u5b83\u5185\u90e8\u7ef4\u62a4\u4e86\u4e00\u4e2a\u8bfb\u53d6\u504f\u79fb&#xff0c;\u5c31\u50cf\u666e\u901a\u6587\u4ef6\u7684\u6587\u4ef6\u4f4d\u7f6e\u5149\u6807\u3002\u6bcf\u6b21 readdir \u90fd\u4f1a\u8ba9\u8fd9\u4e2a\u504f\u79fb\u5f80\u540e\u79fb\u52a8\u4e00\u6761\u76ee\u5f55\u9879&#xff0c;\u6240\u4ee5\u4e00\u6b21\u6253\u5f00\u540e\u53ef\u4ee5\u987a\u5e8f\u8bfb\u5b8c\u6574\u4e2a\u76ee\u5f55\u3002\u6ce8\u610f DIR \u7684\u5177\u4f53\u7ed3\u6784\u7531\u7cfb\u7edf\u5b9e\u73b0&#xff0c;\u6211\u4eec\u4e00\u822c\u4e0d\u9700\u8981\u4e5f\u4e0d\u5e94\u8be5\u624b\u52a8\u89e3\u6790\u5b83\u7684\u5185\u90e8\u5b57\u6bb5\u3002<\/p>\n<h4>2.2 readdir&#xff1a;\u8bfb\u53d6\u4e00\u6761\u76ee\u5f55\u9879<\/h4>\n<p>struct dirent *readdir(DIR *dirp);<\/p>\n<ul>\n<li>\u529f\u80fd&#xff1a;\u4ece\u76ee\u5f55\u6d41\u91cc\u8bfb\u53d6\u4e00\u6761\u76ee\u5f55\u9879&#xff0c;\u5373\u4e00\u4e2a\u6587\u4ef6\u6216\u6587\u4ef6\u5939\u7684\u4fe1\u606f&#xff0c;\u8fd4\u56de struct dirent *\u3002<\/li>\n<li>\u53c2\u6570&#xff1a;opendir \u8fd4\u56de\u7684\u76ee\u5f55\u6d41\u6307\u9488\u3002<\/li>\n<li>\u8fd4\u56de&#xff1a;\u8bfb\u5230\u6761\u76ee\u65f6\u8fd4\u56de dirent \u6307\u9488&#xff1b;\u8bfb\u5230\u76ee\u5f55\u672b\u5c3e\u6216\u51fa\u9519\u65f6\u8fd4\u56de NULL\u3002<\/li>\n<\/ul>\n<p>\u91cd\u70b9\u6210\u5458\u4e0e\u4f7f\u7528\u8981\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u8fd4\u56de\u503c\u4e0d\u9700\u8981 free&#xff1a;readdir \u8fd4\u56de\u7684 struct dirent * \u6307\u5411\u5e93\u5185\u90e8\u7ef4\u62a4\u7684\u7f13\u51b2\u533a&#xff0c;\u4e0d\u80fd\u624b\u52a8\u91ca\u653e&#xff0c;closedir \u65f6\u4f1a\u81ea\u52a8\u56de\u6536\u3002<\/li>\n<li>\u5185\u90e8\u7f13\u51b2\u533a\u4f1a\u88ab\u8986\u76d6&#xff1a;\u56e0\u4e3a\u8fd4\u56de\u7684\u662f\u540c\u4e00\u5757\u5185\u90e8\u7f13\u51b2\u533a&#xff0c;\u8fde\u7eed\u8c03\u7528 readdir \u65f6&#xff0c;\u4e0a\u4e00\u6b21\u62ff\u5230\u7684 d_name \u5185\u5bb9\u53ef\u80fd\u5df2\u7ecf\u88ab\u4e0b\u4e00\u6b21\u8bfb\u53d6\u8986\u76d6\u3002\u5982\u679c\u4e4b\u540e\u8fd8\u8981\u4f7f\u7528\u6587\u4ef6\u540d&#xff0c;\u5fc5\u987b\u7acb\u523b\u7528 strcpy \u6216 strdup \u590d\u5236\u51fa\u53bb&#xff0c;\u4e0d\u80fd\u53ea\u4fdd\u5b58\u6307\u9488\u3002<\/li>\n<li>d_type \u8868\u793a\u6587\u4ef6\u7c7b\u578b&#xff1a;\u5e38\u89c1\u503c\u6709 DT_REG \u666e\u901a\u6587\u4ef6\u3001DT_DIR \u76ee\u5f55\u3001DT_LNK \u8f6f\u94fe\u63a5\u7b49&#xff0c;\u6b63\u597d\u53ef\u4ee5\u7528\u5728 switch \u91cc\u505a\u7c7b\u578b\u5224\u65ad\u3002<\/li>\n<li>d_name \u53ea\u6709\u540d\u5b57&#xff0c;\u4e0d\u662f\u5b8c\u6574\u8def\u5f84&#xff1a;d_name \u53ea\u4fdd\u5b58\u6587\u4ef6\u540d\u672c\u8eab&#xff0c;\u4e0d\u5305\u542b\u7236\u76ee\u5f55\u8def\u5f84\u3002\u9012\u5f52\u904d\u5386\u76ee\u5f55\u65f6\u6700\u5bb9\u6613\u5728\u8fd9\u91cc\u8e29\u5751&#xff0c;\u5fc5\u987b\u624b\u52a8\u62fc\u63a5\u5b8c\u6574\u8def\u5f84\u3002<\/li>\n<li>d_type \u53ef\u80fd\u4e0d\u53ef\u7528&#xff1a;\u90e8\u5206\u6587\u4ef6\u7cfb\u7edf\u4e0d\u652f\u6301 d_type&#xff0c;\u4f1a\u8fd4\u56de DT_UNKNOWN&#xff0c;\u6b64\u65f6\u9700\u8981\u7528 stat \/ lstat \u8fdb\u4e00\u6b65\u5224\u65ad\u6587\u4ef6\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p>\u5ef6\u4f38\u7406\u89e3&#xff1a;\u8bfb\u53d6 &#034;.&#034; \u76ee\u5f55\u65f6&#xff0c;\u8fd4\u56de\u7684\u524d\u4e24\u9879\u901a\u5e38\u5c31\u662f &#034;.&#034; \u548c &#034;..&#034;\u3002\u5982\u679c\u505a\u9012\u5f52\u904d\u5386&#xff0c;\u4e00\u5b9a\u8981\u628a\u8fd9\u4e24\u4e2a\u6761\u76ee\u8fc7\u6ee4\u6389&#xff0c;\u5426\u5219\u4f1a\u9677\u5165\u65e0\u9650\u9012\u5f52&#xff0c;\u8fd9\u4e24\u4e2a\u540d\u5b57\u5206\u522b\u4ee3\u8868\u5f53\u524d\u76ee\u5f55\u548c\u4e0a\u7ea7\u76ee\u5f55&#xff0c;\u5b83\u4eec\u672c\u8eab\u4e0d\u662f\u201c\u65b0\u76ee\u5f55\u201d\u3002<\/p>\n<h4>2.3 closedir&#xff1a;\u5173\u95ed\u76ee\u5f55\u6d41<\/h4>\n<p>int closedir(DIR *dirp);<\/p>\n<ul>\n<li>\u529f\u80fd&#xff1a;\u5173\u95ed\u76ee\u5f55\u6d41&#xff0c;\u91ca\u653e opendir \u5206\u914d\u7684\u5185\u90e8\u8d44\u6e90\u3002<\/li>\n<li>\u53c2\u6570&#xff1a;opendir \u8fd4\u56de\u7684 DIR *\u3002<\/li>\n<li>\u8fd4\u56de&#xff1a;\u6210\u529f\u8fd4\u56de 0&#xff0c;\u5931\u8d25\u8fd4\u56de -1\u3002<\/li>\n<\/ul>\n<p>\u5b83\u548c\u666e\u901a IO \u7684 close \u5bf9\u5e94\u3002\u6253\u5f00\u76ee\u5f55\u540e\u65e0\u8bba\u8bfb\u53d6\u8fc7\u7a0b\u662f\u5426\u53d1\u751f\u9519\u8bef&#xff0c;\u90fd\u8981\u8bb0\u5f97\u8c03\u7528 closedir&#xff0c;\u5426\u5219\u4f1a\u9020\u6210\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<h3>\u4e09\u3001\u76ee\u5f55\u8f85\u52a9\u51fd\u6570<\/h3>\n<p>\u8fd9\u4e9b\u51fd\u6570\u5206\u522b\u89e3\u51b3\u300c\u5207\u6362\u76ee\u5f55\u3001\u83b7\u53d6\u5f53\u524d\u76ee\u5f55\u3001\u65b0\u5efa\u76ee\u5f55\u3001\u5220\u9664\u76ee\u5f55\u300d\u56db\u7c7b\u95ee\u9898&#xff0c;\u58f0\u660e\u5728 &lt;unistd.h&gt; \u548c &lt;sys\/stat.h&gt; \u4e2d\u3002<\/p>\n<h4>3.1 chdir&#xff1a;\u5207\u6362\u5de5\u4f5c\u76ee\u5f55<\/h4>\n<p>int chdir(const char *path);<\/p>\n<p>\u529f\u80fd&#xff1a;\u5207\u6362\u5f53\u524d\u8fdb\u7a0b\u7684\u5de5\u4f5c\u76ee\u5f55&#xff0c;\u7b49\u4ef7\u4e8e\u547d\u4ee4\u884c\u91cc\u7684 cd\u3002<\/p>\n<p>\u91cd\u8981\u533a\u522b&#xff1a;\u5b83\u53ea\u6539\u53d8\u5f53\u524d\u7a0b\u5e8f\u81ea\u5df1\u7684\u8fdb\u7a0b\u72b6\u6001&#xff0c;\u4e0d\u4f1a\u5f71\u54cd\u7ec8\u7aef&#xff0c;\u56e0\u4e3a\u7ec8\u7aef\u662f\u53e6\u4e00\u4e2a\u72ec\u7acb\u8fdb\u7a0b\u3002\u5207\u6362\u4e4b\u540e&#xff0c;\u7a0b\u5e8f\u91cc\u540e\u7eed\u6240\u6709\u7684\u76f8\u5bf9\u8def\u5f84 open\u3001fopen \u90fd\u4f1a\u4ee5\u65b0\u76ee\u5f55\u4e3a\u57fa\u51c6\u3002<\/p>\n<p>\u4f8b\u5982\u6267\u884c chdir(&#034;..\/&#034;) \u5207\u6362\u5230\u4e0a\u7ea7\u76ee\u5f55\u540e&#xff0c;\u518d\u8c03\u7528 fopen(&#034;aaa&#034;,&#034;w&#034;)&#xff0c;\u5c31\u4f1a\u5728\u4e0a\u7ea7\u76ee\u5f55\u521b\u5efa\u6587\u4ef6 aaa&#xff0c;\u800c\u4e0d\u662f\u5728\u539f\u76ee\u5f55\u521b\u5efa\u3002<\/p>\n<h4>3.2 getcwd&#xff1a;\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/h4>\n<p>char *getcwd(char *buf, size_t size);<\/p>\n<p>\u529f\u80fd&#xff1a;\u83b7\u53d6\u7a0b\u5e8f\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u7edd\u5bf9\u8def\u5f84&#xff0c;\u7b49\u4ef7\u4e8e\u547d\u4ee4\u884c\u91cc\u7684 pwd\u3002<\/p>\n<ul>\n<li>buf&#xff1a;\u7528\u4e8e\u5b58\u653e\u8def\u5f84\u5b57\u7b26\u4e32\u7684\u7f13\u51b2\u533a\u3002<\/li>\n<li>size&#xff1a;\u7f13\u51b2\u533a\u5927\u5c0f&#xff0c;\u9632\u6b62\u6ea2\u51fa\u3002<\/li>\n<li>\u8fd4\u56de&#xff1a;\u6210\u529f\u8fd4\u56de buf&#xff0c;\u5931\u8d25\u8fd4\u56de NULL\u3002<\/li>\n<\/ul>\n<p>\u5178\u578b\u7528\u6cd5\u662f chdir \u4e4b\u540e\u518d\u8c03\u7528 getcwd \u9a8c\u8bc1\u662f\u5426\u5207\u6362\u6210\u529f&#xff0c;\u76f8\u5f53\u4e8e\u300ccd \u4e4b\u540e pwd\u300d\u7684\u7ec4\u5408\u3002<\/p>\n<h4>3.3 mkdir&#xff1a;\u65b0\u5efa\u76ee\u5f55<\/h4>\n<p>int mkdir(const char *pathname, mode_t mode);<\/p>\n<p>\u529f\u80fd&#xff1a;\u65b0\u5efa\u76ee\u5f55\u3002mode \u662f\u6743\u9650&#xff0c;\u5982 0775\u30010755 \u8fd9\u7c7b\u516b\u8fdb\u5236\u6743\u9650\u3002<\/p>\n<p>\u5ef6\u4f38\u7406\u89e3&#xff1a;\u5b9e\u9645\u521b\u5efa\u51fa\u6765\u7684\u76ee\u5f55\u6743\u9650\u4f1a\u53d7\u5230\u8fdb\u7a0b umask \u7684\u5f71\u54cd&#xff0c;\u6700\u7ec8\u6743\u9650\u901a\u5e38\u662f mode &amp; ~umask\u3002\u4e5f\u5c31\u662f\u8bf4&#xff0c;\u5373\u4f7f\u4f20\u4e86 0777&#xff0c;\u5982\u679c umask \u662f 0002&#xff0c;\u6700\u7ec8\u5f97\u5230\u7684\u5f80\u5f80\u8fd8\u662f 0775\u3002\u60f3\u8981\u7cbe\u786e\u63a7\u5236\u6743\u9650&#xff0c;\u9700\u8981\u5728\u521b\u5efa\u540e\u663e\u5f0f\u8c03\u7528 chmod&#xff0c;\u6216\u8005\u5148\u4e34\u65f6\u4fee\u6539 umask\u3002<\/p>\n<h4>3.4 rmdir&#xff1a;\u5220\u9664\u76ee\u5f55<\/h4>\n<p>int rmdir(const char *pathname);<\/p>\n<p>\u529f\u80fd&#xff1a;\u5220\u9664\u76ee\u5f55\u3002<\/p>\n<p>\u786c\u6027\u9650\u5236&#xff1a;\u53ea\u80fd\u5220\u9664\u7a7a\u76ee\u5f55&#xff0c;\u76ee\u5f55\u91cc\u5b58\u5728\u6587\u4ef6\u6216\u5b50\u76ee\u5f55\u65f6\u4f1a\u76f4\u63a5\u62a5\u9519\u3002<\/p>\n<p>rm -r \u547d\u4ee4\u7684\u5e95\u5c42\u5e76\u4e0d\u662f\u5355\u7eaf\u8c03\u7528 rmdir&#xff0c;\u800c\u662f\u5148\u9012\u5f52\u5220\u9664\u76ee\u5f55\u91cc\u7684\u6240\u6709\u5185\u5bb9&#xff0c;\u6700\u540e\u518d\u5bf9\u7a7a\u76ee\u5f55\u8c03\u7528 rmdir\u3002\u7406\u89e3\u8fd9\u4e00\u70b9&#xff0c;\u5c31\u80fd\u660e\u767d\u4e3a\u4ec0\u4e48\u4e0d\u80fd\u6307\u671b rmdir \u4e00\u53e5\u547d\u4ee4\u6e05\u7a7a\u6574\u4e2a\u76ee\u5f55\u6811\u3002<\/p>\n<h3>\u56db\u3001stat&#xff1a;\u9ad8\u9891\u6587\u4ef6\u5c5e\u6027\u63a5\u53e3<\/h3>\n<p>stat \u7528\u4e8e\u8bfb\u53d6\u6587\u4ef6\u6216\u76ee\u5f55\u7684\u5e95\u5c42\u5c5e\u6027&#xff0c;\u4e5f\u5c31\u662f\u5143\u4fe1\u606f&#xff0c;\u5305\u542b inode\u3001\u5927\u5c0f\u3001\u6743\u9650\u3001\u65f6\u95f4\u3001\u94fe\u63a5\u6570\u7b49\u3002\u58f0\u660e\u5728 &lt;sys\/stat.h&gt; \u4e2d\u3002<\/p>\n<h4>4.1 \u51fd\u6570\u539f\u578b\u4e0e\u53c2\u6570<\/h4>\n<p>int stat(const char *path, struct stat *buf);<\/p>\n<ul>\n<li>path&#xff1a;\u6587\u4ef6\u8def\u5f84\u3002<\/li>\n<li>buf&#xff1a;struct stat \u7ed3\u6784\u4f53&#xff0c;\u7528\u6765\u5b58\u653e\u8bfb\u53d6\u5230\u7684\u5c5e\u6027\u4fe1\u606f\u3002<\/li>\n<li>\u8fd4\u56de&#xff1a;\u6210\u529f\u8fd4\u56de 0&#xff0c;\u5931\u8d25\u8fd4\u56de -1\u3002<\/li>\n<\/ul>\n<h4>4.2 \u91cd\u70b9\u6210\u5458<\/h4>\n<table>\n<tr>\u6210\u5458\u542b\u4e49\u6253\u5370\u65b9\u5f0f<\/tr>\n<tbody>\n<tr>\n<td>st_ino<\/td>\n<td>inode \u53f7&#xff0c;\u5bf9\u5e94 ls -i<\/td>\n<td>%lu<\/td>\n<\/tr>\n<tr>\n<td>st_mode<\/td>\n<td>\u6743\u9650\u548c\u6587\u4ef6\u7c7b\u578b&#xff0c;\u914d\u5408 S_ISREG \/ S_ISDIR \/ S_ISLNK \u5224\u65ad\u7c7b\u578b<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>st_nlink<\/td>\n<td>\u786c\u94fe\u63a5\u8ba1\u6570<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>st_uid \/ st_gid<\/td>\n<td>\u6240\u6709\u8005\u7528\u6237 ID\u3001\u7ec4 ID<\/td>\n<td>%d<\/td>\n<\/tr>\n<tr>\n<td>st_size<\/td>\n<td>\u6587\u4ef6\u5927\u5c0f&#xff0c;\u5355\u4f4d\u5b57\u8282&#xff0c;\u548c lseek \u62ff\u5230\u7684\u5927\u5c0f\u4e00\u81f4<\/td>\n<td>%lu<\/td>\n<\/tr>\n<tr>\n<td>st_mtim.tv_sec<\/td>\n<td>mtime&#xff0c;\u5185\u5bb9\u4fee\u6539\u65f6\u95f4\u6233&#xff0c;Unix \u79d2\u6570<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5ef6\u4f38\u7406\u89e3&#xff1a;st_mode \u91cc\u540c\u65f6\u4fdd\u5b58\u4e86\u6587\u4ef6\u7c7b\u578b\u548c\u6743\u9650\u4f4d\u3002\u4f4e 9 \u4f4d\u662f\u6743\u9650&#xff0c;\u4f8b\u5982\u53ef\u4ee5\u7528 st.st_mode &amp; 0777 \u53d6\u51fa rwxrwxrwx \u5bf9\u5e94\u7684\u6743\u9650\u503c\u3002\u7c7b\u578b\u5224\u65ad\u5219\u63a8\u8350\u7edf\u4e00\u4f7f\u7528\u5b8f&#xff1a;<\/p>\n<p>if (S_ISREG(st.st_mode)) {<br \/>\n    \/* \u666e\u901a\u6587\u4ef6 *\/<br \/>\n} else if (S_ISDIR(st.st_mode)) {<br \/>\n    \/* \u76ee\u5f55 *\/<br \/>\n} else if (S_ISLNK(st.st_mode)) {<br \/>\n    \/* \u7b26\u53f7\u94fe\u63a5 *\/<br \/>\n}<\/p>\n<p>\u8fd9\u6837\u6bd4\u624b\u52a8\u6bd4\u8f83 st_mode &amp; S_IFMT \u66f4\u6e05\u6670&#xff0c;\u4e5f\u66f4\u4e0d\u5bb9\u6613\u51fa\u9519\u3002<\/p>\n<h4>4.3 stat \u4e0e lstat \u7684\u533a\u522b<\/h4>\n<p>stat \u4f1a\u8ddf\u8fdb\u8f6f\u94fe\u63a5&#xff0c;\u62ff\u5230\u7684\u662f\u94fe\u63a5\u76ee\u6807\u6587\u4ef6\u7684\u4fe1\u606f&#xff1b;lstat \u4e0d\u8ddf\u8fdb&#xff0c;\u62ff\u5230\u7684\u662f\u8f6f\u94fe\u63a5\u672c\u8eab\u7684\u4fe1\u606f\u3002\u56e0\u6b64\u5728\u9700\u8981\u5224\u65ad\u300c\u8fd9\u4e2a\u8def\u5f84\u672c\u8eab\u662f\u4e0d\u662f\u8f6f\u94fe\u63a5\u300d\u65f6&#xff0c;\u5fc5\u987b\u4f7f\u7528 lstat&#xff0c;\u5426\u5219\u4f1a\u88ab stat \u5e26\u504f&#xff0c;\u628a\u8f6f\u94fe\u63a5\u8bef\u5224\u6210\u76ee\u6807\u6587\u4ef6\u7684\u7c7b\u578b\u3002<\/p>\n<h4>4.4 stat \u5b8c\u6574\u793a\u4f8b<\/h4>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;sys\/stat.h&gt;<\/p>\n<p>int main(int argc, char *argv[])<br \/>\n{<br \/>\n    const char *path &#061; argc &gt; 1 ? argv[1] : &#034;.&#034;;<br \/>\n    struct stat st;<\/p>\n<p>    if (stat(path, &amp;st) !&#061; 0) {<br \/>\n        perror(&#034;stat&#034;);<br \/>\n        return 1;<br \/>\n    }<\/p>\n<p>    printf(&#034;inode:     %lu\\\\n&#034;, (unsigned long)st.st_ino);<br \/>\n    printf(&#034;\u786c\u94fe\u63a5\u6570:  %lu\\\\n&#034;, (unsigned long)st.st_nlink);<br \/>\n    printf(&#034;\u5927\u5c0f:      %lu \u5b57\u8282\\\\n&#034;, (unsigned long)st.st_size);<\/p>\n<p>    if (S_ISDIR(st.st_mode)) {<br \/>\n        printf(&#034;\u7c7b\u578b:      \u76ee\u5f55\\\\n&#034;);<br \/>\n    } else if (S_ISREG(st.st_mode)) {<br \/>\n        printf(&#034;\u7c7b\u578b:      \u666e\u901a\u6587\u4ef6\\\\n&#034;);<br \/>\n    } else if (S_ISLNK(st.st_mode)) {<br \/>\n        printf(&#034;\u7c7b\u578b:      \u7b26\u53f7\u94fe\u63a5\\\\n&#034;);<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h3>\u4e94\u3001Unix \u65f6\u95f4\u6233<\/h3>\n<p>Linux \u4e2d\u7684\u65f6\u95f4\u7edf\u4e00\u4f7f\u7528 Unix \u65f6\u95f4\u6233\u8868\u793a&#xff1a;\u4ece 1970-01-01 00:00:00 UTC \u8d77&#xff0c;\u5230\u73b0\u5728\u7ecf\u8fc7\u7684\u603b\u79d2\u6570\u3002\u76f8\u5173\u51fd\u6570\u58f0\u660e\u5728 &lt;time.h&gt; \u4e2d\u3002<\/p>\n<h4>5.1 time&#xff1a;\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233<\/h4>\n<p>time_t time(time_t *t);<\/p>\n<ul>\n<li>\u529f\u80fd&#xff1a;\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233&#xff0c;\u5355\u4f4d\u662f\u79d2\u3002<\/li>\n<li>\u53c2\u6570&#xff1a;\u53ef\u4ee5\u4f20 NULL&#xff1b;\u5982\u679c\u4f20\u5165\u53d8\u91cf\u5730\u5740&#xff0c;\u79d2\u6570\u4e5f\u4f1a\u540c\u65f6\u5b58\u5165 *t\u3002<\/li>\n<li>\u8fd4\u56de&#xff1a;\u65f6\u95f4\u6233\u3002<\/li>\n<\/ul>\n<h4>5.2 localtime&#xff1a;\u79d2\u6570\u8f6c\u7ed3\u6784\u4f53<\/h4>\n<p>struct tm *localtime(const time_t *timep);<\/p>\n<p>\u529f\u80fd&#xff1a;\u628a\u79d2\u6570\u65f6\u95f4\u6233\u8f6c\u6362\u6210\u5305\u542b\u5e74\u6708\u65e5\u65f6\u5206\u79d2\u7684 struct tm\u3002<\/p>\n<p>struct tm \u5e38\u7528\u6210\u5458\u6709 tm_year\u3001tm_mon\u3001tm_mday\u3001tm_hour\u3001tm_min\u3001tm_sec\u3002<\/p>\n<p>\u6ce8\u610f&#xff1a;tm_year \u4fdd\u5b58\u7684\u662f\u300c\u5e74\u4efd\u51cf\u53bb 1900\u300d\u540e\u7684\u503c&#xff0c;tm_mon \u7684\u8303\u56f4\u662f 0 \u5230 11\u3002\u56e0\u6b64\u6253\u5370\u5e74\u4efd\u8981\u5199 tm_year &#043; 1900&#xff0c;\u6253\u5370\u6708\u4efd\u8981\u5199 tm_mon &#043; 1&#xff0c;\u8fd9\u662f\u65b0\u624b\u6700\u5bb9\u6613\u5199\u9519\u7684\u4e24\u4e2a\u504f\u79fb\u91cf\u3002<\/p>\n<h4>5.3 ctime&#xff1a;\u79d2\u6570\u76f4\u63a5\u8f6c\u5b57\u7b26\u4e32<\/h4>\n<p>char *ctime(const time_t *timep);<\/p>\n<p>\u529f\u80fd&#xff1a;\u628a\u79d2\u6570\u65f6\u95f4\u6233\u76f4\u63a5\u8f6c\u6362\u6210\u4eba\u7c7b\u53ef\u8bfb\u7684\u65f6\u95f4\u5b57\u7b26\u4e32&#xff0c;\u6548\u679c\u7c7b\u4f3c date \u547d\u4ee4\u8f93\u51fa&#xff0c;\u4f8b\u5982&#xff1a;<\/p>\n<p>Wed Oct 22 20:30:00 2026<\/p>\n<p>\u5ef6\u4f38\u7406\u89e3&#xff1a;<\/p>\n<ul>\n<li>localtime \u8fd4\u56de\u7684\u662f\u5185\u90e8\u9759\u6001\u7f13\u51b2\u533a&#xff0c;\u548c readdir \u7c7b\u4f3c&#xff0c;\u5185\u5bb9\u53ef\u80fd\u88ab\u4e0b\u4e00\u6b21\u8c03\u7528\u8986\u76d6&#xff1b;\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u66f4\u5e94\u4f7f\u7528\u7ebf\u7a0b\u5b89\u5168\u7248\u672c localtime_r\u3002<\/li>\n<li>localtime \u5f97\u5230\u672c\u5730\u65f6\u95f4&#xff0c;gmtime \u5f97\u5230 UTC \u65f6\u95f4\u3002\u670d\u52a1\u5668\u65e5\u5fd7\u7ecf\u5e38\u7edf\u4e00\u5b58 UTC&#xff0c;\u6253\u5370\u65f6\u518d\u8f6c\u672c\u5730\u65f6\u95f4\u3002<\/li>\n<li>ctime \u672c\u8d28\u4e0a\u7b49\u4ef7\u4e8e asctime(localtime(&#8230;))&#xff0c;\u6240\u4ee5\u5b83\u8f93\u51fa\u7684\u4e5f\u662f\u672c\u5730\u65f6\u95f4\u3002<\/li>\n<\/ul>\n<h4>5.4 \u65f6\u95f4\u51fd\u6570\u7efc\u5408\u793a\u4f8b<\/h4>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;time.h&gt;<\/p>\n<p>int main(void)<br \/>\n{<br \/>\n    time_t now &#061; time(NULL);<br \/>\n    printf(&#034;\u65f6\u95f4\u6233: %ld\\\\n&#034;, (long)now);<\/p>\n<p>    char *s &#061; ctime(&amp;now);<br \/>\n    printf(&#034;ctime: %s&#034;, s);<\/p>\n<p>    struct tm *t &#061; localtime(&amp;now);<br \/>\n    printf(&#034;%d-%02d-%02d %02d:%02d:%02d\\\\n&#034;,<br \/>\n           t-&gt;tm_year &#043; 1900,<br \/>\n           t-&gt;tm_mon &#043; 1,<br \/>\n           t-&gt;tm_mday,<br \/>\n           t-&gt;tm_hour,<br \/>\n           t-&gt;tm_min,<br \/>\n           t-&gt;tm_sec);<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h3>\u516d\u3001perror \u4e0e errno<\/h3>\n<p>void perror(const char *s);<\/p>\n<p>perror \u7684\u5e95\u5c42\u4f9d\u8d56\u5168\u5c40\u53d8\u91cf errno\u3002\u5f53\u7cfb\u7edf\u8c03\u7528\u51fa\u9519\u65f6&#xff0c;\u5185\u6838\u4f1a\u628a\u9519\u8bef\u7f16\u53f7\u5199\u5165 errno&#xff0c;\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5b83\u5b9a\u4f4d\u539f\u56e0\u3002\u58f0\u660e\u5728 &lt;stdio.h&gt; \u4e2d&#xff0c;errno \u5b9a\u4e49\u5728 &lt;errno.h&gt; \u4e2d\u3002<\/p>\n<ul>\n<li>\u4f5c\u7528&#xff1a;\u5148\u6253\u5370\u4f60\u4f20\u5165\u7684\u5b57\u7b26\u4e32 s&#xff0c;\u518d\u81ea\u52a8\u62fc\u63a5\u4e0a errno \u5bf9\u5e94\u7684\u6587\u5b57\u9519\u8bef\u4fe1\u606f\u3002<\/li>\n<li>\u8f93\u51fa\u76ee\u6807&#xff1a;\u6807\u51c6\u9519\u8bef stderr&#xff0c;\u548c fprintf(stderr, &#8230;) \u662f\u540c\u4e00\u7c7b\u3002<\/li>\n<li>\u5bf9\u6bd4&#xff1a;strerror(errno) \u53ea\u8fd4\u56de\u9519\u8bef\u5b57\u7b26\u4e32\u4f46\u4e0d\u6253\u5370&#xff0c;\u4f7f\u7528\u4e0a\u66f4\u7075\u6d3b&#xff1b;perror \u5219\u9002\u5408\u76f4\u63a5\u62a5\u9519\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>if (-1 &#061;&#061; ret) {<br \/>\n    perror(&#034;chdir&#034;);<br \/>\n    \/* \u53ef\u80fd\u8f93\u51fa&#xff1a;chdir: No such file or directory *\/<br \/>\n}<\/p>\n<p>\u5ef6\u4f38\u7406\u89e3&#xff1a;errno \u53ea\u5728\u7cfb\u7edf\u8c03\u7528\u6216\u90e8\u5206\u5e93\u51fd\u6570\u5931\u8d25\u65f6\u624d\u6709\u610f\u4e49&#xff0c;\u6210\u529f\u65f6\u5b83\u7684\u503c\u53ef\u80fd\u6b8b\u7559\u65e7\u9519\u8bef\u3002\u4e25\u8c28\u505a\u6cd5\u662f\u5148\u5224\u65ad\u8fd4\u56de\u503c\u662f\u5426\u8868\u793a\u5931\u8d25&#xff0c;\u518d\u8bfb\u53d6 errno&#xff0c;\u6216\u8005\u5728\u8c03\u7528\u524d\u624b\u52a8\u628a errno \u6e05\u96f6&#xff0c;\u907f\u514d\u8bef\u8bfb\u65e7\u503c\u3002<\/p>\n<h3>\u4e03\u3001\u6574\u4f53\u4e32\u8054\u4e0e\u6613\u9519\u8003\u70b9\u6c47\u603b<\/h3>\n<p>\u4ee5\u4e0b\u662f\u53ef\u4ee5\u76f4\u63a5\u80cc\u8bf5\u7684\u7b80\u7b54\u7248\u91cd\u70b9&#xff1a;<\/p>\n<li>\u76ee\u5f55\u662f\u7279\u6b8a\u6587\u4ef6&#xff0c;\u5b58\u653e\u76ee\u5f55\u6761\u76ee\u4fe1\u606f&#xff1b;\u904d\u5386\u6d41\u7a0b\u56fa\u5b9a\u4e3a opendir \u2192 \u5faa\u73af readdir \u2192 closedir\u3002<\/li>\n<li>readdir \u8fd4\u56de\u7684 dirent * \u4e0d\u7528\u624b\u52a8 free&#xff0c;\u5b83\u4f9d\u9644\u4e8e DIR \u76ee\u5f55\u6d41&#xff0c;closedir \u65f6\u4f1a\u81ea\u52a8\u91ca\u653e\u3002<\/li>\n<li>d_name \u53ea\u6709\u6587\u4ef6\u540d&#xff0c;\u4e0d\u662f\u5b8c\u6574\u8def\u5f84&#xff0c;\u9012\u5f52\u904d\u5386\u5fc5\u987b\u624b\u52a8\u62fc\u63a5\u8def\u5f84\u3002<\/li>\n<li>chdir \u53ea\u6539\u5f53\u524d\u8fdb\u7a0b\u7684\u5de5\u4f5c\u76ee\u5f55&#xff0c;\u7ec8\u7aef\u4e0d\u53d7\u5f71\u54cd&#xff1b;\u7a0b\u5e8f\u5185\u7684\u76f8\u5bf9\u8def\u5f84\u4f9d\u8d56\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002<\/li>\n<li>rmdir \u53ea\u80fd\u5220\u9664\u7a7a\u76ee\u5f55&#xff0c;\u4e0d\u80fd\u9012\u5f52\u5220\u9664\u3002<\/li>\n<li>stat \u83b7\u53d6\u6587\u4ef6\u5168\u90e8\u5c5e\u6027&#xff1b;st_size \u662f\u6587\u4ef6\u5b57\u8282\u5927\u5c0f&#xff1b;st_mode \u540c\u65f6\u5305\u542b\u6743\u9650\u548c\u6587\u4ef6\u7c7b\u578b\u3002<\/li>\n<li>Unix \u65f6\u95f4\u4ee5 1970-01-01 \u4e3a\u8d77\u70b9\u8ba1\u7b97\u79d2\u6570&#xff1b;ctime \u76f4\u63a5\u8f6c\u5b57\u7b26\u4e32&#xff0c;localtime \u8f6c tm \u7ed3\u6784\u4f53&#xff0c;\u6ce8\u610f\u5e74\u4efd\u548c\u6708\u4efd\u8981\u52a0\u504f\u79fb\u3002<\/li>\n<li>perror \u8bfb\u53d6\u5168\u5c40 errno&#xff0c;\u5411\u6807\u51c6\u9519\u8bef\u6253\u5370\u9519\u8bef\u4fe1\u606f\u3002<\/li>\n<h3>\u516b\u3001\u6781\u7b80 ls \u904d\u5386\u6a21\u677f<\/h3>\n<p>\u4e0b\u9762\u662f\u6700\u57fa\u7840\u7684\u76ee\u5f55\u904d\u5386\u6a21\u677f&#xff0c;\u53ea\u6253\u5370 &#034;.&#034; \u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6761\u76ee\u540d&#xff1a;<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;dirent.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>int main(void)<br \/>\n{<br \/>\n    DIR *dir &#061; opendir(&#034;.&#034;);<br \/>\n    if (NULL &#061;&#061; dir) {<br \/>\n        perror(&#034;opendir&#034;);<br \/>\n        return 1;<br \/>\n    }<\/p>\n<p>    struct dirent *info;<br \/>\n    while ((info &#061; readdir(dir)) !&#061; NULL) {<br \/>\n        printf(&#034;%s\\\\n&#034;, info-&gt;d_name);<br \/>\n    }<\/p>\n<p>    closedir(dir);<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>8.1 \u5ef6\u4f38\u4e00&#xff1a;\u9012\u5f52\u904d\u5386\u76ee\u5f55\u5e76\u62fc\u63a5\u8def\u5f84<\/h4>\n<p>\u4e0b\u9762\u7684\u793a\u4f8b\u6f14\u793a\u4e86\u6700\u5bb9\u6613\u8e29\u5751\u7684\u4e24\u70b9&#xff1a;\u8fc7\u6ee4 &#034;.&#034; \u548c &#034;..&#034;&#xff0c;\u4ee5\u53ca\u7528\u5b8c\u6574\u8def\u5f84\u9012\u5f52\u8fdb\u5165\u5b50\u76ee\u5f55\u3002\u56e0\u4e3a d_name \u53ea\u6709\u540d\u5b57&#xff0c;\u8fdb\u5165\u5b50\u76ee\u5f55\u524d\u5fc5\u987b\u62fc\u4e0a\u7236\u76ee\u5f55\u8def\u5f84\u3002<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;dirent.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>void list_dir(const char *path, int depth)<br \/>\n{<br \/>\n    DIR *dir &#061; opendir(path);<br \/>\n    if (NULL &#061;&#061; dir) {<br \/>\n        perror(&#034;opendir&#034;);<br \/>\n        return;<br \/>\n    }<\/p>\n<p>    struct dirent *info;<br \/>\n    while ((info &#061; readdir(dir)) !&#061; NULL) {<br \/>\n        if (strcmp(info-&gt;d_name, &#034;.&#034;) &#061;&#061; 0 ||<br \/>\n            strcmp(info-&gt;d_name, &#034;..&#034;) &#061;&#061; 0) {<br \/>\n            continue;<br \/>\n        }<\/p>\n<p>        for (int i &#061; 0; i &lt; depth; i&#043;&#043;) {<br \/>\n            printf(&#034;  &#034;);<br \/>\n        }<\/p>\n<p>        char full_path[1024];<br \/>\n        snprintf(full_path, sizeof(full_path), &#034;%s\/%s&#034;,<br \/>\n                 path, info-&gt;d_name);<\/p>\n<p>        if (info-&gt;d_type &#061;&#061; DT_DIR) {<br \/>\n            printf(&#034;[DIR]  %s\\\\n&#034;, info-&gt;d_name);<br \/>\n            list_dir(full_path, depth &#043; 1);<br \/>\n        } else {<br \/>\n            printf(&#034;[FILE] %s\\\\n&#034;, info-&gt;d_name);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    closedir(dir);<br \/>\n}<\/p>\n<p>int main(int argc, char *argv[])<br \/>\n{<br \/>\n    const char *path &#061; argc &gt; 1 ? argv[1] : &#034;.&#034;;<br \/>\n    list_dir(path, 0);<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>8.2 \u5ef6\u4f38\u4e8c&#xff1a;d_type \u4e0d\u53ef\u7528\u65f6\u7684 stat \u515c\u5e95<\/h4>\n<p>\u90e8\u5206\u6587\u4ef6\u7cfb\u7edf\u4e0d\u652f\u6301 d_type&#xff0c;\u4f1a\u8fd4\u56de DT_UNKNOWN\u3002\u6b64\u65f6\u5e94\u62fc\u63a5\u5b8c\u6574\u8def\u5f84\u540e\u8c03\u7528 lstat&#xff0c;\u518d\u901a\u8fc7 S_ISREG \/ S_ISDIR \/ S_ISLNK \u5b8f\u5224\u65ad\u7c7b\u578b\u3002\u8fd9\u91cc\u523b\u610f\u4f7f\u7528 lstat&#xff0c;\u786e\u4fdd\u80fd\u6b63\u786e\u8bc6\u522b\u8f6f\u94fe\u63a5\u672c\u8eab&#xff0c;\u800c\u4e0d\u662f\u5b83\u6307\u5411\u7684\u76ee\u6807\u3002<\/p>\n<p>#include &lt;string.h&gt;<br \/>\n#include &lt;dirent.h&gt;<br \/>\n#include &lt;sys\/stat.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>int safe_type(const char *dir_path, struct dirent *info)<br \/>\n{<br \/>\n    if (info-&gt;d_type !&#061; DT_UNKNOWN) {<br \/>\n        return info-&gt;d_type;<br \/>\n    }<\/p>\n<p>    char full_path[1024];<br \/>\n    snprintf(full_path, sizeof(full_path), &#034;%s\/%s&#034;,<br \/>\n             dir_path, info-&gt;d_name);<\/p>\n<p>    struct stat st;<br \/>\n    if (lstat(full_path, &amp;st) &#061;&#061; 0) {<br \/>\n        if (S_ISDIR(st.st_mode)) return DT_DIR;<br \/>\n        if (S_ISLNK(st.st_mode)) return DT_LNK;<br \/>\n        if (S_ISREG(st.st_mode)) return DT_REG;<br \/>\n    }<\/p>\n<p>    return DT_UNKNOWN;<br \/>\n}<\/p>\n<h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<p>\u76ee\u5f55\u64cd\u4f5c\u7684\u5b66\u4e60\u53ef\u4ee5\u6293\u4f4f\u4e24\u6761\u4e3b\u7ebf&#xff1a;<\/p>\n<ul>\n<li>\u904d\u5386\u8fd9\u6761\u7ebf&#xff1a;\u628a\u76ee\u5f55\u5f53\u4f5c\u4e00\u79cd\u7279\u6b8a\u6587\u4ef6&#xff0c;\u7528 opendir\u3001readdir\u3001closedir \u5b8c\u6210\u987a\u5e8f\u8bfb\u53d6&#xff0c;\u540c\u65f6\u8b66\u60d5 d_name \u4e0d\u542b\u8def\u5f84\u3001\u8fd4\u56de\u503c\u7f13\u51b2\u533a\u4f1a\u88ab\u8986\u76d6\u3001d_type \u53ef\u80fd\u5931\u6548\u8fd9\u4e09\u4e2a\u5751\u3002<\/li>\n<li>\u5c5e\u6027\u8fd9\u6761\u7ebf&#xff1a;\u7528 stat \/ lstat \u83b7\u53d6 inode\u3001\u7c7b\u578b\u3001\u5927\u5c0f\u3001\u786c\u94fe\u63a5\u6570\u3001\u65f6\u95f4\u7b49\u5143\u4fe1\u606f&#xff0c;\u914d\u5408 chdir \/ getcwd \/ mkdir \/ rmdir \u7ba1\u7406\u76ee\u5f55\u672c\u8eab&#xff0c;\u518d\u7528 time \/ localtime \/ ctime \u628a\u65f6\u95f4\u6233\u7ffb\u8bd1\u6210\u4eba\u80fd\u770b\u61c2\u7684\u5f62\u5f0f\u3002<\/li>\n<\/ul>\n<p>\u628a\u8fd9\u4e24\u6761\u7ebf\u4e32\u8d77\u6765&#xff0c;\u518d\u8bb0\u4f4f errno \/ perror \u8d1f\u8d23\u9519\u8bef\u5b9a\u4f4d&#xff0c;\u5c31\u80fd\u5b8c\u6574\u8986\u76d6 Linux \u76ee\u5f55\u64cd\u4f5c\u7684\u57fa\u7840\u9762\u3002\u540e\u7eed\u9047\u5230 nftw \u8fd9\u7c7b\u9012\u5f52\u904d\u5386\u5de5\u5177&#xff0c;\u672c\u8d28\u4e0a\u4e5f\u53ea\u662f\u5bf9 opendir \/ readdir \/ closedir \u6d41\u7a0b\u7684\u8fdb\u4e00\u6b65\u5c01\u88c5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u8981&#xff1a;\u672c\u6587\u7cfb\u7edf\u68b3\u7406 Linux \u76ee\u5f55\u64cd\u4f5c\u7684\u6838\u5fc3 API&#xff0c;\u56f4\u7ed5\u300c\u76ee\u5f55\u672c\u8d28\u4e5f\u662f\u6587\u4ef6\u300d\u8fd9\u4e00\u5927\u524d\u63d0&#xff0c;\u4f9d\u6b21\u8bb2\u89e3 opendir\u3001readdir\u3001closedir \u4e09\u5927\u76ee\u5f55\u51fd\u6570&#xff0c;\u518d\u5ef6\u4f38\u5230 chdir\u3001getcwd\u3001mkdir\u3001rmdir\u3001stat \u7b49\u8f85\u52a9\u51fd\u6570&#xff0c;\u5e76\u7ed3\u5408 Unix \u65f6\u95f4\u6233\u4e0e errno \u9519\u8bef\u5904\u7406\u505a\u4e32\u8054\u3002\u6587\u4e2d\u8865\u5145\u4e86 dirent \u7f13\u51b2\u533a\u9677\u9631\u3001d_type \u515c\u5e95\u65b9\u6848\u3001\u9012\u5f52\u8def\u5f84\u62fc\u63a5\u3001umask \u5bf9 mkdir \u7684<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,305,44],"topic":[],"class_list":["post-118138","post","type-post","status-publish","format-standard","hentry","category-server","tag-linux","tag-305","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Linux \u76ee\u5f55\u64cd\u4f5c\u5168\u5957\u7b14\u8bb0\u603b\u7ed3\uff1a\u4ece opendir \u5230 stat \u4e0e\u65f6\u95f4\u6233 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/118138.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linux \u76ee\u5f55\u64cd\u4f5c\u5168\u5957\u7b14\u8bb0\u603b\u7ed3\uff1a\u4ece opendir \u5230 stat \u4e0e\u65f6\u95f4\u6233 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6458\u8981&#xff1a;\u672c\u6587\u7cfb\u7edf\u68b3\u7406 Linux \u76ee\u5f55\u64cd\u4f5c\u7684\u6838\u5fc3 API&#xff0c;\u56f4\u7ed5\u300c\u76ee\u5f55\u672c\u8d28\u4e5f\u662f\u6587\u4ef6\u300d\u8fd9\u4e00\u5927\u524d\u63d0&#xff0c;\u4f9d\u6b21\u8bb2\u89e3 opendir\u3001readdir\u3001closedir \u4e09\u5927\u76ee\u5f55\u51fd\u6570&#xff0c;\u518d\u5ef6\u4f38\u5230 chdir\u3001getcwd\u3001mkdir\u3001rmdir\u3001stat \u7b49\u8f85\u52a9\u51fd\u6570&#xff0c;\u5e76\u7ed3\u5408 Unix \u65f6\u95f4\u6233\u4e0e errno \u9519\u8bef\u5904\u7406\u505a\u4e32\u8054\u3002\u6587\u4e2d\u8865\u5145\u4e86 dirent \u7f13\u51b2\u533a\u9677\u9631\u3001d_type \u515c\u5e95\u65b9\u6848\u3001\u9012\u5f52\u8def\u5f84\u62fc\u63a5\u3001umask \u5bf9 mkdir \u7684\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/118138.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-26T04:08:25+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/118138.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/118138.html\",\"name\":\"Linux \u76ee\u5f55\u64cd\u4f5c\u5168\u5957\u7b14\u8bb0\u603b\u7ed3\uff1a\u4ece opendir \u5230 stat \u4e0e\u65f6\u95f4\u6233 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-10-26T04:08:25+00:00\",\"dateModified\":\"2026-10-26T04:08:25+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/118138.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/118138.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/118138.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux \u76ee\u5f55\u64cd\u4f5c\u5168\u5957\u7b14\u8bb0\u603b\u7ed3\uff1a\u4ece opendir \u5230 stat \u4e0e\u65f6\u95f4\u6233\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Linux \u76ee\u5f55\u64cd\u4f5c\u5168\u5957\u7b14\u8bb0\u603b\u7ed3\uff1a\u4ece opendir \u5230 stat \u4e0e\u65f6\u95f4\u6233 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/118138.html","og_locale":"zh_CN","og_type":"article","og_title":"Linux \u76ee\u5f55\u64cd\u4f5c\u5168\u5957\u7b14\u8bb0\u603b\u7ed3\uff1a\u4ece opendir \u5230 stat \u4e0e\u65f6\u95f4\u6233 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6458\u8981&#xff1a;\u672c\u6587\u7cfb\u7edf\u68b3\u7406 Linux \u76ee\u5f55\u64cd\u4f5c\u7684\u6838\u5fc3 API&#xff0c;\u56f4\u7ed5\u300c\u76ee\u5f55\u672c\u8d28\u4e5f\u662f\u6587\u4ef6\u300d\u8fd9\u4e00\u5927\u524d\u63d0&#xff0c;\u4f9d\u6b21\u8bb2\u89e3 opendir\u3001readdir\u3001closedir \u4e09\u5927\u76ee\u5f55\u51fd\u6570&#xff0c;\u518d\u5ef6\u4f38\u5230 chdir\u3001getcwd\u3001mkdir\u3001rmdir\u3001stat \u7b49\u8f85\u52a9\u51fd\u6570&#xff0c;\u5e76\u7ed3\u5408 Unix \u65f6\u95f4\u6233\u4e0e errno \u9519\u8bef\u5904\u7406\u505a\u4e32\u8054\u3002\u6587\u4e2d\u8865\u5145\u4e86 dirent \u7f13\u51b2\u533a\u9677\u9631\u3001d_type \u515c\u5e95\u65b9\u6848\u3001\u9012\u5f52\u8def\u5f84\u62fc\u63a5\u3001umask \u5bf9 mkdir \u7684","og_url":"https:\/\/www.wsisp.com\/helps\/118138.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-10-26T04:08:25+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"7 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/118138.html","url":"https:\/\/www.wsisp.com\/helps\/118138.html","name":"Linux \u76ee\u5f55\u64cd\u4f5c\u5168\u5957\u7b14\u8bb0\u603b\u7ed3\uff1a\u4ece opendir \u5230 stat \u4e0e\u65f6\u95f4\u6233 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-10-26T04:08:25+00:00","dateModified":"2026-10-26T04:08:25+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/118138.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/118138.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/118138.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Linux \u76ee\u5f55\u64cd\u4f5c\u5168\u5957\u7b14\u8bb0\u603b\u7ed3\uff1a\u4ece opendir \u5230 stat \u4e0e\u65f6\u95f4\u6233"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/118138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=118138"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/118138\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=118138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=118138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=118138"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=118138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}