{"id":1019,"date":"2010-08-06T04:41:31","date_gmt":"2010-08-06T10:41:31","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=1019"},"modified":"2010-08-09T19:26:58","modified_gmt":"2010-08-10T01:26:58","slug":"%e5%ae%9e%e7%8e%b0cp%e5%91%bd%e4%bb%a4-%e5%af%b9%e6%96%87%e4%bb%b6%e6%9d%83%e9%99%90%e7%9a%84%e4%bc%98%e5%8c%96","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/1019","title":{"rendered":"\u5b9e\u73b0cp\u547d\u4ee4(6)"},"content":{"rendered":"<p><strong><em><span style=\"color: #ff0000;\">Updata:8\/10<\/span><\/em><\/strong><\/p>\n<p>\u5173\u4e8e\u6587\u4ef6\u590d\u5236\u65f6\u7684\u6743\u9650\u95ee\u9898\uff0c\u66f4\u5b8c\u6574\u548c\u8be6\u7ec6\u7684\u89e3\u7b54\u89c1\u5b9e\u73b0cp\u547d\u4ee4(8)\u3002 \u6b64\u6587\u53ef\u80fd\u6709\u4e0d\u6b63\u786e\u6216\u8005\u4e0d\u5b8c\u6574\u7684\u89e3\u91ca\uff0c\u4f46\u662f\u5e0c\u671b\u4f60\u7ee7\u7eed\u9605\u8bfb\uff0c\u627e\u51fa\u95ee\u9898\u3002<\/p>\n<p><strong><em><span style=\"color: #ff0000;\">Update:8\/09<\/span><\/em><\/strong><\/p>\n<p>\u4e0a\u6b21\u7ed9\u5927\u5bb6\u5c06\u4e86my_cp\u4e4b\u540e\uff0c\u4e00\u4e9b\u540c\u5b66\u63d0\u51fa\u5173\u4e8e\u6587\u4ef6\u6743\u9650\u7684\u95ee\u9898\uff0c\u6bd4\u5982\u4e0d\u540c\u7528\u6237\u7ec4\u4e4b\u95f4\u7684\u6587\u4ef6\u62f7\u8d1d\u6d89\u53ca\u5230\u7684\u6743\u9650\u95ee\u9898\u3002\u4e4b\u524d\u5bf9my_cp\u7684\u6d4b\u8bd5\u4ee5\u53ca\u6574\u4f53\u8bbe\u8ba1\u6211\u90fd\u53ea\u662f\u9488\u5bf9\u6587\u4ef6\u6240\u6709\u8005\u800c\u8a00\uff0c\u56e0\u6b64\u8fd9\u4e2a\u95ee\u9898\u8fd8\u6709\u5f85\u4e8e\u5b8c\u5584\u3002\u9488\u5bf9\u6743\u9650\u95ee\u9898\uff0c\u6211\u89c9\u5f97\u53ef\u80fd\u8fd8\u6709\u4e0d\u5c11BUG\uff0c\u6240\u4ee5\u4eca\u5929\u5c31\u53cd\u590d\u6d4b\u8bd5\uff0c\u7ed3\u679c\u5c31\u53d1\u73b0\u4e86\u5982\u4e0b\u95ee\u9898\u3002<\/p>\n<p>\u6211\u4eec\u5728\u8fdb\u884c\u62f7\u8d1d\u6587\u4ef6\u65f6\uff0c\u5f53\u76ee\u7684\u6587\u4ef6\u4e0d\u5b58\u5728\u65f6\u4f1a\u65b0\u5efa\u8fd9\u4e2a\u76ee\u7684\u6587\u4ef6\uff0c\u5e76\u8fdb\u884c\u5185\u5bb9\u62f7\u8d1d\u3002\u76ee\u7684\u6587\u4ef6\u7684\u6743\u9650\u4e3a\u9ed8\u8ba4\u7684644\u3002\u53c2\u8003\u4e0b\u9762\uff1a<\/p>\n<pre class=\"brush:c\">gues@huangwei-desktop:~\/code\/shell_command$ ls -l test.c\r\n-rw-rw-r-- 1 gues gues 5327 2010-08-09 10:31 test.c\r\ngues@huangwei-desktop:~\/code\/shell_command$ cp test.c newtest.c\r\ngues@huangwei-desktop:~\/code\/shell_command$ ls -l test.c newtest.c\r\n-rw-r--r-- 1 gues gues 5327 2010-08-09 10:45 newtest.c\r\n-rw-rw-r-- 1 gues gues 5327 2010-08-09 10:31 test.c<\/pre>\n<p>\u76ee\u7684\u6587\u4ef6\u5df2\u7ecf\u5b58\u5728\u65f6\uff0c\u4e5f\u4f1a\u5982\u540c\u4e0a\u8ff0\u7ed3\u679c\uff1a\u53ea\u62f7\u8d1d\u6743\u9650\uff0c\u4e0d\u5f71\u54cd\u76ee\u7684\u6587\u4ef6\u6743\u9650\uff0c\u5373\u4fdd\u6301\u539f\u76ee\u7684\u6587\u4ef6\u7684\u5c5e\u6027\u3002<\/p>\n<p><strong>\u90a3\u4e48\u5982\u4f55\u624d\u80fd\u62f7\u8d1d\u6743\u9650\uff1f\u6b64\u65f6\u5c31\u5f97\u52a0\u5165-p\u9009\u9879\u3002\u76ee\u524d\u7684my_cp\u5e76\u6ca1\u6709\u6ce8\u610f\u5230\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ea\u8981\u6d89\u53ca\u5230\u62f7\u8d1d\u6587\u4ef6\uff0c\u90fd\u4f1a\u5c06\u5185\u5bb9\u548c\u5c5e\u6027\u4e00\u8d77\u62f7\u8d1d\u3002\u56e0\u6b64\u6211\u4eec\u63a5\u4e0b\u6765\u4fee\u6539\u4ee3\u7801\uff1a\u4fee\u590dmy_cp\u5b58\u5728\u7684\u4e0a\u8ff0\u6f0f\u6d1e\uff0c\u5e76\u4e14\u52a0\u5165-p\u9009\u9879\u3002\u540c\u65f6\uff0c\u6211\u4eec\u5e94\u8be5\u7a0d\u5fae\u4fee\u6539\u4ee3\u7801\u7684\u903b\u8f91\u5224\u65ad\uff0c\u4e3a\u5176\u4ed6\u9009\u9879\u7559\u6761\u540e\u8def\u3002\u597d\u4e86\uff0c\u5f00\u59cb\u5427\uff01<\/strong><\/p>\n<p>\u9996\u5148\u5728\u4e3b\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u589e\u52a0\u4e00\u4e2a\u5bf9\u9009\u9879-p\u7684\u5224\u65ad\uff0c\u6ce8\u610f\u540c\u65f6\u8981\u5728\u58f0\u660e\u76f8\u5e94\u7684\u53d8\u91cfparam_p\u548cindex_p\u6570\u7ec4\uff0c\u529f\u80fd\u548c-r\u9009\u9879\u76f8\u540c\uff0c\u5177\u4f53\u53ef\u53c2\u89c1\u524d\u6587\u3002\u5e76\u4e14\uff0c\u5982\u679c\u65e5\u540e\u8981\u6dfb\u52a0\u5176\u4ed6\u9009\u9879\u90a3\u4e48\u76f4\u63a5\u6dfb\u52a0\u5373\u53ef\u3002\u6ce8\u610f\uff0c\u6211\u8fd8\u589e\u52a0\u4e86\u4e00\u4e2aparam\u5168\u5c40\u53d8\u91cf\uff08\u5982\u679c\u9009\u9879\u4e0d\u5408\u6cd5\uff0c\u4e5f\u4e0d\u788d\u4e8b\uff09\uff0c\u53ea\u6709\u547d\u4ee4\u4e2d\u542b\u6709\u4efb\u4f55\u9009\u9879\uff0c\u90a3\u4e48\u5b83\u5c31\u4e3a\u771f\uff08\u4e0b\u9762\u5c31\u5c06\u7528\u5230\uff09\u3002<\/p>\n<pre class=\"brush:c\">\t\/\/check the legality of the options,only -r or -R\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\tparam=1;\/\/have a option\r\n\t\t\tif((!strcmp(argv[i],\"-r\")||!strcmp(argv[i],\"-R\")))\r\n\t\t\t{\r\n\t\t\t\tparam_r+=1;\r\n\t\t\t\tindex_r[i]=i;\r\n\t\t\t}\r\n\t\t\telse if(!strcmp(argv[i],\"-p\"))\r\n\t\t\t{\r\n\t\t\t\tparam_p+=1;\r\n\t\t\t\tindex_p[i]=i;\r\n\t\t\t}\r\n\t\t\t\/*\r\n\t\t\t *if the command include another option,you can add the similar code in here.\r\n\t\t\t *You must also add the variable param_?  and index_?[]\r\n\t\t\t *\/\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\tprintf(\"my_cp:\u65e0\u6548\u9009\u9879: %s\\n\",argv[i]);\r\n\t\t\t\texit(1);\r\n\t\t\t}\r\n\t\t}\r\n\t}<\/pre>\n<p>\u5bf9\u4e8e\u83b7\u5f97\u6587\u4ef6\u64cd\u4f5c\u6570\u4e2a\u6570\u548c\u6e90\u6587\u4ef6\u4e2a\u6570\u7684\u4ee3\u7801\u4e5f\u8981\u4fee\u6539\u3002\u6ce8\u610f\u8fd9\u91cc\u7684param_r\u6216param_p\u5e76\u4e0d\u4e00\u5b9a\u4e3a1\uff0c\u5f53\u5b58\u5728\u76f8\u5e94\u9009\u9879\u65f6\u3002\u56e0\u4e3a\u8f93\u5165\u591a\u4e2a\u76f8\u540c\u9009\u9879\uff0c\u5176\u6548\u679c\u548c\u4e00\u4e2a\u9009\u9879\u662f\u4e00\u6837\u7684\u3002<\/p>\n<pre class=\"brush:c\">\t\/\/get the number of src file\r\n\tif(param)\r\n\t{\r\n\t\tnum=argc-1;\/\/because of \".\/my_cp\"\r\n\r\n\t\tif(param_r)\r\n      \t        {\r\n\t\t       num-=param_r;\r\n         \t}\r\n\r\n         \tif(param_p)\r\n        \t{\r\n\t\t\tnum-=param_p;\r\n        \t}\r\n\r\n\t\t\/*if the command include another option,you can add the similar code in here*\/\r\n\r\n\t\tsrc_num=num-1;\/\/because of \"dest file\"\r\n\t}\r\n\telse\r\n\t{\r\n\t\t\/\/the command doesn't include any option\r\n\t\tnum=argc-1;\r\n\t\tsrc_num=num-1;\r\n\t}<\/pre>\n<p>\u53e6\u5916\uff0c\u5f53\u6587\u4ef6\u6570\u76ee\u4e0d\u5408\u6cd5\u65f6\uff0c\u6211\u4e5f\u5bf9\u5176\u5224\u65ad\u505a\u4e86\u4f18\u5316\uff0c\u5177\u4f53\u53c2\u89c1\u4ee3\u7801\u3002<\/p>\n<pre class=\"brush:c\">\tif(num&lt;2)\r\n\t{\r\n\t\tif(num==0)\r\n\t\t{\r\n\t\t\tprintf(\"my_cp:\u7f3a\u5c11\u4e86\u6587\u4ef6\u64cd\u4f5c\u6570\\n\");\r\n\t\t\texit(1);\r\n\t\t}\r\n\t\tif(num==1)\r\n\t\t{\r\n\t\t\tprintf(\"my_cp:\u7f3a\u5c11\u4e86\u8981\u64cd\u4f5c\u7684\u76ee\u6807\u6587\u4ef6\\n\");\r\n\t        \texit(1);\r\n\t\t}\r\n\t}<\/pre>\n<p>\u53e6\u5916\u5728\u83b7\u53d6\u76ee\u7684\u6587\u4ef6\u548c\u6e90\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u4e5f\u5e94\u8be5\u76f8\u5e94\u6dfb\u52a0\u5bf9-p\u9009\u9879\u7684\u5224\u65ad\u3002\u4ee5\u4e0a\u4fee\u6539\u4f7f\u5f97\u6211\u4eec\u589e\u52a0\u5176\u4ed6\u9009\u578b\u7684\u65f6\u5019\u4f1a\u53d8\u5f97\u66f4\u5bb9\u6613\uff0c\u4ee3\u7801\u53ef\u8bfb\u6027\u66f4\u9ad8\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u5c31\u662f\u4fee\u8865\u4e0a\u8ff0my_cp\u7684bug\u4e86\u3002\u6839\u636e\u4e0a\u8ff0\u4e3e\u4f8b\uff0c\u53ea\u6709\u52a0\u5165\u4e86-p\u9009\u9879\u65f6\uff0c\u624d\u4f1a\u5c06\u6e90\u6587\u4ef6\u7684\u5c5e\u6027\u590d\u5236\u7ed9\u76ee\u7684\u6587\u4ef6\uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u8981\u5c06\u4e0a\u8ff0\u6587\u5b57\u7ffb\u8bd1\u6210\u4ee3\u7801\u5373\u53ef\u3002<\/p>\n<pre class=\"brush:c\">\t\/\/set the dset file's access right when the \u201c-p\u201d existed\r\n\tif(param_p)\r\n\t{\r\n\t\tif(fchmod(fdwt,buf.st_mode)==-1)\r\n        \t{\r\n\t\t\tperror(\"fchmod\");\r\n              \t\texit(1);\r\n         \t}\r\n        \t\/\/set file's user id and group id\r\n         \tif(fchown(fdwt,buf.st_uid,buf.st_gid)==-1)\r\n         \t{\r\n\t\t\tperror(\"fchown\");\r\n              \t\texit(1);\r\n         \t}\r\n\t}<\/pre>\n<p>\u8fd9\u91cc\u8fd8\u6ca1\u6709\u5b8c\uff0c\u8fd8\u8bb0\u5f97\u6211\u4eec\u4e0a\u9762\u8ba8\u8bba\u5206\u76ee\u7684\u6587\u4ef6\u5b58\u5728\u548c\u76ee\u7684\u6587\u4ef6\u4e0d\u5b58\u5728\u4e24\u79cd\u60c5\u51b5\u5417\uff1f\u6211\u4eec\u8fd8\u5fc5\u987b\u4fee\u6539\u6253\u5f00\u76ee\u7684\u6587\u4ef6\u65f6open\u51fd\u6570\u7684\u53c2\u6570\u3002\u8fd9\u4e9b\u53c2\u6570\u7684\u7684\u529f\u80fd\uff1a\u5f53\u76ee\u7684\u6587\u4ef6\u4e0d\u5b58\u5728\u65f6\uff0c\u65b0\u5efa\u8fd9\u4e2a\u6587\u4ef6\uff0c\u6587\u4ef6\u5c5e\u6027\u7531opne\u51fd\u6570\u7b2c\u4e09\u4e2a\u53c2\u6570\u51b3\u5b9a\uff08644\uff09\uff1b\u5426\u5219\uff0c\u53ea\u662f\u6e05\u7a7a\u76ee\u7684\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u3002\u8fd9\u4e9b\u53c2\u6570\u521a\u597d\u6ee1\u8db3\u6211\u4eec\u4e00\u5f00\u59cb\u4e3e\u4f8b\u540e\u63cf\u8ff0\u7684\u60c5\u51b5\u3002<\/p>\n<pre class=\"brush:c\">\t\/\/open the dest file\r\n\tif((fdwt=open(dest_path,O_CREAT|O_TRUNC|O_RDWR,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH))==-1)\r\n\t{\r\n\t\tperror(\"open\");\r\n\t\texit(1);\r\n\t}<\/pre>\n<p>OK\uff0c\u8fd9\u4e0b\u5c31\u53ef\u4ee5\u89e3\u51b3\u4e0a\u8ff0\u6f0f\u6d1e\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Updata:8\/10 \u5173\u4e8e\u6587\u4ef6\u590d\u5236\u65f6\u7684\u6743\u9650\u95ee\u9898\uff0c\u66f4\u5b8c\u6574\u548c\u8be6\u7ec6\u7684\u89e3\u7b54\u89c1\u5b9e\u73b0cp\u547d\u4ee4(8)\u3002 \u6b64\u6587\u53ef\u80fd\u6709\u4e0d\u6b63\u786e\u6216 [&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":[133,125,107],"class_list":["post-1019","post","type-post","status-publish","format-standard","hentry","category-linuxc","tag-cp","tag-linux","tag-107"],"views":6907,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/1019","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=1019"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/1019\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=1019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=1019"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=1019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}