{"id":904,"date":"2010-07-27T09:10:45","date_gmt":"2010-07-27T15:10:45","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=904"},"modified":"2010-08-03T03:58:22","modified_gmt":"2010-08-03T09:58:22","slug":"%e5%ae%9e%e7%8e%b0cp%e5%91%bd%e4%bb%a4-%e6%96%87%e4%bb%b6%e5%a4%b9%e7%9a%84%e6%8b%b7%e8%b4%9d","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/904","title":{"rendered":"\u5b9e\u73b0cp\u547d\u4ee4\u2013\u6587\u4ef6\u5939\u7684\u62f7\u8d1d"},"content":{"rendered":"<p>\u521a\u521a\u5b8c\u6210\u4e86my_cp\u7684\u53e6\u4e00\u4e2a\u529f\u80fd\uff1a\u5c06\u4e00\u4e2a\u76ee\u5f55\u62f7\u8d1d\u5230\u6307\u5b9a\u76ee\u5f55\u3002\u52a0\u4e0a\u6628\u5929\u5b9e\u73b0\u7684\u5c06\u4e00\u4e2a\u6587\u4ef6\u62f7\u8d1d\u5230\u6307\u5b9a\u5730\u5740\u4e0b\uff0c\u73b0\u5728\u5df2\u7ecf\u5b8c\u6210\u4e86\u6211\u4eec\u5b9e\u73b0\u524d\u6240\u5b9a\u4e0b\u7684\u8981\u6c42\u3002\u4e5f\u8bb8\u4f60\u4f1a\u6709\u7591\u95ee\uff0c\u90a3\u591a\u4e2a\u6587\u4ef6\u7684\u62f7\u8d1d\u7684\u5b9e\u73b0\u5462\uff1f\u6211\u9762\u524d\u9762\u5df2\u7ecf\u8bf4\u8fc7\uff0c\u53ea\u8981\u5b8c\u6210\u4e0a\u8ff0\u4e24\u4e2a\u529f\u80fd\uff0c\u5e76\u4e14\u4f60\u5728\u4e3b\u51fd\u6570\u4e2d\u201c\u5206\u6d41\u201d\u6b63\u786e\uff0c\u90a3\u4e48\u53ea\u8981\u5728\u5408\u9002\u7684\u4f4d\u7f6e\u8c03\u7528\u8fd9\u4e24\u4e2a\u51fd\u6570\u5373\u53ef\uff0c\u5177\u4f53\u529e\u6cd5\u6211\u4eec\u4e0b\u9762\u4f1a\u8ba8\u8bba\u3002<\/p>\n<p>\u5728\u8be6\u89e3\u5982\u4f55\u5b9e\u73b0\u5c06\u4e00\u4e2a\u76ee\u5f55\u62f7\u8d1d\u5230\u6307\u5b9a\u76ee\u5f55\uff08cp_directory\u51fd\u6570\uff09\u4e4b\u524d\uff0c\u6211\u4eec\u9996\u5148\u5e94\u8be5\u5f04\u660e\u767d\u4e0b\u9762\u7684\u5185\u5bb9\uff1a<\/p>\n<p>1.\u5982\u679c\u76ee\u6807\u76ee\u5f55\u4e2d\u7684\u6700\u4f4e\u7ea7\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u65b0\u5efa\u8fd9\u4e2a\u76ee\u5f55\uff0c\u5e76\u628a<strong>\u6e90\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6<\/strong>\u62f7\u8d1d\u5230\u6b64\u65b0\u5efa\u7684\u76ee\u5f55\u4e0b\u3002\u6bd4\u5982cp -r dir .\/newdir\u3002\u6211\u4eec\u53ef\u4ee5\u770b\u5230.\/newdir\uff08\u8fd9\u4e2a\u8def\u5f84\u4e2d\u6700\u4f4e\u7ea7\u7684\u76ee\u5f55\u662fnewdir\uff09\u5728cp\u524d\u662f\u4e0d\u5b58\u5728\u7684\uff0c\u4f46\u662fcp\u540e\u65b0\u5efa\u4e86\u8fd9\u4e2a\u76ee\u5f55\uff0c\u5e76\u4e14\u5c06dir\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u62f7\u8d1d\u5230\u8fd9\u4e2a\u65b0\u5efa\u7684\u76ee\u5f55\u4e0b\u3002<\/p>\n<pre class=\"brush:c\">gues@huangwei-desktop:~\/code\/shell_command$ ls\r\ncptest  ls   my_cp   my_cp.c  my_ls_plus    my_shell.c    nothisdirectory  tdir         test\r\ndir     ls1  my_cp1  my_ls.c  my_ls_plus.c  newdirectory  nothisfile       tdirmy_ls.c  ttfile.c\r\ngues@huangwei-desktop:~\/code\/shell_command$ ls dir\r\ned  my_cp1  test  ttfile.c\r\ngues@huangwei-desktop:~\/code\/shell_command$ cp -r dir .\/newdir\r\ngues@huangwei-desktop:~\/code\/shell_command$ ls newdir\r\ned  my_cp1  test  ttfile.c<\/pre>\n<p>2.\u5982\u679c\u6700\u4f4e\u7ea7\u7684\u76ee\u6807\u76ee\u5f55\u5b58\u5728\uff0c\u5219\u4f1a\u5c06<strong>\u6e90\u76ee\u5f55\uff08\u5f53\u7136\u4e5f\u5305\u542b\u6e90\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\uff09<\/strong>\u62f7\u8d1d\u5230\u8fd9\u4e2a\u76ee\u6807\u76ee\u5f55\u3002\u6211\u4eec\u4ecd\u6267\u884c\u4e0a\u9762\u90a3\u4e2a\u547d\u4ee4\uff1acp -r dir .\/newdir\u3002\u4f46\u662f\u8fd9\u6b21\u7ed3\u679c\u662f\u4e0d\u4e00\u6837\u7684\uff0c\u7531\u4e8e1\u7684\u64cd\u4f5c\uff0cnewdir\u76ee\u5f55\u5df2\u7ecf\u5b58\u5728\uff0c\u8fd9\u6b21cp\u540e\u5c06dir\u76ee\u5f55\u62f7\u8d1d\u5230\u4e86\u5df2\u5b58\u5728\u7684newdir\u76ee\u5f55\u4e0b(\u5373.\/newdir\/dir\/)\u3002<\/p>\n<pre class=\"brush:c\">gues@huangwei-desktop:~\/code\/shell_command$ ls newdir\r\ned  my_cp1  test  ttfile.c\r\ngues@huangwei-desktop:~\/code\/shell_command$ cp .\/dir -r .\/newdir\r\ngues@huangwei-desktop:~\/code\/shell_command$ ls newdir\r\ndir  ed  my_cp1  test  ttfile.c<\/pre>\n<p>\u5982\u679c\u6211\u8bf4\u7684\u8fd8\u4e0d\u591f\u660e\u767d\uff0c\u4f60\u4e5f\u53ef\u4ee5\u81ea\u5df1\u4eb2\u81ea\u9a8c\u8bc1\u4e00\u4e0bcp\u547d\u4ee4\u3002<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u6765\u8be6\u89e3\u3002\u8fd8\u662f\u5148\u4fdd\u7559\u4f20\u9012\u8fc7\u6765\u7684\u8def\u5f84\u3002\u7136\u540e\u5982\u679c\u6e90\u6587\u4ef6\u5939\u4e0d\u5305\u542b\/\uff0c\u5219\u6dfb\u52a0\u3002<\/p>\n<pre class=\"brush:c\">void cp_directory(char* original_src_path,char* original_dest_path)\r\n{\r\n\tstruct stat buf;\r\n\tDIR *dir;\r\n\tstruct dirent *ptr;\r\n\tchar path[PATH_MAX+1];\r\n\tchar src_path[PATH_MAX+1],dest_path[PATH_MAX+1];\r\n\r\n\tstrcpy(src_path,original_src_path);\r\n\tstrcpy(dest_path,original_dest_path);\r\n\r\n\tif(src_path[strlen(src_path)-1]!='\/')\r\n\t{\r\n\t\tstrncat(src_path,\"\/\",1);\r\n\t}\r\n        \/\/the following code be omited\r\n}<\/pre>\n<p>\u5982\u679c\u76ee\u6807\u76ee\u5f55\u4e2d\u6700\u4f4e\u7ea7\u7684\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\u5b83\u3002\u5982\u679c\u6b21\u4f4e\u7ea7\u76ee\u5f55\u4e5f\u4e0d\u5b58\u5728\uff0c\u5219\u5728\u521b\u5efa\u7684\u65f6\u5019\u5c31\u53d1\u751f\u9519\u8bef\u3002\u5982\u679c\u76ee\u6807\u76ee\u5f55\u5b58\u5728\uff0c\u5e76\u4e14\u662f\u76ee\u5f55\u6587\u4ef6\uff0c\u90a3\u4e48\u5c31\u5982\u540c\u4e0a\u9762\u4e3e\u4f8b2\u4e2d\u6240\u8ff0\uff0c\u6211\u4eec\u9700\u8981\u5c06\u6e90\u8def\u5f84\u4e2d\u6700\u4f4e\u7ea7\u7684\u76ee\u5f55\u62f7\u8d1d\u5230\u76ee\u6807\u76ee\u5f55\u4e2d\u3002\u8fd9\u91cc\u9762\u8bbe\u8ba1\u5230\u63d0\u63d0\u53d6\u6e90\u8def\u5f84\u6700\u4f4e\u7ea7\u7684\u76ee\u5f55\uff0c\u4ee5\u53ca\u5c06\u5176\u8fde\u63a5\u5728\u76ee\u6807\u76ee\u5f55\u540e\u7b49\u3002\u8fd9\u4e9b\u90fd\u4e0d\u96be\u7406\u89e3\u3002\u6ce8\u610f\u5f53\u5b8c\u6210\u76ee\u6807\u8def\u5f84\u7684\u62fc\u63a5\u540e\uff0c\u5982\u679c\u8fd9\u4e2a\u76ee\u5f55\u672c\u8eab\u5c31\u5b58\u5728\uff0c\u90a3\u4e48\u6211\u4eec\u5c06\u5176\u5220\u9664\uff0c\u521b\u5efa\u65b0\u76ee\u5f55\u3002<\/p>\n<pre class=\"brush:c\">if(stat(dest_path,&amp;buf)==-1)\r\n\t{\r\n\t\t\/\/create a directory which name is dest_path\r\n\t\tstat(src_path,&amp;buf);\r\n\t\tif(mkdir(dest_path,buf.st_mode)==-1)\r\n\t\t{\r\n\t\t\tprintf(\"my_cp:create the directory \\\"%s\\\" error.\\n\",dest_path);\r\n\t\t\treturn ;\r\n\t\t}\r\n \t}\r\n\telse\r\n\t{\r\n\t\t\/\/exist\r\n\t\tif(!S_ISDIR(buf.st_mode))\r\n\t\t{\r\n\t\t\tprintf(\"my_cp:the directory \\\"%s\\\" can't cover the no-directory \\\"%s\\\".\\n\",src_path,dest_path);\r\n\t\t\treturn ;\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tif(dest_path[strlen(dest_path)-1]!='\/')\r\n\t\t\t{\r\n\t\t\t\tstrncat(dest_path,\"\/\",1);\r\n\t\t\t}\r\n\t\t\t\/\/extract the lowest directory\r\n\t\t\tint i,k=0;\r\n\t\t\tchar lowestdir[PATH_MAX+1];\r\n\t\t\tfor(i=strlen(src_path)-1-1;i&gt;\\0;i--)\r\n\t\t\t{\r\n\t\t\t\tif(src_path[i]=='\/')\r\n\t\t\t\t{\r\n\t\t\t\t\ti=i+1;\r\n\t\t\t\t\tbreak;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\tfor(;i&lt;\\strlen(src_path);i++)\r\n\t\t\t{\r\n\t\t\t\tlowestdir[k++]=src_path[i];\r\n\t\t\t}\r\n\t\t\tstrncat(dest_path,lowestdir,strlen(lowestdir));\r\n\t\t\tstruct stat temp_buf;\r\n\t\t\tchar temp_path[PATH_MAX+1]=\"rm -rf \";\r\n\t\t\tif(stat(dest_path,&amp;temp_buf)==0)\r\n\t\t\t{\r\n\t\t\t\tstrcat(temp_path,dest_path);\r\n\t\t\t\tsystem(temp_path);\r\n\t\t\t}\r\n              \t\tif(mkdir(dest_path,buf.st_mode)==-1)\r\n\t        \t{\r\n\t\t\t\tprintf(\"my_cp:create the directory \\\"%s\\\" error.\\n\",dest_path);\r\n\t \t\t        return ;\r\n\t          \t}\r\n\t\t}\r\n\t}<\/pre>\n<p>\u63a5\u7740\u6211\u4eec\u6253\u5f00\u6e90\u76ee\u5f55\uff0c\u8bfb\u53d6\u5176\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u540d\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5728my_ls\u7684\u65f6\u5019\u5c31\u5df2\u7ecf\u4f7f\u7528\u8fc7\u3002\u6211\u4eec\u5c06\u8fd9\u4e9b\u6587\u4ef6\u540d\u4e0e\u76ee\u7684\u8def\u5f84\u62fc\u63a5\u540e\uff0c\u68c0\u67e5\u4ed6\u4eec\u662f\u5426\u662f\u76ee\u5f55\u6587\u4ef6\u3002\u5982\u679c\u662f\u666e\u901a\u6587\u4ef6\u90a3\u4e48\u5c31\u8c03\u7528cp_single\u51fd\u6570\uff0c\u5426\u5219\u8c03\u7528cp_directory\u51fd\u6570\u3002<\/p>\n<pre class=\"brush:c\">\tif((dir=opendir(src_path))==NULL)\r\n\t{\r\n\t\tprintf(\"my_cp:open the srouce path \\\"%s\\\" error.\\n\",src_path);\r\n\t\treturn ;\r\n\t}\r\n\tchar temp_dest_path[PATH_MAX+1];\r\n\tstrcpy(temp_dest_path,dest_path);\r\n\twhile((ptr=readdir(dir))!=NULL)\r\n\t{\r\n\t\tif(!strcmp(ptr-&gt;\\d_name,\".\"))\r\n\t\t\tcontinue;\r\n\t\tif(!strcmp(ptr-&gt;\\d_name,\"..\"))\r\n\t\t\tcontinue;\r\n\t\tstrcpy(path,src_path);\r\n\t\tstrcat(path,ptr-&gt;\\d_name);\r\n\t\tif(stat(path,&amp;buf)==-1)\r\n\t\t{\r\n\t\t\tprintf(\"my_cp:open the file \\\"%s\\\" error.\\n\",path);\r\n\t\t\treturn ;\r\n\t\t}\r\n\t\tstrcpy(dest_path,temp_dest_path);\r\n\t\t\/\/get the right dest_path\r\n\t\tif(S_ISDIR(buf.st_mode))\r\n\t\t{\r\n\t\t\tcp_directory(path,dest_path);\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tcp_single(path,dest_path);\r\n\t\t}\r\n\t}<\/pre>\n<p>\u5176\u5b9e\u8fd9\u662f\u4e00\u4e2a\u9012\u5f52\u7684\u8fc7\u7a0b\uff0c\u5bf9\u4e8e\u9012\u5f52\uff0c\u6700\u91cd\u8981\u7684\u662f\u80fd\u8fd4\u56de\u5230\u8c03\u7528\u51fd\u6570\u3002\u5bf9\u4e8e\u4efb\u4f55\u76ee\u5f55\uff0c\u6700\u7ec8\u8981\u4e48\u8fd9\u4e2a\u76ee\u5f55\u662f\u7a7a\u7684\uff0c\u8981\u4e48\u5168\u662f\u666e\u901a\u6587\u4ef6\uff0c\u6240\u4ee5\u80af\u5b9a\u80fd\u8fd4\u56de\u5230\u4e0a\u4e00\u7ea7\u51fd\u6570\u4e2d\uff0c\u4e0d\u4f1a\u65e0\u9650\u7684\u53bb\u5d4c\u5957\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fmy_cp\u51fd\u6570\u7684\u5b9e\u73b0\u8fc7\u7a0b\uff0c\u9700\u8981\u6e90\u7801\u7684\u540c\u5b66\u7559\u4e0b\u90ae\u7bb1\u5373\u53ef\u3002\u5982\u679c\u53d1\u73b0\u4e86\u4e0d\u59a5\u4e4b\u5904\uff0c\u6b22\u8fce\u6307\u6b63\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u521a\u521a\u5b8c\u6210\u4e86my_cp\u7684\u53e6\u4e00\u4e2a\u529f\u80fd\uff1a\u5c06\u4e00\u4e2a\u76ee\u5f55\u62f7\u8d1d\u5230\u6307\u5b9a\u76ee\u5f55\u3002\u52a0\u4e0a\u6628\u5929\u5b9e\u73b0\u7684\u5c06\u4e00\u4e2a\u6587\u4ef6\u62f7\u8d1d\u5230\u6307\u5b9a\u5730\u5740\u4e0b\uff0c\u73b0\u5728\u5df2\u7ecf [&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":[108,125,107],"class_list":{"0":"post-904","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-linuxc","7":"tag-linux","9":"tag-107"},"views":13558,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/904","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=904"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/904\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=904"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}