C 语言实现cp命令(不带参数的):即实现一个文件的复制
代码实现:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h> // write 's headfile
/*open 's headfile */
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main(int argc ,char *argv[])
{
int i;
int fd1,fd2; // 文件描述符
int number;
char buf[10];
if(argc < 3)
{
fprintf(stderr,"usage:%s origin destination \n",argv[0]);
return 1;
}
if((fd1 = open(argv[1],O_RDONLY,0644)) < 0)
{
fprintf(stderr,"cannot open %s for reading \n",argv[1]);
exit(EXIT_FAILURE);
}
if((fd2 = open(argv[2],O_WRONLY)) < 0)
{
fprintf(stderr,"cannot open %s for writing \n",argv[2]);
exit(EXIT_FAILURE);
}
while((number = read(fd1,buf,10)) > 0)
{
if((write(fd2,buf,number)) < 0)
{
fprintf(stderr , "%s writing error!\n",argv[2]);
exit(EXIT_FAILURE);
}
}
fo