{"id":740,"date":"2010-07-03T22:22:51","date_gmt":"2010-07-04T04:22:51","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=740"},"modified":"2010-07-26T21:14:44","modified_gmt":"2010-07-27T03:14:44","slug":"%e4%b8%8d%e6%96%ad%e7%9a%84%e4%b8%b0%e5%af%8c%e4%bd%a0%e7%9a%84ls","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/740","title":{"rendered":"\u4e30\u5bcc\u4f60\u7684ls(updating\u2026\u2026)"},"content":{"rendered":"<p>\u5982\u679c\u6211\u4eec\u5df2\u7ecf\u5bf9my_ls.c\u7684\u7ed3\u6784\u6709\u4e86\u6df1\u523b\u4e86\u89e3\uff0c\u4ee5\u53ca\u5bf9\u6b64\u7a0b\u5e8f\u4e2d\u6240\u6d89\u53ca\u7cfb\u7edf\u8c03\u7528\u51fd\u6570\u90fd\u5df2\u7406\u89e3\uff0c\u90a3\u4e48\u4e0d\u59a8\u7ee7\u7eed\u589e\u6dfb\u9009\u9009\u578b\uff0c\u8ba9\u4f60\u7684ls\u547d\u4ee4\u7a0b\u5e8f\u4e0d\u65ad\u5b8c\u6574\u3002\u6b63\u5982\u4f60\u6240\u7406\u89e3\u7684\u90a3\u6837\uff0c\u524d\u9762\u6211\u4eec\u5b9e\u73b0\u7684my_ls.c\u5c5e\u4e8e\u4e00\u4e2a\u201c\u63d2\u5ea7\u201d\uff0c\u8981\u5b9e\u73b0\u5176\u4ed6\u9009\u9879\uff0c\u57fa\u672c\u4e0a\u90fd\u662f\u5728my_ls.c\u6e90\u7801\u4e2d\u6dfb\u52a0\u65b0\u4ee3\u7801\u3002\u6211\u4eec\u5c06\u4e0d\u65ad\u5b8c\u5584\u9009\u9879\u529f\u80fd\u7684\u90a3\u4e2a\u6e90\u7801\u79f0\u4e3amy_ls_plus.c\u3002<\/p>\n<p>1.\u589e\u52a0-i\u9009\u9879<\/p>\n<p>ls -i\u53ef\u4ee5\u663e\u793a\u51fa\u6bcf\u4e2a\u6587\u4ef6\u7684i\u8282\u70b9\u7f16\u53f7\u3002\u5177\u4f53\u7684\u5b9e\u73b0\u529e\u6cd5\u662f\u5229\u7528stat\u65cf\u51fd\u6570\uff0c\u5c06\u6587\u4ef6\u7684\u5c5e\u6027\u8fd4\u56de\u5230\u7c7b\u578b\u4e3astruct stat\u7684\u53c2\u6570buf\u4e2d\uff0c\u7136\u540e\u5728\u9700\u8981\u7684\u4f4d\u7f6e\u6253\u5370st_ino\u5b57\u6bb5\u5373\u53ef\u3002\u6ce8\u610fst_ino\u6253\u5370\u7684\u65f6\u5019\u9700\u8981\u957f\u6574\u578b\u3002<\/p>\n<p>\u83b7\u53d6\u6587\u4ef6\u5c5e\u6027\u4e3e\u4f8b:<\/p>\n<pre class=\"brush:c\">if(lstat(pathname,&amp;buf)==-1)\r\n\t{\r\n\t\tmy_err(\"lstat\",errno,__LINE__);\r\n\t}<\/pre>\n<p>\u6253\u5370\u4e3e\u4f8b\uff1a<\/p>\n<pre class=\"brush:\">if(if_i)\r\n\t{\r\n\t\tprintf(\"%ld \",buf.st_ino);\r\n\t}<\/pre>\n<p>2.\u589e\u52a0-t\u9009\u9879<\/p>\n<p>ls -t \u6309\u6bcf\u4e2a\u6587\u4ef6\u6700\u540e\u4e00\u6b21\u88ab\u4fee\u6539\u7684\u65f6\u95f4\uff08st_mtime\uff09\u6392\u5217\u663e\u793a\u6587\u4ef6\u3002\u8fd9\u91cc\u5bf9\u6587\u4ef6\u5c5e\u6027\u4e2d\u4e09\u4e2a\u5173\u4e8e\u65f6\u95f4\u7684\u5b57\u6bb5\u505a\u4ee5\u8bf4\u660e\u3002<\/p>\n<p>st_atime:\u6587\u4ef6\u6700\u540e\u4e00\u6b21\u88ab\u8bbf\u95ee\uff08access\uff09\u7684\u65f6\u95f4\u3002\u8fd9\u4e2a\u597d\u7406\u89e3\u3002<\/p>\n<p>st_mtime:\u6587\u4ef6\u6700\u540e\u4e00\u6b21\u88ab\u4fee\u6539\uff08modification\uff09\u7684\u65f6\u95f4\u3002<\/p>\n<p>st_ctime:\u6587\u4ef6\u5b57\u540e\u4e00\u6b21\u88ab\u66f4\u6539\uff08create\uff09\u7684\u65f6\u95f4\u3002<\/p>\n<p>mtime\u548cctime\u5176\u5b9e\u90fd\u662f\u4fee\u6539\u65f6\u95f4\uff0c\u4f46\u662f\u524d\u8005\u6307\u6587\u4ef6\u5185\u5bb9\u88ab\u4fee\u6539\u540e\u7684\u65f6\u95f4\uff0c\u800c\u540e\u8005\u66f4\u503e\u5411\u4e8e\u6587\u4ef6\u6240\u6709\u8005\u3001\u6240\u5c5e\u7ec4\u4ee5\u53ca\u6587\u4ef6\u6743\u9650\u7b49\u5c5e\u6027\u88ab\u66f4\u6539\u540e\u7684\u65f6\u95f4\u3002\u5f53\u7136\u5982\u679c\u6587\u4ef6\u5185\u5bb9\u4fee\u6539\uff0c\u4e24\u8005\u7684\u65f6\u95f4\u90fd\u4f1a\u88ab\u4fee\u6539\u3002\u6211\u4eec\u4e00\u822c\u7528ls -l\u6240\u663e\u793a\u7684\u65f6\u95f4\u6307mtime\uff0c\u800c\u8981\u663e\u793a\u51factime\u5219\u53ef\u4ee5\u7528: ls -lc\u3002\u663e\u793a\u51faatime\u5219\u53ef\u4ee5\u7528\uff1als -lu\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u8981\u5b9e\u73b0ls -t\u547d\u4ee4\u7684\u4f5c\u7528\uff0c\u6211\u4eec\u5fc5\u987b\u5bf9\u6587\u4ef6\u6309\u7167mtime\u8fdb\u884c\u6392\u5e8f\u3002\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u5168\u5c40\u53d8\u91cfif_t\u6765\u6807\u8bb0\u662f\u5426\u51fa\u73b0\u4e86 -t\u9009\u9879\u3002\u5982\u679c\u51fa\u73b0\u5219\u8c03\u7528\u6309\u7167\u65f6\u95f4\u6392\u5e8f\u7684\u51fd\u6570sort_by_mtime(char filename[][PATH_MAX+1],int count);\u6b64\u51fd\u6570\u7684\u5927\u81f4\u6d41\u7a0b\u662f\uff1a\u9996\u5148\u5229\u7528stat\u65cf\u51fd\u6570\u83b7\u53d6\u6bcf\u4e2a\u6587\u4ef6\u7684mtime\uff0c\u5b58\u4e8e\u5b57\u7b26\u4e32\u6570\u7ec4mtime[256][20]\u4e2d\uff1b\u518d\u5229\u7528\u67d0\u79cd\u6392\u5e8f\u7b97\u6cd5\u6392\u5e8f\u5373\u53ef\u3002\u5982\u679c\u4f60\u5bf9C\u8bed\u8a00\u57fa\u672c\u77e5\u8bc6\u638c\u63e1\u7684\u8fd8\u7b97\u53ef\u4ee5\uff0c\u90a3\u4e48\u8981\u5b9e\u73b0\u4e0a\u8ff0\u8fc7\u7a0b\u5e76\u4e0d\u56f0\u96be\u3002<\/p>\n<p>\u83b7\u53d6\u6587\u4ef6\u7684mtime\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a(\u7531\u4e8e\u4ee3\u7801\u63d2\u4ef6\u95ee\u9898\uff0c\u8bf7\u5ffd\u7565&lt;\u540e\u9762\u7684\u2018\\\u2019)<\/p>\n<pre class=\"brush:c\">for(i=0;i&lt;\\count;i++)\r\n\t{\r\n\t\tif(stat(filename[i],&amp;buf)==-1)\r\n\t\t{\r\n\t\t\tmy_err(\"stat\",errno,__LINE__);\r\n\t\t}\r\n\t\tstrcpy(buf_time,ctime(&amp;buf.st_mtime));\r\n\t\tbuf_time[strlen(buf_time)-1]='\\0';\r\n\t\tconvert_time(mtime[i],buf_time);\r\n\t}<\/pre>\n<p>\u4e0b\u9762\u7684\u6392\u5e8f\u4ee3\u7801\u91c7\u7528\u9009\u62e9\u6392\u5e8f\u6cd5\u8fdb\u884c\u6392\u5e8f\uff0c\u6ce8\u610f\u6211\u4eec\u5728\u6bd4\u8f83\u7684\u65f6\u5019\u5fc5\u7136\u662f\u5bf9\u65f6\u95f4\u8fdb\u884c\u6bd4\u8f83\uff0c\u800c\u5728\u4ea4\u6362\u4e24\u4e2a\u6570\u636e\u7684\u65f6\u5019\uff0c\u5fc5\u987b\u5bf9\u6587\u4ef6\u540d\u548c\u65f6\u95f4\u4e00\u8d77\u4ea4\u6362\u3002\u5f53\u521d\u6211\u53ea\u4ea4\u6362\u4e86\u6587\u4ef6\u540d\uff0c\u627e\u4e86\u597d\u4e45\u7684&#8221;\u9519\u8bef&#8221;\u3002<\/p>\n<pre class=\"brush:c\">\/\/sorting\r\n\tfor(i=0;i&lt;\\count;i++)\r\n\t{\r\n\t\tk=i;\r\n\t\tfor(j=i;j&lt;\\count;j++)\r\n               {\r\n                        if(strcmp(mtime[j],mtime[k])&gt;\\0) k=j;\r\n\t\t}\r\n\t\tif(k!=i)\r\n\t\t{\r\n\t\t\t\/\/exchange mtime\r\n\t\t\tstrcpy(temp,mtime[i]);\r\n\t\t\tstrcpy(mtime[i],mtime[k]);\r\n\t\t\tstrcpy(mtime[k],temp);\r\n\t\t\t\/\/exchange filename\r\n\t\t\tstrcpy(temp,filename[i]);\r\n\t\t\tstrcpy(filename[i],filename[k]);\r\n\t\t\tstrcpy(filename[k],temp);\r\n\t\t}\r\n\t}<\/pre>\n<p>\u8fd9\u6837\uff0c\u4f60\u5c31\u53ef\u4ee5\u5b9e\u73b0-t\u9009\u9879\u7684\u529f\u80fd\u3002<\/p>\n<p>3.\u589e\u52a0-A\u9009\u9879<\/p>\n<p>\u8fd9\u4e2a\u9009\u9879\u7406\u89e3\u6bd4\u8f83\u7b80\u5355\u3002\u6211\u4eec\u77e5\u9053ls -a\u662f\u663e\u793a\u5305\u62ec\u9690\u85cf\u6587\u4ef6\u5728\u5185\u7684\u6240\u6709\u6587\u4ef6\u3002\u800cls -A\u5219\u663e\u793a\u9664\u4e86.\u548c..\u4e4b\u5916\u7684\u6240\u6709\u6587\u4ef6\u3002\u6211\u4eec\u77e5\u9053\u5728my_ls.c\u4e2dvoid display(int flag,char* pathname)\u662f\u6839\u636eflag\u5bf9\u5355\u4e2a\u6587\u4ef6pathname\u8fdb\u884c\u5904\u7406\uff0c\u6839\u636eflag\u7684\u60c5\u51b5\u6765\u5224\u65ad\u662f\u4ee5\u4ec0\u4e48\u65b9\u5f0f\u663e\u793a\u6240\u6709\u6587\u4ef6\u540d\u3002\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u5728\u6b64\u57fa\u7840\u4e0a\u52a0\u5165\u5bf9-A\u9009\u9879\u7684\u5224\u65ad\uff1a<\/p>\n<pre class=\"brush:c\">if((if_A&amp;&amp;!strcmp(pathname,\".\"))||(if_A&amp;&amp;!strcmp(pathname,\"..\")))\r\n\t{\r\n\t\t\/\/blank sentence,do not list this file\r\n\t}\r\n\telse\r\n\t{\r\n\t\t\/\/\u7701\u7565switch\u8bed\u53e5\r\n\t}<\/pre>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u5b9e\u73b0-A\u9009\u9879\u4e86\u3002<\/p>\n<p>4.\u589e\u52a0-B\u9009\u9879<\/p>\n<p>\u5982\u679c\u4f60\u5b8c\u6210\u4e86-A\u9009\u9879\uff0c\u90a3\u4e48\u6b64\u9009\u9879\u5bf9\u4f60\u6765\u8bf4\u8f7b\u800c\u6613\u4e3e\u3002-B\u9009\u9879\u662f\u4e0d\u663e\u793a\u4ee5\uff5e\u7ed3\u5c3e\u7684\u5907\u4efd\u6587\u4ef6\u3002\u90a3\u4e48\u53ea\u8981\u5728-A\u9009\u9879\u7684\u57fa\u7840\u4e0a\u4fee\u6539\u6e90\u4ee3\u7801\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\">if((if_A&amp;&amp;!strcmp(pathname,\".\"))||(if_A&amp;&amp;!strcmp(pathname,\"..\")))\r\n\t{\r\n\t\t\/\/blank sentence,do not list this file\r\n\t}\r\n\telse if(if_B&amp;&amp;pathname[strlen(pathname)-1]=='~')\r\n\t{\r\n\t\t\/\/blank sentence,do not list this file\r\n\t}\r\n\telse\r\n\t{\r\n\t\t\/\/switch\u8bed\u53e5\u7701\u7565\r\n        }<\/pre>\n<p>5.\u589e\u52a0-d\u9009\u9879<\/p>\n<p>-d\u9009\u9879\u53ea\u4f1a\u663e\u793a\u5f53\u524d\u76ee\u5f55\uff08.\uff09\uff0c\u800c\u4e0d\u662f\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u3002\u5982\u679c\u52a0\u5165-l\u5219\u4f1a\u663e\u793a\u5f53\u524d\u76ee\u5f55\u7684\u5404\u79cd\u5c5e\u6027\u3002\u524d\u9762\u7684-B\u7b49\u9009\u9879\u6211\u4eec\u5728\u6dfb\u52a0\u7684\u65f6\u5019\u90fd\u662f\u5728display\u51fd\u6570\u4e2d\u6dfb\u52a0\u67d0\u4e9b\u4ee3\u7801\uff0c\u56e0\u4e3a\u6700\u7ec8\u7ed3\u679c\u8fd8\u8981\u4e0e-a\u548c-l\u9009\u9879\u76f8\u7ed3\u5408\u3002\u6bd4\u5982ls -laA,\u867d\u7136\u4e0d\u663e\u793a.\u548c..\uff0c\u4f46\u662f\u8fd8\u662f\u8981\u663e\u793a\u4ee5.\u5f00\u59cb\u7684\u5176\u4ed6\u6587\u4ef6\u3002\u800c-d\u9009\u9879\u5219\u4e0d\u7528\u770b-a\u7684\u201c\u8138\u8272\u2018\u3002\u56e0\u4e3a\u4e0d\u8bba\u4f60\u662f\u5426\u52a0\u5165-a\u9009\u9879\uff0c\u6b64\u9009\u9879\u53ea\u4f1a\u663e\u793a\u5f53\u524d\u76ee\u5f55\u3002\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\">\r\nedsionte@edsionte-laptop:~\/code\/file$ ls -laAd\r\ndrwxr-xr-x 2 edsionte edsionte 4096 2010-07-06 14:50 .\r\nedsionte@edsionte-laptop:~\/code\/file$ ls -ld\r\ndrwxr-xr-x 2 edsionte edsionte 4096 2010-07-06 14:50 .\r\nedsionte@edsionte-laptop:~\/code\/file$ ls -d\r\n.\r\n<\/pre>\n<p>\u56e0\u6b64\u5173\u4e8e\u6b64\u9009\u9879\u7684\u4ee3\u7801\u5e94\u6dfb\u52a0\u5728display_dir\u51fd\u6570\u4e2d\uff0c\u5177\u4f53\u4f4d\u7f6e\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\">\r\n\/\/if the command include '-d'\r\n\tif(if_d)\r\n\t{\r\n\t\tif(stat(\".\",&buf)<\\0)\r\n\t\t{\r\n\t\t\tmy_err(\"stat\",errno,__LINE__);\r\n\t\t}\r\n\t\tif(flag_param==PARAM_L||flag_param==PARAM_L+PARAM+A)\r\n\t\t\tdisplay_attribute(buf,\".\");\r\n\t\telse\r\n\t\t\tdisplay_single(buf,\".\");\r\n\t}\r\n\telse\r\n\t{\r\n\t\tfor(i=0;i<\\count;i++)\r\n        \t{\r\n\t\t\tdisplay(flag_param,filename[i]);\r\n        \t}\r\n\t}\r\n<\/pre>\n<p>\u8fd9\u6837\u6dfb\u52a0\u5373\u53ef\uff0c\u901a\u8fc7\u4ee5\u4e0a\u51e0\u4e2a\u9009\u9879\u7684\u6dfb\u52a0\uff0c\u4f60\u5e94\u8be5\u5927\u81f4\u4e86\u89e3\u5e94\u8be5\u5728\u4f55\u5904\u6dfb\u52a0\u4ee3\u7801\u4e86\u3002<\/p>\n<p>\u5f85\u7eed\u3002\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c\u6211\u4eec\u5df2\u7ecf\u5bf9my_ls.c\u7684\u7ed3\u6784\u6709\u4e86\u6df1\u523b\u4e86\u89e3\uff0c\u4ee5\u53ca\u5bf9\u6b64\u7a0b\u5e8f\u4e2d\u6240\u6d89\u53ca\u7cfb\u7edf\u8c03\u7528\u51fd\u6570\u90fd\u5df2\u7406\u89e3\uff0c\u90a3\u4e48\u4e0d\u59a8\u7ee7\u7eed\u589e\u6dfb\u9009\u9009 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[108,125,107],"class_list":{"0":"post-740","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-linuxc","7":"tag-linux","9":"tag-107"},"views":3412,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/740","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=740"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/740\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=740"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}