版权声明:在征得作者同意的情况下,本网站的作品允许非盈利性引用,并请注明出处:“作者:luomuxiaoxiao 转载自: www.luomuxiaoxiao.com"字样,以尊重作者的劳动成果。版权归原作者所有。未经允许,严禁转载。 https://blog.csdn.net/luomuxiaoxiao98/article/details/18227941
#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;
}