#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int fd1 = -1;
int fd2 = -1;
int size = 0;
char *buf;
if(argc < 3)
{
printf("用法 copy [源文件/路径] [目标文件/路径]\n");
return -3;
}
if( (fd1 = open(argv[1], O_RDONLY)) < 0 )
{
printf("没有%s\n",argv[1]);
return -1;
}
if( (fd2 = open(argv[2], O_CREAT, 0600)) < 0 )
{
printf("无法创建%s,或者目标已存在.\n",argv[2]);
return -2;
}
else
{
close(fd2);
fd2 = open( argv[2], O_WRONLY);
}
size = lseek(fd1, 0, SEEK_END);
buf = (char *) malloc(size);
lseek(fd1, 0, SEEK_SET);
read(fd1, buf, size);
write(fd2, buf, size);
close(fd1);
close(fd2);
return 0;
}
实现linux cp命令( 系统调用版 )
最新推荐文章于 2023-04-10 14:02:16 发布