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

#include
#include
#include
#include
#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);

}

你可能感兴趣的:(linux-c)