实现cp指令的基本思路:
1.打开源文件以及目标文件
2.读取源文件
3.将读取到的文件写入目标文件
4.关闭两个文件
代码:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[] )
{
int fd_src;
int fd_des;
char *src_buf = NULL;
if(argc != 3)
{
printf("code not suessful\n");
exit(-1);
}
fd_src = open(argv[1],O_RDWR);
int size = lseek(fd_src,0,SEEK_END);
lseek(fd_src,0,SEEK_SET);
src_buf = (char *)malloc(sizeof(char)*size+8);
int n_read = read(fd_src,src_buf,size);
fd_des = open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);
int n_write = write(fd_des,src_buf,strlen(src_buf));
close(fd_src);
close(fd_des);
return 0;
}