cp_log

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




#define DEBUG_LOG
#if defined(DEBUG_LOG)
#define DEBUG(fmt, arg...)  printf(fmt "\r\n", ##arg)
#else
#define DEBUG(fmt, arg...)
#endif


#define ERROR_LOG
#if defined(ERROR_LOG)
#define ERROR(fmt, arg...)  printf("ERROR" fmt "\r\n", ##arg)
#else
#define ERROR(fmt, arg...)
#endif




#define FNAME_MAXLEN 10000
#define UNDERSCORE 1
#define SLASH 2


int isdir(char *path)
{
	struct stat buf;


	if (lstat(path, &buf) < 0)
		DEBUG("Error: isdir lstat error!\n");


	if (S_ISDIR(buf.st_mode))
	{
		//DEBUG("Is a direcotry!\n");
		return 1;
	}
	else 
		return 0;
}


int isreg(char *path)
{
	struct stat buf;


	if (lstat(path, &buf) < 0)
		DEBUG("Error: isreg lstat error!\n");


	if (S_ISREG(buf.st_mode))
	{
		//DEBUG("Is a direcotry!\n");
		return 1;
	}
	else 
		return 0;
}


int connect_symbol(char *first, char *sec, char val)
{
	char temp[3] ;
	
	if (val == 1)
		temp[0] = '_';
	else if (val == 2)
		temp[0] = '/';
	else
		printf("connect_symbol: Error!\n");


	temp[1] = '\0';
	
	strcat(first, temp);
	strcat(first, sec);


	return 0;
}


int contain_dot(char *path, char *before_dot)
{
	char *p; 
	char flag = 0, i = 0;
	char after_dot[5];
	p = path;
	while(*p)
	{
		if (*p == '.')
		{
			p++;
			flag = 1;
			break;
		}	
		*before_dot = *p;
		before_dot++;
		p++;
		i++;
	}
	*before_dot = '\0';
	before_dot -= i;
	
	i = 0;	
	while(*p)
	{
		after_dot[i] = *p;
		p++;
		i++;
	}
	after_dot[i] = '\0';


	if(flag)
	{	
		connect_symbol(before_dot, after_dot, UNDERSCORE);
	}


	return flag;
}


int cp_file(char *old_path, char *new_path)
{
	int fd1, fd2;
	char *buf;
	off_t currpos;
	
	DEBUG("cp_file: old_path = %s\n", old_path);
	DEBUG("cp_file: new_path = %s\n", new_path);


	fd1 = open(old_path, O_RDONLY);
	if (fd1 < 0)
	{
		DEBUG("Open original file failed!\n");
		return 0;
	}
	
	currpos = lseek(fd1, 0, SEEK_END);
	fd1 = open(old_path, O_RDONLY);


	buf = (char *)malloc(currpos);
	if (strlen(buf) < 0)
		DEBUG("Assign temporary buffer failed!\n");


	fd2 = creat(new_path, 0777);
	if (fd2 < 0)
	{
		DEBUG("Creat new file failed!\n");
		return 0;
	}	
	
	if (read(fd1, buf, currpos) < 0)
	{
		DEBUG("Read original file failed!\n");
		return 0;
	}
	
	if (write(fd2, buf, currpos) < 0)
	{
		DEBUG("Write new file failed!\n");
		return 0;
	}
	
	free(buf);
	
	return 1;
}


int cp_log(char *old_path, char *new_path)
{
	char ret;	
	DEBUG("cp_log: old_path = %s\n", old_path);
	DEBUG("cp_log: new_path = %s\n", new_path);
	if (isreg(old_path))
	{
		if(ret = cp_file(old_path, new_path))
			DEBUG("cp_log: cp file success!\n");
		else
		{
			DEBUG("cp_log: copy failed!\n");		
			return 0;
		}
	}
	else if(isdir(old_path))
	{
		DEBUG("cp_log: Is a dircotry!\n");
	}
	else
		DEBUG("cp_log: Object file is not a file or directory!\n");
	
	return 1;
}


int do_copy(char *source, char *dest_filename)
{
	char add_log[10] = ".log";
	char currname[FNAME_MAXLEN];
	char ret;


	if (contain_dot(source, currname))				// ie: input "test/test.c"  currname = "test/test_c"
	{
		DEBUG("do_copy: source = %s\n", source);
		strcat(dest_filename, currname);				// add prefix and save the finnal name in "dest_filename"
		strcat(dest_filename, add_log);
		DEBUG("do_copy: currdir = %s\n", dest_filename);
		if(ret = cp_log(source, dest_filename))
			DEBUG("do_copy: cp to log success!\n");
		else
			DEBUG("do_copy: copy failed!\n");		
	}


	return 1;
}


/* cd a directory and  ergodic it, save all the files in *rtdir_file[FNAME_MAXLEN]*/
int ergodic_dir(char *dir, char *rtdir_file[FNAME_MAXLEN])
{
	DIR *dp;
	unsigned int i = 0;
	struct dirent *dirp;
	//char rtdir_file[200][200];
		if ((dp = opendir(dir)) == NULL)
		printf("ergodic_dir: can't open %s\n", rtdir_file[1]);
		
	while((dirp = readdir(dp)) != NULL)
	{
		printf("ergodic_dir: dirp->d_name = %s\n",  dirp->d_name);
		if ((strcmp("..", dirp->d_name) == 0) || (strcmp(".", dirp->d_name) == 0))
			;
		else
		{
			//printf("ergodic_dir: dir = %s, strlen(dir) = %d\n", dir, strlen(dir));
			//printf("ergodic_dir: dirp->d_name = %s, strlen(dirp->d_name) = %d\n", dirp->d_name, strlen(dirp->d_name));
			connect_symbol(dir, dirp->d_name, SLASH);
			printf("ergodic_dir: dir = %s\n", dir);
			strcpy(rtdir_file[i++], dir);
		}
	}
	closedir(dp);
	
	while(*rtdir_file[i])
		printf("ergodic_dir: rtdir_file[%d] = %s\n", i, rtdir_file[i++]);
	
	return 0;
}




int copy(char *object, char *dest_filename)
{
	char dest_lenth;
	char rtdir_file[500][FNAME_MAXLEN];
	dest_lenth = strlen(dest_filename);
	
	if (isreg(object))
	{
		DEBUG("copy: argv[i] = %s\n", object);
		DEBUG("copy: currdir = %s\n", dest_filename);
		if(do_copy(object, dest_filename))
			printf("copy: cp to log success!\n");
		else
			printf("copy: copy failed!\n");	
	
	}
	else if(isdir(object))
	{
		strcat(dest_filename, object);
		// 1. create the same-name directory
		if ((mkdir(dest_filename, 0755)) < 0)
			DEBUG("copy: mkdir %s failed !\n", dest_filename);
			
		//2. enter this directory, call cp_log recursively
		ergodic_dir(object, rtdir_file[FNAME_MAXLEN]);
		//copy(object, dest_filename);
		
		printf("Is a dircotry!\n");
	}		
	dest_filename[dest_lenth] = '\0';


	return 0;
}

int main(int argc, char *argv[])
{
	char i = 0;
	char dest_filename[FNAME_MAXLEN] = "./test_dir/";


	copy(argv[1], dest_filename);
	
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值