64,650
社区成员
发帖
与我相关
我的任务
分享
int my_cp(const char* from_path, const char* to_path)
{
if(from_path == NULL || to_path == NULL)
{
return -1;
}
int from_fd, to_fd;
if(-1 == (from_fd = open(from_path, O_RDONLY)))
{
perror("open");
return -1;
}
if(file_exist(to_path))
{
if(-1 == (to_fd = open(to_path, O_WRONLY | O_TRUNC)))
{
perror("open");
close(from_fd);
return -1;
}
}
else
{
if(-1 == (to_fd = open(to_path, O_WRONLY | O_CREAT | O_TRUNC, 00777)))//这里创建的文件什么权限都没有。
{
perror("open");
fprintf(stderr, "open:%s\n",strerror(errno));
close(from_fd);
return -1;
}
}
if(cp_file(from_fd, to_fd))
{
unlink(to_path);
close(from_fd);
close(to_fd);
return -1;
}
close(from_fd);
close(to_fd);
return 0;
}