windows版本:
- #include <windows.h>
- #include <windowsx.h>
- #include <tchar.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #define chDIMOF(Array) (sizeof(Array)/sizeof(Array[0]))
- static BOOL IsChildDir(WIN32_FIND_DATA * lpFindData)
- {
- return ((lpFindData->dwFileAttributes&
- FILE_ATTRIBUTE_DIRECTORY)!=0)&&
- (lstrcmp(lpFindData->cFileName,__TEXT("."))!=0)&&
- (lstrcmp(lpFindData->cFileName,__TEXT(".."))!=0);
- }
- static BOOL FindNextChildDir(HANDLE hFindFile,WIN32_FIND_DATA *lpFindData)
- {
- BOOL fFound=FALSE;
- do
- {
- fFound=FindNextFile(hFindFile,lpFindData);
- }while (fFound&&!IsChildDir(lpFindData));
- return fFound;
- }
- static HANDLE FindFirstChildDir(LPTSTR szPath,WIN32_FIND_DATA * lpFindData)
- {
- BOOL fFound;
- HANDLE hFindFile=FindFirstFile(szPath,lpFindData);
- if (hFindFile!=INVALID_HANDLE_VALUE)
- {
- fFound=IsChildDir(lpFindData);
- if (!fFound)
- {
- fFound=FindNextChildDir(hFindFile,lpFindData);
- }
- if (!fFound)
- {
- FindClose(hFindFile);
- hFindFile=INVALID_HANDLE_VALUE;
- }
- }
- return hFindFile;
- }
- typedef struct
- {
- int nDepth;
- BOOL fRecurse;
- TCHAR szBuf[1000];
- int nIndent;
- BOOL fOk;
- BOOL fIsDir;
- WIN32_FIND_DATA FindData;
- }DIRWALKDATA,*LPDIRWALKDATA;
- static void DirWalkRecurse(LPDIRWALKDATA pDW,LPTSTR pszRootPath,LPTSTR pszdestPath)
- {
- HANDLE hFind;
- LPTSTR temp;
- LPTSTR ttemp;
- temp=(LPTSTR)malloc(100*sizeof(LPTSTR));
- ttemp=(LPTSTR)malloc(100*sizeof(LPTSTR));
- pDW->nDepth++;
- pDW->nIndent=3*pDW->nDepth;
- _stprintf(pDW->szBuf,__TEXT("%*s"),pDW->nIndent,__TEXT(""));
- GetCurrentDirectory(chDIMOF(pDW->szBuf)-pDW->nIndent,&pDW->szBuf[pDW->nIndent]);
- //printf("%s\n",pDW->szBuf);
- hFind=FindFirstFile(__TEXT("*.*"),&pDW->FindData);
- pDW->fOk=(hFind!=INVALID_HANDLE_VALUE);
- while(pDW->fOk)
- {
- pDW->fIsDir=pDW->FindData.dwFileAttributes&
- FILE_ATTRIBUTE_DIRECTORY;
- if (!pDW->fIsDir||
- (!pDW->fRecurse&&IsChildDir(&pDW->FindData)))
- {
- _stprintf(pDW->szBuf,
- pDW->fIsDir?__TEXT("%*s[%s]"):__TEXT("%*s%s"),
- pDW->nIndent,__TEXT(""),
- pDW->FindData.cFileName);
- //printf("%s\n",pDW->szBuf);
- lstrcpy(temp,pszRootPath);
- lstrcat(temp,"\\");
- lstrcat(temp,pDW->FindData.cFileName);
- lstrcpy(ttemp,pszdestPath);
- lstrcat(ttemp,"\\");
- lstrcat(ttemp,pDW->FindData.cFileName);
- HANDLE hfile=CreateFile(
- ttemp,
- GENERIC_READ|GENERIC_WRITE,
- 0,
- NULL,
- OPEN_ALWAYS,
- pDW->FindData.dwFileAttributes,
- NULL);
- HANDLE dfile=CreateFile(
- temp,
- GENERIC_READ|GENERIC_WRITE,
- 0,
- NULL,
- OPEN_EXISTING,
- pDW->FindData.dwFileAttributes,
- NULL);
- DWORD filesize=GetFileSize(dfile,NULL);
- char* buffer=new char[filesize+1];
- DWORD readsize;
- ReadFile(dfile,buffer,filesize,&readsize,NULL);
- WriteFile(hfile,buffer,filesize,&readsize,NULL);
- buffer[filesize]=0;
- CloseHandle(hfile);
- CloseHandle(dfile);
- }
- else if(pDW->fIsDir&&IsChildDir(&pDW->FindData))
- {
- SECURITY_ATTRIBUTES attribute;
- attribute.nLength = sizeof(attribute);
- attribute.lpSecurityDescriptor = NULL;
- attribute.bInheritHandle = FALSE;
- SetCurrentDirectory(pszdestPath);
- CreateDirectory(
- pDW->FindData.cFileName,
- &attribute
- );
- SetCurrentDirectory(pszRootPath);
- }
- pDW->fOk=FindNextFile(hFind,&pDW->FindData);
- }
- if (hFind != INVALID_HANDLE_VALUE)
- {
- FindClose(hFind);
- }
- if (pDW->fRecurse)
- {
- hFind=FindFirstChildDir(__TEXT("*.*"),&pDW->FindData);
- pDW->fOk=(hFind!=INVALID_HANDLE_VALUE);
- while(pDW->fOk)
- {
- if (SetCurrentDirectory(pDW->FindData.cFileName))
- {
- lstrcpy(temp,pszRootPath);
- lstrcat(temp,"\\");
- lstrcat(temp,pDW->FindData.cFileName);
- lstrcpy(ttemp,pszdestPath);
- lstrcat(ttemp,"\\");
- lstrcat(ttemp,pDW->FindData.cFileName);
- DirWalkRecurse(pDW,temp,ttemp);
- SetCurrentDirectory(__TEXT(".."));
- }
- pDW->fOk=FindNextChildDir(hFind,&pDW->FindData);
- }
- if (hFind!=INVALID_HANDLE_VALUE)
- {
- FindClose(hFind);
- }
- }
- pDW->nDepth--;
- }
- void DirWalk(LPTSTR pszRootPath,LPTSTR pszdestPath,BOOL fResourse)
- {
- TCHAR szCurrDir[_MAX_DIR];
- DIRWALKDATA DW;
- GetCurrentDirectory(chDIMOF(szCurrDir),szCurrDir);
- SetCurrentDirectory(pszRootPath);
- DW.nDepth=-1;
- DW.fRecurse=fResourse;
- DirWalkRecurse(&DW,pszRootPath,pszdestPath);
- SetCurrentDirectory(szCurrDir);
- }
- int main(int argc,char *argv[])
- {
- char tmp[100],ttmp[100];
- int i;
- if (argc!=3)
- {
- printf("Argument Error\n");
- exit(0);
- }
- strcpy(tmp,argv[1]);
- if (strlen(tmp)!=1&&tmp[strlen(tmp)-1] == '\\')
- {
- tmp[strlen(tmp)-1]=0;
- }
- for ( i = strlen(tmp); i >= 0; i--)
- {
- if ( tmp[i] == '\\')
- {
- break;
- }
- }
- //printf("%s\n",&tmp[i+1]);
- strcpy(ttmp,argv[2]);
- if (strcmp(&tmp[i+1],"")&&strcmp(&tmp[i+1],"..")&&strcmp(&tmp[i+1],"."))
- {
- if (ttmp[strlen(ttmp)-1] != '\\')
- {
- strcat(ttmp,"/");
- }
- strcat(ttmp,&tmp[i+1]);
- //printf("cp %s %s\n",tmp,ttmp);
- SECURITY_ATTRIBUTES attribute;
- attribute.nLength = sizeof(attribute);
- attribute.lpSecurityDescriptor = NULL;
- attribute.bInheritHandle = FALSE;
- SetCurrentDirectory(argv[2]);
- CreateDirectory(
- &tmp[i+1],
- &attribute
- );
- }
- DirWalk((char *)argv[1],(char *)ttmp,TRUE);
- return 0;
- }
linux版本:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <dirent.h>
- #include <sys/stat.h>
- #include <dirent.h>
- #include <fcntl.h>
- #define N 4096
- int cp(char dir[],char tdir[],int k)
- {
- int i,j,n;
- int fd,tfd;
- DIR *dp;
- char buf[N];
- char tmp[N],ttmp[N],ch[N];
- struct dirent *dirp[N],*dirptmp;
- struct stat st[N];
- if ( (dp = opendir(dir)) == NULL)
- {
- printf("cp: cannot access %s: No such file or directory\n",dir);
- return 1;
- }
- i=0;
- n=0;
- while ( (dirp[i] = readdir(dp)) != NULL)
- {
- if (dirp[i]->d_name[0]=='.')
- {
- continue;
- }
- strcpy(tmp,dir);
- if (tmp[strlen(tmp)-1] != '/')
- {
- strcat(tmp,"/");
- }
- strcat(tmp,dirp[i]->d_name);
- //printf("%s\n",tmp);
- if (lstat(tmp,&st[i]) < 0)
- {
- printf("Cannot open the directory\n");
- break;
- }
- i++;
- n++;
- }
- for ( i = 0; i < n; i++)
- {
- //printf("%d\n",i);
- memset(tmp,0,sizeof(tmp));
- memset(ttmp,0,sizeof(ttmp));
- //printf("%s\n",dirp[i]->d_name);
- if (S_ISDIR(st[i].st_mode))
- {
- strcpy(tmp,dir);
- if (tmp[strlen(tmp)-1] != '/')
- {
- strcat(tmp,"/");
- }
- strcat(tmp,dirp[i]->d_name);
- strcpy(ttmp,tdir);
- if (ttmp[strlen(ttmp)-1] != '/')
- {
- strcat(ttmp,"/");
- }
- strcat(ttmp,dirp[i]->d_name);
- //printf("cp %s %s\n",tmp,ttmp);
- if (mkdir(ttmp,st[i].st_mode) != 0)
- {
- //printf("The directory %s is exist.\n",ttmp);
- //continue;
- }
- cp(tmp,ttmp,k+1);
- //printf("%s\n",tmp);
- }
- else if(S_ISLNK(st[i].st_mode))
- {
- continue;
- }
- else
- {
- strcpy(tmp,dir);
- if (tmp[strlen(tmp)-1] != '/')
- {
- strcat(tmp,"/");
- }
- strcat(tmp,dirp[i]->d_name);
- fd=open(tmp,O_RDONLY);
- if (fd == -1)
- {
- printf("open file error\n");
- continue;
- }
- strcpy(ttmp,tdir);
- strcat(ttmp,"/");
- strcat(ttmp,dirp[i]->d_name);
- tfd=creat(ttmp,0644);
- if (tfd == -1)
- {
- printf("open file error\n");
- continue;
- }
- //printf("file %s %s\n",tmp,ttmp);
- while((j=read(fd,buf,512))>0)
- {
- write(tfd,buf,j);
- }
- close(fd);
- close(tfd);
- }
- }
- closedir(dp);
- return 0;
- }
- int main(int argc, char *argv[])
- {
- int i;
- char tmp[N],ttmp[N];
- struct stat st[N];
- if (argc < 2)
- {
- printf("Argument error\n");
- return 0;
- }
- else if (argc == 2)
- {
- printf("Argument error\n");
- return 0;
- }
- else if (argc == 3)
- {
- strcpy(tmp,argv[1]);
- if (strlen(tmp)!=1&&tmp[strlen(tmp)-1] == '/')
- {
- tmp[strlen(tmp)-1]=0;
- }
- for ( i = strlen(tmp); i >= 0; i--)
- {
- if ( tmp[i] == '/')
- {
- break;
- }
- }
- //printf("%s\n",&tmp[i+1]);
- strcpy(ttmp,argv[2]);
- if (strcmp(&tmp[i+1],"")&&strcmp(&tmp[i+1],"..")&&strcmp(&tmp[i+1],"."))
- {
- if (ttmp[strlen(ttmp)-1] != '/')
- {
- strcat(ttmp,"/");
- }
- strcat(ttmp,&tmp[i+1]);
- //printf("cp %s %s\n",tmp,ttmp);
- if (lstat(tmp,&st[i]) < 0)
- {
- printf("Cannot open the directory\n");
- exit(0);
- }
- mkdir(ttmp,st[i].st_mode);
- }
- //printf("cp %s %s\n",argv[1],ttmp);
- cp(argv[1],ttmp,0);//参数应为绝对路径
- }
- else
- {
- printf("Argument error\n");
- return 0;
- }
- return 0;
- }