CP命令源碼

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

#define BUFFERSIZE 4096
#define COPYMODE 0644

vodi oops( char *, char *);

main( int ac,char *av[])
{
    int in_fd, out_fd, n_chars;
    char buf[BUFFERSIZE];
    
    if( ac!=3){
        fprintf(stderr,"usage: %s source destination\n", *av);
        exit(1);
    }
    
    if ((in_fd = open(av[1],O_REONLY)) == -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,buf,BUFFERSIZE))>0 )
        if(write(out_fd,buf,n_chars)!=n_chars)
            oops("Write 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","");
}

void oops(char * s1 ,char * s2)
{
     fprintf(stderr,"Error: % s",s1);
     perror(s2);
     exit(1);
    
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容