实现一个简单的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]);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值