cat 系統函數C語言實現

at是將文本連接起來的程序,將第二個及以後的文件存到第一個裏

#include <stdio.h>
#include <fcntl.h>
 
void filecopy(int fdin, int fdout)
{
    char c;
    while( read(fdin, &c, 1) == 1 ) {
        write(fdout, &c, 1);	
	}

}
 
int main( int argc, char *argv[] )
{
    int fdin, fdout;
    if((fdin = open(*++argv, O_RDONLY, 0)) == -1) {
            write( 2, "ERROR :Can't open", 17);
            write( 2, *argv, sizeof( *argv ));
    }
 
    if( argc == 1 ) {
        filecopy( 0, 1 );
	} else {
        while( --argc );
	}

	if( (fdout = open(*++argv, O_WRONLY, 0)) == -1 ) {
		write( 2, "ERROR :", 7);
		write( 2, *argv, 20);
	} else {
		filecopy(fdin, fdout);
	}
	
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章