实现一个简单的cp命令

#include<sys/types.h>
#include<sys/mman.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int copyAFileToAnother(char* oFile,char* tFile)
{
	struct stat oFileStat;
	int fileSize;
	int oFileHandle,tFileHandle;
	char* buffer;

	stat(oFile,&oFileStat);
	fileSize=oFileStat.st_size;
	
	buffer = (char *)malloc(fileSize+1);

	oFileHandle = open(oFile,O_RDONLY);
	read(oFileHandle,buffer,fileSize);

	tFileHandle = open(tFile,O_RDWR|O_CREAT,0644);
	write(tFileHandle,buffer,fileSize);

	close(tFileHandle);
	free(buffer);
	close(oFileHandle);
}

int copyFileToDirectory(char* file,char* directory)
{
	int dirLen = strlen(directory);	
	int fileNameLen = strlen(file);
	char* tFile;

	if(directory[dirLen-1]!='/')
	{
		tFile = (char *)malloc(fileNameLen+dirLen+2);
		strcpy(tFile,directory);
		tFile[dirLen]='/';
		strcpy(tFile+dirLen+1,file);
	}else
	{
		tFile = (char *)malloc(fileNameLen+dirLen+1);
		strcpy(tFile,directory);
		strcpy(tFile+dirLen,file);
	}
	printf("%s\n\n%s\n\n",file,tFile);
	copyAFileToAnother(file,tFile);
}

int main(int argc,char* argv[])
{
	struct stat tFileStat;

	stat(argv[2],&tFileStat);

	if((tFileStat.st_mode&S_IFMT)==S_IFREG)
		copyAFileToAnother(argv[1],argv[2]);	
	else if((tFileStat.st_mode&S_IFMT)==S_IFDIR)
		copyFileToDirectory(argv[1],argv[2]);
	else printf("This file can not be handled.\n");

}




version2: 

#include<sys/types.h>
#include<sys/mman.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/stat.h>
#include<dirent.h>

#define NAME_MAX_LENGTH=100;

void getLastSlashElement(const char * path,char * element)
{
	int len = strlen(path);
	int headIndex=len-2,tailIndex=(path[len-1]=='/'?len-1:len);

	while(headIndex>=0 && path[headIndex]!='/')
		headIndex--;
	
	headIndex++;
	int index;

	for(index=0;index+headIndex<tailIndex;index++)
		element[index]=path[index+headIndex];
	element[index]=0;
}

int copyAFileToAnother(char* oFile,char* tFile)
{
	struct stat oFileStat;
	int fileSize;
	int oFileHandle,tFileHandle;
	char* buffer;

	stat(oFile,&oFileStat);
	fileSize=oFileStat.st_size;
	
	buffer = (char *)malloc(fileSize+1);

	oFileHandle = open(oFile,O_RDONLY);
	read(oFileHandle,buffer,fileSize);

	tFileHandle = open(tFile,O_RDWR|O_CREAT,0644);
	write(tFileHandle,buffer,fileSize);

	close(tFileHandle);
	free(buffer);
	close(oFileHandle);
}

int copyFileToDirectory(char* file,char* directory)
{
	int dirLen = strlen(directory);	
	int fileNameLen;
	char* tFile;
	char* simpleFileName;

	simpleFileName = (char *)malloc(strlen(file)+1);
	getLastSlashElement(file,simpleFileName);
	fileNameLen = strlen(simpleFileName);

/*	if(directory[dirLen-1]!='/')
	{
		tFile = (char *)malloc(fileNameLen+dirLen+2);
		strcpy(tFile,directory);
		tFile[dirLen]='/';
		strcpy(tFile+dirLen+1,simpleFileName);
	}else
	{
*/
	tFile = (char *)malloc(fileNameLen+dirLen+1);
	strcpy(tFile,directory);
	strcpy(tFile+dirLen,simpleFileName);

	copyAFileToAnother(file,tFile);

	free(simpleFileName);
	free(tFile);
}

void nonRecursiveCopy(char* arg1,char* arg2)
{
	struct stat tFileStat;

	stat(arg2,&tFileStat);

	if((tFileStat.st_mode&S_IFMT)==S_IFREG)
		copyAFileToAnother(arg1,arg2);	
	else if((tFileStat.st_mode&S_IFMT)==S_IFDIR)
		copyFileToDirectory(arg1,arg2);
	else printf("This file can not be handled.\n");

}

void recursiveDemo(char* oFile,char* tDir)
{
	struct stat oFileStat;
	stat(oFile,&oFileStat);
	if((oFileStat.st_mode&S_IFMT)==S_IFREG)
		copyFileToDirectory(oFile,tDir);
	else if((oFileStat.st_mode&S_IFMT)==S_IFDIR)
	{
		char* simpleDirName = (char *)malloc(strlen(oFile)+1);
		getLastSlashElement(oFile,simpleDirName);

		int tDirLen = strlen(tDir);
		int sDirLen = strlen(simpleDirName);
		int oDirLen = strlen(oFile);

		char* newTDir=(char *)malloc(tDirLen+sDirLen+2);
		strcpy(newTDir,tDir);
		strcpy(newTDir+tDirLen,simpleDirName);
		newTDir[tDirLen+sDirLen]='/';
		newTDir[tDirLen+sDirLen+1]=0;
		mkdir(newTDir,0777);

		DIR *dirp;
		struct dirent *dp;
		char* newOName=(char *)malloc(oDirLen+32);
		
		strcpy(newOName,oFile);
		newOName[oDirLen]='/';

		dirp = opendir(oFile);

//		printf("newTDir:%s\n\n",newTDir);
		while((dp=readdir(dirp))!=NULL)	
			if(strcmp(dp->d_name,".")&&strcmp(dp->d_name,".."))
			{
				strcpy(newOName+oDirLen+1,dp->d_name);
				printf("newOName:%s  newTDir:%s\n",newOName,newTDir);
				recursiveDemo(newOName,newTDir);
			}

		free(newOName);
		closedir(oFile);
		free(newTDir);
		free(simpleDirName);
	}
}

void recursiveCopy(char* arg1,char* arg2)
{
	struct stat tFileStat;
	stat(arg2,&tFileStat);

	if((tFileStat.st_mode&S_IFMT)!=S_IFDIR)
	{
		printf("The target must be a directory! exit\n");
		return;
	}
	
	recursiveDemo(arg1,arg2);
}

int main(int argc,char* argv[])
{
	if(strcmp(argv[1],"-r")==0)
		recursiveCopy(argv[2],argv[3]);
	else 
		nonRecursiveCopy(argv[1],argv[2]);
}


阅读更多
个人分类: linux学习之路
上一篇shell_猜数字游戏
下一篇Spring中AutowireCapableBeanFactory的使用
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭