源文件cp.c
#include
#include
#include
#include
int main(int argc, char *argv[])
{
printf("cpd tool, Tommy jiang@2007.\n\n");
if(argc != 3)
{
printf("Usage : cpd SOURCEIDR DESTDIR.\n");
printf("Example : cpd /webs /mnt/sda1.\n\n");
return 0;
}
FILE *fd1;
FILE *fd2;
int size = 0;
DIR *dp;
struct dirent *entry;
struct stat statbuf;
dp = opendir(argv[1]);
chdir(argv[1]);
char buf[1024];
char name[256];
memset(name, 0, 256);
while((entry = readdir(dp)) != NULL)
{
lstat(entry->d_name, &statbuf);
if(S_ISDIR(statbuf.st_mode))
continue;
fd1 = fopen(entry->d_name, "r");
strcpy(name, argv[2]);
strcat(name, "/");
strcat(name, entry->d_name);
printf("name = %s\n", name);
fd2 = fopen(name, "w");
while((size = fread(buf, 1, 1024, fd1)))
fwrite(buf, 1, size, fd2);
memset(name, 0, 256);
fflush(fd2);
sleep(1);
}
fclose(fd1);
fclose(fd2);
chdir("..");
closedir(dp);
}
编译命令:
gcc -o cpd cp.c
xcwen 于 2007-11-22 09:09:56发表:
cp -rf dir1 dir2
jhx0301 于 2007-11-22 09:05:39发表:
顶!继续努力