您的位置:首页 > 运维架构 > Linux

小白笔记--------------linux下的cp命令

2016-03-25 16:35 483 查看
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdlib.h>
#define COPYMODE  0644
#define BUFFSIZE  4096
main(int ac, char ** av){
int in_fd,out_fd;
char n_chars;
char buff[BUFFSIZE];
if((in_fd = open(av[1],O_RDONLY)) == -1){
oops("cannot open",av[1]);
}
if((out_fd = creat(av[2],COPYMODE))== -1){
oops("cannot creat",av[2]);
}
while((n_chars = read(in_fd,buff,BUFFSIZE))> 0){
if(write(out_fd,buff,n_chars) != n_chars){
oops("wirte error to ",av[2]);
}
}
if(n_chars == -1){
oops("read error from",av[1]);
}
if(close(in_fd) == -1 || close(out_fd) == -1){
oops("error closing files","");
}
}
oops(char *s1,char *s2){
fprintf(stderr,"error is : %s",s1);
perror(s2);
exit(1);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: