資源簡介
主要包括兩個文件,分別在Linux和Windows環境下實現文件拷貝功能。

代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
#include?
//to?copy?a?single?file
void?mycp_file(char?*psrc_path?char?*pdest_path)
{
????int?i?j;
????int?len=strlen(psrc_path);
????char?src_path[PATH_MAX];
????char?dest_path[PATH_MAX];
????strcpy(src_pathpsrc_path);
????strcpy(dest_pathpdest_path);
????//get?the?absolute?src_path
????for(i=0;?i ????{
????????if(src_path[i]==‘/‘)?break;
????}
????if(i==len)
????{
????????char?tmp[PATH_MAX]=“./“;
????????strcat(tmpsrc_path);
????????strcpy(src_pathtmp);
????????len=strlen(src_path);
????}
????//extract?the?filename?from?src_path
????for(i=len-1;?i>0;?i--)
????{
????????if(src_path[i]==‘/‘)?break;
????}
????char?filename[PATH_MAX];
????for(j=0;?i ????{
????????filename[j++]=src_path[i];
????}
????filename[j]=‘\0‘;
????//check?if?the?dest_path?is?existed
????struct?stat?buf;
????if(stat(dest_path&buf)==0)
????{
????????if(S_ISDIR(buf.st_mode))
????????{
????????????if(dest_path[strlen(dest_path)-1]==‘/‘)
????????????????dest_path[strlen(dest_path)-1]=‘\0‘;
????????????strcat(dest_pathfilename);
????????}
????}
????//open?the?src_file?and?dest_file
????int?fd_r?fd_w;
????if((fd_r=open(src_pathO_RDONLY))==-1)
????{
????????perror(src_path);
????????exit(1);
????}
????if((fd_w=open(dest_pathO_CREAT|O_TRUNC|O_RDWRS_IRWXU))==-1)
????{
????????perror(dest_path);
????????exit(1);
????}?
????//copy?the?content?to?dest?file
????char?ch[3];
????while(read(fd_rch1)==1)
????{
????????if(write(fd_wch1)!=1)
????????{
????????????perror(“write“);
????????????exit(1);
????????}
????}
????//copy?the?file‘s?attributes
????if(fstat(fd_r&buf)==-1)
????{??
????????perror(“fstat“);
????????exit(1);
????}
????if(fchmod(fd_wbuf.st_mode)==-1)
????{
????????perror(“fchmod“);
????????exit(1);
????}
????
????if(fchown(fd_wbuf.st_uidbuf.st_gid)==-1)
????{
????????perror(“fchown“);
????????exit(1);
????}
????//copy?the?file‘s?access?and?modify?time
????struct?utimbuf?u_time;
????u_time.actime=buf.st_atime;
????u_time.modtime=buf.st_mtime;
????utime(dest_path&u_time);
????close(fd_r);
????close(fd_w);
}
//to?copy?a?directory
void?mycp_dir(char?*psrc_path?char?*pdest_path)
{
????char?src_path[PATH_MAX];
????char?dest_path[PATH_MAX];
????strcpy(src_pathpsrc_path);
????strcpy(dest_pathpdest_path);
????if(src_path[strlen(src_path)-1]!=‘/‘)
????{
????????strcat(src_path“/“);
????}
????struct?stat?buf;
????//if?the?dest_path?existes
????if(stat(dest_path&buf)==0)
????{
????????if(!S_ISDIR(buf.st_mode))
????????{
????????????printf(“mycp:?cannot?overwrite?non-dir?‘%s‘\n“dest_path);
????????????exit(1);
????????}
????????if(dest_path[strlen(dest_path)-1]!=‘/‘)
????????{
????????????strcat(dest_path“/“);
????????}
????????int?i?j;
????????int?len=strlen(src_path);
????????char?ldir[PATH_MAX];
????????//to?get?the?absolute?dest_path
????????for(i=len-2;?i>0;?i--)
????????{
???????????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????12178??2011-04-12?22:40??mycp\linux\mycp
?????文件???????5026??2011-04-12?22:39??mycp\linux\mycp.c
?????文件???????3300??2011-04-13?09:14??mycp\windows\mycp.cpp
?????文件?????727792??2011-04-13?09:16??mycp\windows\mycp.exe
?????目錄??????????0??2011-04-27?23:00??mycp\linux
?????目錄??????????0??2011-04-27?23:00??mycp\windows
?????目錄??????????0??2011-04-27?23:00??mycp
-----------?---------??----------?-----??----
???????????????748296????????????????????7
- 上一篇:3DS格式文件詳解及讀取方法
- 下一篇:高靈敏度GPS程序.rar
評論
共有 條評論