|
楼主 |
发表于 2006-4-23 09:57:14
|
显示全部楼层
噢我知道了,这里是整个程序,我想实现的功能是连同文件夹内的所由文件的各种属性一同复制的程序.使用方法是"./cpdir 原文件夹路径 要复制到的路径"
[code:1]
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
void cpdir(const char *src,const char *des);
void err_sys(const char* info);
void err_sys(const char* info)
{
/* perror(info);*/
printf(info);
exit(1);
}
int main(int argc, char *argv[])
{
struct stat *attrib; /*record the attrib of every file copied*/
if(argc!=3)
{
printf("Usage: cpdir <src-dirtory> <des-dirtory>\n");
exit(0);
}
else if(strcmp(argv[1],argv[2])==0||stat(argv[2],attrib)==0)
{
printf("Error: This program should not to be used to overwrite an exist dirtory!\n");
exit(0);
}
lstat(argv[1],attrib);
/*check if the file is a folder*/
if(!S_ISDIR(attrib->st_mode))
{
printf("Error: The src-path you specificed is not a dirtory!\n");
exit(0);
}
/*make the root folder which have the same mode of the src-dir*/
if(mkdir(argv[2],attrib->st_mode))
{
printf("Error: Fail to create the folder!\nPlease check your permission and the memory space available!\n");
exit(0);
}
/*chang the owner of the folder*/
if(chown(argv[2],attrib->st_uid,attrib->st_gid))
{
printf("Error: Fail to assign the owner of the folder!\n");
exit(0);
}
/*copy the files and folders in the folder*/
cpdir(argv[1],argv[2]);
return 0;
}
void cpdir(const char *src,const char *des)
/*src is the path of the folder in which the files is to be copied,des is the path of the folder which is already created*/
{
struct stat *attrib;
struct dirent *fname;
int fd_src;
int fd_des;
char buf[BUFSIZ];
int m;
DIR *dp;
int n;
char *path_in_src;
char *path_in_des;
for(n=0;;)
{
if((dp=opendir(src))==NULL)
err_sys("Open dirtory file error!\n");
seekdir(dp,n++);
if((fname=readdir(dp))==NULL) /*the end of the file*/
{
closedir(dp);
break;
}
if(strcmp(fname->d_name,"..")==0||strcmp(fname->d_name,".")==0)
{
closedir(dp);
continue;
}
/*get the path and mode of the source file*/
path_in_src=(char*)malloc(sizeof(char)*(strlen(src)+strlen(fname->d_name)+3));
strcpy(path_in_src,src);
strcat(path_in_src,"/\0");
strcat(path_in_src,fname->d_name);
lstat(path_in_src,attrib);
/*get the path of the destinated file*/
path_in_des=(char*)malloc(sizeof(char)*(strlen(des)+strlen(fname->d_name)+3));
strcpy(path_in_des,des);
strcat(path_in_des,"/\0");
strcat(path_in_des,fname->d_name);
closedir(dp);
if(S_ISDIR(attrib->st_mode)) /*if the src is a folder*/
{
if(mkdir(path_in_des,attrib->st_mode))
err_sys("Create folder error!\n");
if(chown(path_in_des,attrib->st_uid,attrib->st_gid))
err_sys("Assign folder to owner error!\n");
cpdir(path_in_src,path_in_des);
}
else
{
if((fd_des=creat(path_in_des,attrib->st_mode))==-1)
err_sys("Create destinated file error!\n");
if((fd_src=open(path_in_src,0))==-1)/*open the file in readonly mode*/
err_sys("Open source file error!\n");
while((m=read(fd_src,buf,BUFSIZ))>0)
if(write(fd_des,buf,m)!=m)
err_sys("Copy file error!\n");
close(fd_src);
close(fd_des);
if(chown(path_in_des,attrib->st_uid,attrib->st_gid))
err_sys("Assign file to owner error!\n");
}
free(path_in_src);
free(path_in_des);
}
}
[/code:1] |
|