📄 a_cat.cpp
字号:
/* Copyright is licensed under GNU LGPL. by I.J.Wang 2005 Dump the argument given file(s) to the standard output Build: make a_cat*/#include "../src/wybyteflow.h"#include "../src/wy_uty.h"#include <termios.h>// [Syn] Read data from inf untill EOF, and write to outf.//// [Ret] Ok// ....//static WyRet cat_file(WyByteFlow& outf, WyByteFlow& inf){ const size_t Btr(1024); // bytes to read WyRet r; // loop: read data from inf and write to outf for(;;) { size_t n_rd; size_t n_wr; WyStr buf; if((r=inf.read(buf,Btr,n_rd))!=Ok) { WY_RETURN(r); } if(n_rd==0) { break; // eof } if((r=Wy::full_write(outf,buf,n_wr))!=Ok) { WY_RETURN(r); } if(n_rd!=n_wr) { WY_RETURN(Wym_EIO); } } return(Ok);};// Command-Line Message//static const char *cmd_syntax= "\n" "Dump the argument given file(s) to the standard output\n" "\n" " [Usage]$a_cat {file}\n" "\n" ;int main(int argc, char *argv[])try { // Command-line option process // { const char optstr[]="h"; int optch; while((optch=::getopt(argc,argv,optstr))!=-1) { switch(optch) { case 'h': Wy::cout << cmd_syntax; return(0); case ':': // missing parameter case '?': // unknown option Wy::cerr << "parameter error\n"; Wy::cerr << cmd_syntax; return(-1); default: Wy::cerr << "parameter fault\n"; return(-1); } } } WyRet r; // check the cout is not a block device WyFileStat ostt; if((r=Wy::cout.stat(ostt))!=Ok) { WY_THROW(r); } if(ostt.is_blk()) { Wy::cerr << "output file is a block device\n"; return(-1); } // loop each non-option command argument for( ; optind<argc; optind++) { // setup infile from argv and check whether it is unwanted device const WyStr fname(argv[optind]); WyByteFlow infile(fname,O_RDONLY); WyFileStat istt; if((r=infile.stat(istt))!=Ok) { WY_THROW(r); } if((istt.sf_dev()==ostt.sf_dev())&&(istt.sf_ino()==ostt.sf_ino())) { Wy::cerr << "input file(" << fname << ") is identical to output file\n"; return(-1); } if(istt.is_blk()) { Wy::cerr << "input file(" << fname << ") is a block device\n"; return(-1); } // print file contents of infile to standard output if((r=cat_file(Wy::cout, infile))!=Ok) { Wy::cerr << "input file(" << fname << "): "; WY_THROW(r); } } // flush Wy::cout if(Wy::cout.ftype()==S_IFREG) { Wy::cout._fdatasync(); } else if(Wy::cout._isatty()) { Wy::cout._tcdrain(); } else {}; return(0);}catch(const WyRet& e) { if(e!=Ok) { Wy::cerr << Wy::wrd(e) << '\n'; } return e->c_repcode();}catch(...) { Wy::cerr << "main caught(...)\n"; return(-1);};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -