{"id":673,"date":"2010-06-21T09:33:12","date_gmt":"2010-06-21T15:33:12","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=673"},"modified":"2010-08-09T07:11:11","modified_gmt":"2010-08-09T13:11:11","slug":"673","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/673","title":{"rendered":"\u5b9e\u73b0\u81ea\u5df1ls\u547d\u4ee4\uff08\u4e0b\uff09"},"content":{"rendered":"<p>\u5728\u672c\u6587\u4e0a\u90e8\u5206\u4e2d\uff0c\u4e3b\u8981\u9648\u8ff0my_ls.c\u7684\u5927\u81f4\u7ed3\u6784\uff0c\u672c\u6587\u4e3b\u8981\u6765\u5206\u6790my_ls\u4e2d\u4e3b\u51fd\u6570\u662f\u5982\u4f55\u5b9e\u73b0\u7684\u3002<\/p>\n<p>\uff081\uff09\u8fdb\u5165main\u51fd\u6570\u3002\u9996\u5148\u5bf9\u547d\u4ee4\u884c\u53c2\u6570\u8fdb\u884c\u89e3\u6790\uff0c\u5373\u63d0\u53d6\u547d\u4ee4\u884c\u53c2\u6570\u4e2d\u2018-\u2019\u540e\u7684\u9009\u9879\u3002\u7528\u6237\u7684\u8f93\u5165\u6709\u591a\u6837\u6027\uff0c\u5982ls -l -a;ls -la\u3002\u6211\u4eec\u7528\u4e24\u5c42\u5faa\u73af\u7c7b\u6765\u89e3\u6790\u53c2\u6570\uff0c\u5916\u5c42\u5faa\u73af\u5bf9argv[]\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u4f9d\u6b21\u8fdb\u884c\u5185\u5c42\u5faa\u73af\u7684\u89e3\u6790\uff0c\u4e8c\u5c42\u5faa\u73af\u5bf9\u4ee5\u2018-\u2019\u4e3a\u9996\u7684\u5b57\u7b26\u4e32\u8fdb\u884c\u9009\u9879\u63d0\u53d6\uff0c\u5e76\u628a\u6bcf\u4e2a\u9009\u9879\u5b58\u4e8eparam[]\u6570\u7ec4\u5f53\u4e2d\uff0c\u7528num\u6765\u8bb0\u4e0b\u2018-\u2019\u7684\u6570\u76ee\uff0c\u4ee5\u5907\u540e\u7528\u3002\u800c\u547d\u4ee4\u884c\u53c2\u6570\u4e2d\u7684\u603b\u9009\u9879\u6570\u76ee\u5219\u7528j\u8ba1\u6570\u3002\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\">     j=0;\r\n\tnum=0;\r\n\tfor(i=1;i&lt;\\argc;i++)\r\n\t{\r\n\t\tif(argv[i][0]=='-')\r\n\t\t{\r\n\t\t\tfor(k=1;k&lt;\\strlen(argv[i]);k++)\r\n\t\t\t{\r\n\t\t\t\tparam[j++]=argv[i][k];\r\n\t\t\t}\r\n\t\t\tnum++;\/\/count the number of '-'\r\n\t\t}\r\n\t}<\/pre>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u6765\u68c0\u67e5\u521a\u521a\u63d0\u53d6\u7684\u9009\u9879\u662f\u5426\u5408\u6cd5\u3002\u672c\u7a0b\u5e8f\u6211\u4eec\u53ea\u652f\u6301\u9009\u9879-l\u548c-a\uff0c\u56e0\u6b64\u5982\u679c\u6211\u4eec\u589e\u6dfb\u5176\u4ed6\u9009\u9879\uff0c\u53ea\u8981\u9002\u5f53\u6dfb\u52a0\u4ee3\u7801\u5373\u53ef\u3002\u5e76\u4e14\u6211\u4eec\u7528\u6216\u8fd0\u7b97\u8bb0\u5f55\u53c2\u6570\uff0c\u4ee5\u5907\u540e\u7528\u3002\u6700\u540e\u4e3a\u9009\u9879\u6570\u7ec4\u7684\u672b\u5c3e\u5143\u7d20\u8d4b\u2018\\0\u2019\u3002<\/p>\n<pre class=\"brush:c\">\/\/check the argument because of only supporting -a and -l\r\n\tfor(i=0;i&lt;\\j;i++)\r\n\t{\r\n\t\tif(param[i]=='a')\r\n\t\t{\r\n\t\t\tflag_param|=PARAM_A;\r\n\t\t\tcontinue;\r\n\t\t}\r\n\t\telse if(param[i]=='l')\r\n\t\t{\r\n\t\t\tflag_param|=PARAM_L;\r\n\t\t\tcontinue;\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tprintf(\"error:my_ls invalid option -%c\\n\",param[i]);\r\n\t\t\texit(1);\r\n\t\t}\r\n\t}\r\n\tparam[j]='\\0';<\/pre>\n<p>\u7531\u4e0a\u9762\u6211\u4eec\u6240\u77e5num\u8bb0\u5f55\u7684\u662f\u53c2\u6570\u4e2d\u2018-\u2019\u7684\u6570\u91cf\uff0c\u56e0\u6b64\u5982\u679cnum+1==argc\uff0c\u90a3\u8bf4\u660e\u7528\u6237\u6240\u8f93\u5165\u7684\u547d\u4ee4\u884c\u53c2\u6570\u4e0d\u5305\u542b\u76ee\u5f55\u6216\u6587\u4ef6\u540d\u3002\u53ea\u662f\u663e\u793a\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u3002\u56e0\u4e3a\u8fd9\u662f\u6211\u4eec\u5fc5\u987b\u81ea\u52a8\u5c06path\u8d4b\u503c\u4e3a\u5f53\u524d\u76ee\u5f55\u3002\u4e3a\u4e86\u4f7f\u5176\u79f0\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5fc5\u987b\u5728\u672b\u5c3e\u52a0\u2018\\0\u2019\u3002\u7136\u540e\u8fdb\u5165display_dir\u51fd\u6570\u3002\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\">\/\/print the information of  current directory if the command without the name of target file and current directory\r\n        if((num+1)==argc)\r\n\t{\r\n\t\tstrcpy(path,\".\/\");\r\n\t\tpath[2]='\\0';\r\n\t\tdisplay_dir(flag_param,path);\r\n\t\treturn 0;\r\n\t}<\/pre>\n<p>\u5982\u679c\u547d\u4ee4\u884c\u53c2\u6570\u5305\u542b\u76ee\u5f55\u6216\u8005\u6587\u4ef6\u540d\uff0c\u90a3\u4e48\u6211\u4eec\u8981\u68c0\u67e5\u5176\u5408\u6cd5\u6027\uff08\u53c2\u6570\u4e2d\u7684\u76ee\u5f55\u6216\u8005\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff09\u3002\u8fd9\u91cc\u6211\u4eec\u5229\u7528stat\u65cf\u51fd\u6570\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u5c5e\u6027\uff0c\u5b9e\u73b0\u4e0a\u8ff0\u529f\u80fd\u3002stat\u65cf\u51fd\u6570\u901a\u5e38\u6709\u4e24\u4e2a\u53c2\u6570\uff1a\u6587\u4ef6\u8def\u5f84\/\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0cstruct stat *buf\u7c7b\u578b\u7684\u7ed3\u6784\u4f53\u3002\u5982\u679c\u64cd\u4f5c\u6210\u529f\uff0c\u90a3\u4e48buf\u5c06\u4fdd\u5b58\u6587\u4ef6\u7684\u5c5e\u6027\u3002\u82e5\u5408\u6cd5\uff0c\u5229\u7528\u5b8fS_ISDIR(buf.st_mode);\u5224\u65ad\u6b64\u6587\u4ef6\u662f\u5426\u4e3a\u76ee\u5f55\u6587\u4ef6\u3002\u82e5\u4e3a\u76ee\u5f55\u6587\u4ef6\u5219\u8fdb\u5165display_dir\u51fd\u6570\uff0c\u5426\u5219\u8fdb\u5165display\u51fd\u6570\u3002\u901a\u5e38\u60c5\u51b5\uff0cdisplay_dir\u51fd\u6570\u662f\u83b7\u53d6path\u76ee\u5f55\u4e0b\u6240\u6709\u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84\u540d\uff0c\u5728\u4f7f\u6bcf\u4e2a\u6587\u4ef6\u6267\u884cdsiplay\u51fd\u6570\u3002\u56e0\u6b64\u5982\u679c\u53c2\u6570\u4e2d\u662f\u6307\u5b9a\u7684\u6587\u4ef6\u540d\uff0c\u5219\u53ef\u7ed5\u8fc7display_dir\u51fd\u6570\uff0c\u76f4\u63a5\u8fdb\u5165display\u51fd\u6570\u3002<\/p>\n<pre class=\"brush:c\">        i=1;\r\n\tdo\r\n\t{\r\n\t\t\/\/the current argument doesn't comprise the target file name and dirctory name\r\n\t\tif(argv[i][0]=='-')\r\n\t\t{\r\n\t\t\ti++;\r\n\t\t\tcontinue;\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tstrcpy(path,argv[i]);\r\n\t\t\t\/\/detect if the \"path\" exsit\r\n\t\t\tif(stat(path,&amp;buf)==-1)\r\n\t\t\t{\r\n\t\t\t\tmy_err(\"stat\",errno,__LINE__);\r\n\t\t\t}\r\n\t\t\t\/\/detect the \"path\" is a file or a directory\r\n\t\t\tif(S_ISDIR(buf.st_mode))\r\n\t\t\t{\r\n\t\t\t\t\/\/directory\r\n\t\t\t\t\/\/detect if the last character of the \"path\" is '\/'\r\n\t\t\t\tif(path[strlen(path)-1]=='\/')\r\n\t\t\t\t{\r\n\t\t\t\t\tpath[strlen(path)]=='\\0';\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t{\r\n\t\t\t\t\tpath[strlen(path)]='\/';\r\n\t\t\t\t\tpath[strlen(path)]='\\0';\r\n\t\t\t\t}\r\n\t\t\t\tdisplay_dir(flag_param,path);\r\n\t\t\t\ti++;\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\t\/\/file\r\n\t\t\t\tdisplay(flag_param,path);\r\n\t\t\t\ti++;\r\n\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t}while(i&lt;\\argc);<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u672c\u6587\u4e0a\u90e8\u5206\u4e2d\uff0c\u4e3b\u8981\u9648\u8ff0my_ls.c\u7684\u5927\u81f4\u7ed3\u6784\uff0c\u672c\u6587\u4e3b\u8981\u6765\u5206\u6790my_ls\u4e2d\u4e3b\u51fd\u6570\u662f\u5982\u4f55\u5b9e\u73b0\u7684\u3002 \uff081\uff09\u8fdb\u5165m [&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],"class_list":["post-673","post","type-post","status-publish","format-standard","hentry","category-linuxc","tag-c","tag-linux","tag-107"],"views":6822,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/673","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=673"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/673\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=673"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}