我正在尝试创建一个复制程序,它接受源和目标目录并将目录从源复制到目标。两个目录名都是文件路径的情况,它的工作原理。对于source是文件而目标是目录的情况,它可以工作。但是,对于源和目录都是目录的情况,它不能正常工作,甚至不是非多级别目录的简单情况。 。请建议版本。
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
#include<stdlib.h>
#include<unistd.h>
void copyfile(char source[],char destination[]);
void traverse(char source[],char destination[]);
int isdir(char path[]);
int main(int argc,char *argv[])
{
if(argc<3)
{
printf("ERROR: Improper syntax\n");
printf("\t./copy [source path] [destination path]\n");
exit(0);
}
char *source=(char *)malloc(sizeof(char)*strlen(argv[1])+1);
char *destination=(char *)malloc(sizeof(char)*strlen(argv[2])+1);
strcpy(source,argv[1]);
strcpy(destination,argv[2]);
if(isdir(source)&&!isdir(destination))
{
printf("Destination cannot be a file\n");
exit(0);
}
if(isdir(source)&&isdir(destination))
traverse(source,destination);
else copyfile(source,destination);
return 0;
}
void copyfile(char source[],char destination[])
{
int c;
if(!isdir(source)&&isdir(destination))
{
char *fname=strrchr(source,'/');
if(realloc(destination,strlen(destination)+strlen(source)+2)==NULL)
{
printf("Memory reallocation error\n");
exit(0);
}
strcat(destination,fname);
}
FILE *f1,*f2;
f1=fopen(source,"r");
f2=fopen(destination,"w");
if(f1==NULL)
{
printf("File does not exists");
return;
}
if(f2==NULL)
{
printf("File copying error: Directory may or may not exist\n");
return;
}
while((c=fgetc(f1))!=EOF)
fputc(c,f2);
fclose(f1);
fclose(f2);
return;
}
int isdir(char path[])
{
struct stat dir;
stat(path,&dir);
if(S_ISREG(dir.st_mode))
return 0;
if(S_ISDIR(dir.st_mode))
return 1;
printf("The source is not a file, neither a dir\n");
exit(0);
}
void traverse(char source[],char destination[])
{
char *fname=strrchr(source,'/');
char *tdest=(char *)malloc(strlen(destination)+strlen(fname)+1+6);
strcpy(tdest,"mkdir ");
strcat(tdest,destination);
strcat(tdest,fname);
system(tdest);
if(realloc(destination,(strlen(destination)+strlen(fname))*sizeof(char)+1)==NULL)
{
printf("Destination rellocaion pointer inturrupted\n");
exit(0);
}
strcat(destination,fname);
DIR *d1;
if((d1=opendir(source))==NULL)
{
printf("Source access inturrupted\n");
exit(0);
}
struct dirent *cwd;
struct stat dir;
while((cwd=readdir(d1))!=NULL)
{
char *tsource=(char *)malloc((strlen(source)+strlen(cwd->d_name))*sizeof(char)+2);
stat(cwd->d_name,&dir);
if(S_ISDIR(dir.st_mode))
{
if(strcmp(cwd->d_name,".")==0||strcmp(cwd->d_name,"..")==0)continue;
traverse(source,destination);
}
strcpy(tsource,source);
strcat(tsource,"/");
strcat(tsource,cwd->d_name);
copyfile(tsource,destination);
}
closedir(d1);
return;
}
答案 0 :(得分:2)
您使用realloc失败:必须将其返回值分配(通常是重新分配)给指针。
npm install node-gyp rebuild
请注意,对malloc的所有调用都应该避免转换返回值,并且所有返回的值都必须为NULL检查。
正如@Quentin正确评论的那样,请注意,如果destination = realloc(destination, (strlen(destination)+strlen(fname))*sizeof(char)+1);
if (destination == NULL)
{
printf("Destination rellocaion pointer inturrupted\n");
exit(0);
}
失败,目标指向的内存将被泄露:该内存的地址将丢失。所以要非常安全地做到这一点,你可以使用临时变量:
realloc
还要注意,必须释放所有char *temp = realloc(destination, (strlen(destination)+strlen(fname))*sizeof(char)+1);
if (temp == NULL)
{
printf("Destination rellocaion pointer inturrupted\n");
exit(0);
}
destination = temp;
个块,即使程序终止时OS会为您释放它。这是编码和避免未来内存泄漏问题的最佳方法:malloc
内存,直到特定内存不再使用。